Input: ALPS - fix wheel decoding
[linux-2.6] / drivers / net / wireless / ipw2100.c
1 /******************************************************************************
2
3   Copyright(c) 2003 - 2005 Intel Corporation. All rights reserved.
4
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8
9   This program is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12   more details.
13
14   You should have received a copy of the GNU General Public License along with
15   this program; if not, write to the Free Software Foundation, Inc., 59
16   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18   The full GNU General Public License is included in this distribution in the
19   file called LICENSE.
20
21   Contact Information:
22   James P. Ketrenos <ipw2100-admin@linux.intel.com>
23   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25   Portions of this file are based on the sample_* files provided by Wireless
26   Extensions 0.26 package and copyright (c) 1997-2003 Jean Tourrilhes
27   <jt@hpl.hp.com>
28
29   Portions of this file are based on the Host AP project,
30   Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
31     <jkmaline@cc.hut.fi>
32   Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
33
34   Portions of ipw2100_mod_firmware_load, ipw2100_do_mod_firmware_load, and
35   ipw2100_fw_load are loosely based on drivers/sound/sound_firmware.c
36   available in the 2.4.25 kernel sources, and are copyright (c) Alan Cox
37
38 ******************************************************************************/
39 /*
40
41  Initial driver on which this is based was developed by Janusz Gorycki,
42  Maciej Urbaniak, and Maciej Sosnowski.
43
44  Promiscuous mode support added by Jacek Wysoczynski and Maciej Urbaniak.
45
46 Theory of Operation
47
48 Tx - Commands and Data
49
50 Firmware and host share a circular queue of Transmit Buffer Descriptors (TBDs)
51 Each TBD contains a pointer to the physical (dma_addr_t) address of data being
52 sent to the firmware as well as the length of the data.
53
54 The host writes to the TBD queue at the WRITE index.  The WRITE index points
55 to the _next_ packet to be written and is advanced when after the TBD has been
56 filled.
57
58 The firmware pulls from the TBD queue at the READ index.  The READ index points
59 to the currently being read entry, and is advanced once the firmware is
60 done with a packet.
61
62 When data is sent to the firmware, the first TBD is used to indicate to the
63 firmware if a Command or Data is being sent.  If it is Command, all of the
64 command information is contained within the physical address referred to by the
65 TBD.  If it is Data, the first TBD indicates the type of data packet, number
66 of fragments, etc.  The next TBD then referrs to the actual packet location.
67
68 The Tx flow cycle is as follows:
69
70 1) ipw2100_tx() is called by kernel with SKB to transmit
71 2) Packet is move from the tx_free_list and appended to the transmit pending
72    list (tx_pend_list)
73 3) work is scheduled to move pending packets into the shared circular queue.
74 4) when placing packet in the circular queue, the incoming SKB is DMA mapped
75    to a physical address.  That address is entered into a TBD.  Two TBDs are
76    filled out.  The first indicating a data packet, the second referring to the
77    actual payload data.
78 5) the packet is removed from tx_pend_list and placed on the end of the
79    firmware pending list (fw_pend_list)
80 6) firmware is notified that the WRITE index has
81 7) Once the firmware has processed the TBD, INTA is triggered.
82 8) For each Tx interrupt received from the firmware, the READ index is checked
83    to see which TBDs are done being processed.
84 9) For each TBD that has been processed, the ISR pulls the oldest packet
85    from the fw_pend_list.
86 10)The packet structure contained in the fw_pend_list is then used
87    to unmap the DMA address and to free the SKB originally passed to the driver
88    from the kernel.
89 11)The packet structure is placed onto the tx_free_list
90
91 The above steps are the same for commands, only the msg_free_list/msg_pend_list
92 are used instead of tx_free_list/tx_pend_list
93
94 ...
95
96 Critical Sections / Locking :
97
98 There are two locks utilized.  The first is the low level lock (priv->low_lock)
99 that protects the following:
100
101 - Access to the Tx/Rx queue lists via priv->low_lock. The lists are as follows:
102
103   tx_free_list : Holds pre-allocated Tx buffers.
104     TAIL modified in __ipw2100_tx_process()
105     HEAD modified in ipw2100_tx()
106
107   tx_pend_list : Holds used Tx buffers waiting to go into the TBD ring
108     TAIL modified ipw2100_tx()
109     HEAD modified by ipw2100_tx_send_data()
110
111   msg_free_list : Holds pre-allocated Msg (Command) buffers
112     TAIL modified in __ipw2100_tx_process()
113     HEAD modified in ipw2100_hw_send_command()
114
115   msg_pend_list : Holds used Msg buffers waiting to go into the TBD ring
116     TAIL modified in ipw2100_hw_send_command()
117     HEAD modified in ipw2100_tx_send_commands()
118
119   The flow of data on the TX side is as follows:
120
121   MSG_FREE_LIST + COMMAND => MSG_PEND_LIST => TBD => MSG_FREE_LIST
122   TX_FREE_LIST + DATA => TX_PEND_LIST => TBD => TX_FREE_LIST
123
124   The methods that work on the TBD ring are protected via priv->low_lock.
125
126 - The internal data state of the device itself
127 - Access to the firmware read/write indexes for the BD queues
128   and associated logic
129
130 All external entry functions are locked with the priv->action_lock to ensure
131 that only one external action is invoked at a time.
132
133
134 */
135
136 #include <linux/compiler.h>
137 #include <linux/config.h>
138 #include <linux/errno.h>
139 #include <linux/if_arp.h>
140 #include <linux/in6.h>
141 #include <linux/in.h>
142 #include <linux/ip.h>
143 #include <linux/kernel.h>
144 #include <linux/kmod.h>
145 #include <linux/module.h>
146 #include <linux/netdevice.h>
147 #include <linux/ethtool.h>
148 #include <linux/pci.h>
149 #include <linux/dma-mapping.h>
150 #include <linux/proc_fs.h>
151 #include <linux/skbuff.h>
152 #include <asm/uaccess.h>
153 #include <asm/io.h>
154 #define __KERNEL_SYSCALLS__
155 #include <linux/fs.h>
156 #include <linux/mm.h>
157 #include <linux/slab.h>
158 #include <linux/unistd.h>
159 #include <linux/stringify.h>
160 #include <linux/tcp.h>
161 #include <linux/types.h>
162 #include <linux/version.h>
163 #include <linux/time.h>
164 #include <linux/firmware.h>
165 #include <linux/acpi.h>
166 #include <linux/ctype.h>
167
168 #include "ipw2100.h"
169
170 #define IPW2100_VERSION "1.1.0"
171
172 #define DRV_NAME        "ipw2100"
173 #define DRV_VERSION     IPW2100_VERSION
174 #define DRV_DESCRIPTION "Intel(R) PRO/Wireless 2100 Network Driver"
175 #define DRV_COPYRIGHT   "Copyright(c) 2003-2004 Intel Corporation"
176
177
178 /* Debugging stuff */
179 #ifdef CONFIG_IPW_DEBUG
180 #define CONFIG_IPW2100_RX_DEBUG   /* Reception debugging */
181 #endif
182
183 MODULE_DESCRIPTION(DRV_DESCRIPTION);
184 MODULE_VERSION(DRV_VERSION);
185 MODULE_AUTHOR(DRV_COPYRIGHT);
186 MODULE_LICENSE("GPL");
187
188 static int debug = 0;
189 static int mode = 0;
190 static int channel = 0;
191 static int associate = 1;
192 static int disable = 0;
193 #ifdef CONFIG_PM
194 static struct ipw2100_fw ipw2100_firmware;
195 #endif
196
197 #include <linux/moduleparam.h>
198 module_param(debug, int, 0444);
199 module_param(mode, int, 0444);
200 module_param(channel, int, 0444);
201 module_param(associate, int, 0444);
202 module_param(disable, int, 0444);
203
204 MODULE_PARM_DESC(debug, "debug level");
205 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
206 MODULE_PARM_DESC(channel, "channel");
207 MODULE_PARM_DESC(associate, "auto associate when scanning (default on)");
208 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
209
210 static u32 ipw2100_debug_level = IPW_DL_NONE;
211
212 #ifdef CONFIG_IPW_DEBUG
213 #define IPW_DEBUG(level, message...) \
214 do { \
215         if (ipw2100_debug_level & (level)) { \
216                 printk(KERN_DEBUG "ipw2100: %c %s ", \
217                        in_interrupt() ? 'I' : 'U',  __FUNCTION__); \
218                 printk(message); \
219         } \
220 } while (0)
221 #else
222 #define IPW_DEBUG(level, message...) do {} while (0)
223 #endif /* CONFIG_IPW_DEBUG */
224
225 #ifdef CONFIG_IPW_DEBUG
226 static const char *command_types[] = {
227         "undefined",
228         "unused", /* HOST_ATTENTION */
229         "HOST_COMPLETE",
230         "unused", /* SLEEP */
231         "unused", /* HOST_POWER_DOWN */
232         "unused",
233         "SYSTEM_CONFIG",
234         "unused", /* SET_IMR */
235         "SSID",
236         "MANDATORY_BSSID",
237         "AUTHENTICATION_TYPE",
238         "ADAPTER_ADDRESS",
239         "PORT_TYPE",
240         "INTERNATIONAL_MODE",
241         "CHANNEL",
242         "RTS_THRESHOLD",
243         "FRAG_THRESHOLD",
244         "POWER_MODE",
245         "TX_RATES",
246         "BASIC_TX_RATES",
247         "WEP_KEY_INFO",
248         "unused",
249         "unused",
250         "unused",
251         "unused",
252         "WEP_KEY_INDEX",
253         "WEP_FLAGS",
254         "ADD_MULTICAST",
255         "CLEAR_ALL_MULTICAST",
256         "BEACON_INTERVAL",
257         "ATIM_WINDOW",
258         "CLEAR_STATISTICS",
259         "undefined",
260         "undefined",
261         "undefined",
262         "undefined",
263         "TX_POWER_INDEX",
264         "undefined",
265         "undefined",
266         "undefined",
267         "undefined",
268         "undefined",
269         "undefined",
270         "BROADCAST_SCAN",
271         "CARD_DISABLE",
272         "PREFERRED_BSSID",
273         "SET_SCAN_OPTIONS",
274         "SCAN_DWELL_TIME",
275         "SWEEP_TABLE",
276         "AP_OR_STATION_TABLE",
277         "GROUP_ORDINALS",
278         "SHORT_RETRY_LIMIT",
279         "LONG_RETRY_LIMIT",
280         "unused", /* SAVE_CALIBRATION */
281         "unused", /* RESTORE_CALIBRATION */
282         "undefined",
283         "undefined",
284         "undefined",
285         "HOST_PRE_POWER_DOWN",
286         "unused", /* HOST_INTERRUPT_COALESCING */
287         "undefined",
288         "CARD_DISABLE_PHY_OFF",
289         "MSDU_TX_RATES"
290         "undefined",
291         "undefined",
292         "SET_STATION_STAT_BITS",
293         "CLEAR_STATIONS_STAT_BITS",
294         "LEAP_ROGUE_MODE",
295         "SET_SECURITY_INFORMATION",
296         "DISASSOCIATION_BSSID",
297         "SET_WPA_ASS_IE"
298 };
299 #endif
300
301
302 /* Pre-decl until we get the code solid and then we can clean it up */
303 static void ipw2100_tx_send_commands(struct ipw2100_priv *priv);
304 static void ipw2100_tx_send_data(struct ipw2100_priv *priv);
305 static int ipw2100_adapter_setup(struct ipw2100_priv *priv);
306
307 static void ipw2100_queues_initialize(struct ipw2100_priv *priv);
308 static void ipw2100_queues_free(struct ipw2100_priv *priv);
309 static int ipw2100_queues_allocate(struct ipw2100_priv *priv);
310
311 static int ipw2100_fw_download(struct ipw2100_priv *priv,
312                                struct ipw2100_fw *fw);
313 static int ipw2100_get_firmware(struct ipw2100_priv *priv,
314                                 struct ipw2100_fw *fw);
315 static int ipw2100_get_fwversion(struct ipw2100_priv *priv, char *buf,
316                                  size_t max);
317 static int ipw2100_get_ucodeversion(struct ipw2100_priv *priv, char *buf,
318                                     size_t max);
319 static void ipw2100_release_firmware(struct ipw2100_priv *priv,
320                                      struct ipw2100_fw *fw);
321 static int ipw2100_ucode_download(struct ipw2100_priv *priv,
322                                   struct ipw2100_fw *fw);
323 static void ipw2100_wx_event_work(struct ipw2100_priv *priv);
324 static struct iw_statistics *ipw2100_wx_wireless_stats(struct net_device * dev);
325 static struct iw_handler_def ipw2100_wx_handler_def;
326
327
328 static inline void read_register(struct net_device *dev, u32 reg, u32 *val)
329 {
330         *val = readl((void *)(dev->base_addr + reg));
331         IPW_DEBUG_IO("r: 0x%08X => 0x%08X\n", reg, *val);
332 }
333
334 static inline void write_register(struct net_device *dev, u32 reg, u32 val)
335 {
336         writel(val, (void *)(dev->base_addr + reg));
337         IPW_DEBUG_IO("w: 0x%08X <= 0x%08X\n", reg, val);
338 }
339
340 static inline void read_register_word(struct net_device *dev, u32 reg, u16 *val)
341 {
342         *val = readw((void *)(dev->base_addr + reg));
343         IPW_DEBUG_IO("r: 0x%08X => %04X\n", reg, *val);
344 }
345
346 static inline void read_register_byte(struct net_device *dev, u32 reg, u8 *val)
347 {
348         *val = readb((void *)(dev->base_addr + reg));
349         IPW_DEBUG_IO("r: 0x%08X => %02X\n", reg, *val);
350 }
351
352 static inline void write_register_word(struct net_device *dev, u32 reg, u16 val)
353 {
354         writew(val, (void *)(dev->base_addr + reg));
355         IPW_DEBUG_IO("w: 0x%08X <= %04X\n", reg, val);
356 }
357
358
359 static inline void write_register_byte(struct net_device *dev, u32 reg, u8 val)
360 {
361         writeb(val, (void *)(dev->base_addr + reg));
362         IPW_DEBUG_IO("w: 0x%08X =< %02X\n", reg, val);
363 }
364
365 static inline void read_nic_dword(struct net_device *dev, u32 addr, u32 *val)
366 {
367         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
368                        addr & IPW_REG_INDIRECT_ADDR_MASK);
369         read_register(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
370 }
371
372 static inline void write_nic_dword(struct net_device *dev, u32 addr, u32 val)
373 {
374         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
375                        addr & IPW_REG_INDIRECT_ADDR_MASK);
376         write_register(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
377 }
378
379 static inline void read_nic_word(struct net_device *dev, u32 addr, u16 *val)
380 {
381         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
382                        addr & IPW_REG_INDIRECT_ADDR_MASK);
383         read_register_word(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
384 }
385
386 static inline void write_nic_word(struct net_device *dev, u32 addr, u16 val)
387 {
388         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
389                        addr & IPW_REG_INDIRECT_ADDR_MASK);
390         write_register_word(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
391 }
392
393 static inline void read_nic_byte(struct net_device *dev, u32 addr, u8 *val)
394 {
395         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
396                        addr & IPW_REG_INDIRECT_ADDR_MASK);
397         read_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
398 }
399
400 static inline void write_nic_byte(struct net_device *dev, u32 addr, u8 val)
401 {
402         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
403                        addr & IPW_REG_INDIRECT_ADDR_MASK);
404         write_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
405 }
406
407 static inline void write_nic_auto_inc_address(struct net_device *dev, u32 addr)
408 {
409         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
410                        addr & IPW_REG_INDIRECT_ADDR_MASK);
411 }
412
413 static inline void write_nic_dword_auto_inc(struct net_device *dev, u32 val)
414 {
415         write_register(dev, IPW_REG_AUTOINCREMENT_DATA, val);
416 }
417
418 static inline void write_nic_memory(struct net_device *dev, u32 addr, u32 len,
419                                     const u8 *buf)
420 {
421         u32 aligned_addr;
422         u32 aligned_len;
423         u32 dif_len;
424         u32 i;
425
426         /* read first nibble byte by byte */
427         aligned_addr = addr & (~0x3);
428         dif_len = addr - aligned_addr;
429         if (dif_len) {
430                 /* Start reading at aligned_addr + dif_len */
431                 write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
432                                aligned_addr);
433                 for (i = dif_len; i < 4; i++, buf++)
434                         write_register_byte(
435                                 dev, IPW_REG_INDIRECT_ACCESS_DATA + i,
436                                 *buf);
437
438                 len -= dif_len;
439                 aligned_addr += 4;
440         }
441
442         /* read DWs through autoincrement registers */
443         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
444                        aligned_addr);
445         aligned_len = len & (~0x3);
446         for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
447                 write_register(
448                         dev, IPW_REG_AUTOINCREMENT_DATA, *(u32 *)buf);
449
450         /* copy the last nibble */
451         dif_len = len - aligned_len;
452         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS, aligned_addr);
453         for (i = 0; i < dif_len; i++, buf++)
454                 write_register_byte(
455                         dev, IPW_REG_INDIRECT_ACCESS_DATA + i, *buf);
456 }
457
458 static inline void read_nic_memory(struct net_device *dev, u32 addr, u32 len,
459                                    u8 *buf)
460 {
461         u32 aligned_addr;
462         u32 aligned_len;
463         u32 dif_len;
464         u32 i;
465
466         /* read first nibble byte by byte */
467         aligned_addr = addr & (~0x3);
468         dif_len = addr - aligned_addr;
469         if (dif_len) {
470                 /* Start reading at aligned_addr + dif_len */
471                 write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
472                                aligned_addr);
473                 for (i = dif_len; i < 4; i++, buf++)
474                         read_register_byte(
475                                 dev, IPW_REG_INDIRECT_ACCESS_DATA + i, buf);
476
477                 len -= dif_len;
478                 aligned_addr += 4;
479         }
480
481         /* read DWs through autoincrement registers */
482         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
483                        aligned_addr);
484         aligned_len = len & (~0x3);
485         for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
486                 read_register(dev, IPW_REG_AUTOINCREMENT_DATA,
487                               (u32 *)buf);
488
489         /* copy the last nibble */
490         dif_len = len - aligned_len;
491         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
492                        aligned_addr);
493         for (i = 0; i < dif_len; i++, buf++)
494                 read_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA +
495                                    i, buf);
496 }
497
498 static inline int ipw2100_hw_is_adapter_in_system(struct net_device *dev)
499 {
500         return (dev->base_addr &&
501                 (readl((void *)(dev->base_addr + IPW_REG_DOA_DEBUG_AREA_START))
502                  == IPW_DATA_DOA_DEBUG_VALUE));
503 }
504
505 static int ipw2100_get_ordinal(struct ipw2100_priv *priv, u32 ord,
506                                void *val, u32 *len)
507 {
508         struct ipw2100_ordinals *ordinals = &priv->ordinals;
509         u32 addr;
510         u32 field_info;
511         u16 field_len;
512         u16 field_count;
513         u32 total_length;
514
515         if (ordinals->table1_addr == 0) {
516                 printk(KERN_WARNING DRV_NAME ": attempt to use fw ordinals "
517                        "before they have been loaded.\n");
518                 return -EINVAL;
519         }
520
521         if (IS_ORDINAL_TABLE_ONE(ordinals, ord)) {
522                 if (*len < IPW_ORD_TAB_1_ENTRY_SIZE) {
523                         *len = IPW_ORD_TAB_1_ENTRY_SIZE;
524
525                         printk(KERN_WARNING DRV_NAME
526                                ": ordinal buffer length too small, need %zd\n",
527                                IPW_ORD_TAB_1_ENTRY_SIZE);
528
529                         return -EINVAL;
530                 }
531
532                 read_nic_dword(priv->net_dev, ordinals->table1_addr + (ord << 2),
533                                &addr);
534                 read_nic_dword(priv->net_dev, addr, val);
535
536                 *len = IPW_ORD_TAB_1_ENTRY_SIZE;
537
538                 return 0;
539         }
540
541         if (IS_ORDINAL_TABLE_TWO(ordinals, ord)) {
542
543                 ord -= IPW_START_ORD_TAB_2;
544
545                 /* get the address of statistic */
546                 read_nic_dword(priv->net_dev, ordinals->table2_addr + (ord << 3),
547                                &addr);
548
549                 /* get the second DW of statistics ;
550                  * two 16-bit words - first is length, second is count */
551                 read_nic_dword(priv->net_dev,
552                                ordinals->table2_addr + (ord << 3) + sizeof(u32),
553                                &field_info);
554
555                 /* get each entry length */
556                 field_len = *((u16 *)&field_info);
557
558                 /* get number of entries */
559                 field_count = *(((u16 *)&field_info) + 1);
560
561                 /* abort if no enought memory */
562                 total_length = field_len * field_count;
563                 if (total_length > *len) {
564                         *len = total_length;
565                         return -EINVAL;
566                 }
567
568                 *len = total_length;
569                 if (!total_length)
570                         return 0;
571
572                 /* read the ordinal data from the SRAM */
573                 read_nic_memory(priv->net_dev, addr, total_length, val);
574
575                 return 0;
576         }
577
578         printk(KERN_WARNING DRV_NAME ": ordinal %d neither in table 1 nor "
579                "in table 2\n", ord);
580
581         return -EINVAL;
582 }
583
584 static int ipw2100_set_ordinal(struct ipw2100_priv *priv, u32 ord, u32 *val,
585                                u32 *len)
586 {
587         struct ipw2100_ordinals *ordinals = &priv->ordinals;
588         u32 addr;
589
590         if (IS_ORDINAL_TABLE_ONE(ordinals, ord)) {
591                 if (*len != IPW_ORD_TAB_1_ENTRY_SIZE) {
592                         *len = IPW_ORD_TAB_1_ENTRY_SIZE;
593                         IPW_DEBUG_INFO("wrong size\n");
594                         return -EINVAL;
595                 }
596
597                 read_nic_dword(priv->net_dev, ordinals->table1_addr + (ord << 2),
598                                &addr);
599
600                 write_nic_dword(priv->net_dev, addr, *val);
601
602                 *len = IPW_ORD_TAB_1_ENTRY_SIZE;
603
604                 return 0;
605         }
606
607         IPW_DEBUG_INFO("wrong table\n");
608         if (IS_ORDINAL_TABLE_TWO(ordinals, ord))
609                 return -EINVAL;
610
611         return -EINVAL;
612 }
613
614 static char *snprint_line(char *buf, size_t count,
615                           const u8 *data, u32 len, u32 ofs)
616 {
617         int out, i, j, l;
618         char c;
619
620         out = snprintf(buf, count, "%08X", ofs);
621
622         for (l = 0, i = 0; i < 2; i++) {
623                 out += snprintf(buf + out, count - out, " ");
624                 for (j = 0; j < 8 && l < len; j++, l++)
625                         out += snprintf(buf + out, count - out, "%02X ",
626                                         data[(i * 8 + j)]);
627                 for (; j < 8; j++)
628                         out += snprintf(buf + out, count - out, "   ");
629         }
630
631         out += snprintf(buf + out, count - out, " ");
632         for (l = 0, i = 0; i < 2; i++) {
633                 out += snprintf(buf + out, count - out, " ");
634                 for (j = 0; j < 8 && l < len; j++, l++) {
635                         c = data[(i * 8 + j)];
636                         if (!isascii(c) || !isprint(c))
637                                 c = '.';
638
639                         out += snprintf(buf + out, count - out, "%c", c);
640                 }
641
642                 for (; j < 8; j++)
643                         out += snprintf(buf + out, count - out, " ");
644         }
645
646         return buf;
647 }
648
649 static void printk_buf(int level, const u8 *data, u32 len)
650 {
651         char line[81];
652         u32 ofs = 0;
653         if (!(ipw2100_debug_level & level))
654                 return;
655
656         while (len) {
657                 printk(KERN_DEBUG "%s\n",
658                        snprint_line(line, sizeof(line), &data[ofs],
659                                     min(len, 16U), ofs));
660                 ofs += 16;
661                 len -= min(len, 16U);
662         }
663 }
664
665
666
667 #define MAX_RESET_BACKOFF 10
668
669 static inline void schedule_reset(struct ipw2100_priv *priv)
670 {
671         unsigned long now = get_seconds();
672
673         /* If we haven't received a reset request within the backoff period,
674          * then we can reset the backoff interval so this reset occurs
675          * immediately */
676         if (priv->reset_backoff &&
677             (now - priv->last_reset > priv->reset_backoff))
678                 priv->reset_backoff = 0;
679
680         priv->last_reset = get_seconds();
681
682         if (!(priv->status & STATUS_RESET_PENDING)) {
683                 IPW_DEBUG_INFO("%s: Scheduling firmware restart (%ds).\n",
684                                priv->net_dev->name, priv->reset_backoff);
685                 netif_carrier_off(priv->net_dev);
686                 netif_stop_queue(priv->net_dev);
687                 priv->status |= STATUS_RESET_PENDING;
688                 if (priv->reset_backoff)
689                         queue_delayed_work(priv->workqueue, &priv->reset_work,
690                                            priv->reset_backoff * HZ);
691                 else
692                         queue_work(priv->workqueue, &priv->reset_work);
693
694                 if (priv->reset_backoff < MAX_RESET_BACKOFF)
695                         priv->reset_backoff++;
696
697                 wake_up_interruptible(&priv->wait_command_queue);
698         } else
699                 IPW_DEBUG_INFO("%s: Firmware restart already in progress.\n",
700                                priv->net_dev->name);
701
702 }
703
704 #define HOST_COMPLETE_TIMEOUT (2 * HZ)
705 static int ipw2100_hw_send_command(struct ipw2100_priv *priv,
706                                    struct host_command * cmd)
707 {
708         struct list_head *element;
709         struct ipw2100_tx_packet *packet;
710         unsigned long flags;
711         int err = 0;
712
713         IPW_DEBUG_HC("Sending %s command (#%d), %d bytes\n",
714                      command_types[cmd->host_command], cmd->host_command,
715                      cmd->host_command_length);
716         printk_buf(IPW_DL_HC, (u8*)cmd->host_command_parameters,
717                    cmd->host_command_length);
718
719         spin_lock_irqsave(&priv->low_lock, flags);
720
721         if (priv->fatal_error) {
722                 IPW_DEBUG_INFO("Attempt to send command while hardware in fatal error condition.\n");
723                 err = -EIO;
724                 goto fail_unlock;
725         }
726
727         if (!(priv->status & STATUS_RUNNING)) {
728                 IPW_DEBUG_INFO("Attempt to send command while hardware is not running.\n");
729                 err = -EIO;
730                 goto fail_unlock;
731         }
732
733         if (priv->status & STATUS_CMD_ACTIVE) {
734                 IPW_DEBUG_INFO("Attempt to send command while another command is pending.\n");
735                 err = -EBUSY;
736                 goto fail_unlock;
737         }
738
739         if (list_empty(&priv->msg_free_list)) {
740                 IPW_DEBUG_INFO("no available msg buffers\n");
741                 goto fail_unlock;
742         }
743
744         priv->status |= STATUS_CMD_ACTIVE;
745         priv->messages_sent++;
746
747         element = priv->msg_free_list.next;
748
749         packet = list_entry(element, struct ipw2100_tx_packet, list);
750         packet->jiffy_start = jiffies;
751
752         /* initialize the firmware command packet */
753         packet->info.c_struct.cmd->host_command_reg = cmd->host_command;
754         packet->info.c_struct.cmd->host_command_reg1 = cmd->host_command1;
755         packet->info.c_struct.cmd->host_command_len_reg = cmd->host_command_length;
756         packet->info.c_struct.cmd->sequence = cmd->host_command_sequence;
757
758         memcpy(packet->info.c_struct.cmd->host_command_params_reg,
759                cmd->host_command_parameters,
760                sizeof(packet->info.c_struct.cmd->host_command_params_reg));
761
762         list_del(element);
763         DEC_STAT(&priv->msg_free_stat);
764
765         list_add_tail(element, &priv->msg_pend_list);
766         INC_STAT(&priv->msg_pend_stat);
767
768         ipw2100_tx_send_commands(priv);
769         ipw2100_tx_send_data(priv);
770
771         spin_unlock_irqrestore(&priv->low_lock, flags);
772
773         /*
774          * We must wait for this command to complete before another
775          * command can be sent...  but if we wait more than 3 seconds
776          * then there is a problem.
777          */
778
779         err = wait_event_interruptible_timeout(
780                 priv->wait_command_queue, !(priv->status & STATUS_CMD_ACTIVE),
781                 HOST_COMPLETE_TIMEOUT);
782
783         if (err == 0) {
784                 IPW_DEBUG_INFO("Command completion failed out after %dms.\n",
785                                HOST_COMPLETE_TIMEOUT / (HZ / 100));
786                 priv->fatal_error = IPW2100_ERR_MSG_TIMEOUT;
787                 priv->status &= ~STATUS_CMD_ACTIVE;
788                 schedule_reset(priv);
789                 return -EIO;
790         }
791
792         if (priv->fatal_error) {
793                 printk(KERN_WARNING DRV_NAME ": %s: firmware fatal error\n",
794                        priv->net_dev->name);
795                 return -EIO;
796         }
797
798         /* !!!!! HACK TEST !!!!!
799          * When lots of debug trace statements are enabled, the driver
800          * doesn't seem to have as many firmware restart cycles...
801          *
802          * As a test, we're sticking in a 1/100s delay here */
803         set_current_state(TASK_UNINTERRUPTIBLE);
804         schedule_timeout(HZ / 100);
805
806         return 0;
807
808  fail_unlock:
809         spin_unlock_irqrestore(&priv->low_lock, flags);
810
811         return err;
812 }
813
814
815 /*
816  * Verify the values and data access of the hardware
817  * No locks needed or used.  No functions called.
818  */
819 static int ipw2100_verify(struct ipw2100_priv *priv)
820 {
821         u32 data1, data2;
822         u32 address;
823
824         u32 val1 = 0x76543210;
825         u32 val2 = 0xFEDCBA98;
826
827         /* Domain 0 check - all values should be DOA_DEBUG */
828         for (address = IPW_REG_DOA_DEBUG_AREA_START;
829              address < IPW_REG_DOA_DEBUG_AREA_END;
830              address += sizeof(u32)) {
831                 read_register(priv->net_dev, address, &data1);
832                 if (data1 != IPW_DATA_DOA_DEBUG_VALUE)
833                         return -EIO;
834         }
835
836         /* Domain 1 check - use arbitrary read/write compare  */
837         for (address = 0; address < 5; address++) {
838                 /* The memory area is not used now */
839                 write_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x32,
840                                val1);
841                 write_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x36,
842                                val2);
843                 read_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x32,
844                               &data1);
845                 read_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x36,
846                               &data2);
847                 if (val1 == data1 && val2 == data2)
848                         return 0;
849         }
850
851         return -EIO;
852 }
853
854 /*
855  *
856  * Loop until the CARD_DISABLED bit is the same value as the
857  * supplied parameter
858  *
859  * TODO: See if it would be more efficient to do a wait/wake
860  *       cycle and have the completion event trigger the wakeup
861  *
862  */
863 #define IPW_CARD_DISABLE_COMPLETE_WAIT              100 // 100 milli
864 static int ipw2100_wait_for_card_state(struct ipw2100_priv *priv, int state)
865 {
866         int i;
867         u32 card_state;
868         u32 len = sizeof(card_state);
869         int err;
870
871         for (i = 0; i <= IPW_CARD_DISABLE_COMPLETE_WAIT * 1000; i += 50) {
872                 err = ipw2100_get_ordinal(priv, IPW_ORD_CARD_DISABLED,
873                                           &card_state, &len);
874                 if (err) {
875                         IPW_DEBUG_INFO("Query of CARD_DISABLED ordinal "
876                                        "failed.\n");
877                         return 0;
878                 }
879
880                 /* We'll break out if either the HW state says it is
881                  * in the state we want, or if HOST_COMPLETE command
882                  * finishes */
883                 if ((card_state == state) ||
884                     ((priv->status & STATUS_ENABLED) ?
885                      IPW_HW_STATE_ENABLED : IPW_HW_STATE_DISABLED) == state) {
886                         if (state == IPW_HW_STATE_ENABLED)
887                                 priv->status |= STATUS_ENABLED;
888                         else
889                                 priv->status &= ~STATUS_ENABLED;
890
891                         return 0;
892                 }
893
894                 udelay(50);
895         }
896
897         IPW_DEBUG_INFO("ipw2100_wait_for_card_state to %s state timed out\n",
898                        state ? "DISABLED" : "ENABLED");
899         return -EIO;
900 }
901
902
903 /*********************************************************************
904     Procedure   :   sw_reset_and_clock
905     Purpose     :   Asserts s/w reset, asserts clock initialization
906                     and waits for clock stabilization
907  ********************************************************************/
908 static int sw_reset_and_clock(struct ipw2100_priv *priv)
909 {
910         int i;
911         u32 r;
912
913         // assert s/w reset
914         write_register(priv->net_dev, IPW_REG_RESET_REG,
915                        IPW_AUX_HOST_RESET_REG_SW_RESET);
916
917         // wait for clock stabilization
918         for (i = 0; i < 1000; i++) {
919                 udelay(IPW_WAIT_RESET_ARC_COMPLETE_DELAY);
920
921                 // check clock ready bit
922                 read_register(priv->net_dev, IPW_REG_RESET_REG, &r);
923                 if (r & IPW_AUX_HOST_RESET_REG_PRINCETON_RESET)
924                         break;
925         }
926
927         if (i == 1000)
928                 return -EIO;    // TODO: better error value
929
930         /* set "initialization complete" bit to move adapter to
931          * D0 state */
932         write_register(priv->net_dev, IPW_REG_GP_CNTRL,
933                        IPW_AUX_HOST_GP_CNTRL_BIT_INIT_DONE);
934
935         /* wait for clock stabilization */
936         for (i = 0; i < 10000; i++) {
937                 udelay(IPW_WAIT_CLOCK_STABILIZATION_DELAY * 4);
938
939                 /* check clock ready bit */
940                 read_register(priv->net_dev, IPW_REG_GP_CNTRL, &r);
941                 if (r & IPW_AUX_HOST_GP_CNTRL_BIT_CLOCK_READY)
942                         break;
943         }
944
945         if (i == 10000)
946                 return -EIO;    /* TODO: better error value */
947
948         /* set D0 standby bit */
949         read_register(priv->net_dev, IPW_REG_GP_CNTRL, &r);
950         write_register(priv->net_dev, IPW_REG_GP_CNTRL,
951                        r | IPW_AUX_HOST_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
952
953         return 0;
954 }
955
956 /*********************************************************************
957     Procedure   :   ipw2100_download_firmware
958     Purpose     :   Initiaze adapter after power on.
959                     The sequence is:
960                     1. assert s/w reset first!
961                     2. awake clocks & wait for clock stabilization
962                     3. hold ARC (don't ask me why...)
963                     4. load Dino ucode and reset/clock init again
964                     5. zero-out shared mem
965                     6. download f/w
966  *******************************************************************/
967 static int ipw2100_download_firmware(struct ipw2100_priv *priv)
968 {
969         u32 address;
970         int err;
971
972 #ifndef CONFIG_PM
973         /* Fetch the firmware and microcode */
974         struct ipw2100_fw ipw2100_firmware;
975 #endif
976
977         if (priv->fatal_error) {
978                 IPW_DEBUG_ERROR("%s: ipw2100_download_firmware called after "
979                        "fatal error %d.  Interface must be brought down.\n",
980                        priv->net_dev->name, priv->fatal_error);
981                 return -EINVAL;
982         }
983
984 #ifdef CONFIG_PM
985         if (!ipw2100_firmware.version) {
986                 err = ipw2100_get_firmware(priv, &ipw2100_firmware);
987                 if (err) {
988                         IPW_DEBUG_ERROR("%s: ipw2100_get_firmware failed: %d\n",
989                                priv->net_dev->name, err);
990                         priv->fatal_error = IPW2100_ERR_FW_LOAD;
991                         goto fail;
992                 }
993         }
994 #else
995         err = ipw2100_get_firmware(priv, &ipw2100_firmware);
996         if (err) {
997                 IPW_DEBUG_ERROR("%s: ipw2100_get_firmware failed: %d\n",
998                        priv->net_dev->name, err);
999                 priv->fatal_error = IPW2100_ERR_FW_LOAD;
1000                 goto fail;
1001         }
1002 #endif
1003         priv->firmware_version = ipw2100_firmware.version;
1004
1005         /* s/w reset and clock stabilization */
1006         err = sw_reset_and_clock(priv);
1007         if (err) {
1008                 IPW_DEBUG_ERROR("%s: sw_reset_and_clock failed: %d\n",
1009                        priv->net_dev->name, err);
1010                 goto fail;
1011         }
1012
1013         err = ipw2100_verify(priv);
1014         if (err) {
1015                 IPW_DEBUG_ERROR("%s: ipw2100_verify failed: %d\n",
1016                        priv->net_dev->name, err);
1017                 goto fail;
1018         }
1019
1020         /* Hold ARC */
1021         write_nic_dword(priv->net_dev,
1022                         IPW_INTERNAL_REGISTER_HALT_AND_RESET,
1023                         0x80000000);
1024
1025         /* allow ARC to run */
1026         write_register(priv->net_dev, IPW_REG_RESET_REG, 0);
1027
1028         /* load microcode */
1029         err = ipw2100_ucode_download(priv, &ipw2100_firmware);
1030         if (err) {
1031                 printk(KERN_ERR DRV_NAME ": %s: Error loading microcode: %d\n",
1032                        priv->net_dev->name, err);
1033                 goto fail;
1034         }
1035
1036         /* release ARC */
1037         write_nic_dword(priv->net_dev,
1038                         IPW_INTERNAL_REGISTER_HALT_AND_RESET,
1039                         0x00000000);
1040
1041         /* s/w reset and clock stabilization (again!!!) */
1042         err = sw_reset_and_clock(priv);
1043         if (err) {
1044                 printk(KERN_ERR DRV_NAME ": %s: sw_reset_and_clock failed: %d\n",
1045                        priv->net_dev->name, err);
1046                 goto fail;
1047         }
1048
1049         /* load f/w */
1050         err = ipw2100_fw_download(priv, &ipw2100_firmware);
1051         if (err) {
1052                 IPW_DEBUG_ERROR("%s: Error loading firmware: %d\n",
1053                        priv->net_dev->name, err);
1054                 goto fail;
1055         }
1056
1057 #ifndef CONFIG_PM
1058         /*
1059          * When the .resume method of the driver is called, the other
1060          * part of the system, i.e. the ide driver could still stay in
1061          * the suspend stage. This prevents us from loading the firmware
1062          * from the disk.  --YZ
1063          */
1064
1065         /* free any storage allocated for firmware image */
1066         ipw2100_release_firmware(priv, &ipw2100_firmware);
1067 #endif
1068
1069         /* zero out Domain 1 area indirectly (Si requirement) */
1070         for (address = IPW_HOST_FW_SHARED_AREA0;
1071              address < IPW_HOST_FW_SHARED_AREA0_END; address += 4)
1072                 write_nic_dword(priv->net_dev, address, 0);
1073         for (address = IPW_HOST_FW_SHARED_AREA1;
1074              address < IPW_HOST_FW_SHARED_AREA1_END; address += 4)
1075                 write_nic_dword(priv->net_dev, address, 0);
1076         for (address = IPW_HOST_FW_SHARED_AREA2;
1077              address < IPW_HOST_FW_SHARED_AREA2_END; address += 4)
1078                 write_nic_dword(priv->net_dev, address, 0);
1079         for (address = IPW_HOST_FW_SHARED_AREA3;
1080              address < IPW_HOST_FW_SHARED_AREA3_END; address += 4)
1081                 write_nic_dword(priv->net_dev, address, 0);
1082         for (address = IPW_HOST_FW_INTERRUPT_AREA;
1083              address < IPW_HOST_FW_INTERRUPT_AREA_END; address += 4)
1084                 write_nic_dword(priv->net_dev, address, 0);
1085
1086         return 0;
1087
1088  fail:
1089         ipw2100_release_firmware(priv, &ipw2100_firmware);
1090         return err;
1091 }
1092
1093 static inline void ipw2100_enable_interrupts(struct ipw2100_priv *priv)
1094 {
1095         if (priv->status & STATUS_INT_ENABLED)
1096                 return;
1097         priv->status |= STATUS_INT_ENABLED;
1098         write_register(priv->net_dev, IPW_REG_INTA_MASK, IPW_INTERRUPT_MASK);
1099 }
1100
1101 static inline void ipw2100_disable_interrupts(struct ipw2100_priv *priv)
1102 {
1103         if (!(priv->status & STATUS_INT_ENABLED))
1104                 return;
1105         priv->status &= ~STATUS_INT_ENABLED;
1106         write_register(priv->net_dev, IPW_REG_INTA_MASK, 0x0);
1107 }
1108
1109
1110 static void ipw2100_initialize_ordinals(struct ipw2100_priv *priv)
1111 {
1112         struct ipw2100_ordinals *ord = &priv->ordinals;
1113
1114         IPW_DEBUG_INFO("enter\n");
1115
1116         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_ORDINALS_TABLE_1,
1117                       &ord->table1_addr);
1118
1119         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_ORDINALS_TABLE_2,
1120                       &ord->table2_addr);
1121
1122         read_nic_dword(priv->net_dev, ord->table1_addr, &ord->table1_size);
1123         read_nic_dword(priv->net_dev, ord->table2_addr, &ord->table2_size);
1124
1125         ord->table2_size &= 0x0000FFFF;
1126
1127         IPW_DEBUG_INFO("table 1 size: %d\n", ord->table1_size);
1128         IPW_DEBUG_INFO("table 2 size: %d\n", ord->table2_size);
1129         IPW_DEBUG_INFO("exit\n");
1130 }
1131
1132 static inline void ipw2100_hw_set_gpio(struct ipw2100_priv *priv)
1133 {
1134         u32 reg = 0;
1135         /*
1136          * Set GPIO 3 writable by FW; GPIO 1 writable
1137          * by driver and enable clock
1138          */
1139         reg = (IPW_BIT_GPIO_GPIO3_MASK | IPW_BIT_GPIO_GPIO1_ENABLE |
1140                IPW_BIT_GPIO_LED_OFF);
1141         write_register(priv->net_dev, IPW_REG_GPIO, reg);
1142 }
1143
1144 static inline int rf_kill_active(struct ipw2100_priv *priv)
1145 {
1146 #define MAX_RF_KILL_CHECKS 5
1147 #define RF_KILL_CHECK_DELAY 40
1148
1149         unsigned short value = 0;
1150         u32 reg = 0;
1151         int i;
1152
1153         if (!(priv->hw_features & HW_FEATURE_RFKILL)) {
1154                 priv->status &= ~STATUS_RF_KILL_HW;
1155                 return 0;
1156         }
1157
1158         for (i = 0; i < MAX_RF_KILL_CHECKS; i++) {
1159                 udelay(RF_KILL_CHECK_DELAY);
1160                 read_register(priv->net_dev, IPW_REG_GPIO, &reg);
1161                 value = (value << 1) | ((reg & IPW_BIT_GPIO_RF_KILL) ? 0 : 1);
1162         }
1163
1164         if (value == 0)
1165                 priv->status |= STATUS_RF_KILL_HW;
1166         else
1167                 priv->status &= ~STATUS_RF_KILL_HW;
1168
1169         return (value == 0);
1170 }
1171
1172 static int ipw2100_get_hw_features(struct ipw2100_priv *priv)
1173 {
1174         u32 addr, len;
1175         u32 val;
1176
1177         /*
1178          * EEPROM_SRAM_DB_START_ADDRESS using ordinal in ordinal table 1
1179          */
1180         len = sizeof(addr);
1181         if (ipw2100_get_ordinal(
1182                     priv, IPW_ORD_EEPROM_SRAM_DB_BLOCK_START_ADDRESS,
1183                     &addr, &len)) {
1184                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1185                        __LINE__);
1186                 return -EIO;
1187         }
1188
1189         IPW_DEBUG_INFO("EEPROM address: %08X\n", addr);
1190
1191         /*
1192          * EEPROM version is the byte at offset 0xfd in firmware
1193          * We read 4 bytes, then shift out the byte we actually want */
1194         read_nic_dword(priv->net_dev, addr + 0xFC, &val);
1195         priv->eeprom_version = (val >> 24) & 0xFF;
1196         IPW_DEBUG_INFO("EEPROM version: %d\n", priv->eeprom_version);
1197
1198         /*
1199          *  HW RF Kill enable is bit 0 in byte at offset 0x21 in firmware
1200          *
1201          *  notice that the EEPROM bit is reverse polarity, i.e.
1202          *     bit = 0  signifies HW RF kill switch is supported
1203          *     bit = 1  signifies HW RF kill switch is NOT supported
1204          */
1205         read_nic_dword(priv->net_dev, addr + 0x20, &val);
1206         if (!((val >> 24) & 0x01))
1207                 priv->hw_features |= HW_FEATURE_RFKILL;
1208
1209         IPW_DEBUG_INFO("HW RF Kill: %ssupported.\n",
1210                            (priv->hw_features & HW_FEATURE_RFKILL) ?
1211                            "" : "not ");
1212
1213         return 0;
1214 }
1215
1216 /*
1217  * Start firmware execution after power on and intialization
1218  * The sequence is:
1219  *  1. Release ARC
1220  *  2. Wait for f/w initialization completes;
1221  */
1222 static int ipw2100_start_adapter(struct ipw2100_priv *priv)
1223 {
1224         int i;
1225         u32 inta, inta_mask, gpio;
1226
1227         IPW_DEBUG_INFO("enter\n");
1228
1229         if (priv->status & STATUS_RUNNING)
1230                 return 0;
1231
1232         /*
1233          * Initialize the hw - drive adapter to DO state by setting
1234          * init_done bit. Wait for clk_ready bit and Download
1235          * fw & dino ucode
1236          */
1237         if (ipw2100_download_firmware(priv)) {
1238                 printk(KERN_ERR DRV_NAME ": %s: Failed to power on the adapter.\n",
1239                        priv->net_dev->name);
1240                 return -EIO;
1241         }
1242
1243         /* Clear the Tx, Rx and Msg queues and the r/w indexes
1244          * in the firmware RBD and TBD ring queue */
1245         ipw2100_queues_initialize(priv);
1246
1247         ipw2100_hw_set_gpio(priv);
1248
1249         /* TODO -- Look at disabling interrupts here to make sure none
1250          * get fired during FW initialization */
1251
1252         /* Release ARC - clear reset bit */
1253         write_register(priv->net_dev, IPW_REG_RESET_REG, 0);
1254
1255         /* wait for f/w intialization complete */
1256         IPW_DEBUG_FW("Waiting for f/w initialization to complete...\n");
1257         i = 5000;
1258         do {
1259                 set_current_state(TASK_UNINTERRUPTIBLE);
1260                 schedule_timeout(40 * HZ / 1000);
1261                 /* Todo... wait for sync command ... */
1262
1263                 read_register(priv->net_dev, IPW_REG_INTA, &inta);
1264
1265                 /* check "init done" bit */
1266                 if (inta & IPW2100_INTA_FW_INIT_DONE) {
1267                         /* reset "init done" bit */
1268                         write_register(priv->net_dev, IPW_REG_INTA,
1269                                        IPW2100_INTA_FW_INIT_DONE);
1270                         break;
1271                 }
1272
1273                 /* check error conditions : we check these after the firmware
1274                  * check so that if there is an error, the interrupt handler
1275                  * will see it and the adapter will be reset */
1276                 if (inta &
1277                     (IPW2100_INTA_FATAL_ERROR | IPW2100_INTA_PARITY_ERROR)) {
1278                         /* clear error conditions */
1279                         write_register(priv->net_dev, IPW_REG_INTA,
1280                                        IPW2100_INTA_FATAL_ERROR |
1281                                        IPW2100_INTA_PARITY_ERROR);
1282                 }
1283         } while (i--);
1284
1285         /* Clear out any pending INTAs since we aren't supposed to have
1286          * interrupts enabled at this point... */
1287         read_register(priv->net_dev, IPW_REG_INTA, &inta);
1288         read_register(priv->net_dev, IPW_REG_INTA_MASK, &inta_mask);
1289         inta &= IPW_INTERRUPT_MASK;
1290         /* Clear out any pending interrupts */
1291         if (inta & inta_mask)
1292                 write_register(priv->net_dev, IPW_REG_INTA, inta);
1293
1294         IPW_DEBUG_FW("f/w initialization complete: %s\n",
1295                      i ? "SUCCESS" : "FAILED");
1296
1297         if (!i) {
1298                 printk(KERN_WARNING DRV_NAME ": %s: Firmware did not initialize.\n",
1299                        priv->net_dev->name);
1300                 return -EIO;
1301         }
1302
1303         /* allow firmware to write to GPIO1 & GPIO3 */
1304         read_register(priv->net_dev, IPW_REG_GPIO, &gpio);
1305
1306         gpio |= (IPW_BIT_GPIO_GPIO1_MASK | IPW_BIT_GPIO_GPIO3_MASK);
1307
1308         write_register(priv->net_dev, IPW_REG_GPIO, gpio);
1309
1310         /* Ready to receive commands */
1311         priv->status |= STATUS_RUNNING;
1312
1313         /* The adapter has been reset; we are not associated */
1314         priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
1315
1316         IPW_DEBUG_INFO("exit\n");
1317
1318         return 0;
1319 }
1320
1321 static inline void ipw2100_reset_fatalerror(struct ipw2100_priv *priv)
1322 {
1323         if (!priv->fatal_error)
1324                 return;
1325
1326         priv->fatal_errors[priv->fatal_index++] = priv->fatal_error;
1327         priv->fatal_index %= IPW2100_ERROR_QUEUE;
1328         priv->fatal_error = 0;
1329 }
1330
1331
1332 /* NOTE: Our interrupt is disabled when this method is called */
1333 static int ipw2100_power_cycle_adapter(struct ipw2100_priv *priv)
1334 {
1335         u32 reg;
1336         int i;
1337
1338         IPW_DEBUG_INFO("Power cycling the hardware.\n");
1339
1340         ipw2100_hw_set_gpio(priv);
1341
1342         /* Step 1. Stop Master Assert */
1343         write_register(priv->net_dev, IPW_REG_RESET_REG,
1344                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
1345
1346         /* Step 2. Wait for stop Master Assert
1347          *         (not more then 50us, otherwise ret error */
1348         i = 5;
1349         do {
1350                 udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
1351                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
1352
1353                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
1354                         break;
1355         }  while(i--);
1356
1357         priv->status &= ~STATUS_RESET_PENDING;
1358
1359         if (!i) {
1360                 IPW_DEBUG_INFO("exit - waited too long for master assert stop\n");
1361                 return -EIO;
1362         }
1363
1364         write_register(priv->net_dev, IPW_REG_RESET_REG,
1365                        IPW_AUX_HOST_RESET_REG_SW_RESET);
1366
1367
1368         /* Reset any fatal_error conditions */
1369         ipw2100_reset_fatalerror(priv);
1370
1371         /* At this point, the adapter is now stopped and disabled */
1372         priv->status &= ~(STATUS_RUNNING | STATUS_ASSOCIATING |
1373                           STATUS_ASSOCIATED | STATUS_ENABLED);
1374
1375         return 0;
1376 }
1377
1378 /*
1379  * Send the CARD_DISABLE_PHY_OFF comamnd to the card to disable it
1380  *
1381  * After disabling, if the card was associated, a STATUS_ASSN_LOST will be sent.
1382  *
1383  * STATUS_CARD_DISABLE_NOTIFICATION will be sent regardless of
1384  * if STATUS_ASSN_LOST is sent.
1385  */
1386 static int ipw2100_hw_phy_off(struct ipw2100_priv *priv)
1387 {
1388
1389 #define HW_PHY_OFF_LOOP_DELAY (HZ / 5000)
1390
1391         struct host_command cmd = {
1392                 .host_command = CARD_DISABLE_PHY_OFF,
1393                 .host_command_sequence = 0,
1394                 .host_command_length = 0,
1395         };
1396         int err, i;
1397         u32 val1, val2;
1398
1399         IPW_DEBUG_HC("CARD_DISABLE_PHY_OFF\n");
1400
1401         /* Turn off the radio */
1402         err = ipw2100_hw_send_command(priv, &cmd);
1403         if (err)
1404                 return err;
1405
1406         for (i = 0; i < 2500; i++) {
1407                 read_nic_dword(priv->net_dev, IPW2100_CONTROL_REG, &val1);
1408                 read_nic_dword(priv->net_dev, IPW2100_COMMAND, &val2);
1409
1410                 if ((val1 & IPW2100_CONTROL_PHY_OFF) &&
1411                     (val2 & IPW2100_COMMAND_PHY_OFF))
1412                         return 0;
1413
1414                 set_current_state(TASK_UNINTERRUPTIBLE);
1415                 schedule_timeout(HW_PHY_OFF_LOOP_DELAY);
1416         }
1417
1418         return -EIO;
1419 }
1420
1421
1422 static int ipw2100_enable_adapter(struct ipw2100_priv *priv)
1423 {
1424         struct host_command cmd = {
1425                 .host_command = HOST_COMPLETE,
1426                 .host_command_sequence = 0,
1427                 .host_command_length = 0
1428         };
1429         int err = 0;
1430
1431         IPW_DEBUG_HC("HOST_COMPLETE\n");
1432
1433         if (priv->status & STATUS_ENABLED)
1434                 return 0;
1435
1436         down(&priv->adapter_sem);
1437
1438         if (rf_kill_active(priv)) {
1439                 IPW_DEBUG_HC("Command aborted due to RF kill active.\n");
1440                 goto fail_up;
1441         }
1442
1443         err = ipw2100_hw_send_command(priv, &cmd);
1444         if (err) {
1445                 IPW_DEBUG_INFO("Failed to send HOST_COMPLETE command\n");
1446                 goto fail_up;
1447         }
1448
1449         err = ipw2100_wait_for_card_state(priv, IPW_HW_STATE_ENABLED);
1450         if (err) {
1451                 IPW_DEBUG_INFO(
1452                        "%s: card not responding to init command.\n",
1453                        priv->net_dev->name);
1454                 goto fail_up;
1455         }
1456
1457         if (priv->stop_hang_check) {
1458                 priv->stop_hang_check = 0;
1459                 queue_delayed_work(priv->workqueue, &priv->hang_check, HZ / 2);
1460         }
1461
1462 fail_up:
1463         up(&priv->adapter_sem);
1464         return err;
1465 }
1466
1467 static int ipw2100_hw_stop_adapter(struct ipw2100_priv *priv)
1468 {
1469 #define HW_POWER_DOWN_DELAY (HZ / 10)
1470
1471         struct host_command cmd = {
1472                 .host_command = HOST_PRE_POWER_DOWN,
1473                 .host_command_sequence = 0,
1474                 .host_command_length = 0,
1475         };
1476         int err, i;
1477         u32 reg;
1478
1479         if (!(priv->status & STATUS_RUNNING))
1480                 return 0;
1481
1482         priv->status |= STATUS_STOPPING;
1483
1484         /* We can only shut down the card if the firmware is operational.  So,
1485          * if we haven't reset since a fatal_error, then we can not send the
1486          * shutdown commands. */
1487         if (!priv->fatal_error) {
1488                 /* First, make sure the adapter is enabled so that the PHY_OFF
1489                  * command can shut it down */
1490                 ipw2100_enable_adapter(priv);
1491
1492                 err = ipw2100_hw_phy_off(priv);
1493                 if (err)
1494                         printk(KERN_WARNING DRV_NAME ": Error disabling radio %d\n", err);
1495
1496                 /*
1497                  * If in D0-standby mode going directly to D3 may cause a
1498                  * PCI bus violation.  Therefore we must change out of the D0
1499                  * state.
1500                  *
1501                  * Sending the PREPARE_FOR_POWER_DOWN will restrict the
1502                  * hardware from going into standby mode and will transition
1503                  * out of D0-standy if it is already in that state.
1504                  *
1505                  * STATUS_PREPARE_POWER_DOWN_COMPLETE will be sent by the
1506                  * driver upon completion.  Once received, the driver can
1507                  * proceed to the D3 state.
1508                  *
1509                  * Prepare for power down command to fw.  This command would
1510                  * take HW out of D0-standby and prepare it for D3 state.
1511                  *
1512                  * Currently FW does not support event notification for this
1513                  * event. Therefore, skip waiting for it.  Just wait a fixed
1514                  * 100ms
1515                  */
1516                 IPW_DEBUG_HC("HOST_PRE_POWER_DOWN\n");
1517
1518                 err = ipw2100_hw_send_command(priv, &cmd);
1519                 if (err)
1520                         printk(KERN_WARNING DRV_NAME ": "
1521                                "%s: Power down command failed: Error %d\n",
1522                                priv->net_dev->name, err);
1523                 else {
1524                         set_current_state(TASK_UNINTERRUPTIBLE);
1525                         schedule_timeout(HW_POWER_DOWN_DELAY);
1526                 }
1527         }
1528
1529         priv->status &= ~STATUS_ENABLED;
1530
1531         /*
1532          * Set GPIO 3 writable by FW; GPIO 1 writable
1533          * by driver and enable clock
1534          */
1535         ipw2100_hw_set_gpio(priv);
1536
1537         /*
1538          * Power down adapter.  Sequence:
1539          * 1. Stop master assert (RESET_REG[9]=1)
1540          * 2. Wait for stop master (RESET_REG[8]==1)
1541          * 3. S/w reset assert (RESET_REG[7] = 1)
1542          */
1543
1544         /* Stop master assert */
1545         write_register(priv->net_dev, IPW_REG_RESET_REG,
1546                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
1547
1548         /* wait stop master not more than 50 usec.
1549          * Otherwise return error. */
1550         for (i = 5; i > 0; i--) {
1551                 udelay(10);
1552
1553                 /* Check master stop bit */
1554                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
1555
1556                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
1557                         break;
1558         }
1559
1560         if (i == 0)
1561                 printk(KERN_WARNING DRV_NAME
1562                        ": %s: Could now power down adapter.\n",
1563                        priv->net_dev->name);
1564
1565         /* assert s/w reset */
1566         write_register(priv->net_dev, IPW_REG_RESET_REG,
1567                        IPW_AUX_HOST_RESET_REG_SW_RESET);
1568
1569         priv->status &= ~(STATUS_RUNNING | STATUS_STOPPING);
1570
1571         return 0;
1572 }
1573
1574
1575 static int ipw2100_disable_adapter(struct ipw2100_priv *priv)
1576 {
1577         struct host_command cmd = {
1578                 .host_command = CARD_DISABLE,
1579                 .host_command_sequence = 0,
1580                 .host_command_length = 0
1581         };
1582         int err = 0;
1583
1584         IPW_DEBUG_HC("CARD_DISABLE\n");
1585
1586         if (!(priv->status & STATUS_ENABLED))
1587                 return 0;
1588
1589         /* Make sure we clear the associated state */
1590         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1591
1592         if (!priv->stop_hang_check) {
1593                 priv->stop_hang_check = 1;
1594                 cancel_delayed_work(&priv->hang_check);
1595         }
1596
1597         down(&priv->adapter_sem);
1598
1599         err = ipw2100_hw_send_command(priv, &cmd);
1600         if (err) {
1601                 printk(KERN_WARNING DRV_NAME ": exit - failed to send CARD_DISABLE command\n");
1602                 goto fail_up;
1603         }
1604
1605         err = ipw2100_wait_for_card_state(priv, IPW_HW_STATE_DISABLED);
1606         if (err) {
1607                 printk(KERN_WARNING DRV_NAME ": exit - card failed to change to DISABLED\n");
1608                 goto fail_up;
1609         }
1610
1611         IPW_DEBUG_INFO("TODO: implement scan state machine\n");
1612
1613 fail_up:
1614         up(&priv->adapter_sem);
1615         return err;
1616 }
1617
1618 static int ipw2100_set_scan_options(struct ipw2100_priv *priv)
1619 {
1620         struct host_command cmd = {
1621                 .host_command = SET_SCAN_OPTIONS,
1622                 .host_command_sequence = 0,
1623                 .host_command_length = 8
1624         };
1625         int err;
1626
1627         IPW_DEBUG_INFO("enter\n");
1628
1629         IPW_DEBUG_SCAN("setting scan options\n");
1630
1631         cmd.host_command_parameters[0] = 0;
1632
1633         if (!(priv->config & CFG_ASSOCIATE))
1634                 cmd.host_command_parameters[0] |= IPW_SCAN_NOASSOCIATE;
1635         if ((priv->sec.flags & SEC_ENABLED) && priv->sec.enabled)
1636                 cmd.host_command_parameters[0] |= IPW_SCAN_MIXED_CELL;
1637         if (priv->config & CFG_PASSIVE_SCAN)
1638                 cmd.host_command_parameters[0] |= IPW_SCAN_PASSIVE;
1639
1640         cmd.host_command_parameters[1] = priv->channel_mask;
1641
1642         err = ipw2100_hw_send_command(priv, &cmd);
1643
1644         IPW_DEBUG_HC("SET_SCAN_OPTIONS 0x%04X\n",
1645                      cmd.host_command_parameters[0]);
1646
1647         return err;
1648 }
1649
1650 static int ipw2100_start_scan(struct ipw2100_priv *priv)
1651 {
1652         struct host_command cmd = {
1653                 .host_command = BROADCAST_SCAN,
1654                 .host_command_sequence = 0,
1655                 .host_command_length = 4
1656         };
1657         int err;
1658
1659         IPW_DEBUG_HC("START_SCAN\n");
1660
1661         cmd.host_command_parameters[0] = 0;
1662
1663         /* No scanning if in monitor mode */
1664         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
1665                 return 1;
1666
1667         if (priv->status & STATUS_SCANNING) {
1668                 IPW_DEBUG_SCAN("Scan requested while already in scan...\n");
1669                 return 0;
1670         }
1671
1672         IPW_DEBUG_INFO("enter\n");
1673
1674         /* Not clearing here; doing so makes iwlist always return nothing...
1675          *
1676          * We should modify the table logic to use aging tables vs. clearing
1677          * the table on each scan start.
1678          */
1679         IPW_DEBUG_SCAN("starting scan\n");
1680
1681         priv->status |= STATUS_SCANNING;
1682         err = ipw2100_hw_send_command(priv, &cmd);
1683         if (err)
1684                 priv->status &= ~STATUS_SCANNING;
1685
1686         IPW_DEBUG_INFO("exit\n");
1687
1688         return err;
1689 }
1690
1691 static int ipw2100_up(struct ipw2100_priv *priv, int deferred)
1692 {
1693         unsigned long flags;
1694         int rc = 0;
1695         u32 lock;
1696         u32 ord_len = sizeof(lock);
1697
1698         /* Quite if manually disabled. */
1699         if (priv->status & STATUS_RF_KILL_SW) {
1700                 IPW_DEBUG_INFO("%s: Radio is disabled by Manual Disable "
1701                                "switch\n", priv->net_dev->name);
1702                 return 0;
1703         }
1704
1705         /* If the interrupt is enabled, turn it off... */
1706         spin_lock_irqsave(&priv->low_lock, flags);
1707         ipw2100_disable_interrupts(priv);
1708
1709         /* Reset any fatal_error conditions */
1710         ipw2100_reset_fatalerror(priv);
1711         spin_unlock_irqrestore(&priv->low_lock, flags);
1712
1713         if (priv->status & STATUS_POWERED ||
1714             (priv->status & STATUS_RESET_PENDING)) {
1715                 /* Power cycle the card ... */
1716                 if (ipw2100_power_cycle_adapter(priv)) {
1717                         printk(KERN_WARNING DRV_NAME ": %s: Could not cycle adapter.\n",
1718                                           priv->net_dev->name);
1719                         rc = 1;
1720                         goto exit;
1721                 }
1722         } else
1723                 priv->status |= STATUS_POWERED;
1724
1725         /* Load the firmware, start the clocks, etc. */
1726         if (ipw2100_start_adapter(priv)) {
1727                 printk(KERN_ERR DRV_NAME ": %s: Failed to start the firmware.\n",
1728                                 priv->net_dev->name);
1729                 rc = 1;
1730                 goto exit;
1731         }
1732
1733         ipw2100_initialize_ordinals(priv);
1734
1735         /* Determine capabilities of this particular HW configuration */
1736         if (ipw2100_get_hw_features(priv)) {
1737                 printk(KERN_ERR DRV_NAME ": %s: Failed to determine HW features.\n",
1738                                 priv->net_dev->name);
1739                 rc = 1;
1740                 goto exit;
1741         }
1742
1743         lock = LOCK_NONE;
1744         if (ipw2100_set_ordinal(priv, IPW_ORD_PERS_DB_LOCK, &lock, &ord_len)) {
1745                 printk(KERN_ERR DRV_NAME ": %s: Failed to clear ordinal lock.\n",
1746                                 priv->net_dev->name);
1747                 rc = 1;
1748                 goto exit;
1749         }
1750
1751         priv->status &= ~STATUS_SCANNING;
1752
1753         if (rf_kill_active(priv)) {
1754                 printk(KERN_INFO "%s: Radio is disabled by RF switch.\n",
1755                        priv->net_dev->name);
1756
1757                 if (priv->stop_rf_kill) {
1758                         priv->stop_rf_kill = 0;
1759                         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
1760                 }
1761
1762                 deferred = 1;
1763         }
1764
1765         /* Turn on the interrupt so that commands can be processed */
1766         ipw2100_enable_interrupts(priv);
1767
1768         /* Send all of the commands that must be sent prior to
1769          * HOST_COMPLETE */
1770         if (ipw2100_adapter_setup(priv)) {
1771                 printk(KERN_ERR DRV_NAME ": %s: Failed to start the card.\n",
1772                                 priv->net_dev->name);
1773                 rc = 1;
1774                 goto exit;
1775         }
1776
1777         if (!deferred) {
1778                 /* Enable the adapter - sends HOST_COMPLETE */
1779                 if (ipw2100_enable_adapter(priv)) {
1780                         printk(KERN_ERR DRV_NAME ": "
1781                                 "%s: failed in call to enable adapter.\n",
1782                                 priv->net_dev->name);
1783                         ipw2100_hw_stop_adapter(priv);
1784                         rc = 1;
1785                         goto exit;
1786                 }
1787
1788
1789                 /* Start a scan . . . */
1790                 ipw2100_set_scan_options(priv);
1791                 ipw2100_start_scan(priv);
1792         }
1793
1794  exit:
1795         return rc;
1796 }
1797
1798 /* Called by register_netdev() */
1799 static int ipw2100_net_init(struct net_device *dev)
1800 {
1801         struct ipw2100_priv *priv = ieee80211_priv(dev);
1802         return ipw2100_up(priv, 1);
1803 }
1804
1805 static void ipw2100_down(struct ipw2100_priv *priv)
1806 {
1807         unsigned long flags;
1808         union iwreq_data wrqu = {
1809                 .ap_addr = {
1810                         .sa_family = ARPHRD_ETHER
1811                 }
1812         };
1813         int associated = priv->status & STATUS_ASSOCIATED;
1814
1815         /* Kill the RF switch timer */
1816         if (!priv->stop_rf_kill) {
1817                 priv->stop_rf_kill = 1;
1818                 cancel_delayed_work(&priv->rf_kill);
1819         }
1820
1821         /* Kill the firmare hang check timer */
1822         if (!priv->stop_hang_check) {
1823                 priv->stop_hang_check = 1;
1824                 cancel_delayed_work(&priv->hang_check);
1825         }
1826
1827         /* Kill any pending resets */
1828         if (priv->status & STATUS_RESET_PENDING)
1829                 cancel_delayed_work(&priv->reset_work);
1830
1831         /* Make sure the interrupt is on so that FW commands will be
1832          * processed correctly */
1833         spin_lock_irqsave(&priv->low_lock, flags);
1834         ipw2100_enable_interrupts(priv);
1835         spin_unlock_irqrestore(&priv->low_lock, flags);
1836
1837         if (ipw2100_hw_stop_adapter(priv))
1838                 printk(KERN_ERR DRV_NAME ": %s: Error stopping adapter.\n",
1839                        priv->net_dev->name);
1840
1841         /* Do not disable the interrupt until _after_ we disable
1842          * the adaptor.  Otherwise the CARD_DISABLE command will never
1843          * be ack'd by the firmware */
1844         spin_lock_irqsave(&priv->low_lock, flags);
1845         ipw2100_disable_interrupts(priv);
1846         spin_unlock_irqrestore(&priv->low_lock, flags);
1847
1848 #ifdef ACPI_CSTATE_LIMIT_DEFINED
1849         if (priv->config & CFG_C3_DISABLED) {
1850                 IPW_DEBUG_INFO(DRV_NAME ": Resetting C3 transitions.\n");
1851                 acpi_set_cstate_limit(priv->cstate_limit);
1852                 priv->config &= ~CFG_C3_DISABLED;
1853         }
1854 #endif
1855
1856         /* We have to signal any supplicant if we are disassociating */
1857         if (associated)
1858                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1859
1860         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1861         netif_carrier_off(priv->net_dev);
1862         netif_stop_queue(priv->net_dev);
1863 }
1864
1865 static void ipw2100_reset_adapter(struct ipw2100_priv *priv)
1866 {
1867         unsigned long flags;
1868         union iwreq_data wrqu = {
1869                 .ap_addr = {
1870                         .sa_family = ARPHRD_ETHER
1871                 }
1872         };
1873         int associated = priv->status & STATUS_ASSOCIATED;
1874
1875         spin_lock_irqsave(&priv->low_lock, flags);
1876         IPW_DEBUG_INFO(DRV_NAME ": %s: Restarting adapter.\n",
1877                        priv->net_dev->name);
1878         priv->resets++;
1879         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1880         priv->status |= STATUS_SECURITY_UPDATED;
1881
1882         /* Force a power cycle even if interface hasn't been opened
1883          * yet */
1884         cancel_delayed_work(&priv->reset_work);
1885         priv->status |= STATUS_RESET_PENDING;
1886         spin_unlock_irqrestore(&priv->low_lock, flags);
1887
1888         down(&priv->action_sem);
1889         /* stop timed checks so that they don't interfere with reset */
1890         priv->stop_hang_check = 1;
1891         cancel_delayed_work(&priv->hang_check);
1892
1893         /* We have to signal any supplicant if we are disassociating */
1894         if (associated)
1895                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1896
1897         ipw2100_up(priv, 0);
1898         up(&priv->action_sem);
1899
1900 }
1901
1902
1903 static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
1904 {
1905
1906 #define MAC_ASSOCIATION_READ_DELAY (HZ)
1907         int ret, len, essid_len;
1908         char essid[IW_ESSID_MAX_SIZE];
1909         u32 txrate;
1910         u32 chan;
1911         char *txratename;
1912         u8 bssid[ETH_ALEN];
1913
1914         /*
1915          * TBD: BSSID is usually 00:00:00:00:00:00 here and not
1916          *      an actual MAC of the AP. Seems like FW sets this
1917          *      address too late. Read it later and expose through
1918          *      /proc or schedule a later task to query and update
1919          */
1920
1921         essid_len = IW_ESSID_MAX_SIZE;
1922         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID,
1923                                   essid, &essid_len);
1924         if (ret) {
1925                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1926                                    __LINE__);
1927                 return;
1928         }
1929
1930         len = sizeof(u32);
1931         ret = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE,
1932                                   &txrate, &len);
1933         if (ret) {
1934                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1935                                    __LINE__);
1936                 return;
1937         }
1938
1939         len = sizeof(u32);
1940         ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &len);
1941         if (ret) {
1942                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1943                                    __LINE__);
1944                 return;
1945         }
1946         len = ETH_ALEN;
1947         ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID, &bssid,  &len);
1948         if (ret) {
1949                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1950                                    __LINE__);
1951                 return;
1952         }
1953         memcpy(priv->ieee->bssid, bssid, ETH_ALEN);
1954
1955
1956         switch (txrate) {
1957         case TX_RATE_1_MBIT:
1958                 txratename = "1Mbps";
1959                 break;
1960         case TX_RATE_2_MBIT:
1961                 txratename = "2Mbsp";
1962                 break;
1963         case TX_RATE_5_5_MBIT:
1964                 txratename = "5.5Mbps";
1965                 break;
1966         case TX_RATE_11_MBIT:
1967                 txratename = "11Mbps";
1968                 break;
1969         default:
1970                 IPW_DEBUG_INFO("Unknown rate: %d\n", txrate);
1971                 txratename = "unknown rate";
1972                 break;
1973         }
1974
1975         IPW_DEBUG_INFO("%s: Associated with '%s' at %s, channel %d (BSSID="
1976                        MAC_FMT ")\n",
1977                        priv->net_dev->name, escape_essid(essid, essid_len),
1978                        txratename, chan, MAC_ARG(bssid));
1979
1980         /* now we copy read ssid into dev */
1981         if (!(priv->config & CFG_STATIC_ESSID)) {
1982                 priv->essid_len = min((u8)essid_len, (u8)IW_ESSID_MAX_SIZE);
1983                 memcpy(priv->essid, essid, priv->essid_len);
1984         }
1985         priv->channel = chan;
1986         memcpy(priv->bssid, bssid, ETH_ALEN);
1987
1988         priv->status |= STATUS_ASSOCIATING;
1989         priv->connect_start = get_seconds();
1990
1991         queue_delayed_work(priv->workqueue, &priv->wx_event_work, HZ / 10);
1992 }
1993
1994
1995 static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
1996                              int length, int batch_mode)
1997 {
1998         int ssid_len = min(length, IW_ESSID_MAX_SIZE);
1999         struct host_command cmd = {
2000                 .host_command = SSID,
2001                 .host_command_sequence = 0,
2002                 .host_command_length = ssid_len
2003         };
2004         int err;
2005
2006         IPW_DEBUG_HC("SSID: '%s'\n", escape_essid(essid, ssid_len));
2007
2008         if (ssid_len)
2009                 memcpy((char*)cmd.host_command_parameters,
2010                        essid, ssid_len);
2011
2012         if (!batch_mode) {
2013                 err = ipw2100_disable_adapter(priv);
2014                 if (err)
2015                         return err;
2016         }
2017
2018         /* Bug in FW currently doesn't honor bit 0 in SET_SCAN_OPTIONS to
2019          * disable auto association -- so we cheat by setting a bogus SSID */
2020         if (!ssid_len && !(priv->config & CFG_ASSOCIATE)) {
2021                 int i;
2022                 u8 *bogus = (u8*)cmd.host_command_parameters;
2023                 for (i = 0; i < IW_ESSID_MAX_SIZE; i++)
2024                         bogus[i] = 0x18 + i;
2025                 cmd.host_command_length = IW_ESSID_MAX_SIZE;
2026         }
2027
2028         /* NOTE:  We always send the SSID command even if the provided ESSID is
2029          * the same as what we currently think is set. */
2030
2031         err = ipw2100_hw_send_command(priv, &cmd);
2032         if (!err) {
2033                 memset(priv->essid + ssid_len, 0,
2034                        IW_ESSID_MAX_SIZE - ssid_len);
2035                 memcpy(priv->essid, essid, ssid_len);
2036                 priv->essid_len = ssid_len;
2037         }
2038
2039         if (!batch_mode) {
2040                 if (ipw2100_enable_adapter(priv))
2041                         err = -EIO;
2042         }
2043
2044         return err;
2045 }
2046
2047 static void isr_indicate_association_lost(struct ipw2100_priv *priv, u32 status)
2048 {
2049         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE | IPW_DL_ASSOC,
2050                   "disassociated: '%s' " MAC_FMT " \n",
2051                   escape_essid(priv->essid, priv->essid_len),
2052                   MAC_ARG(priv->bssid));
2053
2054         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
2055
2056         if (priv->status & STATUS_STOPPING) {
2057                 IPW_DEBUG_INFO("Card is stopping itself, discard ASSN_LOST.\n");
2058                 return;
2059         }
2060
2061         memset(priv->bssid, 0, ETH_ALEN);
2062         memset(priv->ieee->bssid, 0, ETH_ALEN);
2063
2064         netif_carrier_off(priv->net_dev);
2065         netif_stop_queue(priv->net_dev);
2066
2067         if (!(priv->status & STATUS_RUNNING))
2068                 return;
2069
2070         if (priv->status & STATUS_SECURITY_UPDATED)
2071                 queue_work(priv->workqueue, &priv->security_work);
2072
2073         queue_work(priv->workqueue, &priv->wx_event_work);
2074 }
2075
2076 static void isr_indicate_rf_kill(struct ipw2100_priv *priv, u32 status)
2077 {
2078         IPW_DEBUG_INFO("%s: RF Kill state changed to radio OFF.\n",
2079                priv->net_dev->name);
2080
2081         /* RF_KILL is now enabled (else we wouldn't be here) */
2082         priv->status |= STATUS_RF_KILL_HW;
2083
2084 #ifdef ACPI_CSTATE_LIMIT_DEFINED
2085         if (priv->config & CFG_C3_DISABLED) {
2086                 IPW_DEBUG_INFO(DRV_NAME ": Resetting C3 transitions.\n");
2087                 acpi_set_cstate_limit(priv->cstate_limit);
2088                 priv->config &= ~CFG_C3_DISABLED;
2089         }
2090 #endif
2091
2092         /* Make sure the RF Kill check timer is running */
2093         priv->stop_rf_kill = 0;
2094         cancel_delayed_work(&priv->rf_kill);
2095         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
2096 }
2097
2098 static void isr_scan_complete(struct ipw2100_priv *priv, u32 status)
2099 {
2100         IPW_DEBUG_SCAN("scan complete\n");
2101         /* Age the scan results... */
2102         priv->ieee->scans++;
2103         priv->status &= ~STATUS_SCANNING;
2104 }
2105
2106 #ifdef CONFIG_IPW_DEBUG
2107 #define IPW2100_HANDLER(v, f) { v, f, # v }
2108 struct ipw2100_status_indicator {
2109         int status;
2110         void (*cb)(struct ipw2100_priv *priv, u32 status);
2111         char *name;
2112 };
2113 #else
2114 #define IPW2100_HANDLER(v, f) { v, f }
2115 struct ipw2100_status_indicator {
2116         int status;
2117         void (*cb)(struct ipw2100_priv *priv, u32 status);
2118 };
2119 #endif /* CONFIG_IPW_DEBUG */
2120
2121 static void isr_indicate_scanning(struct ipw2100_priv *priv, u32 status)
2122 {
2123         IPW_DEBUG_SCAN("Scanning...\n");
2124         priv->status |= STATUS_SCANNING;
2125 }
2126
2127 static const struct ipw2100_status_indicator status_handlers[] = {
2128         IPW2100_HANDLER(IPW_STATE_INITIALIZED, 0),
2129         IPW2100_HANDLER(IPW_STATE_COUNTRY_FOUND, 0),
2130         IPW2100_HANDLER(IPW_STATE_ASSOCIATED, isr_indicate_associated),
2131         IPW2100_HANDLER(IPW_STATE_ASSN_LOST, isr_indicate_association_lost),
2132         IPW2100_HANDLER(IPW_STATE_ASSN_CHANGED, 0),
2133         IPW2100_HANDLER(IPW_STATE_SCAN_COMPLETE, isr_scan_complete),
2134         IPW2100_HANDLER(IPW_STATE_ENTERED_PSP, 0),
2135         IPW2100_HANDLER(IPW_STATE_LEFT_PSP, 0),
2136         IPW2100_HANDLER(IPW_STATE_RF_KILL, isr_indicate_rf_kill),
2137         IPW2100_HANDLER(IPW_STATE_DISABLED, 0),
2138         IPW2100_HANDLER(IPW_STATE_POWER_DOWN, 0),
2139         IPW2100_HANDLER(IPW_STATE_SCANNING, isr_indicate_scanning),
2140         IPW2100_HANDLER(-1, 0)
2141 };
2142
2143
2144 static void isr_status_change(struct ipw2100_priv *priv, int status)
2145 {
2146         int i;
2147
2148         if (status == IPW_STATE_SCANNING &&
2149             priv->status & STATUS_ASSOCIATED &&
2150             !(priv->status & STATUS_SCANNING)) {
2151                 IPW_DEBUG_INFO("Scan detected while associated, with "
2152                                "no scan request.  Restarting firmware.\n");
2153
2154                 /* Wake up any sleeping jobs */
2155                 schedule_reset(priv);
2156         }
2157
2158         for (i = 0; status_handlers[i].status != -1; i++) {
2159                 if (status == status_handlers[i].status) {
2160                         IPW_DEBUG_NOTIF("Status change: %s\n",
2161                                          status_handlers[i].name);
2162                         if (status_handlers[i].cb)
2163                                 status_handlers[i].cb(priv, status);
2164                         priv->wstats.status = status;
2165                         return;
2166                 }
2167         }
2168
2169         IPW_DEBUG_NOTIF("unknown status received: %04x\n", status);
2170 }
2171
2172 static void isr_rx_complete_command(
2173         struct ipw2100_priv *priv,
2174         struct ipw2100_cmd_header *cmd)
2175 {
2176 #ifdef CONFIG_IPW_DEBUG
2177         if (cmd->host_command_reg < ARRAY_SIZE(command_types)) {
2178                 IPW_DEBUG_HC("Command completed '%s (%d)'\n",
2179                              command_types[cmd->host_command_reg],
2180                              cmd->host_command_reg);
2181         }
2182 #endif
2183         if (cmd->host_command_reg == HOST_COMPLETE)
2184                 priv->status |= STATUS_ENABLED;
2185
2186         if (cmd->host_command_reg == CARD_DISABLE)
2187                 priv->status &= ~STATUS_ENABLED;
2188
2189         priv->status &= ~STATUS_CMD_ACTIVE;
2190
2191         wake_up_interruptible(&priv->wait_command_queue);
2192 }
2193
2194 #ifdef CONFIG_IPW_DEBUG
2195 static const char *frame_types[] = {
2196         "COMMAND_STATUS_VAL",
2197         "STATUS_CHANGE_VAL",
2198         "P80211_DATA_VAL",
2199         "P8023_DATA_VAL",
2200         "HOST_NOTIFICATION_VAL"
2201 };
2202 #endif
2203
2204
2205 static inline int ipw2100_alloc_skb(
2206         struct ipw2100_priv *priv,
2207         struct ipw2100_rx_packet *packet)
2208 {
2209         packet->skb = dev_alloc_skb(sizeof(struct ipw2100_rx));
2210         if (!packet->skb)
2211                 return -ENOMEM;
2212
2213         packet->rxp = (struct ipw2100_rx *)packet->skb->data;
2214         packet->dma_addr = pci_map_single(priv->pci_dev, packet->skb->data,
2215                                           sizeof(struct ipw2100_rx),
2216                                           PCI_DMA_FROMDEVICE);
2217         /* NOTE: pci_map_single does not return an error code, and 0 is a valid
2218          *       dma_addr */
2219
2220         return 0;
2221 }
2222
2223
2224 #define SEARCH_ERROR   0xffffffff
2225 #define SEARCH_FAIL    0xfffffffe
2226 #define SEARCH_SUCCESS 0xfffffff0
2227 #define SEARCH_DISCARD 0
2228 #define SEARCH_SNAPSHOT 1
2229
2230 #define SNAPSHOT_ADDR(ofs) (priv->snapshot[((ofs) >> 12) & 0xff] + ((ofs) & 0xfff))
2231 static inline int ipw2100_snapshot_alloc(struct ipw2100_priv *priv)
2232 {
2233         int i;
2234         if (priv->snapshot[0])
2235                 return 1;
2236         for (i = 0; i < 0x30; i++) {
2237                 priv->snapshot[i] = (u8*)kmalloc(0x1000, GFP_ATOMIC);
2238                 if (!priv->snapshot[i]) {
2239                         IPW_DEBUG_INFO("%s: Error allocating snapshot "
2240                                "buffer %d\n", priv->net_dev->name, i);
2241                         while (i > 0)
2242                                 kfree(priv->snapshot[--i]);
2243                         priv->snapshot[0] = NULL;
2244                         return 0;
2245                 }
2246         }
2247
2248         return 1;
2249 }
2250
2251 static inline void ipw2100_snapshot_free(struct ipw2100_priv *priv)
2252 {
2253         int i;
2254         if (!priv->snapshot[0])
2255                 return;
2256         for (i = 0; i < 0x30; i++)
2257                 kfree(priv->snapshot[i]);
2258         priv->snapshot[0] = NULL;
2259 }
2260
2261 static inline u32 ipw2100_match_buf(struct ipw2100_priv *priv, u8 *in_buf,
2262                                     size_t len, int mode)
2263 {
2264         u32 i, j;
2265         u32 tmp;
2266         u8 *s, *d;
2267         u32 ret;
2268
2269         s = in_buf;
2270         if (mode == SEARCH_SNAPSHOT) {
2271                 if (!ipw2100_snapshot_alloc(priv))
2272                         mode = SEARCH_DISCARD;
2273         }
2274
2275         for (ret = SEARCH_FAIL, i = 0; i < 0x30000; i += 4) {
2276                 read_nic_dword(priv->net_dev, i, &tmp);
2277                 if (mode == SEARCH_SNAPSHOT)
2278                         *(u32 *)SNAPSHOT_ADDR(i) = tmp;
2279                 if (ret == SEARCH_FAIL) {
2280                         d = (u8*)&tmp;
2281                         for (j = 0; j < 4; j++) {
2282                                 if (*s != *d) {
2283                                         s = in_buf;
2284                                         continue;
2285                                 }
2286
2287                                 s++;
2288                                 d++;
2289
2290                                 if ((s - in_buf) == len)
2291                                         ret = (i + j) - len + 1;
2292                         }
2293                 } else if (mode == SEARCH_DISCARD)
2294                         return ret;
2295         }
2296
2297         return ret;
2298 }
2299
2300 /*
2301  *
2302  * 0) Disconnect the SKB from the firmware (just unmap)
2303  * 1) Pack the ETH header into the SKB
2304  * 2) Pass the SKB to the network stack
2305  *
2306  * When packet is provided by the firmware, it contains the following:
2307  *
2308  * .  ieee80211_hdr
2309  * .  ieee80211_snap_hdr
2310  *
2311  * The size of the constructed ethernet
2312  *
2313  */
2314 #ifdef CONFIG_IPW2100_RX_DEBUG
2315 static u8 packet_data[IPW_RX_NIC_BUFFER_LENGTH];
2316 #endif
2317
2318 static inline void ipw2100_corruption_detected(struct ipw2100_priv *priv,
2319                                                int i)
2320 {
2321 #ifdef CONFIG_IPW_DEBUG_C3
2322         struct ipw2100_status *status = &priv->status_queue.drv[i];
2323         u32 match, reg;
2324         int j;
2325 #endif
2326 #ifdef ACPI_CSTATE_LIMIT_DEFINED
2327         int limit;
2328 #endif
2329
2330         IPW_DEBUG_INFO(DRV_NAME ": PCI latency error detected at "
2331                        "0x%04zX.\n", i * sizeof(struct ipw2100_status));
2332
2333 #ifdef ACPI_CSTATE_LIMIT_DEFINED
2334         IPW_DEBUG_INFO(DRV_NAME ": Disabling C3 transitions.\n");
2335         limit = acpi_get_cstate_limit();
2336         if (limit > 2) {
2337                 priv->cstate_limit = limit;
2338                 acpi_set_cstate_limit(2);
2339                 priv->config |= CFG_C3_DISABLED;
2340         }
2341 #endif
2342
2343 #ifdef CONFIG_IPW_DEBUG_C3
2344         /* Halt the fimrware so we can get a good image */
2345         write_register(priv->net_dev, IPW_REG_RESET_REG,
2346                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
2347         j = 5;
2348         do {
2349                 udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
2350                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
2351
2352                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
2353                         break;
2354         }  while (j--);
2355
2356         match = ipw2100_match_buf(priv, (u8*)status,
2357                                   sizeof(struct ipw2100_status),
2358                                   SEARCH_SNAPSHOT);
2359         if (match < SEARCH_SUCCESS)
2360                 IPW_DEBUG_INFO("%s: DMA status match in Firmware at "
2361                                "offset 0x%06X, length %d:\n",
2362                                priv->net_dev->name, match,
2363                                sizeof(struct ipw2100_status));
2364         else
2365                 IPW_DEBUG_INFO("%s: No DMA status match in "
2366                                "Firmware.\n", priv->net_dev->name);
2367
2368         printk_buf((u8*)priv->status_queue.drv,
2369                    sizeof(struct ipw2100_status) * RX_QUEUE_LENGTH);
2370 #endif
2371
2372         priv->fatal_error = IPW2100_ERR_C3_CORRUPTION;
2373         priv->ieee->stats.rx_errors++;
2374         schedule_reset(priv);
2375 }
2376
2377 static inline void isr_rx(struct ipw2100_priv *priv, int i,
2378                           struct ieee80211_rx_stats *stats)
2379 {
2380         struct ipw2100_status *status = &priv->status_queue.drv[i];
2381         struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
2382
2383         IPW_DEBUG_RX("Handler...\n");
2384
2385         if (unlikely(status->frame_size > skb_tailroom(packet->skb))) {
2386                 IPW_DEBUG_INFO("%s: frame_size (%u) > skb_tailroom (%u)!"
2387                                "  Dropping.\n",
2388                                priv->net_dev->name,
2389                                status->frame_size, skb_tailroom(packet->skb));
2390                 priv->ieee->stats.rx_errors++;
2391                 return;
2392         }
2393
2394         if (unlikely(!netif_running(priv->net_dev))) {
2395                 priv->ieee->stats.rx_errors++;
2396                 priv->wstats.discard.misc++;
2397                 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
2398                 return;
2399         }
2400
2401         if (unlikely(priv->ieee->iw_mode == IW_MODE_MONITOR &&
2402                      status->flags & IPW_STATUS_FLAG_CRC_ERROR)) {
2403                 IPW_DEBUG_RX("CRC error in packet.  Dropping.\n");
2404                 priv->ieee->stats.rx_errors++;
2405                 return;
2406         }
2407
2408         if (unlikely(priv->ieee->iw_mode != IW_MODE_MONITOR &&
2409                 !(priv->status & STATUS_ASSOCIATED))) {
2410                 IPW_DEBUG_DROP("Dropping packet while not associated.\n");
2411                 priv->wstats.discard.misc++;
2412                 return;
2413         }
2414
2415
2416         pci_unmap_single(priv->pci_dev,
2417                          packet->dma_addr,
2418                          sizeof(struct ipw2100_rx),
2419                          PCI_DMA_FROMDEVICE);
2420
2421         skb_put(packet->skb, status->frame_size);
2422
2423 #ifdef CONFIG_IPW2100_RX_DEBUG
2424         /* Make a copy of the frame so we can dump it to the logs if
2425          * ieee80211_rx fails */
2426         memcpy(packet_data, packet->skb->data,
2427                min_t(u32, status->frame_size, IPW_RX_NIC_BUFFER_LENGTH));
2428 #endif
2429
2430         if (!ieee80211_rx(priv->ieee, packet->skb, stats)) {
2431 #ifdef CONFIG_IPW2100_RX_DEBUG
2432                 IPW_DEBUG_DROP("%s: Non consumed packet:\n",
2433                                priv->net_dev->name);
2434                 printk_buf(IPW_DL_DROP, packet_data, status->frame_size);
2435 #endif
2436                 priv->ieee->stats.rx_errors++;
2437
2438                 /* ieee80211_rx failed, so it didn't free the SKB */
2439                 dev_kfree_skb_any(packet->skb);
2440                 packet->skb = NULL;
2441         }
2442
2443         /* We need to allocate a new SKB and attach it to the RDB. */
2444         if (unlikely(ipw2100_alloc_skb(priv, packet))) {
2445                 printk(KERN_WARNING DRV_NAME ": "
2446                         "%s: Unable to allocate SKB onto RBD ring - disabling "
2447                         "adapter.\n", priv->net_dev->name);
2448                 /* TODO: schedule adapter shutdown */
2449                 IPW_DEBUG_INFO("TODO: Shutdown adapter...\n");
2450         }
2451
2452         /* Update the RDB entry */
2453         priv->rx_queue.drv[i].host_addr = packet->dma_addr;
2454 }
2455
2456 static inline int ipw2100_corruption_check(struct ipw2100_priv *priv, int i)
2457 {
2458         struct ipw2100_status *status = &priv->status_queue.drv[i];
2459         struct ipw2100_rx *u = priv->rx_buffers[i].rxp;
2460         u16 frame_type = status->status_fields & STATUS_TYPE_MASK;
2461
2462         switch (frame_type) {
2463         case COMMAND_STATUS_VAL:
2464                 return (status->frame_size != sizeof(u->rx_data.command));
2465         case STATUS_CHANGE_VAL:
2466                 return (status->frame_size != sizeof(u->rx_data.status));
2467         case HOST_NOTIFICATION_VAL:
2468                 return (status->frame_size < sizeof(u->rx_data.notification));
2469         case P80211_DATA_VAL:
2470         case P8023_DATA_VAL:
2471 #ifdef CONFIG_IPW2100_MONITOR
2472                 return 0;
2473 #else
2474                 switch (WLAN_FC_GET_TYPE(u->rx_data.header.frame_ctl)) {
2475                 case IEEE80211_FTYPE_MGMT:
2476                 case IEEE80211_FTYPE_CTL:
2477                         return 0;
2478                 case IEEE80211_FTYPE_DATA:
2479                         return (status->frame_size >
2480                                 IPW_MAX_802_11_PAYLOAD_LENGTH);
2481                 }
2482 #endif
2483         }
2484
2485         return 1;
2486 }
2487
2488 /*
2489  * ipw2100 interrupts are disabled at this point, and the ISR
2490  * is the only code that calls this method.  So, we do not need
2491  * to play with any locks.
2492  *
2493  * RX Queue works as follows:
2494  *
2495  * Read index - firmware places packet in entry identified by the
2496  *              Read index and advances Read index.  In this manner,
2497  *              Read index will always point to the next packet to
2498  *              be filled--but not yet valid.
2499  *
2500  * Write index - driver fills this entry with an unused RBD entry.
2501  *               This entry has not filled by the firmware yet.
2502  *
2503  * In between the W and R indexes are the RBDs that have been received
2504  * but not yet processed.
2505  *
2506  * The process of handling packets will start at WRITE + 1 and advance
2507  * until it reaches the READ index.
2508  *
2509  * The WRITE index is cached in the variable 'priv->rx_queue.next'.
2510  *
2511  */
2512 static inline void __ipw2100_rx_process(struct ipw2100_priv *priv)
2513 {
2514         struct ipw2100_bd_queue *rxq = &priv->rx_queue;
2515         struct ipw2100_status_queue *sq = &priv->status_queue;
2516         struct ipw2100_rx_packet *packet;
2517         u16 frame_type;
2518         u32 r, w, i, s;
2519         struct ipw2100_rx *u;
2520         struct ieee80211_rx_stats stats = {
2521                 .mac_time = jiffies,
2522         };
2523
2524         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_READ_INDEX, &r);
2525         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_WRITE_INDEX, &w);
2526
2527         if (r >= rxq->entries) {
2528                 IPW_DEBUG_RX("exit - bad read index\n");
2529                 return;
2530         }
2531
2532         i = (rxq->next + 1) % rxq->entries;
2533         s = i;
2534         while (i != r) {
2535                 /* IPW_DEBUG_RX("r = %d : w = %d : processing = %d\n",
2536                    r, rxq->next, i); */
2537
2538                 packet = &priv->rx_buffers[i];
2539
2540                 /* Sync the DMA for the STATUS buffer so CPU is sure to get
2541                  * the correct values */
2542                 pci_dma_sync_single_for_cpu(
2543                         priv->pci_dev,
2544                         sq->nic + sizeof(struct ipw2100_status) * i,
2545                         sizeof(struct ipw2100_status),
2546                         PCI_DMA_FROMDEVICE);
2547
2548                 /* Sync the DMA for the RX buffer so CPU is sure to get
2549                  * the correct values */
2550                 pci_dma_sync_single_for_cpu(priv->pci_dev, packet->dma_addr,
2551                                             sizeof(struct ipw2100_rx),
2552                                             PCI_DMA_FROMDEVICE);
2553
2554                 if (unlikely(ipw2100_corruption_check(priv, i))) {
2555                         ipw2100_corruption_detected(priv, i);
2556                         goto increment;
2557                 }
2558
2559                 u = packet->rxp;
2560                 frame_type = sq->drv[i].status_fields &
2561                         STATUS_TYPE_MASK;
2562                 stats.rssi = sq->drv[i].rssi + IPW2100_RSSI_TO_DBM;
2563                 stats.len = sq->drv[i].frame_size;
2564
2565                 stats.mask = 0;
2566                 if (stats.rssi != 0)
2567                         stats.mask |= IEEE80211_STATMASK_RSSI;
2568                 stats.freq = IEEE80211_24GHZ_BAND;
2569
2570                 IPW_DEBUG_RX(
2571                         "%s: '%s' frame type received (%d).\n",
2572                         priv->net_dev->name, frame_types[frame_type],
2573                         stats.len);
2574
2575                 switch (frame_type) {
2576                 case COMMAND_STATUS_VAL:
2577                         /* Reset Rx watchdog */
2578                         isr_rx_complete_command(
2579                                 priv, &u->rx_data.command);
2580                         break;
2581
2582                 case STATUS_CHANGE_VAL:
2583                         isr_status_change(priv, u->rx_data.status);
2584                         break;
2585
2586                 case P80211_DATA_VAL:
2587                 case P8023_DATA_VAL:
2588 #ifdef CONFIG_IPW2100_MONITOR
2589                         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
2590                                 isr_rx(priv, i, &stats);
2591                                 break;
2592                         }
2593 #endif
2594                         if (stats.len < sizeof(u->rx_data.header))
2595                                 break;
2596                         switch (WLAN_FC_GET_TYPE(u->rx_data.header.
2597                                                  frame_ctl)) {
2598                         case IEEE80211_FTYPE_MGMT:
2599                                 ieee80211_rx_mgt(priv->ieee,
2600                                                  &u->rx_data.header,
2601                                                  &stats);
2602                                 break;
2603
2604                         case IEEE80211_FTYPE_CTL:
2605                                 break;
2606
2607                         case IEEE80211_FTYPE_DATA:
2608                                 isr_rx(priv, i, &stats);
2609                                 break;
2610
2611                         }
2612                         break;
2613                 }
2614
2615         increment:
2616                 /* clear status field associated with this RBD */
2617                 rxq->drv[i].status.info.field = 0;
2618
2619                 i = (i + 1) % rxq->entries;
2620         }
2621
2622         if (i != s) {
2623                 /* backtrack one entry, wrapping to end if at 0 */
2624                 rxq->next = (i ? i : rxq->entries) - 1;
2625
2626                 write_register(priv->net_dev,
2627                                IPW_MEM_HOST_SHARED_RX_WRITE_INDEX,
2628                                rxq->next);
2629         }
2630 }
2631
2632
2633 /*
2634  * __ipw2100_tx_process
2635  *
2636  * This routine will determine whether the next packet on
2637  * the fw_pend_list has been processed by the firmware yet.
2638  *
2639  * If not, then it does nothing and returns.
2640  *
2641  * If so, then it removes the item from the fw_pend_list, frees
2642  * any associated storage, and places the item back on the
2643  * free list of its source (either msg_free_list or tx_free_list)
2644  *
2645  * TX Queue works as follows:
2646  *
2647  * Read index - points to the next TBD that the firmware will
2648  *              process.  The firmware will read the data, and once
2649  *              done processing, it will advance the Read index.
2650  *
2651  * Write index - driver fills this entry with an constructed TBD
2652  *               entry.  The Write index is not advanced until the
2653  *               packet has been configured.
2654  *
2655  * In between the W and R indexes are the TBDs that have NOT been
2656  * processed.  Lagging behind the R index are packets that have
2657  * been processed but have not been freed by the driver.
2658  *
2659  * In order to free old storage, an internal index will be maintained
2660  * that points to the next packet to be freed.  When all used
2661  * packets have been freed, the oldest index will be the same as the
2662  * firmware's read index.
2663  *
2664  * The OLDEST index is cached in the variable 'priv->tx_queue.oldest'
2665  *
2666  * Because the TBD structure can not contain arbitrary data, the
2667  * driver must keep an internal queue of cached allocations such that
2668  * it can put that data back into the tx_free_list and msg_free_list
2669  * for use by future command and data packets.
2670  *
2671  */
2672 static inline int __ipw2100_tx_process(struct ipw2100_priv *priv)
2673 {
2674         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2675         struct ipw2100_bd *tbd;
2676         struct list_head *element;
2677         struct ipw2100_tx_packet *packet;
2678         int descriptors_used;
2679         int e, i;
2680         u32 r, w, frag_num = 0;
2681
2682         if (list_empty(&priv->fw_pend_list))
2683                 return 0;
2684
2685         element = priv->fw_pend_list.next;
2686
2687         packet = list_entry(element, struct ipw2100_tx_packet, list);
2688         tbd = &txq->drv[packet->index];
2689
2690         /* Determine how many TBD entries must be finished... */
2691         switch (packet->type) {
2692         case COMMAND:
2693                 /* COMMAND uses only one slot; don't advance */
2694                 descriptors_used = 1;
2695                 e = txq->oldest;
2696                 break;
2697
2698         case DATA:
2699                 /* DATA uses two slots; advance and loop position. */
2700                 descriptors_used = tbd->num_fragments;
2701                 frag_num = tbd->num_fragments - 1;
2702                 e = txq->oldest + frag_num;
2703                 e %= txq->entries;
2704                 break;
2705
2706         default:
2707                 printk(KERN_WARNING DRV_NAME ": %s: Bad fw_pend_list entry!\n",
2708                                    priv->net_dev->name);
2709                 return 0;
2710         }
2711
2712         /* if the last TBD is not done by NIC yet, then packet is
2713          * not ready to be released.
2714          *
2715          */
2716         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
2717                       &r);
2718         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
2719                       &w);
2720         if (w != txq->next)
2721                 printk(KERN_WARNING DRV_NAME ": %s: write index mismatch\n",
2722                        priv->net_dev->name);
2723
2724         /*
2725          * txq->next is the index of the last packet written txq->oldest is
2726          * the index of the r is the index of the next packet to be read by
2727          * firmware
2728          */
2729
2730
2731         /*
2732          * Quick graphic to help you visualize the following
2733          * if / else statement
2734          *
2735          * ===>|                     s---->|===============
2736          *                               e>|
2737          * | a | b | c | d | e | f | g | h | i | j | k | l
2738          *       r---->|
2739          *               w
2740          *
2741          * w - updated by driver
2742          * r - updated by firmware
2743          * s - start of oldest BD entry (txq->oldest)
2744          * e - end of oldest BD entry
2745          *
2746          */
2747         if (!((r <= w && (e < r || e >= w)) || (e < r && e >= w))) {
2748                 IPW_DEBUG_TX("exit - no processed packets ready to release.\n");
2749                 return 0;
2750         }
2751
2752         list_del(element);
2753         DEC_STAT(&priv->fw_pend_stat);
2754
2755 #ifdef CONFIG_IPW_DEBUG
2756         {
2757                 int i = txq->oldest;
2758                 IPW_DEBUG_TX(
2759                         "TX%d V=%p P=%04X T=%04X L=%d\n", i,
2760                         &txq->drv[i],
2761                         (u32)(txq->nic + i * sizeof(struct ipw2100_bd)),
2762                         txq->drv[i].host_addr,
2763                         txq->drv[i].buf_length);
2764
2765                 if (packet->type == DATA) {
2766                         i = (i + 1) % txq->entries;
2767
2768                         IPW_DEBUG_TX(
2769                                 "TX%d V=%p P=%04X T=%04X L=%d\n", i,
2770                                 &txq->drv[i],
2771                                 (u32)(txq->nic + i *
2772                                 sizeof(struct ipw2100_bd)),
2773                                 (u32)txq->drv[i].host_addr,
2774                                 txq->drv[i].buf_length);
2775                 }
2776         }
2777 #endif
2778
2779         switch (packet->type) {
2780         case DATA:
2781                 if (txq->drv[txq->oldest].status.info.fields.txType != 0)
2782                         printk(KERN_WARNING DRV_NAME ": %s: Queue mismatch.  "
2783                                "Expecting DATA TBD but pulled "
2784                                "something else: ids %d=%d.\n",
2785                                priv->net_dev->name, txq->oldest, packet->index);
2786
2787                 /* DATA packet; we have to unmap and free the SKB */
2788                 priv->ieee->stats.tx_packets++;
2789                 for (i = 0; i < frag_num; i++) {
2790                         tbd = &txq->drv[(packet->index + 1 + i) %
2791                                         txq->entries];
2792
2793                         IPW_DEBUG_TX(
2794                                 "TX%d P=%08x L=%d\n",
2795                                 (packet->index + 1 + i) % txq->entries,
2796                                 tbd->host_addr, tbd->buf_length);
2797
2798                         pci_unmap_single(priv->pci_dev,
2799                                          tbd->host_addr,
2800                                          tbd->buf_length,
2801                                          PCI_DMA_TODEVICE);
2802                 }
2803
2804                 priv->ieee->stats.tx_bytes += packet->info.d_struct.txb->payload_size;
2805                 ieee80211_txb_free(packet->info.d_struct.txb);
2806                 packet->info.d_struct.txb = NULL;
2807
2808                 list_add_tail(element, &priv->tx_free_list);
2809                 INC_STAT(&priv->tx_free_stat);
2810
2811                 /* We have a free slot in the Tx queue, so wake up the
2812                  * transmit layer if it is stopped. */
2813                 if (priv->status & STATUS_ASSOCIATED &&
2814                     netif_queue_stopped(priv->net_dev)) {
2815                         IPW_DEBUG_INFO(KERN_INFO
2816                                            "%s: Waking net queue.\n",
2817                                            priv->net_dev->name);
2818                         netif_wake_queue(priv->net_dev);
2819                 }
2820
2821                 /* A packet was processed by the hardware, so update the
2822                  * watchdog */
2823                 priv->net_dev->trans_start = jiffies;
2824
2825                 break;
2826
2827         case COMMAND:
2828                 if (txq->drv[txq->oldest].status.info.fields.txType != 1)
2829                         printk(KERN_WARNING DRV_NAME ": %s: Queue mismatch.  "
2830                                "Expecting COMMAND TBD but pulled "
2831                                "something else: ids %d=%d.\n",
2832                                priv->net_dev->name, txq->oldest, packet->index);
2833
2834 #ifdef CONFIG_IPW_DEBUG
2835                 if (packet->info.c_struct.cmd->host_command_reg <
2836                     sizeof(command_types) / sizeof(*command_types))
2837                         IPW_DEBUG_TX(
2838                                 "Command '%s (%d)' processed: %d.\n",
2839                                 command_types[packet->info.c_struct.cmd->host_command_reg],
2840                                 packet->info.c_struct.cmd->host_command_reg,
2841                                 packet->info.c_struct.cmd->cmd_status_reg);
2842 #endif
2843
2844                 list_add_tail(element, &priv->msg_free_list);
2845                 INC_STAT(&priv->msg_free_stat);
2846                 break;
2847         }
2848
2849         /* advance oldest used TBD pointer to start of next entry */
2850         txq->oldest = (e + 1) % txq->entries;
2851         /* increase available TBDs number */
2852         txq->available += descriptors_used;
2853         SET_STAT(&priv->txq_stat, txq->available);
2854
2855         IPW_DEBUG_TX("packet latency (send to process)  %ld jiffies\n",
2856                          jiffies - packet->jiffy_start);
2857
2858         return (!list_empty(&priv->fw_pend_list));
2859 }
2860
2861
2862 static inline void __ipw2100_tx_complete(struct ipw2100_priv *priv)
2863 {
2864         int i = 0;
2865
2866         while (__ipw2100_tx_process(priv) && i < 200) i++;
2867
2868         if (i == 200) {
2869                 printk(KERN_WARNING DRV_NAME ": "
2870                        "%s: Driver is running slow (%d iters).\n",
2871                        priv->net_dev->name, i);
2872         }
2873 }
2874
2875
2876 static void ipw2100_tx_send_commands(struct ipw2100_priv *priv)
2877 {
2878         struct list_head *element;
2879         struct ipw2100_tx_packet *packet;
2880         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2881         struct ipw2100_bd *tbd;
2882         int next = txq->next;
2883
2884         while (!list_empty(&priv->msg_pend_list)) {
2885                 /* if there isn't enough space in TBD queue, then
2886                  * don't stuff a new one in.
2887                  * NOTE: 3 are needed as a command will take one,
2888                  *       and there is a minimum of 2 that must be
2889                  *       maintained between the r and w indexes
2890                  */
2891                 if (txq->available <= 3) {
2892                         IPW_DEBUG_TX("no room in tx_queue\n");
2893                         break;
2894                 }
2895
2896                 element = priv->msg_pend_list.next;
2897                 list_del(element);
2898                 DEC_STAT(&priv->msg_pend_stat);
2899
2900                 packet = list_entry(element,
2901                                     struct ipw2100_tx_packet, list);
2902
2903                 IPW_DEBUG_TX("using TBD at virt=%p, phys=%p\n",
2904                                  &txq->drv[txq->next],
2905                                  (void*)(txq->nic + txq->next *
2906                                          sizeof(struct ipw2100_bd)));
2907
2908                 packet->index = txq->next;
2909
2910                 tbd = &txq->drv[txq->next];
2911
2912                 /* initialize TBD */
2913                 tbd->host_addr = packet->info.c_struct.cmd_phys;
2914                 tbd->buf_length = sizeof(struct ipw2100_cmd_header);
2915                 /* not marking number of fragments causes problems
2916                  * with f/w debug version */
2917                 tbd->num_fragments = 1;
2918                 tbd->status.info.field =
2919                         IPW_BD_STATUS_TX_FRAME_COMMAND |
2920                         IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
2921
2922                 /* update TBD queue counters */
2923                 txq->next++;
2924                 txq->next %= txq->entries;
2925                 txq->available--;
2926                 DEC_STAT(&priv->txq_stat);
2927
2928                 list_add_tail(element, &priv->fw_pend_list);
2929                 INC_STAT(&priv->fw_pend_stat);
2930         }
2931
2932         if (txq->next != next) {
2933                 /* kick off the DMA by notifying firmware the
2934                  * write index has moved; make sure TBD stores are sync'd */
2935                 wmb();
2936                 write_register(priv->net_dev,
2937                                IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
2938                                txq->next);
2939         }
2940 }
2941
2942
2943 /*
2944  * ipw2100_tx_send_data
2945  *
2946  */
2947 static void ipw2100_tx_send_data(struct ipw2100_priv *priv)
2948 {
2949         struct list_head *element;
2950         struct ipw2100_tx_packet *packet;
2951         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2952         struct ipw2100_bd *tbd;
2953         int next = txq->next;
2954         int i = 0;
2955         struct ipw2100_data_header *ipw_hdr;
2956         struct ieee80211_hdr *hdr;
2957
2958         while (!list_empty(&priv->tx_pend_list)) {
2959                 /* if there isn't enough space in TBD queue, then
2960                  * don't stuff a new one in.
2961                  * NOTE: 4 are needed as a data will take two,
2962                  *       and there is a minimum of 2 that must be
2963                  *       maintained between the r and w indexes
2964                  */
2965                 element = priv->tx_pend_list.next;
2966                 packet = list_entry(element, struct ipw2100_tx_packet, list);
2967
2968                 if (unlikely(1 + packet->info.d_struct.txb->nr_frags >
2969                              IPW_MAX_BDS)) {
2970                         /* TODO: Support merging buffers if more than
2971                          * IPW_MAX_BDS are used */
2972                         IPW_DEBUG_INFO(
2973                                "%s: Maximum BD theshold exceeded.  "
2974                                "Increase fragmentation level.\n",
2975                                priv->net_dev->name);
2976                 }
2977
2978                 if (txq->available <= 3 +
2979                     packet->info.d_struct.txb->nr_frags) {
2980                         IPW_DEBUG_TX("no room in tx_queue\n");
2981                         break;
2982                 }
2983
2984                 list_del(element);
2985                 DEC_STAT(&priv->tx_pend_stat);
2986
2987                 tbd = &txq->drv[txq->next];
2988
2989                 packet->index = txq->next;
2990
2991                 ipw_hdr = packet->info.d_struct.data;
2992                 hdr = (struct ieee80211_hdr *)packet->info.d_struct.txb->
2993                         fragments[0]->data;
2994
2995                 if (priv->ieee->iw_mode == IW_MODE_INFRA) {
2996                         /* To DS: Addr1 = BSSID, Addr2 = SA,
2997                            Addr3 = DA */
2998                         memcpy(ipw_hdr->src_addr, hdr->addr2, ETH_ALEN);
2999                         memcpy(ipw_hdr->dst_addr, hdr->addr3, ETH_ALEN);
3000                 } else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
3001                         /* not From/To DS: Addr1 = DA, Addr2 = SA,
3002                            Addr3 = BSSID */
3003                         memcpy(ipw_hdr->src_addr, hdr->addr2, ETH_ALEN);
3004                         memcpy(ipw_hdr->dst_addr, hdr->addr1, ETH_ALEN);
3005                 }
3006
3007                 ipw_hdr->host_command_reg = SEND;
3008                 ipw_hdr->host_command_reg1 = 0;
3009
3010                 /* For now we only support host based encryption */
3011                 ipw_hdr->needs_encryption = 0;
3012                 ipw_hdr->encrypted = packet->info.d_struct.txb->encrypted;
3013                 if (packet->info.d_struct.txb->nr_frags > 1)
3014                         ipw_hdr->fragment_size =
3015                                 packet->info.d_struct.txb->frag_size - IEEE80211_3ADDR_LEN;
3016                 else
3017                         ipw_hdr->fragment_size = 0;
3018
3019                 tbd->host_addr = packet->info.d_struct.data_phys;
3020                 tbd->buf_length = sizeof(struct ipw2100_data_header);
3021                 tbd->num_fragments = 1 + packet->info.d_struct.txb->nr_frags;
3022                 tbd->status.info.field =
3023                         IPW_BD_STATUS_TX_FRAME_802_3 |
3024                         IPW_BD_STATUS_TX_FRAME_NOT_LAST_FRAGMENT;
3025                 txq->next++;
3026                 txq->next %= txq->entries;
3027
3028                 IPW_DEBUG_TX(
3029                         "data header tbd TX%d P=%08x L=%d\n",
3030                         packet->index, tbd->host_addr,
3031                         tbd->buf_length);
3032 #ifdef CONFIG_IPW_DEBUG
3033                 if (packet->info.d_struct.txb->nr_frags > 1)
3034                         IPW_DEBUG_FRAG("fragment Tx: %d frames\n",
3035                                        packet->info.d_struct.txb->nr_frags);
3036 #endif
3037
3038                 for (i = 0; i < packet->info.d_struct.txb->nr_frags; i++) {
3039                         tbd = &txq->drv[txq->next];
3040                         if (i == packet->info.d_struct.txb->nr_frags - 1)
3041                                 tbd->status.info.field =
3042                                         IPW_BD_STATUS_TX_FRAME_802_3 |
3043                                         IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
3044                         else
3045                                 tbd->status.info.field =
3046                                         IPW_BD_STATUS_TX_FRAME_802_3 |
3047                                         IPW_BD_STATUS_TX_FRAME_NOT_LAST_FRAGMENT;
3048
3049                         tbd->buf_length = packet->info.d_struct.txb->
3050                                 fragments[i]->len - IEEE80211_3ADDR_LEN;
3051
3052                         tbd->host_addr = pci_map_single(
3053                                 priv->pci_dev,
3054                                 packet->info.d_struct.txb->fragments[i]->data +
3055                                 IEEE80211_3ADDR_LEN,
3056                                 tbd->buf_length,
3057                                 PCI_DMA_TODEVICE);
3058
3059                         IPW_DEBUG_TX(
3060                                 "data frag tbd TX%d P=%08x L=%d\n",
3061                                 txq->next, tbd->host_addr, tbd->buf_length);
3062
3063                         pci_dma_sync_single_for_device(
3064                                 priv->pci_dev, tbd->host_addr,
3065                                 tbd->buf_length,
3066                                 PCI_DMA_TODEVICE);
3067
3068                         txq->next++;
3069                         txq->next %= txq->entries;
3070                 }
3071
3072                 txq->available -= 1 + packet->info.d_struct.txb->nr_frags;
3073                 SET_STAT(&priv->txq_stat, txq->available);
3074
3075                 list_add_tail(element, &priv->fw_pend_list);
3076                 INC_STAT(&priv->fw_pend_stat);
3077         }
3078
3079         if (txq->next != next) {
3080                 /* kick off the DMA by notifying firmware the
3081                  * write index has moved; make sure TBD stores are sync'd */
3082                 write_register(priv->net_dev,
3083                                IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
3084                                txq->next);
3085         }
3086         return;
3087 }
3088
3089 static void ipw2100_irq_tasklet(struct ipw2100_priv *priv)
3090 {
3091         struct net_device *dev = priv->net_dev;
3092         unsigned long flags;
3093         u32 inta, tmp;
3094
3095         spin_lock_irqsave(&priv->low_lock, flags);
3096         ipw2100_disable_interrupts(priv);
3097
3098         read_register(dev, IPW_REG_INTA, &inta);
3099
3100         IPW_DEBUG_ISR("enter - INTA: 0x%08lX\n",
3101                       (unsigned long)inta & IPW_INTERRUPT_MASK);
3102
3103         priv->in_isr++;
3104         priv->interrupts++;
3105
3106         /* We do not loop and keep polling for more interrupts as this
3107          * is frowned upon and doesn't play nicely with other potentially
3108          * chained IRQs */
3109         IPW_DEBUG_ISR("INTA: 0x%08lX\n",
3110                       (unsigned long)inta & IPW_INTERRUPT_MASK);
3111
3112         if (inta & IPW2100_INTA_FATAL_ERROR) {
3113                 printk(KERN_WARNING DRV_NAME
3114                                   ": Fatal interrupt. Scheduling firmware restart.\n");
3115                 priv->inta_other++;
3116                 write_register(
3117                         dev, IPW_REG_INTA,
3118                         IPW2100_INTA_FATAL_ERROR);
3119
3120                 read_nic_dword(dev, IPW_NIC_FATAL_ERROR, &priv->fatal_error);
3121                 IPW_DEBUG_INFO("%s: Fatal error value: 0x%08X\n",
3122                                priv->net_dev->name, priv->fatal_error);
3123
3124                 read_nic_dword(dev, IPW_ERROR_ADDR(priv->fatal_error), &tmp);
3125                 IPW_DEBUG_INFO("%s: Fatal error address value: 0x%08X\n",
3126                                priv->net_dev->name, tmp);
3127
3128                 /* Wake up any sleeping jobs */
3129                 schedule_reset(priv);
3130         }
3131
3132         if (inta & IPW2100_INTA_PARITY_ERROR) {
3133                 printk(KERN_ERR DRV_NAME ": ***** PARITY ERROR INTERRUPT !!!! \n");
3134                 priv->inta_other++;
3135                 write_register(
3136                         dev, IPW_REG_INTA,
3137                         IPW2100_INTA_PARITY_ERROR);
3138         }
3139
3140         if (inta & IPW2100_INTA_RX_TRANSFER) {
3141                 IPW_DEBUG_ISR("RX interrupt\n");
3142
3143                 priv->rx_interrupts++;
3144
3145                 write_register(
3146                         dev, IPW_REG_INTA,
3147                         IPW2100_INTA_RX_TRANSFER);
3148
3149                 __ipw2100_rx_process(priv);
3150                 __ipw2100_tx_complete(priv);
3151         }
3152
3153         if (inta & IPW2100_INTA_TX_TRANSFER) {
3154                 IPW_DEBUG_ISR("TX interrupt\n");
3155
3156                 priv->tx_interrupts++;
3157
3158                 write_register(dev, IPW_REG_INTA,
3159                                IPW2100_INTA_TX_TRANSFER);
3160
3161                 __ipw2100_tx_complete(priv);
3162                 ipw2100_tx_send_commands(priv);
3163                 ipw2100_tx_send_data(priv);
3164         }
3165
3166         if (inta & IPW2100_INTA_TX_COMPLETE) {
3167                 IPW_DEBUG_ISR("TX complete\n");
3168                 priv->inta_other++;
3169                 write_register(
3170                         dev, IPW_REG_INTA,
3171                         IPW2100_INTA_TX_COMPLETE);
3172
3173                 __ipw2100_tx_complete(priv);
3174         }
3175
3176         if (inta & IPW2100_INTA_EVENT_INTERRUPT) {
3177                 /* ipw2100_handle_event(dev); */
3178                 priv->inta_other++;
3179                 write_register(
3180                         dev, IPW_REG_INTA,
3181                         IPW2100_INTA_EVENT_INTERRUPT);
3182         }
3183
3184         if (inta & IPW2100_INTA_FW_INIT_DONE) {
3185                 IPW_DEBUG_ISR("FW init done interrupt\n");
3186                 priv->inta_other++;
3187
3188                 read_register(dev, IPW_REG_INTA, &tmp);
3189                 if (tmp & (IPW2100_INTA_FATAL_ERROR |
3190                            IPW2100_INTA_PARITY_ERROR)) {
3191                         write_register(
3192                                 dev, IPW_REG_INTA,
3193                                 IPW2100_INTA_FATAL_ERROR |
3194                                 IPW2100_INTA_PARITY_ERROR);
3195                 }
3196
3197                 write_register(dev, IPW_REG_INTA,
3198                                IPW2100_INTA_FW_INIT_DONE);
3199         }
3200
3201         if (inta & IPW2100_INTA_STATUS_CHANGE) {
3202                 IPW_DEBUG_ISR("Status change interrupt\n");
3203                 priv->inta_other++;
3204                 write_register(
3205                         dev, IPW_REG_INTA,
3206                         IPW2100_INTA_STATUS_CHANGE);
3207         }
3208
3209         if (inta & IPW2100_INTA_SLAVE_MODE_HOST_COMMAND_DONE) {
3210                 IPW_DEBUG_ISR("slave host mode interrupt\n");
3211                 priv->inta_other++;
3212                 write_register(
3213                         dev, IPW_REG_INTA,
3214                         IPW2100_INTA_SLAVE_MODE_HOST_COMMAND_DONE);
3215         }
3216
3217         priv->in_isr--;
3218         ipw2100_enable_interrupts(priv);
3219
3220         spin_unlock_irqrestore(&priv->low_lock, flags);
3221
3222         IPW_DEBUG_ISR("exit\n");
3223 }
3224
3225
3226 static irqreturn_t ipw2100_interrupt(int irq, void *data,
3227                                      struct pt_regs *regs)
3228 {
3229         struct ipw2100_priv *priv = data;
3230         u32 inta, inta_mask;
3231
3232         if (!data)
3233                 return IRQ_NONE;
3234
3235         spin_lock(&priv->low_lock);
3236
3237         /* We check to see if we should be ignoring interrupts before
3238          * we touch the hardware.  During ucode load if we try and handle
3239          * an interrupt we can cause keyboard problems as well as cause
3240          * the ucode to fail to initialize */
3241         if (!(priv->status & STATUS_INT_ENABLED)) {
3242                 /* Shared IRQ */
3243                 goto none;
3244         }
3245
3246         read_register(priv->net_dev, IPW_REG_INTA_MASK, &inta_mask);
3247         read_register(priv->net_dev, IPW_REG_INTA, &inta);
3248
3249         if (inta == 0xFFFFFFFF) {
3250                 /* Hardware disappeared */
3251                 printk(KERN_WARNING DRV_NAME ": IRQ INTA == 0xFFFFFFFF\n");
3252                 goto none;
3253         }
3254
3255         inta &= IPW_INTERRUPT_MASK;
3256
3257         if (!(inta & inta_mask)) {
3258                 /* Shared interrupt */
3259                 goto none;
3260         }
3261
3262         /* We disable the hardware interrupt here just to prevent unneeded
3263          * calls to be made.  We disable this again within the actual
3264          * work tasklet, so if another part of the code re-enables the
3265          * interrupt, that is fine */
3266         ipw2100_disable_interrupts(priv);
3267
3268         tasklet_schedule(&priv->irq_tasklet);
3269         spin_unlock(&priv->low_lock);
3270
3271         return IRQ_HANDLED;
3272  none:
3273         spin_unlock(&priv->low_lock);
3274         return IRQ_NONE;
3275 }
3276
3277 static int ipw2100_tx(struct ieee80211_txb *txb, struct net_device *dev)
3278 {
3279         struct ipw2100_priv *priv = ieee80211_priv(dev);
3280         struct list_head *element;
3281         struct ipw2100_tx_packet *packet;
3282         unsigned long flags;
3283
3284         spin_lock_irqsave(&priv->low_lock, flags);
3285
3286         if (!(priv->status & STATUS_ASSOCIATED)) {
3287                 IPW_DEBUG_INFO("Can not transmit when not connected.\n");
3288                 priv->ieee->stats.tx_carrier_errors++;
3289                 netif_stop_queue(dev);
3290                 goto fail_unlock;
3291         }
3292
3293         if (list_empty(&priv->tx_free_list))
3294                 goto fail_unlock;
3295
3296         element = priv->tx_free_list.next;
3297         packet = list_entry(element, struct ipw2100_tx_packet, list);
3298
3299         packet->info.d_struct.txb = txb;
3300
3301         IPW_DEBUG_TX("Sending fragment (%d bytes):\n",
3302                          txb->fragments[0]->len);
3303         printk_buf(IPW_DL_TX, txb->fragments[0]->data,
3304                    txb->fragments[0]->len);
3305
3306         packet->jiffy_start = jiffies;
3307
3308         list_del(element);
3309         DEC_STAT(&priv->tx_free_stat);
3310
3311         list_add_tail(element, &priv->tx_pend_list);
3312         INC_STAT(&priv->tx_pend_stat);
3313
3314         ipw2100_tx_send_data(priv);
3315
3316         spin_unlock_irqrestore(&priv->low_lock, flags);
3317         return 0;
3318
3319  fail_unlock:
3320         netif_stop_queue(dev);
3321         spin_unlock_irqrestore(&priv->low_lock, flags);
3322         return 1;
3323 }
3324
3325
3326 static int ipw2100_msg_allocate(struct ipw2100_priv *priv)
3327 {
3328         int i, j, err = -EINVAL;
3329         void *v;
3330         dma_addr_t p;
3331
3332         priv->msg_buffers = (struct ipw2100_tx_packet *)kmalloc(
3333                 IPW_COMMAND_POOL_SIZE * sizeof(struct ipw2100_tx_packet),
3334                 GFP_KERNEL);
3335         if (!priv->msg_buffers) {
3336                 printk(KERN_ERR DRV_NAME ": %s: PCI alloc failed for msg "
3337                        "buffers.\n", priv->net_dev->name);
3338                 return -ENOMEM;
3339         }
3340
3341         for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++) {
3342                 v = pci_alloc_consistent(
3343                         priv->pci_dev,
3344                         sizeof(struct ipw2100_cmd_header),
3345                         &p);
3346                 if (!v) {
3347                         printk(KERN_ERR DRV_NAME ": "
3348                                "%s: PCI alloc failed for msg "
3349                                "buffers.\n",
3350                                priv->net_dev->name);
3351                         err = -ENOMEM;
3352                         break;
3353                 }
3354
3355                 memset(v, 0, sizeof(struct ipw2100_cmd_header));
3356
3357                 priv->msg_buffers[i].type = COMMAND;
3358                 priv->msg_buffers[i].info.c_struct.cmd =
3359                         (struct ipw2100_cmd_header*)v;
3360                 priv->msg_buffers[i].info.c_struct.cmd_phys = p;
3361         }
3362
3363         if (i == IPW_COMMAND_POOL_SIZE)
3364                 return 0;
3365
3366         for (j = 0; j < i; j++) {
3367                 pci_free_consistent(
3368                         priv->pci_dev,
3369                         sizeof(struct ipw2100_cmd_header),
3370                         priv->msg_buffers[j].info.c_struct.cmd,
3371                         priv->msg_buffers[j].info.c_struct.cmd_phys);
3372         }
3373
3374         kfree(priv->msg_buffers);
3375         priv->msg_buffers = NULL;
3376
3377         return err;
3378 }
3379
3380 static int ipw2100_msg_initialize(struct ipw2100_priv *priv)
3381 {
3382         int i;
3383
3384         INIT_LIST_HEAD(&priv->msg_free_list);
3385         INIT_LIST_HEAD(&priv->msg_pend_list);
3386
3387         for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++)
3388                 list_add_tail(&priv->msg_buffers[i].list, &priv->msg_free_list);
3389         SET_STAT(&priv->msg_free_stat, i);
3390
3391         return 0;
3392 }
3393
3394 static void ipw2100_msg_free(struct ipw2100_priv *priv)
3395 {
3396         int i;
3397
3398         if (!priv->msg_buffers)
3399                 return;
3400
3401         for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++) {
3402                 pci_free_consistent(priv->pci_dev,
3403                                     sizeof(struct ipw2100_cmd_header),
3404                                     priv->msg_buffers[i].info.c_struct.cmd,
3405                                     priv->msg_buffers[i].info.c_struct.cmd_phys);
3406         }
3407
3408         kfree(priv->msg_buffers);
3409         priv->msg_buffers = NULL;
3410 }
3411
3412 static ssize_t show_pci(struct device *d, struct device_attribute *attr,
3413                         char *buf)
3414 {
3415         struct pci_dev *pci_dev = container_of(d, struct pci_dev, dev);
3416         char *out = buf;
3417         int i, j;
3418         u32 val;
3419
3420         for (i = 0; i < 16; i++) {
3421                 out += sprintf(out, "[%08X] ", i * 16);
3422                 for (j = 0; j < 16; j += 4) {
3423                         pci_read_config_dword(pci_dev, i * 16 + j, &val);
3424                         out += sprintf(out, "%08X ", val);
3425                 }
3426                 out += sprintf(out, "\n");
3427         }
3428
3429         return out - buf;
3430 }
3431 static DEVICE_ATTR(pci, S_IRUGO, show_pci, NULL);
3432
3433 static ssize_t show_cfg(struct device *d, struct device_attribute *attr,
3434                         char *buf)
3435 {
3436         struct ipw2100_priv *p = d->driver_data;
3437         return sprintf(buf, "0x%08x\n", (int)p->config);
3438 }
3439 static DEVICE_ATTR(cfg, S_IRUGO, show_cfg, NULL);
3440
3441 static ssize_t show_status(struct device *d, struct device_attribute *attr,
3442                         char *buf)
3443 {
3444         struct ipw2100_priv *p = d->driver_data;
3445         return sprintf(buf, "0x%08x\n", (int)p->status);
3446 }
3447 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3448
3449 static ssize_t show_capability(struct device *d, struct device_attribute *attr,
3450                                 char *buf)
3451 {
3452         struct ipw2100_priv *p = d->driver_data;
3453         return sprintf(buf, "0x%08x\n", (int)p->capability);
3454 }
3455 static DEVICE_ATTR(capability, S_IRUGO, show_capability, NULL);
3456
3457
3458 #define IPW2100_REG(x) { IPW_ ##x, #x }
3459 static const struct {
3460         u32 addr;
3461         const char *name;
3462 } hw_data[] = {
3463         IPW2100_REG(REG_GP_CNTRL),
3464         IPW2100_REG(REG_GPIO),
3465         IPW2100_REG(REG_INTA),
3466         IPW2100_REG(REG_INTA_MASK),
3467         IPW2100_REG(REG_RESET_REG),
3468 };
3469 #define IPW2100_NIC(x, s) { x, #x, s }
3470 static const struct {
3471         u32 addr;
3472         const char *name;
3473         size_t size;
3474 } nic_data[] = {
3475         IPW2100_NIC(IPW2100_CONTROL_REG, 2),
3476         IPW2100_NIC(0x210014, 1),
3477         IPW2100_NIC(0x210000, 1),
3478 };
3479 #define IPW2100_ORD(x, d) { IPW_ORD_ ##x, #x, d }
3480 static const struct {
3481         u8 index;
3482         const char *name;
3483         const char *desc;
3484 } ord_data[] = {
3485         IPW2100_ORD(STAT_TX_HOST_REQUESTS, "requested Host Tx's (MSDU)"),
3486         IPW2100_ORD(STAT_TX_HOST_COMPLETE, "successful Host Tx's (MSDU)"),
3487         IPW2100_ORD(STAT_TX_DIR_DATA,      "successful Directed Tx's (MSDU)"),
3488         IPW2100_ORD(STAT_TX_DIR_DATA1,     "successful Directed Tx's (MSDU) @ 1MB"),
3489         IPW2100_ORD(STAT_TX_DIR_DATA2,     "successful Directed Tx's (MSDU) @ 2MB"),
3490         IPW2100_ORD(STAT_TX_DIR_DATA5_5,   "successful Directed Tx's (MSDU) @ 5_5MB"),
3491         IPW2100_ORD(STAT_TX_DIR_DATA11,    "successful Directed Tx's (MSDU) @ 11MB"),
3492         IPW2100_ORD(STAT_TX_NODIR_DATA1,   "successful Non_Directed Tx's (MSDU) @ 1MB"),
3493         IPW2100_ORD(STAT_TX_NODIR_DATA2,   "successful Non_Directed Tx's (MSDU) @ 2MB"),
3494         IPW2100_ORD(STAT_TX_NODIR_DATA5_5, "successful Non_Directed Tx's (MSDU) @ 5.5MB"),
3495         IPW2100_ORD(STAT_TX_NODIR_DATA11,  "successful Non_Directed Tx's (MSDU) @ 11MB"),
3496         IPW2100_ORD(STAT_NULL_DATA,        "successful NULL data Tx's"),
3497         IPW2100_ORD(STAT_TX_RTS,           "successful Tx RTS"),
3498         IPW2100_ORD(STAT_TX_CTS,           "successful Tx CTS"),
3499         IPW2100_ORD(STAT_TX_ACK,           "successful Tx ACK"),
3500         IPW2100_ORD(STAT_TX_ASSN,          "successful Association Tx's"),
3501         IPW2100_ORD(STAT_TX_ASSN_RESP,     "successful Association response Tx's"),
3502         IPW2100_ORD(STAT_TX_REASSN,        "successful Reassociation Tx's"),
3503         IPW2100_ORD(STAT_TX_REASSN_RESP,   "successful Reassociation response Tx's"),
3504         IPW2100_ORD(STAT_TX_PROBE,         "probes successfully transmitted"),
3505         IPW2100_ORD(STAT_TX_PROBE_RESP,    "probe responses successfully transmitted"),
3506         IPW2100_ORD(STAT_TX_BEACON,        "tx beacon"),
3507         IPW2100_ORD(STAT_TX_ATIM,          "Tx ATIM"),
3508         IPW2100_ORD(STAT_TX_DISASSN,       "successful Disassociation TX"),
3509         IPW2100_ORD(STAT_TX_AUTH,          "successful Authentication Tx"),
3510         IPW2100_ORD(STAT_TX_DEAUTH,        "successful Deauthentication TX"),
3511         IPW2100_ORD(STAT_TX_TOTAL_BYTES,   "Total successful Tx data bytes"),
3512         IPW2100_ORD(STAT_TX_RETRIES,       "Tx retries"),
3513         IPW2100_ORD(STAT_TX_RETRY1,        "Tx retries at 1MBPS"),
3514         IPW2100_ORD(STAT_TX_RETRY2,        "Tx retries at 2MBPS"),
3515         IPW2100_ORD(STAT_TX_RETRY5_5,      "Tx retries at 5.5MBPS"),
3516         IPW2100_ORD(STAT_TX_RETRY11,       "Tx retries at 11MBPS"),
3517         IPW2100_ORD(STAT_TX_FAILURES,      "Tx Failures"),
3518         IPW2100_ORD(STAT_TX_MAX_TRIES_IN_HOP,"times max tries in a hop failed"),
3519         IPW2100_ORD(STAT_TX_DISASSN_FAIL,       "times disassociation failed"),
3520         IPW2100_ORD(STAT_TX_ERR_CTS,         "missed/bad CTS frames"),
3521         IPW2100_ORD(STAT_TX_ERR_ACK,    "tx err due to acks"),
3522         IPW2100_ORD(STAT_RX_HOST,       "packets passed to host"),
3523         IPW2100_ORD(STAT_RX_DIR_DATA,   "directed packets"),
3524         IPW2100_ORD(STAT_RX_DIR_DATA1,  "directed packets at 1MB"),
3525         IPW2100_ORD(STAT_RX_DIR_DATA2,  "directed packets at 2MB"),
3526         IPW2100_ORD(STAT_RX_DIR_DATA5_5,        "directed packets at 5.5MB"),
3527         IPW2100_ORD(STAT_RX_DIR_DATA11, "directed packets at 11MB"),
3528         IPW2100_ORD(STAT_RX_NODIR_DATA,"nondirected packets"),
3529         IPW2100_ORD(STAT_RX_NODIR_DATA1,        "nondirected packets at 1MB"),
3530         IPW2100_ORD(STAT_RX_NODIR_DATA2,        "nondirected packets at 2MB"),
3531         IPW2100_ORD(STAT_RX_NODIR_DATA5_5,      "nondirected packets at 5.5MB"),
3532         IPW2100_ORD(STAT_RX_NODIR_DATA11,       "nondirected packets at 11MB"),
3533         IPW2100_ORD(STAT_RX_NULL_DATA,  "null data rx's"),
3534         IPW2100_ORD(STAT_RX_RTS,        "Rx RTS"),
3535         IPW2100_ORD(STAT_RX_CTS,        "Rx CTS"),
3536         IPW2100_ORD(STAT_RX_ACK,        "Rx ACK"),
3537         IPW2100_ORD(STAT_RX_CFEND,      "Rx CF End"),
3538         IPW2100_ORD(STAT_RX_CFEND_ACK,  "Rx CF End + CF Ack"),
3539         IPW2100_ORD(STAT_RX_ASSN,       "Association Rx's"),
3540         IPW2100_ORD(STAT_RX_ASSN_RESP,  "Association response Rx's"),
3541         IPW2100_ORD(STAT_RX_REASSN,     "Reassociation Rx's"),
3542         IPW2100_ORD(STAT_RX_REASSN_RESP,        "Reassociation response Rx's"),
3543         IPW2100_ORD(STAT_RX_PROBE,      "probe Rx's"),
3544         IPW2100_ORD(STAT_RX_PROBE_RESP, "probe response Rx's"),
3545         IPW2100_ORD(STAT_RX_BEACON,     "Rx beacon"),
3546         IPW2100_ORD(STAT_RX_ATIM,       "Rx ATIM"),
3547         IPW2100_ORD(STAT_RX_DISASSN,    "disassociation Rx"),
3548         IPW2100_ORD(STAT_RX_AUTH,       "authentication Rx"),
3549         IPW2100_ORD(STAT_RX_DEAUTH,     "deauthentication Rx"),
3550         IPW2100_ORD(STAT_RX_TOTAL_BYTES,"Total rx data bytes received"),
3551         IPW2100_ORD(STAT_RX_ERR_CRC,     "packets with Rx CRC error"),
3552         IPW2100_ORD(STAT_RX_ERR_CRC1,    "Rx CRC errors at 1MB"),
3553         IPW2100_ORD(STAT_RX_ERR_CRC2,    "Rx CRC errors at 2MB"),
3554         IPW2100_ORD(STAT_RX_ERR_CRC5_5,  "Rx CRC errors at 5.5MB"),
3555         IPW2100_ORD(STAT_RX_ERR_CRC11,   "Rx CRC errors at 11MB"),
3556         IPW2100_ORD(STAT_RX_DUPLICATE1, "duplicate rx packets at 1MB"),
3557         IPW2100_ORD(STAT_RX_DUPLICATE2,  "duplicate rx packets at 2MB"),
3558         IPW2100_ORD(STAT_RX_DUPLICATE5_5,        "duplicate rx packets at 5.5MB"),
3559         IPW2100_ORD(STAT_RX_DUPLICATE11,         "duplicate rx packets at 11MB"),
3560         IPW2100_ORD(STAT_RX_DUPLICATE, "duplicate rx packets"),
3561         IPW2100_ORD(PERS_DB_LOCK,       "locking fw permanent  db"),
3562         IPW2100_ORD(PERS_DB_SIZE,       "size of fw permanent  db"),
3563         IPW2100_ORD(PERS_DB_ADDR,       "address of fw permanent  db"),
3564         IPW2100_ORD(STAT_RX_INVALID_PROTOCOL,   "rx frames with invalid protocol"),
3565         IPW2100_ORD(SYS_BOOT_TIME,      "Boot time"),
3566         IPW2100_ORD(STAT_RX_NO_BUFFER,  "rx frames rejected due to no buffer"),
3567         IPW2100_ORD(STAT_RX_MISSING_FRAG,       "rx frames dropped due to missing fragment"),
3568         IPW2100_ORD(STAT_RX_ORPHAN_FRAG,        "rx frames dropped due to non-sequential fragment"),
3569         IPW2100_ORD(STAT_RX_ORPHAN_FRAME,       "rx frames dropped due to unmatched 1st frame"),
3570         IPW2100_ORD(STAT_RX_FRAG_AGEOUT,        "rx frames dropped due to uncompleted frame"),
3571         IPW2100_ORD(STAT_RX_ICV_ERRORS, "ICV errors during decryption"),
3572         IPW2100_ORD(STAT_PSP_SUSPENSION,"times adapter suspended"),
3573         IPW2100_ORD(STAT_PSP_BCN_TIMEOUT,       "beacon timeout"),
3574         IPW2100_ORD(STAT_PSP_POLL_TIMEOUT,      "poll response timeouts"),
3575         IPW2100_ORD(STAT_PSP_NONDIR_TIMEOUT, "timeouts waiting for last {broad,multi}cast pkt"),
3576         IPW2100_ORD(STAT_PSP_RX_DTIMS,  "PSP DTIMs received"),
3577         IPW2100_ORD(STAT_PSP_RX_TIMS,   "PSP TIMs received"),
3578         IPW2100_ORD(STAT_PSP_STATION_ID,"PSP Station ID"),
3579         IPW2100_ORD(LAST_ASSN_TIME,     "RTC time of last association"),
3580         IPW2100_ORD(STAT_PERCENT_MISSED_BCNS,"current calculation of % missed beacons"),
3581         IPW2100_ORD(STAT_PERCENT_RETRIES,"current calculation of % missed tx retries"),
3582         IPW2100_ORD(ASSOCIATED_AP_PTR,  "0 if not associated, else pointer to AP table entry"),
3583         IPW2100_ORD(AVAILABLE_AP_CNT,   "AP's decsribed in the AP table"),
3584         IPW2100_ORD(AP_LIST_PTR,        "Ptr to list of available APs"),
3585         IPW2100_ORD(STAT_AP_ASSNS,      "associations"),
3586         IPW2100_ORD(STAT_ASSN_FAIL,     "association failures"),
3587         IPW2100_ORD(STAT_ASSN_RESP_FAIL,"failures due to response fail"),
3588         IPW2100_ORD(STAT_FULL_SCANS,    "full scans"),
3589         IPW2100_ORD(CARD_DISABLED,      "Card Disabled"),
3590         IPW2100_ORD(STAT_ROAM_INHIBIT,  "times roaming was inhibited due to activity"),
3591         IPW2100_ORD(RSSI_AT_ASSN,       "RSSI of associated AP at time of association"),
3592         IPW2100_ORD(STAT_ASSN_CAUSE1,   "reassociation: no probe response or TX on hop"),
3593         IPW2100_ORD(STAT_ASSN_CAUSE2,   "reassociation: poor tx/rx quality"),
3594         IPW2100_ORD(STAT_ASSN_CAUSE3,   "reassociation: tx/rx quality (excessive AP load"),
3595         IPW2100_ORD(STAT_ASSN_CAUSE4,   "reassociation: AP RSSI level"),
3596         IPW2100_ORD(STAT_ASSN_CAUSE5,   "reassociations due to load leveling"),
3597         IPW2100_ORD(STAT_AUTH_FAIL,     "times authentication failed"),
3598         IPW2100_ORD(STAT_AUTH_RESP_FAIL,"times authentication response failed"),
3599         IPW2100_ORD(STATION_TABLE_CNT,  "entries in association table"),
3600         IPW2100_ORD(RSSI_AVG_CURR,      "Current avg RSSI"),
3601         IPW2100_ORD(POWER_MGMT_MODE,    "Power mode - 0=CAM, 1=PSP"),
3602         IPW2100_ORD(COUNTRY_CODE,       "IEEE country code as recv'd from beacon"),
3603         IPW2100_ORD(COUNTRY_CHANNELS,   "channels suported by country"),
3604         IPW2100_ORD(RESET_CNT,  "adapter resets (warm)"),
3605         IPW2100_ORD(BEACON_INTERVAL,    "Beacon interval"),
3606         IPW2100_ORD(ANTENNA_DIVERSITY,  "TRUE if antenna diversity is disabled"),
3607         IPW2100_ORD(DTIM_PERIOD,        "beacon intervals between DTIMs"),
3608         IPW2100_ORD(OUR_FREQ,   "current radio freq lower digits - channel ID"),
3609         IPW2100_ORD(RTC_TIME,   "current RTC time"),
3610         IPW2100_ORD(PORT_TYPE,  "operating mode"),
3611         IPW2100_ORD(CURRENT_TX_RATE,    "current tx rate"),
3612         IPW2100_ORD(SUPPORTED_RATES,    "supported tx rates"),
3613         IPW2100_ORD(ATIM_WINDOW,        "current ATIM Window"),
3614         IPW2100_ORD(BASIC_RATES,        "basic tx rates"),
3615         IPW2100_ORD(NIC_HIGHEST_RATE,   "NIC highest tx rate"),
3616         IPW2100_ORD(AP_HIGHEST_RATE,    "AP highest tx rate"),
3617         IPW2100_ORD(CAPABILITIES,       "Management frame capability field"),
3618         IPW2100_ORD(AUTH_TYPE,  "Type of authentication"),
3619         IPW2100_ORD(RADIO_TYPE, "Adapter card platform type"),
3620         IPW2100_ORD(RTS_THRESHOLD,      "Min packet length for RTS handshaking"),
3621         IPW2100_ORD(INT_MODE,   "International mode"),
3622         IPW2100_ORD(FRAGMENTATION_THRESHOLD,    "protocol frag threshold"),
3623         IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_START_ADDRESS, "EEPROM offset in SRAM"),
3624         IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_SIZE,  "EEPROM size in SRAM"),
3625         IPW2100_ORD(EEPROM_SKU_CAPABILITY,      "EEPROM SKU Capability"),
3626         IPW2100_ORD(EEPROM_IBSS_11B_CHANNELS,   "EEPROM IBSS 11b channel set"),
3627         IPW2100_ORD(MAC_VERSION,        "MAC Version"),
3628         IPW2100_ORD(MAC_REVISION,       "MAC Revision"),
3629         IPW2100_ORD(RADIO_VERSION,      "Radio Version"),
3630         IPW2100_ORD(NIC_MANF_DATE_TIME, "MANF Date/Time STAMP"),
3631         IPW2100_ORD(UCODE_VERSION,      "Ucode Version"),
3632 };
3633
3634
3635 static ssize_t show_registers(struct device *d, struct device_attribute *attr,
3636                                 char *buf)
3637 {
3638         int i;
3639         struct ipw2100_priv *priv = dev_get_drvdata(d);
3640         struct net_device *dev = priv->net_dev;
3641         char * out = buf;
3642         u32 val = 0;
3643
3644         out += sprintf(out, "%30s [Address ] : Hex\n", "Register");
3645
3646         for (i = 0; i < (sizeof(hw_data) / sizeof(*hw_data)); i++) {
3647                 read_register(dev, hw_data[i].addr, &val);
3648                 out += sprintf(out, "%30s [%08X] : %08X\n",
3649                                hw_data[i].name, hw_data[i].addr, val);
3650         }
3651
3652         return out - buf;
3653 }
3654 static DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
3655
3656
3657 static ssize_t show_hardware(struct device *d, struct device_attribute *attr,
3658                                 char *buf)
3659 {
3660         struct ipw2100_priv *priv = dev_get_drvdata(d);
3661         struct net_device *dev = priv->net_dev;
3662         char * out = buf;
3663         int i;
3664
3665         out += sprintf(out, "%30s [Address ] : Hex\n", "NIC entry");
3666
3667         for (i = 0; i < (sizeof(nic_data) / sizeof(*nic_data)); i++) {
3668                 u8 tmp8;
3669                 u16 tmp16;
3670                 u32 tmp32;
3671
3672                 switch (nic_data[i].size) {
3673                 case 1:
3674                         read_nic_byte(dev, nic_data[i].addr, &tmp8);
3675                         out += sprintf(out, "%30s [%08X] : %02X\n",
3676                                        nic_data[i].name, nic_data[i].addr,
3677                                        tmp8);
3678                         break;
3679                 case 2:
3680                         read_nic_word(dev, nic_data[i].addr, &tmp16);
3681                         out += sprintf(out, "%30s [%08X] : %04X\n",
3682                                        nic_data[i].name, nic_data[i].addr,
3683                                        tmp16);
3684                         break;
3685                 case 4:
3686                         read_nic_dword(dev, nic_data[i].addr, &tmp32);
3687                         out += sprintf(out, "%30s [%08X] : %08X\n",
3688                                        nic_data[i].name, nic_data[i].addr,
3689                                        tmp32);
3690                         break;
3691                 }
3692         }
3693         return out - buf;
3694 }
3695 static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
3696
3697
3698 static ssize_t show_memory(struct device *d, struct device_attribute *attr,
3699                                 char *buf)
3700 {
3701         struct ipw2100_priv *priv = dev_get_drvdata(d);
3702         struct net_device *dev = priv->net_dev;
3703         static unsigned long loop = 0;
3704         int len = 0;
3705         u32 buffer[4];
3706         int i;
3707         char line[81];
3708
3709         if (loop >= 0x30000)
3710                 loop = 0;
3711
3712         /* sysfs provides us PAGE_SIZE buffer */
3713         while (len < PAGE_SIZE - 128 && loop < 0x30000) {
3714
3715                 if (priv->snapshot[0]) for (i = 0; i < 4; i++)
3716                         buffer[i] = *(u32 *)SNAPSHOT_ADDR(loop + i * 4);
3717                 else for (i = 0; i < 4; i++)
3718                         read_nic_dword(dev, loop + i * 4, &buffer[i]);
3719
3720                 if (priv->dump_raw)
3721                         len += sprintf(buf + len,
3722                                        "%c%c%c%c"
3723                                        "%c%c%c%c"
3724                                        "%c%c%c%c"
3725                                        "%c%c%c%c",
3726                                        ((u8*)buffer)[0x0],
3727                                        ((u8*)buffer)[0x1],
3728                                        ((u8*)buffer)[0x2],
3729                                        ((u8*)buffer)[0x3],
3730                                        ((u8*)buffer)[0x4],
3731                                        ((u8*)buffer)[0x5],
3732                                        ((u8*)buffer)[0x6],
3733                                        ((u8*)buffer)[0x7],
3734                                        ((u8*)buffer)[0x8],
3735                                        ((u8*)buffer)[0x9],
3736                                        ((u8*)buffer)[0xa],
3737                                        ((u8*)buffer)[0xb],
3738                                        ((u8*)buffer)[0xc],
3739                                        ((u8*)buffer)[0xd],
3740                                        ((u8*)buffer)[0xe],
3741                                        ((u8*)buffer)[0xf]);
3742                 else
3743                         len += sprintf(buf + len, "%s\n",
3744                                        snprint_line(line, sizeof(line),
3745                                                     (u8*)buffer, 16, loop));
3746                 loop += 16;
3747         }
3748
3749         return len;
3750 }
3751
3752 static ssize_t store_memory(struct device *d, struct device_attribute *attr,
3753                                 const char *buf, size_t count)
3754 {
3755         struct ipw2100_priv *priv = dev_get_drvdata(d);
3756         struct net_device *dev = priv->net_dev;
3757         const char *p = buf;
3758
3759         if (count < 1)
3760                 return count;
3761
3762         if (p[0] == '1' ||
3763             (count >= 2 && tolower(p[0]) == 'o' && tolower(p[1]) == 'n')) {
3764                 IPW_DEBUG_INFO("%s: Setting memory dump to RAW mode.\n",
3765                        dev->name);
3766                 priv->dump_raw = 1;
3767
3768         } else if (p[0] == '0' || (count >= 2 && tolower(p[0]) == 'o' &&
3769                                   tolower(p[1]) == 'f')) {
3770                 IPW_DEBUG_INFO("%s: Setting memory dump to HEX mode.\n",
3771                        dev->name);
3772                 priv->dump_raw = 0;
3773
3774         } else if (tolower(p[0]) == 'r') {
3775                 IPW_DEBUG_INFO("%s: Resetting firmware snapshot.\n",
3776                        dev->name);
3777                 ipw2100_snapshot_free(priv);
3778
3779         } else
3780                 IPW_DEBUG_INFO("%s: Usage: 0|on = HEX, 1|off = RAW, "
3781                        "reset = clear memory snapshot\n",
3782                        dev->name);
3783
3784         return count;
3785 }
3786 static DEVICE_ATTR(memory, S_IWUSR|S_IRUGO, show_memory, store_memory);
3787
3788
3789 static ssize_t show_ordinals(struct device *d, struct device_attribute *attr,
3790                                 char *buf)
3791 {
3792         struct ipw2100_priv *priv = dev_get_drvdata(d);
3793         u32 val = 0;
3794         int len = 0;
3795         u32 val_len;
3796         static int loop = 0;
3797
3798         if (loop >= sizeof(ord_data) / sizeof(*ord_data))
3799                 loop = 0;
3800
3801         /* sysfs provides us PAGE_SIZE buffer */
3802         while (len < PAGE_SIZE - 128 &&
3803                loop < (sizeof(ord_data) / sizeof(*ord_data))) {
3804
3805                 val_len = sizeof(u32);
3806
3807                 if (ipw2100_get_ordinal(priv, ord_data[loop].index, &val,
3808                                         &val_len))
3809                         len += sprintf(buf + len, "[0x%02X] = ERROR    %s\n",
3810                                        ord_data[loop].index,
3811                                        ord_data[loop].desc);
3812                 else
3813                         len += sprintf(buf + len, "[0x%02X] = 0x%08X %s\n",
3814                                        ord_data[loop].index, val,
3815                                        ord_data[loop].desc);
3816                 loop++;
3817         }
3818
3819         return len;
3820 }
3821 static DEVICE_ATTR(ordinals, S_IRUGO, show_ordinals, NULL);
3822
3823
3824 static ssize_t show_stats(struct device *d, struct device_attribute *attr,
3825                                 char *buf)
3826 {
3827         struct ipw2100_priv *priv = dev_get_drvdata(d);
3828         char * out = buf;
3829
3830         out += sprintf(out, "interrupts: %d {tx: %d, rx: %d, other: %d}\n",
3831                        priv->interrupts, priv->tx_interrupts,
3832                        priv->rx_interrupts, priv->inta_other);
3833         out += sprintf(out, "firmware resets: %d\n", priv->resets);
3834         out += sprintf(out, "firmware hangs: %d\n", priv->hangs);
3835 #ifdef CONFIG_IPW_DEBUG
3836         out += sprintf(out, "packet mismatch image: %s\n",
3837                        priv->snapshot[0] ? "YES" : "NO");
3838 #endif
3839
3840         return out - buf;
3841 }
3842 static DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
3843
3844
3845 static int ipw2100_switch_mode(struct ipw2100_priv *priv, u32 mode)
3846 {
3847         int err;
3848
3849         if (mode == priv->ieee->iw_mode)
3850                 return 0;
3851
3852         err = ipw2100_disable_adapter(priv);
3853         if (err) {
3854                 printk(KERN_ERR DRV_NAME ": %s: Could not disable adapter %d\n",
3855                        priv->net_dev->name, err);
3856                 return err;
3857         }
3858
3859         switch (mode) {
3860         case IW_MODE_INFRA:
3861                 priv->net_dev->type = ARPHRD_ETHER;
3862                 break;
3863         case IW_MODE_ADHOC:
3864                 priv->net_dev->type = ARPHRD_ETHER;
3865                 break;
3866 #ifdef CONFIG_IPW2100_MONITOR
3867         case IW_MODE_MONITOR:
3868                 priv->last_mode = priv->ieee->iw_mode;
3869                 priv->net_dev->type = ARPHRD_IEEE80211;
3870                 break;
3871 #endif /* CONFIG_IPW2100_MONITOR */
3872         }
3873
3874         priv->ieee->iw_mode = mode;
3875
3876 #ifdef CONFIG_PM
3877         /* Indicate ipw2100_download_firmware download firmware
3878          * from disk instead of memory. */
3879         ipw2100_firmware.version = 0;
3880 #endif
3881
3882         printk(KERN_INFO "%s: Reseting on mode change.\n",
3883                 priv->net_dev->name);
3884         priv->reset_backoff = 0;
3885         schedule_reset(priv);
3886
3887         return 0;
3888 }
3889
3890 static ssize_t show_internals(struct device *d, struct device_attribute *attr,
3891                                 char *buf)
3892 {
3893         struct ipw2100_priv *priv = dev_get_drvdata(d);
3894         int len = 0;
3895
3896 #define DUMP_VAR(x,y) len += sprintf(buf + len, # x ": %" # y "\n", priv-> x)
3897
3898         if (priv->status & STATUS_ASSOCIATED)
3899                 len += sprintf(buf + len, "connected: %lu\n",
3900                                get_seconds() - priv->connect_start);
3901         else
3902                 len += sprintf(buf + len, "not connected\n");
3903
3904         DUMP_VAR(ieee->crypt[priv->ieee->tx_keyidx], p);
3905         DUMP_VAR(status, 08lx);
3906         DUMP_VAR(config, 08lx);
3907         DUMP_VAR(capability, 08lx);
3908
3909         len += sprintf(buf + len, "last_rtc: %lu\n", (unsigned long)priv->last_rtc);
3910
3911         DUMP_VAR(fatal_error, d);
3912         DUMP_VAR(stop_hang_check, d);
3913         DUMP_VAR(stop_rf_kill, d);
3914         DUMP_VAR(messages_sent, d);
3915
3916         DUMP_VAR(tx_pend_stat.value, d);
3917         DUMP_VAR(tx_pend_stat.hi, d);
3918
3919         DUMP_VAR(tx_free_stat.value, d);
3920         DUMP_VAR(tx_free_stat.lo, d);
3921
3922         DUMP_VAR(msg_free_stat.value, d);
3923         DUMP_VAR(msg_free_stat.lo, d);
3924
3925         DUMP_VAR(msg_pend_stat.value, d);
3926         DUMP_VAR(msg_pend_stat.hi, d);
3927
3928         DUMP_VAR(fw_pend_stat.value, d);
3929         DUMP_VAR(fw_pend_stat.hi, d);
3930
3931         DUMP_VAR(txq_stat.value, d);
3932         DUMP_VAR(txq_stat.lo, d);
3933
3934         DUMP_VAR(ieee->scans, d);
3935         DUMP_VAR(reset_backoff, d);
3936
3937         return len;
3938 }
3939 static DEVICE_ATTR(internals, S_IRUGO, show_internals, NULL);
3940
3941
3942 static ssize_t show_bssinfo(struct device *d, struct device_attribute *attr,
3943                                 char *buf)
3944 {
3945         struct ipw2100_priv *priv = dev_get_drvdata(d);
3946         char essid[IW_ESSID_MAX_SIZE + 1];
3947         u8 bssid[ETH_ALEN];
3948         u32 chan = 0;
3949         char * out = buf;
3950         int length;
3951         int ret;
3952
3953         memset(essid, 0, sizeof(essid));
3954         memset(bssid, 0, sizeof(bssid));
3955
3956         length = IW_ESSID_MAX_SIZE;
3957         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID, essid, &length);
3958         if (ret)
3959                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3960                                __LINE__);
3961
3962         length = sizeof(bssid);
3963         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
3964                                   bssid, &length);
3965         if (ret)
3966                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3967                                __LINE__);
3968
3969         length = sizeof(u32);
3970         ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &length);
3971         if (ret)
3972                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3973                                __LINE__);
3974
3975         out += sprintf(out, "ESSID: %s\n", essid);
3976         out += sprintf(out, "BSSID:   %02x:%02x:%02x:%02x:%02x:%02x\n",
3977                        bssid[0], bssid[1], bssid[2],
3978                        bssid[3], bssid[4], bssid[5]);
3979         out += sprintf(out, "Channel: %d\n", chan);
3980
3981         return out - buf;
3982 }
3983 static DEVICE_ATTR(bssinfo, S_IRUGO, show_bssinfo, NULL);
3984
3985
3986 #ifdef CONFIG_IPW_DEBUG
3987 static ssize_t show_debug_level(struct device_driver *d, char *buf)
3988 {
3989         return sprintf(buf, "0x%08X\n", ipw2100_debug_level);
3990 }
3991
3992 static ssize_t store_debug_level(struct device_driver *d, const char *buf,
3993                                  size_t count)
3994 {
3995         char *p = (char *)buf;
3996         u32 val;
3997
3998         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
3999                 p++;
4000                 if (p[0] == 'x' || p[0] == 'X')
4001                         p++;
4002                 val = simple_strtoul(p, &p, 16);
4003         } else
4004                 val = simple_strtoul(p, &p, 10);
4005         if (p == buf)
4006                 IPW_DEBUG_INFO(DRV_NAME
4007                        ": %s is not in hex or decimal form.\n", buf);
4008         else
4009                 ipw2100_debug_level = val;
4010
4011         return strnlen(buf, count);
4012 }
4013 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO, show_debug_level,
4014                    store_debug_level);
4015 #endif /* CONFIG_IPW_DEBUG */
4016
4017
4018 static ssize_t show_fatal_error(struct device *d,
4019                         struct device_attribute *attr, char *buf)
4020 {
4021         struct ipw2100_priv *priv = dev_get_drvdata(d);
4022         char *out = buf;
4023         int i;
4024
4025         if (priv->fatal_error)
4026                 out += sprintf(out, "0x%08X\n",
4027                                priv->fatal_error);
4028         else
4029                 out += sprintf(out, "0\n");
4030
4031         for (i = 1; i <= IPW2100_ERROR_QUEUE; i++) {
4032                 if (!priv->fatal_errors[(priv->fatal_index - i) %
4033                                         IPW2100_ERROR_QUEUE])
4034                         continue;
4035
4036                 out += sprintf(out, "%d. 0x%08X\n", i,
4037                                priv->fatal_errors[(priv->fatal_index - i) %
4038                                                   IPW2100_ERROR_QUEUE]);
4039         }
4040
4041         return out - buf;
4042 }
4043
4044 static ssize_t store_fatal_error(struct device *d,
4045                 struct device_attribute *attr, const char *buf, size_t count)
4046 {
4047         struct ipw2100_priv *priv = dev_get_drvdata(d);
4048         schedule_reset(priv);
4049         return count;
4050 }
4051 static DEVICE_ATTR(fatal_error, S_IWUSR|S_IRUGO, show_fatal_error, store_fatal_error);
4052
4053
4054 static ssize_t show_scan_age(struct device *d, struct device_attribute *attr,
4055                                 char *buf)
4056 {
4057         struct ipw2100_priv *priv = dev_get_drvdata(d);
4058         return sprintf(buf, "%d\n", priv->ieee->scan_age);
4059 }
4060
4061 static ssize_t store_scan_age(struct device *d, struct device_attribute *attr,
4062                                 const char *buf, size_t count)
4063 {
4064         struct ipw2100_priv *priv = dev_get_drvdata(d);
4065         struct net_device *dev = priv->net_dev;
4066         char buffer[] = "00000000";
4067         unsigned long len =
4068             (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
4069         unsigned long val;
4070         char *p = buffer;
4071
4072         IPW_DEBUG_INFO("enter\n");
4073
4074         strncpy(buffer, buf, len);
4075         buffer[len] = 0;
4076
4077         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
4078                 p++;
4079                 if (p[0] == 'x' || p[0] == 'X')
4080                         p++;
4081                 val = simple_strtoul(p, &p, 16);
4082         } else
4083                 val = simple_strtoul(p, &p, 10);
4084         if (p == buffer) {
4085                 IPW_DEBUG_INFO("%s: user supplied invalid value.\n",
4086                        dev->name);
4087         } else {
4088                 priv->ieee->scan_age = val;
4089                 IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
4090         }
4091
4092         IPW_DEBUG_INFO("exit\n");
4093         return len;
4094 }
4095 static DEVICE_ATTR(scan_age, S_IWUSR | S_IRUGO, show_scan_age, store_scan_age);
4096
4097
4098 static ssize_t show_rf_kill(struct device *d, struct device_attribute *attr,
4099                                 char *buf)
4100 {
4101         /* 0 - RF kill not enabled
4102            1 - SW based RF kill active (sysfs)
4103            2 - HW based RF kill active
4104            3 - Both HW and SW baed RF kill active */
4105         struct ipw2100_priv *priv = (struct ipw2100_priv *)d->driver_data;
4106         int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
4107                 (rf_kill_active(priv) ? 0x2 : 0x0);
4108         return sprintf(buf, "%i\n", val);
4109 }
4110
4111 static int ipw_radio_kill_sw(struct ipw2100_priv *priv, int disable_radio)
4112 {
4113         if ((disable_radio ? 1 : 0) ==
4114             (priv->status & STATUS_RF_KILL_SW ? 1 : 0))
4115                 return 0 ;
4116
4117         IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
4118                           disable_radio ? "OFF" : "ON");
4119
4120         down(&priv->action_sem);
4121
4122         if (disable_radio) {
4123                 priv->status |= STATUS_RF_KILL_SW;
4124                 ipw2100_down(priv);
4125         } else {
4126                 priv->status &= ~STATUS_RF_KILL_SW;
4127                 if (rf_kill_active(priv)) {
4128                         IPW_DEBUG_RF_KILL("Can not turn radio back on - "
4129                                           "disabled by HW switch\n");
4130                         /* Make sure the RF_KILL check timer is running */
4131                         priv->stop_rf_kill = 0;
4132                         cancel_delayed_work(&priv->rf_kill);
4133                         queue_delayed_work(priv->workqueue, &priv->rf_kill,
4134                                            HZ);
4135                 } else
4136                         schedule_reset(priv);
4137         }
4138
4139         up(&priv->action_sem);
4140         return 1;
4141 }
4142
4143 static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
4144                                 const char *buf, size_t count)
4145 {
4146         struct ipw2100_priv *priv = dev_get_drvdata(d);
4147         ipw_radio_kill_sw(priv, buf[0] == '1');
4148         return count;
4149 }
4150 static DEVICE_ATTR(rf_kill, S_IWUSR|S_IRUGO, show_rf_kill, store_rf_kill);
4151
4152
4153 static struct attribute *ipw2100_sysfs_entries[] = {
4154         &dev_attr_hardware.attr,
4155         &dev_attr_registers.attr,
4156         &dev_attr_ordinals.attr,
4157         &dev_attr_pci.attr,
4158         &dev_attr_stats.attr,
4159         &dev_attr_internals.attr,
4160         &dev_attr_bssinfo.attr,
4161         &dev_attr_memory.attr,
4162         &dev_attr_scan_age.attr,
4163         &dev_attr_fatal_error.attr,
4164         &dev_attr_rf_kill.attr,
4165         &dev_attr_cfg.attr,
4166         &dev_attr_status.attr,
4167         &dev_attr_capability.attr,
4168         NULL,
4169 };
4170
4171 static struct attribute_group ipw2100_attribute_group = {
4172         .attrs = ipw2100_sysfs_entries,
4173 };
4174
4175
4176 static int status_queue_allocate(struct ipw2100_priv *priv, int entries)
4177 {
4178         struct ipw2100_status_queue *q = &priv->status_queue;
4179
4180         IPW_DEBUG_INFO("enter\n");
4181
4182         q->size = entries * sizeof(struct ipw2100_status);
4183         q->drv = (struct ipw2100_status *)pci_alloc_consistent(
4184                 priv->pci_dev, q->size, &q->nic);
4185         if (!q->drv) {
4186                 IPW_DEBUG_WARNING(
4187                        "Can not allocate status queue.\n");
4188                 return -ENOMEM;
4189         }
4190
4191         memset(q->drv, 0, q->size);
4192
4193         IPW_DEBUG_INFO("exit\n");
4194
4195         return 0;
4196 }
4197
4198 static void status_queue_free(struct ipw2100_priv *priv)
4199 {
4200         IPW_DEBUG_INFO("enter\n");
4201
4202         if (priv->status_queue.drv) {
4203                 pci_free_consistent(
4204                         priv->pci_dev, priv->status_queue.size,
4205                         priv->status_queue.drv, priv->status_queue.nic);
4206                 priv->status_queue.drv = NULL;
4207         }
4208
4209         IPW_DEBUG_INFO("exit\n");
4210 }
4211
4212 static int bd_queue_allocate(struct ipw2100_priv *priv,
4213                              struct ipw2100_bd_queue *q, int entries)
4214 {
4215         IPW_DEBUG_INFO("enter\n");
4216
4217         memset(q, 0, sizeof(struct ipw2100_bd_queue));
4218
4219         q->entries = entries;
4220         q->size = entries * sizeof(struct ipw2100_bd);
4221         q->drv = pci_alloc_consistent(priv->pci_dev, q->size, &q->nic);
4222         if (!q->drv) {
4223                 IPW_DEBUG_INFO("can't allocate shared memory for buffer descriptors\n");
4224                 return -ENOMEM;
4225         }
4226         memset(q->drv, 0, q->size);
4227
4228         IPW_DEBUG_INFO("exit\n");
4229
4230         return 0;
4231 }
4232
4233 static void bd_queue_free(struct ipw2100_priv *priv,
4234                           struct ipw2100_bd_queue *q)
4235 {
4236         IPW_DEBUG_INFO("enter\n");
4237
4238         if (!q)
4239                 return;
4240
4241         if (q->drv) {
4242                 pci_free_consistent(priv->pci_dev,
4243                                     q->size, q->drv, q->nic);
4244                 q->drv = NULL;
4245         }
4246
4247         IPW_DEBUG_INFO("exit\n");
4248 }
4249
4250 static void bd_queue_initialize(
4251         struct ipw2100_priv *priv, struct ipw2100_bd_queue * q,
4252         u32 base, u32 size, u32 r, u32 w)
4253 {
4254         IPW_DEBUG_INFO("enter\n");
4255
4256         IPW_DEBUG_INFO("initializing bd queue at virt=%p, phys=%08x\n", q->drv, (u32)q->nic);
4257
4258         write_register(priv->net_dev, base, q->nic);
4259         write_register(priv->net_dev, size, q->entries);
4260         write_register(priv->net_dev, r, q->oldest);
4261         write_register(priv->net_dev, w, q->next);
4262
4263         IPW_DEBUG_INFO("exit\n");
4264 }
4265
4266 static void ipw2100_kill_workqueue(struct ipw2100_priv *priv)
4267 {
4268         if (priv->workqueue) {
4269                 priv->stop_rf_kill = 1;
4270                 priv->stop_hang_check = 1;
4271                 cancel_delayed_work(&priv->reset_work);
4272                 cancel_delayed_work(&priv->security_work);
4273                 cancel_delayed_work(&priv->wx_event_work);
4274                 cancel_delayed_work(&priv->hang_check);
4275                 cancel_delayed_work(&priv->rf_kill);
4276                 destroy_workqueue(priv->workqueue);
4277                 priv->workqueue = NULL;
4278         }
4279 }
4280
4281 static int ipw2100_tx_allocate(struct ipw2100_priv *priv)
4282 {
4283         int i, j, err = -EINVAL;
4284         void *v;
4285         dma_addr_t p;
4286
4287         IPW_DEBUG_INFO("enter\n");
4288
4289         err = bd_queue_allocate(priv, &priv->tx_queue, TX_QUEUE_LENGTH);
4290         if (err) {
4291                 IPW_DEBUG_ERROR("%s: failed bd_queue_allocate\n",
4292                        priv->net_dev->name);
4293                 return err;
4294         }
4295
4296         priv->tx_buffers = (struct ipw2100_tx_packet *)kmalloc(
4297                 TX_PENDED_QUEUE_LENGTH * sizeof(struct ipw2100_tx_packet),
4298                 GFP_ATOMIC);
4299         if (!priv->tx_buffers) {
4300                 printk(KERN_ERR DRV_NAME ": %s: alloc failed form tx buffers.\n",
4301                        priv->net_dev->name);
4302                 bd_queue_free(priv, &priv->tx_queue);
4303                 return -ENOMEM;
4304         }
4305
4306         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4307                 v = pci_alloc_consistent(
4308                         priv->pci_dev, sizeof(struct ipw2100_data_header), &p);
4309                 if (!v) {
4310                         printk(KERN_ERR DRV_NAME ": %s: PCI alloc failed for tx "
4311                                "buffers.\n", priv->net_dev->name);
4312                         err = -ENOMEM;
4313                         break;
4314                 }
4315
4316                 priv->tx_buffers[i].type = DATA;
4317                 priv->tx_buffers[i].info.d_struct.data = (struct ipw2100_data_header*)v;
4318                 priv->tx_buffers[i].info.d_struct.data_phys = p;
4319                 priv->tx_buffers[i].info.d_struct.txb = NULL;
4320         }
4321
4322         if (i == TX_PENDED_QUEUE_LENGTH)
4323                 return 0;
4324
4325         for (j = 0; j < i; j++) {
4326                 pci_free_consistent(
4327                         priv->pci_dev,
4328                         sizeof(struct ipw2100_data_header),
4329                         priv->tx_buffers[j].info.d_struct.data,
4330                         priv->tx_buffers[j].info.d_struct.data_phys);
4331         }
4332
4333         kfree(priv->tx_buffers);
4334         priv->tx_buffers = NULL;
4335
4336         return err;
4337 }
4338
4339 static void ipw2100_tx_initialize(struct ipw2100_priv *priv)
4340 {
4341         int i;
4342
4343         IPW_DEBUG_INFO("enter\n");
4344
4345         /*
4346          * reinitialize packet info lists
4347          */
4348         INIT_LIST_HEAD(&priv->fw_pend_list);
4349         INIT_STAT(&priv->fw_pend_stat);
4350
4351         /*
4352          * reinitialize lists
4353          */
4354         INIT_LIST_HEAD(&priv->tx_pend_list);
4355         INIT_LIST_HEAD(&priv->tx_free_list);
4356         INIT_STAT(&priv->tx_pend_stat);
4357         INIT_STAT(&priv->tx_free_stat);
4358
4359         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4360                 /* We simply drop any SKBs that have been queued for
4361                  * transmit */
4362                 if (priv->tx_buffers[i].info.d_struct.txb) {
4363                         ieee80211_txb_free(priv->tx_buffers[i].info.d_struct.txb);
4364                         priv->tx_buffers[i].info.d_struct.txb = NULL;
4365                 }
4366
4367                 list_add_tail(&priv->tx_buffers[i].list, &priv->tx_free_list);
4368         }
4369
4370         SET_STAT(&priv->tx_free_stat, i);
4371
4372         priv->tx_queue.oldest = 0;
4373         priv->tx_queue.available = priv->tx_queue.entries;
4374         priv->tx_queue.next = 0;
4375         INIT_STAT(&priv->txq_stat);
4376         SET_STAT(&priv->txq_stat, priv->tx_queue.available);
4377
4378         bd_queue_initialize(priv, &priv->tx_queue,
4379                             IPW_MEM_HOST_SHARED_TX_QUEUE_BD_BASE,
4380                             IPW_MEM_HOST_SHARED_TX_QUEUE_BD_SIZE,
4381                             IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
4382                             IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX);
4383
4384         IPW_DEBUG_INFO("exit\n");
4385
4386 }
4387
4388 static void ipw2100_tx_free(struct ipw2100_priv *priv)
4389 {
4390         int i;
4391
4392         IPW_DEBUG_INFO("enter\n");
4393
4394         bd_queue_free(priv, &priv->tx_queue);
4395
4396         if (!priv->tx_buffers)
4397                 return;
4398
4399         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4400                 if (priv->tx_buffers[i].info.d_struct.txb) {
4401                         ieee80211_txb_free(priv->tx_buffers[i].info.d_struct.txb);
4402                         priv->tx_buffers[i].info.d_struct.txb = NULL;
4403                 }
4404                 if (priv->tx_buffers[i].info.d_struct.data)
4405                         pci_free_consistent(
4406                                 priv->pci_dev,
4407                                 sizeof(struct ipw2100_data_header),
4408                                 priv->tx_buffers[i].info.d_struct.data,
4409                                 priv->tx_buffers[i].info.d_struct.data_phys);
4410         }
4411
4412         kfree(priv->tx_buffers);
4413         priv->tx_buffers = NULL;
4414
4415         IPW_DEBUG_INFO("exit\n");
4416 }
4417
4418
4419
4420 static int ipw2100_rx_allocate(struct ipw2100_priv *priv)
4421 {
4422         int i, j, err = -EINVAL;
4423
4424         IPW_DEBUG_INFO("enter\n");
4425
4426         err = bd_queue_allocate(priv, &priv->rx_queue, RX_QUEUE_LENGTH);
4427         if (err) {
4428                 IPW_DEBUG_INFO("failed bd_queue_allocate\n");
4429                 return err;
4430         }
4431
4432         err = status_queue_allocate(priv, RX_QUEUE_LENGTH);
4433         if (err) {
4434                 IPW_DEBUG_INFO("failed status_queue_allocate\n");
4435                 bd_queue_free(priv, &priv->rx_queue);
4436                 return err;
4437         }
4438
4439         /*
4440          * allocate packets
4441          */
4442         priv->rx_buffers = (struct ipw2100_rx_packet *)
4443             kmalloc(RX_QUEUE_LENGTH * sizeof(struct ipw2100_rx_packet),
4444                     GFP_KERNEL);
4445         if (!priv->rx_buffers) {
4446                 IPW_DEBUG_INFO("can't allocate rx packet buffer table\n");
4447
4448                 bd_queue_free(priv, &priv->rx_queue);
4449
4450                 status_queue_free(priv);
4451
4452                 return -ENOMEM;
4453         }
4454
4455         for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4456                 struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
4457
4458                 err = ipw2100_alloc_skb(priv, packet);
4459                 if (unlikely(err)) {
4460                         err = -ENOMEM;
4461                         break;
4462                 }
4463
4464                 /* The BD holds the cache aligned address */
4465                 priv->rx_queue.drv[i].host_addr = packet->dma_addr;
4466                 priv->rx_queue.drv[i].buf_length = IPW_RX_NIC_BUFFER_LENGTH;
4467                 priv->status_queue.drv[i].status_fields = 0;
4468         }
4469
4470         if (i == RX_QUEUE_LENGTH)
4471                 return 0;
4472
4473         for (j = 0; j < i; j++) {
4474                 pci_unmap_single(priv->pci_dev, priv->rx_buffers[j].dma_addr,
4475                                  sizeof(struct ipw2100_rx_packet),
4476                                  PCI_DMA_FROMDEVICE);
4477                 dev_kfree_skb(priv->rx_buffers[j].skb);
4478         }
4479
4480         kfree(priv->rx_buffers);
4481         priv->rx_buffers = NULL;
4482
4483         bd_queue_free(priv, &priv->rx_queue);
4484
4485         status_queue_free(priv);
4486
4487         return err;
4488 }
4489
4490 static void ipw2100_rx_initialize(struct ipw2100_priv *priv)
4491 {
4492         IPW_DEBUG_INFO("enter\n");
4493
4494         priv->rx_queue.oldest = 0;
4495         priv->rx_queue.available = priv->rx_queue.entries - 1;
4496         priv->rx_queue.next = priv->rx_queue.entries - 1;
4497
4498         INIT_STAT(&priv->rxq_stat);
4499         SET_STAT(&priv->rxq_stat, priv->rx_queue.available);
4500
4501         bd_queue_initialize(priv, &priv->rx_queue,
4502                             IPW_MEM_HOST_SHARED_RX_BD_BASE,
4503                             IPW_MEM_HOST_SHARED_RX_BD_SIZE,
4504                             IPW_MEM_HOST_SHARED_RX_READ_INDEX,
4505                             IPW_MEM_HOST_SHARED_RX_WRITE_INDEX);
4506
4507         /* set up the status queue */
4508         write_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_STATUS_BASE,
4509                        priv->status_queue.nic);
4510
4511         IPW_DEBUG_INFO("exit\n");
4512 }
4513
4514 static void ipw2100_rx_free(struct ipw2100_priv *priv)
4515 {
4516         int i;
4517
4518         IPW_DEBUG_INFO("enter\n");
4519
4520         bd_queue_free(priv, &priv->rx_queue);
4521         status_queue_free(priv);
4522
4523         if (!priv->rx_buffers)
4524                 return;
4525
4526         for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4527                 if (priv->rx_buffers[i].rxp) {
4528                         pci_unmap_single(priv->pci_dev,
4529                                          priv->rx_buffers[i].dma_addr,
4530                                          sizeof(struct ipw2100_rx),
4531                                          PCI_DMA_FROMDEVICE);
4532                         dev_kfree_skb(priv->rx_buffers[i].skb);
4533                 }
4534         }
4535
4536         kfree(priv->rx_buffers);
4537         priv->rx_buffers = NULL;
4538
4539         IPW_DEBUG_INFO("exit\n");
4540 }
4541
4542 static int ipw2100_read_mac_address(struct ipw2100_priv *priv)
4543 {
4544         u32 length = ETH_ALEN;
4545         u8 mac[ETH_ALEN];
4546
4547         int err;
4548
4549         err = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ADAPTER_MAC,
4550                                   mac, &length);
4551         if (err) {
4552                 IPW_DEBUG_INFO("MAC address read failed\n");
4553                 return -EIO;
4554         }
4555         IPW_DEBUG_INFO("card MAC is %02X:%02X:%02X:%02X:%02X:%02X\n",
4556                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4557
4558         memcpy(priv->net_dev->dev_addr, mac, ETH_ALEN);
4559
4560         return 0;
4561 }
4562
4563 /********************************************************************
4564  *
4565  * Firmware Commands
4566  *
4567  ********************************************************************/
4568
4569 static int ipw2100_set_mac_address(struct ipw2100_priv *priv, int batch_mode)
4570 {
4571         struct host_command cmd = {
4572                 .host_command = ADAPTER_ADDRESS,
4573                 .host_command_sequence = 0,
4574                 .host_command_length = ETH_ALEN
4575         };
4576         int err;
4577
4578         IPW_DEBUG_HC("SET_MAC_ADDRESS\n");
4579
4580         IPW_DEBUG_INFO("enter\n");
4581
4582         if (priv->config & CFG_CUSTOM_MAC) {
4583                 memcpy(cmd.host_command_parameters, priv->mac_addr,
4584                        ETH_ALEN);
4585                 memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
4586         } else
4587                 memcpy(cmd.host_command_parameters, priv->net_dev->dev_addr,
4588                        ETH_ALEN);
4589
4590         err = ipw2100_hw_send_command(priv, &cmd);
4591
4592         IPW_DEBUG_INFO("exit\n");
4593         return err;
4594 }
4595
4596 static int ipw2100_set_port_type(struct ipw2100_priv *priv, u32 port_type,
4597                                  int batch_mode)
4598 {
4599         struct host_command cmd = {
4600                 .host_command = PORT_TYPE,
4601                 .host_command_sequence = 0,
4602                 .host_command_length = sizeof(u32)
4603         };
4604         int err;
4605
4606         switch (port_type) {
4607         case IW_MODE_INFRA:
4608                 cmd.host_command_parameters[0] = IPW_BSS;
4609                 break;
4610         case IW_MODE_ADHOC:
4611                 cmd.host_command_parameters[0] = IPW_IBSS;
4612                 break;
4613         }
4614
4615         IPW_DEBUG_HC("PORT_TYPE: %s\n",
4616                      port_type == IPW_IBSS ? "Ad-Hoc" : "Managed");
4617
4618         if (!batch_mode) {
4619                 err = ipw2100_disable_adapter(priv);
4620                 if (err) {
4621                         printk(KERN_ERR DRV_NAME ": %s: Could not disable adapter %d\n",
4622                                priv->net_dev->name, err);
4623                         return err;
4624                 }
4625         }
4626
4627         /* send cmd to firmware */
4628         err = ipw2100_hw_send_command(priv, &cmd);
4629
4630         if (!batch_mode)
4631                 ipw2100_enable_adapter(priv);
4632
4633         return err;
4634 }
4635
4636
4637 static int ipw2100_set_channel(struct ipw2100_priv *priv, u32 channel,
4638                                int batch_mode)
4639 {
4640         struct host_command cmd = {
4641                 .host_command = CHANNEL,
4642                 .host_command_sequence = 0,
4643                 .host_command_length = sizeof(u32)
4644         };
4645         int err;
4646
4647         cmd.host_command_parameters[0] = channel;
4648
4649         IPW_DEBUG_HC("CHANNEL: %d\n", channel);
4650
4651         /* If BSS then we don't support channel selection */
4652         if (priv->ieee->iw_mode == IW_MODE_INFRA)
4653                 return 0;
4654
4655         if ((channel != 0) &&
4656             ((channel < REG_MIN_CHANNEL) || (channel > REG_MAX_CHANNEL)))
4657                 return -EINVAL;
4658
4659         if (!batch_mode) {
4660                 err = ipw2100_disable_adapter(priv);
4661                 if (err)
4662                         return err;
4663         }
4664
4665         err = ipw2100_hw_send_command(priv, &cmd);
4666         if (err) {
4667                 IPW_DEBUG_INFO("Failed to set channel to %d",
4668                                channel);
4669                 return err;
4670         }
4671
4672         if (channel)
4673                 priv->config |= CFG_STATIC_CHANNEL;
4674         else
4675                 priv->config &= ~CFG_STATIC_CHANNEL;
4676
4677         priv->channel = channel;
4678
4679         if (!batch_mode) {
4680                 err = ipw2100_enable_adapter(priv);
4681                 if (err)
4682                         return err;
4683         }
4684
4685         return 0;
4686 }
4687
4688 static int ipw2100_system_config(struct ipw2100_priv *priv, int batch_mode)
4689 {
4690         struct host_command cmd = {
4691                 .host_command = SYSTEM_CONFIG,
4692                 .host_command_sequence = 0,
4693                 .host_command_length = 12,
4694         };
4695         u32 ibss_mask, len = sizeof(u32);
4696         int err;
4697
4698         /* Set system configuration */
4699
4700         if (!batch_mode) {
4701                 err = ipw2100_disable_adapter(priv);
4702                 if (err)
4703                         return err;
4704         }
4705
4706         if (priv->ieee->iw_mode == IW_MODE_ADHOC)
4707                 cmd.host_command_parameters[0] |= IPW_CFG_IBSS_AUTO_START;
4708
4709         cmd.host_command_parameters[0] |= IPW_CFG_IBSS_MASK |
4710                 IPW_CFG_BSS_MASK |
4711                 IPW_CFG_802_1x_ENABLE;
4712
4713         if (!(priv->config & CFG_LONG_PREAMBLE))
4714                 cmd.host_command_parameters[0] |= IPW_CFG_PREAMBLE_AUTO;
4715
4716         err = ipw2100_get_ordinal(priv,
4717                                   IPW_ORD_EEPROM_IBSS_11B_CHANNELS,
4718                                   &ibss_mask,  &len);
4719         if (err)
4720                 ibss_mask = IPW_IBSS_11B_DEFAULT_MASK;
4721
4722         cmd.host_command_parameters[1] = REG_CHANNEL_MASK;
4723         cmd.host_command_parameters[2] = REG_CHANNEL_MASK & ibss_mask;
4724
4725         /* 11b only */
4726         /*cmd.host_command_parameters[0] |= DIVERSITY_ANTENNA_A;*/
4727
4728         err = ipw2100_hw_send_command(priv, &cmd);
4729         if (err)
4730                 return err;
4731
4732 /* If IPv6 is configured in the kernel then we don't want to filter out all
4733  * of the multicast packets as IPv6 needs some. */
4734 #if !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE)
4735         cmd.host_command = ADD_MULTICAST;
4736         cmd.host_command_sequence = 0;
4737         cmd.host_command_length = 0;
4738
4739         ipw2100_hw_send_command(priv, &cmd);
4740 #endif
4741         if (!batch_mode) {
4742                 err = ipw2100_enable_adapter(priv);
4743                 if (err)
4744                         return err;
4745         }
4746
4747         return 0;
4748 }
4749
4750 static int ipw2100_set_tx_rates(struct ipw2100_priv *priv, u32 rate,
4751                                 int batch_mode)
4752 {
4753         struct host_command cmd = {
4754                 .host_command = BASIC_TX_RATES,
4755                 .host_command_sequence = 0,
4756                 .host_command_length = 4
4757         };
4758         int err;
4759
4760         cmd.host_command_parameters[0] = rate & TX_RATE_MASK;
4761
4762         if (!batch_mode) {
4763                 err = ipw2100_disable_adapter(priv);
4764                 if (err)
4765                         return err;
4766         }
4767
4768         /* Set BASIC TX Rate first */
4769         ipw2100_hw_send_command(priv, &cmd);
4770
4771         /* Set TX Rate */
4772         cmd.host_command = TX_RATES;
4773         ipw2100_hw_send_command(priv, &cmd);
4774
4775         /* Set MSDU TX Rate */
4776         cmd.host_command = MSDU_TX_RATES;
4777         ipw2100_hw_send_command(priv, &cmd);
4778
4779         if (!batch_mode) {
4780                 err = ipw2100_enable_adapter(priv);
4781                 if (err)
4782                         return err;
4783         }
4784
4785         priv->tx_rates = rate;
4786
4787         return 0;
4788 }
4789
4790 static int ipw2100_set_power_mode(struct ipw2100_priv *priv,
4791                                   int power_level)
4792 {
4793         struct host_command cmd = {
4794                 .host_command = POWER_MODE,
4795                 .host_command_sequence = 0,
4796                 .host_command_length = 4
4797         };
4798         int err;
4799
4800         cmd.host_command_parameters[0] = power_level;
4801
4802         err = ipw2100_hw_send_command(priv, &cmd);
4803         if (err)
4804                 return err;
4805
4806         if (power_level == IPW_POWER_MODE_CAM)
4807                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
4808         else
4809                 priv->power_mode = IPW_POWER_ENABLED | power_level;
4810
4811 #ifdef CONFIG_IPW2100_TX_POWER
4812         if (priv->port_type == IBSS &&
4813             priv->adhoc_power != DFTL_IBSS_TX_POWER) {
4814                 /* Set beacon interval */
4815                 cmd.host_command = TX_POWER_INDEX;
4816                 cmd.host_command_parameters[0] = (u32)priv->adhoc_power;
4817
4818                 err = ipw2100_hw_send_command(priv, &cmd);
4819                 if (err)
4820                         return err;
4821         }
4822 #endif
4823
4824         return 0;
4825 }
4826
4827
4828 static int ipw2100_set_rts_threshold(struct ipw2100_priv *priv, u32 threshold)
4829 {
4830         struct host_command cmd = {
4831                 .host_command = RTS_THRESHOLD,
4832                 .host_command_sequence = 0,
4833                 .host_command_length = 4
4834         };
4835         int err;
4836
4837         if (threshold & RTS_DISABLED)
4838                 cmd.host_command_parameters[0] = MAX_RTS_THRESHOLD;
4839         else
4840                 cmd.host_command_parameters[0] = threshold & ~RTS_DISABLED;
4841
4842         err = ipw2100_hw_send_command(priv, &cmd);
4843         if (err)
4844                 return err;
4845
4846         priv->rts_threshold = threshold;
4847
4848         return 0;
4849 }
4850
4851 #if 0
4852 int ipw2100_set_fragmentation_threshold(struct ipw2100_priv *priv,
4853                                         u32 threshold, int batch_mode)
4854 {
4855         struct host_command cmd = {
4856                 .host_command = FRAG_THRESHOLD,
4857                 .host_command_sequence = 0,
4858                 .host_command_length = 4,
4859                 .host_command_parameters[0] = 0,
4860         };
4861         int err;
4862
4863         if (!batch_mode) {
4864                 err = ipw2100_disable_adapter(priv);
4865                 if (err)
4866                         return err;
4867         }
4868
4869         if (threshold == 0)
4870                 threshold = DEFAULT_FRAG_THRESHOLD;
4871         else {
4872                 threshold = max(threshold, MIN_FRAG_THRESHOLD);
4873                 threshold = min(threshold, MAX_FRAG_THRESHOLD);
4874         }
4875
4876         cmd.host_command_parameters[0] = threshold;
4877
4878         IPW_DEBUG_HC("FRAG_THRESHOLD: %u\n", threshold);
4879
4880         err = ipw2100_hw_send_command(priv, &cmd);
4881
4882         if (!batch_mode)
4883                 ipw2100_enable_adapter(priv);
4884
4885         if (!err)
4886                 priv->frag_threshold = threshold;
4887
4888         return err;
4889 }
4890 #endif
4891
4892 static int ipw2100_set_short_retry(struct ipw2100_priv *priv, u32 retry)
4893 {
4894         struct host_command cmd = {
4895                 .host_command = SHORT_RETRY_LIMIT,
4896                 .host_command_sequence = 0,
4897                 .host_command_length = 4
4898         };
4899         int err;
4900
4901         cmd.host_command_parameters[0] = retry;
4902
4903         err = ipw2100_hw_send_command(priv, &cmd);
4904         if (err)
4905                 return err;
4906
4907         priv->short_retry_limit = retry;
4908
4909         return 0;
4910 }
4911
4912 static int ipw2100_set_long_retry(struct ipw2100_priv *priv, u32 retry)
4913 {
4914         struct host_command cmd = {
4915                 .host_command = LONG_RETRY_LIMIT,
4916                 .host_command_sequence = 0,
4917                 .host_command_length = 4
4918         };
4919         int err;
4920
4921         cmd.host_command_parameters[0] = retry;
4922
4923         err = ipw2100_hw_send_command(priv, &cmd);
4924         if (err)
4925                 return err;
4926
4927         priv->long_retry_limit = retry;
4928
4929         return 0;
4930 }
4931
4932
4933 static int ipw2100_set_mandatory_bssid(struct ipw2100_priv *priv, u8 *bssid,
4934                                        int batch_mode)
4935 {
4936         struct host_command cmd = {
4937                 .host_command = MANDATORY_BSSID,
4938                 .host_command_sequence = 0,
4939                 .host_command_length = (bssid == NULL) ? 0 : ETH_ALEN
4940         };
4941         int err;
4942
4943 #ifdef CONFIG_IPW_DEBUG
4944         if (bssid != NULL)
4945                 IPW_DEBUG_HC(
4946                         "MANDATORY_BSSID: %02X:%02X:%02X:%02X:%02X:%02X\n",
4947                         bssid[0], bssid[1], bssid[2], bssid[3], bssid[4],
4948                         bssid[5]);
4949         else
4950                 IPW_DEBUG_HC("MANDATORY_BSSID: <clear>\n");
4951 #endif
4952         /* if BSSID is empty then we disable mandatory bssid mode */
4953         if (bssid != NULL)
4954                 memcpy((u8 *)cmd.host_command_parameters, bssid, ETH_ALEN);
4955
4956         if (!batch_mode) {
4957                 err = ipw2100_disable_adapter(priv);
4958                 if (err)
4959                         return err;
4960         }
4961
4962         err = ipw2100_hw_send_command(priv, &cmd);
4963
4964         if (!batch_mode)
4965                 ipw2100_enable_adapter(priv);
4966
4967         return err;
4968 }
4969
4970 #ifdef CONFIG_IEEE80211_WPA
4971 static int ipw2100_disassociate_bssid(struct ipw2100_priv *priv)
4972 {
4973         struct host_command cmd = {
4974                 .host_command = DISASSOCIATION_BSSID,
4975                 .host_command_sequence = 0,
4976                 .host_command_length = ETH_ALEN
4977         };
4978         int err;
4979         int len;
4980
4981         IPW_DEBUG_HC("DISASSOCIATION_BSSID\n");
4982
4983         len = ETH_ALEN;
4984         /* The Firmware currently ignores the BSSID and just disassociates from
4985          * the currently associated AP -- but in the off chance that a future
4986          * firmware does use the BSSID provided here, we go ahead and try and
4987          * set it to the currently associated AP's BSSID */
4988         memcpy(cmd.host_command_parameters, priv->bssid, ETH_ALEN);
4989
4990         err = ipw2100_hw_send_command(priv, &cmd);
4991
4992         return err;
4993 }
4994 #endif
4995
4996 /*
4997  * Pseudo code for setting up wpa_frame:
4998  */
4999 #if 0
5000 void x(struct ieee80211_assoc_frame *wpa_assoc)
5001 {
5002         struct ipw2100_wpa_assoc_frame frame;
5003         frame->fixed_ie_mask = IPW_WPA_CAPABILTIES |
5004                 IPW_WPA_LISTENINTERVAL |
5005                 IPW_WPA_AP_ADDRESS;
5006         frame->capab_info = wpa_assoc->capab_info;
5007         frame->lisen_interval = wpa_assoc->listent_interval;
5008         memcpy(frame->current_ap, wpa_assoc->current_ap, ETH_ALEN);
5009
5010         /* UNKNOWN -- I'm not postivive about this part; don't have any WPA
5011          * setup here to test it with.
5012          *
5013          * Walk the IEs in the wpa_assoc and figure out the total size of all
5014          * that data.  Stick that into frame->var_ie_len.  Then memcpy() all of
5015          * the IEs from wpa_frame into frame.
5016          */
5017         frame->var_ie_len = calculate_ie_len(wpa_assoc);
5018         memcpy(frame->var_ie,  wpa_assoc->variable, frame->var_ie_len);
5019
5020         ipw2100_set_wpa_ie(priv, &frame, 0);
5021 }
5022 #endif
5023
5024
5025
5026
5027 static int ipw2100_set_wpa_ie(struct ipw2100_priv *,
5028                               struct ipw2100_wpa_assoc_frame *, int)
5029 __attribute__ ((unused));
5030
5031 static int ipw2100_set_wpa_ie(struct ipw2100_priv *priv,
5032                               struct ipw2100_wpa_assoc_frame *wpa_frame,
5033                               int batch_mode)
5034 {
5035         struct host_command cmd = {
5036                 .host_command = SET_WPA_IE,
5037                 .host_command_sequence = 0,
5038                 .host_command_length = sizeof(struct ipw2100_wpa_assoc_frame),
5039         };
5040         int err;
5041
5042         IPW_DEBUG_HC("SET_WPA_IE\n");
5043
5044         if (!batch_mode) {
5045                 err = ipw2100_disable_adapter(priv);
5046                 if (err)
5047                         return err;
5048         }
5049
5050         memcpy(cmd.host_command_parameters, wpa_frame,
5051                sizeof(struct ipw2100_wpa_assoc_frame));
5052
5053         err = ipw2100_hw_send_command(priv, &cmd);
5054
5055         if (!batch_mode) {
5056                 if (ipw2100_enable_adapter(priv))
5057                         err = -EIO;
5058         }
5059
5060         return err;
5061 }
5062
5063 struct security_info_params {
5064         u32 allowed_ciphers;
5065         u16 version;
5066         u8 auth_mode;
5067         u8 replay_counters_number;
5068         u8 unicast_using_group;
5069 } __attribute__ ((packed));
5070
5071 static int ipw2100_set_security_information(struct ipw2100_priv *priv,
5072                                             int auth_mode,
5073                                             int security_level,
5074                                             int unicast_using_group,
5075                                             int batch_mode)
5076 {
5077         struct host_command cmd = {
5078                 .host_command = SET_SECURITY_INFORMATION,
5079                 .host_command_sequence = 0,
5080                 .host_command_length = sizeof(struct security_info_params)
5081         };
5082         struct security_info_params *security =
5083                 (struct security_info_params *)&cmd.host_command_parameters;
5084         int err;
5085         memset(security, 0, sizeof(*security));
5086
5087         /* If shared key AP authentication is turned on, then we need to
5088          * configure the firmware to try and use it.
5089          *
5090          * Actual data encryption/decryption is handled by the host. */
5091         security->auth_mode = auth_mode;
5092         security->unicast_using_group = unicast_using_group;
5093
5094         switch (security_level) {
5095         default:
5096         case SEC_LEVEL_0:
5097                 security->allowed_ciphers = IPW_NONE_CIPHER;
5098                 break;
5099         case SEC_LEVEL_1:
5100                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5101                         IPW_WEP104_CIPHER;
5102                 break;
5103         case SEC_LEVEL_2:
5104                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5105                         IPW_WEP104_CIPHER | IPW_TKIP_CIPHER;
5106                 break;
5107         case SEC_LEVEL_2_CKIP:
5108                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5109                         IPW_WEP104_CIPHER | IPW_CKIP_CIPHER;
5110                 break;
5111         case SEC_LEVEL_3:
5112                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5113                         IPW_WEP104_CIPHER | IPW_TKIP_CIPHER | IPW_CCMP_CIPHER;
5114                 break;
5115         }
5116
5117         IPW_DEBUG_HC(
5118                 "SET_SECURITY_INFORMATION: auth:%d cipher:0x%02X (level %d)\n",
5119                 security->auth_mode, security->allowed_ciphers, security_level);
5120
5121         security->replay_counters_number = 0;
5122
5123         if (!batch_mode) {
5124                 err = ipw2100_disable_adapter(priv);
5125                 if (err)
5126                         return err;
5127         }
5128
5129         err = ipw2100_hw_send_command(priv, &cmd);
5130
5131         if (!batch_mode)
5132                 ipw2100_enable_adapter(priv);
5133
5134         return err;
5135 }
5136
5137 static int ipw2100_set_tx_power(struct ipw2100_priv *priv,
5138                                 u32 tx_power)
5139 {
5140         struct host_command cmd = {
5141                 .host_command = TX_POWER_INDEX,
5142                 .host_command_sequence = 0,
5143                 .host_command_length = 4
5144         };
5145         int err = 0;
5146
5147         cmd.host_command_parameters[0] = tx_power;
5148
5149         if (priv->ieee->iw_mode == IW_MODE_ADHOC)
5150                 err = ipw2100_hw_send_command(priv, &cmd);
5151         if (!err)
5152                 priv->tx_power = tx_power;
5153
5154         return 0;
5155 }
5156
5157 static int ipw2100_set_ibss_beacon_interval(struct ipw2100_priv *priv,
5158                                             u32 interval, int batch_mode)
5159 {
5160         struct host_command cmd = {
5161                 .host_command = BEACON_INTERVAL,
5162                 .host_command_sequence = 0,
5163                 .host_command_length = 4
5164         };
5165         int err;
5166
5167         cmd.host_command_parameters[0] = interval;
5168
5169         IPW_DEBUG_INFO("enter\n");
5170
5171         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5172                 if (!batch_mode) {
5173                         err = ipw2100_disable_adapter(priv);
5174                         if (err)
5175                                 return err;
5176                 }
5177
5178                 ipw2100_hw_send_command(priv, &cmd);
5179
5180                 if (!batch_mode) {
5181                         err = ipw2100_enable_adapter(priv);
5182                         if (err)
5183                                 return err;
5184                 }
5185         }
5186
5187         IPW_DEBUG_INFO("exit\n");
5188
5189         return 0;
5190 }
5191
5192
5193 void ipw2100_queues_initialize(struct ipw2100_priv *priv)
5194 {
5195         ipw2100_tx_initialize(priv);
5196         ipw2100_rx_initialize(priv);
5197         ipw2100_msg_initialize(priv);
5198 }
5199
5200 void ipw2100_queues_free(struct ipw2100_priv *priv)
5201 {
5202         ipw2100_tx_free(priv);
5203         ipw2100_rx_free(priv);
5204         ipw2100_msg_free(priv);
5205 }
5206
5207 int ipw2100_queues_allocate(struct ipw2100_priv *priv)
5208 {
5209         if (ipw2100_tx_allocate(priv) ||
5210             ipw2100_rx_allocate(priv) ||
5211             ipw2100_msg_allocate(priv))
5212                 goto fail;
5213
5214         return 0;
5215
5216  fail:
5217         ipw2100_tx_free(priv);
5218         ipw2100_rx_free(priv);
5219         ipw2100_msg_free(priv);
5220         return -ENOMEM;
5221 }
5222
5223 #define IPW_PRIVACY_CAPABLE 0x0008
5224
5225 static int ipw2100_set_wep_flags(struct ipw2100_priv *priv, u32 flags,
5226                                  int batch_mode)
5227 {
5228         struct host_command cmd = {
5229                 .host_command = WEP_FLAGS,
5230                 .host_command_sequence = 0,
5231                 .host_command_length = 4
5232         };
5233         int err;
5234
5235         cmd.host_command_parameters[0] = flags;
5236
5237         IPW_DEBUG_HC("WEP_FLAGS: flags = 0x%08X\n", flags);
5238
5239         if (!batch_mode) {
5240                 err = ipw2100_disable_adapter(priv);
5241                 if (err) {
5242                         printk(KERN_ERR DRV_NAME ": %s: Could not disable adapter %d\n",
5243                                priv->net_dev->name, err);
5244                         return err;
5245                 }
5246         }
5247
5248         /* send cmd to firmware */
5249         err = ipw2100_hw_send_command(priv, &cmd);
5250
5251         if (!batch_mode)
5252                 ipw2100_enable_adapter(priv);
5253
5254         return err;
5255 }
5256
5257 struct ipw2100_wep_key {
5258         u8 idx;
5259         u8 len;
5260         u8 key[13];
5261 };
5262
5263 /* Macros to ease up priting WEP keys */
5264 #define WEP_FMT_64  "%02X%02X%02X%02X-%02X"
5265 #define WEP_FMT_128 "%02X%02X%02X%02X-%02X%02X%02X%02X-%02X%02X%02X"
5266 #define WEP_STR_64(x) x[0],x[1],x[2],x[3],x[4]
5267 #define WEP_STR_128(x) x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9],x[10]
5268
5269
5270 /**
5271  * Set a the wep key
5272  *
5273  * @priv: struct to work on
5274  * @idx: index of the key we want to set
5275  * @key: ptr to the key data to set
5276  * @len: length of the buffer at @key
5277  * @batch_mode: FIXME perform the operation in batch mode, not
5278  *              disabling the device.
5279  *
5280  * @returns 0 if OK, < 0 errno code on error.
5281  *
5282  * Fill out a command structure with the new wep key, length an
5283  * index and send it down the wire.
5284  */
5285 static int ipw2100_set_key(struct ipw2100_priv *priv,
5286                            int idx, char *key, int len, int batch_mode)
5287 {
5288         int keylen = len ? (len <= 5 ? 5 : 13) : 0;
5289         struct host_command cmd = {
5290                 .host_command = WEP_KEY_INFO,
5291                 .host_command_sequence = 0,
5292                 .host_command_length = sizeof(struct ipw2100_wep_key),
5293         };
5294         struct ipw2100_wep_key *wep_key = (void*)cmd.host_command_parameters;
5295         int err;
5296
5297         IPW_DEBUG_HC("WEP_KEY_INFO: index = %d, len = %d/%d\n",
5298                                  idx, keylen, len);
5299
5300         /* NOTE: We don't check cached values in case the firmware was reset
5301          * or some other problem is occuring.  If the user is setting the key,
5302          * then we push the change */
5303
5304         wep_key->idx = idx;
5305         wep_key->len = keylen;
5306
5307         if (keylen) {
5308                 memcpy(wep_key->key, key, len);
5309                 memset(wep_key->key + len, 0, keylen - len);
5310         }
5311
5312         /* Will be optimized out on debug not being configured in */
5313         if (keylen == 0)
5314                 IPW_DEBUG_WEP("%s: Clearing key %d\n",
5315                                   priv->net_dev->name, wep_key->idx);
5316         else if (keylen == 5)
5317                 IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_64 "\n",
5318                                   priv->net_dev->name, wep_key->idx, wep_key->len,
5319                                   WEP_STR_64(wep_key->key));
5320         else
5321                 IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_128
5322                                   "\n",
5323                                   priv->net_dev->name, wep_key->idx, wep_key->len,
5324                                   WEP_STR_128(wep_key->key));
5325
5326         if (!batch_mode) {
5327                 err = ipw2100_disable_adapter(priv);
5328                 /* FIXME: IPG: shouldn't this prink be in _disable_adapter()? */
5329                 if (err) {
5330                         printk(KERN_ERR DRV_NAME ": %s: Could not disable adapter %d\n",
5331                                priv->net_dev->name, err);
5332                         return err;
5333                 }
5334         }
5335
5336         /* send cmd to firmware */
5337         err = ipw2100_hw_send_command(priv, &cmd);
5338
5339         if (!batch_mode) {
5340                 int err2 = ipw2100_enable_adapter(priv);
5341                 if (err == 0)
5342                         err = err2;
5343         }
5344         return err;
5345 }
5346
5347 static int ipw2100_set_key_index(struct ipw2100_priv *priv,
5348                                  int idx, int batch_mode)
5349 {
5350         struct host_command cmd = {
5351                 .host_command = WEP_KEY_INDEX,
5352                 .host_command_sequence = 0,
5353                 .host_command_length = 4,
5354                 .host_command_parameters = { idx },
5355         };
5356         int err;
5357
5358         IPW_DEBUG_HC("WEP_KEY_INDEX: index = %d\n", idx);
5359
5360         if (idx < 0 || idx > 3)
5361                 return -EINVAL;
5362
5363         if (!batch_mode) {
5364                 err = ipw2100_disable_adapter(priv);
5365                 if (err) {
5366                         printk(KERN_ERR DRV_NAME ": %s: Could not disable adapter %d\n",
5367                                priv->net_dev->name, err);
5368                         return err;
5369                 }
5370         }
5371
5372         /* send cmd to firmware */
5373         err = ipw2100_hw_send_command(priv, &cmd);
5374
5375         if (!batch_mode)
5376                 ipw2100_enable_adapter(priv);
5377
5378         return err;
5379 }
5380
5381
5382 static int ipw2100_configure_security(struct ipw2100_priv *priv,
5383                                       int batch_mode)
5384 {
5385         int i, err, auth_mode, sec_level, use_group;
5386
5387         if (!(priv->status & STATUS_RUNNING))
5388                 return 0;
5389
5390         if (!batch_mode) {
5391                 err = ipw2100_disable_adapter(priv);
5392                 if (err)
5393                         return err;
5394         }
5395
5396         if (!priv->sec.enabled) {
5397                 err = ipw2100_set_security_information(
5398                         priv, IPW_AUTH_OPEN, SEC_LEVEL_0, 0, 1);
5399         } else {
5400                 auth_mode = IPW_AUTH_OPEN;
5401                 if ((priv->sec.flags & SEC_AUTH_MODE) &&
5402                     (priv->sec.auth_mode == WLAN_AUTH_SHARED_KEY))
5403                         auth_mode = IPW_AUTH_SHARED;
5404
5405                 sec_level = SEC_LEVEL_0;
5406                 if (priv->sec.flags & SEC_LEVEL)
5407                         sec_level = priv->sec.level;
5408
5409                 use_group = 0;
5410                 if (priv->sec.flags & SEC_UNICAST_GROUP)
5411                         use_group = priv->sec.unicast_uses_group;
5412
5413                 err = ipw2100_set_security_information(
5414                             priv, auth_mode, sec_level, use_group, 1);
5415         }
5416
5417         if (err)
5418                 goto exit;
5419
5420         if (priv->sec.enabled) {
5421                 for (i = 0; i < 4; i++) {
5422                         if (!(priv->sec.flags & (1 << i))) {
5423                                 memset(priv->sec.keys[i], 0, WEP_KEY_LEN);
5424                                 priv->sec.key_sizes[i] = 0;
5425                         } else {
5426                                 err = ipw2100_set_key(priv, i,
5427                                                       priv->sec.keys[i],
5428                                                       priv->sec.key_sizes[i],
5429                                                       1);
5430                                 if (err)
5431                                         goto exit;
5432                         }
5433                 }
5434
5435                 ipw2100_set_key_index(priv, priv->ieee->tx_keyidx, 1);
5436         }
5437
5438         /* Always enable privacy so the Host can filter WEP packets if
5439          * encrypted data is sent up */
5440         err = ipw2100_set_wep_flags(
5441                 priv, priv->sec.enabled ? IPW_PRIVACY_CAPABLE : 0, 1);
5442         if (err)
5443                 goto exit;
5444
5445         priv->status &= ~STATUS_SECURITY_UPDATED;
5446
5447  exit:
5448         if (!batch_mode)
5449                 ipw2100_enable_adapter(priv);
5450
5451         return err;
5452 }
5453
5454 static void ipw2100_security_work(struct ipw2100_priv *priv)
5455 {
5456         /* If we happen to have reconnected before we get a chance to
5457          * process this, then update the security settings--which causes
5458          * a disassociation to occur */
5459         if (!(priv->status & STATUS_ASSOCIATED) &&
5460             priv->status & STATUS_SECURITY_UPDATED)
5461                 ipw2100_configure_security(priv, 0);
5462 }
5463
5464 static void shim__set_security(struct net_device *dev,
5465                                struct ieee80211_security *sec)
5466 {
5467         struct ipw2100_priv *priv = ieee80211_priv(dev);
5468         int i, force_update = 0;
5469
5470         down(&priv->action_sem);
5471         if (!(priv->status & STATUS_INITIALIZED))
5472                 goto done;
5473
5474         for (i = 0; i < 4; i++) {
5475                 if (sec->flags & (1 << i)) {
5476                         priv->sec.key_sizes[i] = sec->key_sizes[i];
5477                         if (sec->key_sizes[i] == 0)
5478                                 priv->sec.flags &= ~(1 << i);
5479                         else
5480                                 memcpy(priv->sec.keys[i], sec->keys[i],
5481                                        sec->key_sizes[i]);
5482                         priv->sec.flags |= (1 << i);
5483                         priv->status |= STATUS_SECURITY_UPDATED;
5484                 }
5485         }
5486
5487         if ((sec->flags & SEC_ACTIVE_KEY) &&
5488             priv->sec.active_key != sec->active_key) {
5489                 if (sec->active_key <= 3) {
5490                         priv->sec.active_key = sec->active_key;
5491                         priv->sec.flags |= SEC_ACTIVE_KEY;
5492                 } else
5493                         priv->sec.flags &= ~SEC_ACTIVE_KEY;
5494
5495                 priv->status |= STATUS_SECURITY_UPDATED;
5496         }
5497
5498         if ((sec->flags & SEC_AUTH_MODE) &&
5499             (priv->sec.auth_mode != sec->auth_mode)) {
5500                 priv->sec.auth_mode = sec->auth_mode;
5501                 priv->sec.flags |= SEC_AUTH_MODE;
5502                 priv->status |= STATUS_SECURITY_UPDATED;
5503         }
5504
5505         if (sec->flags & SEC_ENABLED &&
5506             priv->sec.enabled != sec->enabled) {
5507                 priv->sec.flags |= SEC_ENABLED;
5508                 priv->sec.enabled = sec->enabled;
5509                 priv->status |= STATUS_SECURITY_UPDATED;
5510                 force_update = 1;
5511         }
5512
5513         if (sec->flags & SEC_LEVEL &&
5514             priv->sec.level != sec->level) {
5515                 priv->sec.level = sec->level;
5516                 priv->sec.flags |= SEC_LEVEL;
5517                 priv->status |= STATUS_SECURITY_UPDATED;
5518         }
5519
5520         IPW_DEBUG_WEP("Security flags: %c %c%c%c%c %c%c%c%c\n",
5521                           priv->sec.flags & (1<<8) ? '1' : '0',
5522                           priv->sec.flags & (1<<7) ? '1' : '0',
5523                           priv->sec.flags & (1<<6) ? '1' : '0',
5524                           priv->sec.flags & (1<<5) ? '1' : '0',
5525                           priv->sec.flags & (1<<4) ? '1' : '0',
5526                           priv->sec.flags & (1<<3) ? '1' : '0',
5527                           priv->sec.flags & (1<<2) ? '1' : '0',
5528                           priv->sec.flags & (1<<1) ? '1' : '0',
5529                           priv->sec.flags & (1<<0) ? '1' : '0');
5530
5531 /* As a temporary work around to enable WPA until we figure out why
5532  * wpa_supplicant toggles the security capability of the driver, which
5533  * forces a disassocation with force_update...
5534  *
5535  *      if (force_update || !(priv->status & STATUS_ASSOCIATED))*/
5536         if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
5537                 ipw2100_configure_security(priv, 0);
5538 done:
5539         up(&priv->action_sem);
5540 }
5541
5542 static int ipw2100_adapter_setup(struct ipw2100_priv *priv)
5543 {
5544         int err;
5545         int batch_mode = 1;
5546         u8 *bssid;
5547
5548         IPW_DEBUG_INFO("enter\n");
5549
5550         err = ipw2100_disable_adapter(priv);
5551         if (err)
5552                 return err;
5553 #ifdef CONFIG_IPW2100_MONITOR
5554         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
5555                 err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5556                 if (err)
5557                         return err;
5558
5559                 IPW_DEBUG_INFO("exit\n");
5560
5561                 return 0;
5562         }
5563 #endif /* CONFIG_IPW2100_MONITOR */
5564
5565         err = ipw2100_read_mac_address(priv);
5566         if (err)
5567                 return -EIO;
5568
5569         err = ipw2100_set_mac_address(priv, batch_mode);
5570         if (err)
5571                 return err;
5572
5573         err = ipw2100_set_port_type(priv, priv->ieee->iw_mode, batch_mode);
5574         if (err)
5575                 return err;
5576
5577         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5578                 err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5579                 if (err)
5580                         return err;
5581         }
5582
5583         err  = ipw2100_system_config(priv, batch_mode);
5584         if (err)
5585                 return err;
5586
5587         err = ipw2100_set_tx_rates(priv, priv->tx_rates, batch_mode);
5588         if (err)
5589                 return err;
5590
5591         /* Default to power mode OFF */
5592         err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
5593         if (err)
5594                 return err;
5595
5596         err = ipw2100_set_rts_threshold(priv, priv->rts_threshold);
5597         if (err)
5598                 return err;
5599
5600         if (priv->config & CFG_STATIC_BSSID)
5601                 bssid = priv->bssid;
5602         else
5603                 bssid = NULL;
5604         err = ipw2100_set_mandatory_bssid(priv, bssid, batch_mode);
5605         if (err)
5606                 return err;
5607
5608         if (priv->config & CFG_STATIC_ESSID)
5609                 err = ipw2100_set_essid(priv, priv->essid, priv->essid_len,
5610                                         batch_mode);
5611         else
5612                 err = ipw2100_set_essid(priv, NULL, 0, batch_mode);
5613         if (err)
5614                 return err;
5615
5616         err = ipw2100_configure_security(priv, batch_mode);
5617         if (err)
5618                 return err;
5619
5620         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5621                 err = ipw2100_set_ibss_beacon_interval(
5622                         priv, priv->beacon_interval, batch_mode);
5623                 if (err)
5624                         return err;
5625
5626                 err = ipw2100_set_tx_power(priv, priv->tx_power);
5627                 if (err)
5628                         return err;
5629         }
5630
5631         /*
5632           err = ipw2100_set_fragmentation_threshold(
5633           priv, priv->frag_threshold, batch_mode);
5634           if (err)
5635           return err;
5636         */
5637
5638         IPW_DEBUG_INFO("exit\n");
5639
5640         return 0;
5641 }
5642
5643
5644 /*************************************************************************
5645  *
5646  * EXTERNALLY CALLED METHODS
5647  *
5648  *************************************************************************/
5649
5650 /* This method is called by the network layer -- not to be confused with
5651  * ipw2100_set_mac_address() declared above called by this driver (and this
5652  * method as well) to talk to the firmware */
5653 static int ipw2100_set_address(struct net_device *dev, void *p)
5654 {
5655         struct ipw2100_priv *priv = ieee80211_priv(dev);
5656         struct sockaddr *addr = p;
5657         int err = 0;
5658
5659         if (!is_valid_ether_addr(addr->sa_data))
5660                 return -EADDRNOTAVAIL;
5661
5662         down(&priv->action_sem);
5663
5664         priv->config |= CFG_CUSTOM_MAC;
5665         memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
5666
5667         err = ipw2100_set_mac_address(priv, 0);
5668         if (err)
5669                 goto done;
5670
5671         priv->reset_backoff = 0;
5672         up(&priv->action_sem);
5673         ipw2100_reset_adapter(priv);
5674         return 0;
5675
5676  done:
5677         up(&priv->action_sem);
5678         return err;
5679 }
5680
5681 static int ipw2100_open(struct net_device *dev)
5682 {
5683         struct ipw2100_priv *priv = ieee80211_priv(dev);
5684         unsigned long flags;
5685         IPW_DEBUG_INFO("dev->open\n");
5686
5687         spin_lock_irqsave(&priv->low_lock, flags);
5688         if (priv->status & STATUS_ASSOCIATED) {
5689                 netif_carrier_on(dev);
5690                 netif_start_queue(dev);
5691         }
5692         spin_unlock_irqrestore(&priv->low_lock, flags);
5693
5694         return 0;
5695 }
5696
5697 static int ipw2100_close(struct net_device *dev)
5698 {
5699         struct ipw2100_priv *priv = ieee80211_priv(dev);
5700         unsigned long flags;
5701         struct list_head *element;
5702         struct ipw2100_tx_packet *packet;
5703
5704         IPW_DEBUG_INFO("enter\n");
5705
5706         spin_lock_irqsave(&priv->low_lock, flags);
5707
5708         if (priv->status & STATUS_ASSOCIATED)
5709                 netif_carrier_off(dev);
5710         netif_stop_queue(dev);
5711
5712         /* Flush the TX queue ... */
5713         while (!list_empty(&priv->tx_pend_list)) {
5714                 element = priv->tx_pend_list.next;
5715                 packet = list_entry(element, struct ipw2100_tx_packet, list);
5716
5717                 list_del(element);
5718                 DEC_STAT(&priv->tx_pend_stat);
5719
5720                 ieee80211_txb_free(packet->info.d_struct.txb);
5721                 packet->info.d_struct.txb = NULL;
5722
5723                 list_add_tail(element, &priv->tx_free_list);
5724                 INC_STAT(&priv->tx_free_stat);
5725         }
5726         spin_unlock_irqrestore(&priv->low_lock, flags);
5727
5728         IPW_DEBUG_INFO("exit\n");
5729
5730         return 0;
5731 }
5732
5733
5734
5735 /*
5736  * TODO:  Fix this function... its just wrong
5737  */
5738 static void ipw2100_tx_timeout(struct net_device *dev)
5739 {
5740         struct ipw2100_priv *priv = ieee80211_priv(dev);
5741
5742         priv->ieee->stats.tx_errors++;
5743
5744 #ifdef CONFIG_IPW2100_MONITOR
5745         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
5746                 return;
5747 #endif
5748
5749         IPW_DEBUG_INFO("%s: TX timed out.  Scheduling firmware restart.\n",
5750                        dev->name);
5751         schedule_reset(priv);
5752 }
5753
5754
5755 /*
5756  * TODO: reimplement it so that it reads statistics
5757  *       from the adapter using ordinal tables
5758  *       instead of/in addition to collecting them
5759  *       in the driver
5760  */
5761 static struct net_device_stats *ipw2100_stats(struct net_device *dev)
5762 {
5763         struct ipw2100_priv *priv = ieee80211_priv(dev);
5764
5765         return &priv->ieee->stats;
5766 }
5767
5768 /* Support for wpa_supplicant. Will be replaced with WEXT once
5769  * they get WPA support. */
5770 #ifdef CONFIG_IEEE80211_WPA
5771
5772 /* following definitions must match definitions in driver_ipw2100.c */
5773
5774 #define IPW2100_IOCTL_WPA_SUPPLICANT            SIOCIWFIRSTPRIV+30
5775
5776 #define IPW2100_CMD_SET_WPA_PARAM               1
5777 #define IPW2100_CMD_SET_WPA_IE                  2
5778 #define IPW2100_CMD_SET_ENCRYPTION              3
5779 #define IPW2100_CMD_MLME                        4
5780
5781 #define IPW2100_PARAM_WPA_ENABLED               1
5782 #define IPW2100_PARAM_TKIP_COUNTERMEASURES      2
5783 #define IPW2100_PARAM_DROP_UNENCRYPTED          3
5784 #define IPW2100_PARAM_PRIVACY_INVOKED           4
5785 #define IPW2100_PARAM_AUTH_ALGS                 5
5786 #define IPW2100_PARAM_IEEE_802_1X               6
5787
5788 #define IPW2100_MLME_STA_DEAUTH                 1
5789 #define IPW2100_MLME_STA_DISASSOC               2
5790
5791 #define IPW2100_CRYPT_ERR_UNKNOWN_ALG           2
5792 #define IPW2100_CRYPT_ERR_UNKNOWN_ADDR          3
5793 #define IPW2100_CRYPT_ERR_CRYPT_INIT_FAILED     4
5794 #define IPW2100_CRYPT_ERR_KEY_SET_FAILED        5
5795 #define IPW2100_CRYPT_ERR_TX_KEY_SET_FAILED     6
5796 #define IPW2100_CRYPT_ERR_CARD_CONF_FAILED      7
5797
5798 #define IPW2100_CRYPT_ALG_NAME_LEN              16
5799
5800 struct ipw2100_param {
5801         u32 cmd;
5802         u8 sta_addr[ETH_ALEN];
5803         union {
5804                 struct {
5805                         u8 name;
5806                         u32 value;
5807                 } wpa_param;
5808                 struct {
5809                         u32 len;
5810                         u8 *data;
5811                 } wpa_ie;
5812                 struct{
5813                         int command;
5814                         int reason_code;
5815                 } mlme;
5816                 struct {
5817                         u8 alg[IPW2100_CRYPT_ALG_NAME_LEN];
5818                         u8 set_tx;
5819                         u32 err;
5820                         u8 idx;
5821                         u8 seq[8]; /* sequence counter (set: RX, get: TX) */
5822                         u16 key_len;
5823                         u8 key[0];
5824                 } crypt;
5825
5826         } u;
5827 };
5828
5829 /* end of driver_ipw2100.c code */
5830
5831 static int ipw2100_wpa_enable(struct ipw2100_priv *priv, int value){
5832
5833         struct ieee80211_device *ieee = priv->ieee;
5834         struct ieee80211_security sec = {
5835                 .flags = SEC_LEVEL | SEC_ENABLED,
5836         };
5837         int ret = 0;
5838
5839         ieee->wpa_enabled = value;
5840
5841         if (value){
5842                 sec.level = SEC_LEVEL_3;
5843                 sec.enabled = 1;
5844         } else {
5845                 sec.level = SEC_LEVEL_0;
5846                 sec.enabled = 0;
5847         }
5848
5849         if (ieee->set_security)
5850                 ieee->set_security(ieee->dev, &sec);
5851         else
5852                 ret = -EOPNOTSUPP;
5853
5854         return ret;
5855 }
5856
5857 #define AUTH_ALG_OPEN_SYSTEM                    0x1
5858 #define AUTH_ALG_SHARED_KEY                     0x2
5859
5860 static int ipw2100_wpa_set_auth_algs(struct ipw2100_priv *priv, int value){
5861
5862         struct ieee80211_device *ieee = priv->ieee;
5863         struct ieee80211_security sec = {
5864                 .flags = SEC_AUTH_MODE,
5865         };
5866         int ret = 0;
5867
5868         if (value & AUTH_ALG_SHARED_KEY){
5869                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
5870                 ieee->open_wep = 0;
5871         } else {
5872                 sec.auth_mode = WLAN_AUTH_OPEN;
5873                 ieee->open_wep = 1;
5874         }
5875
5876         if (ieee->set_security)
5877                 ieee->set_security(ieee->dev, &sec);
5878         else
5879                 ret = -EOPNOTSUPP;
5880
5881         return ret;
5882 }
5883
5884
5885 static int ipw2100_wpa_set_param(struct net_device *dev, u8 name, u32 value){
5886
5887         struct ipw2100_priv *priv = ieee80211_priv(dev);
5888         int ret=0;
5889
5890         switch(name){
5891                 case IPW2100_PARAM_WPA_ENABLED:
5892                         ret = ipw2100_wpa_enable(priv, value);
5893                         break;
5894
5895                 case IPW2100_PARAM_TKIP_COUNTERMEASURES:
5896                         priv->ieee->tkip_countermeasures=value;
5897                         break;
5898
5899                 case IPW2100_PARAM_DROP_UNENCRYPTED:
5900                         priv->ieee->drop_unencrypted=value;
5901                         break;
5902
5903                 case IPW2100_PARAM_PRIVACY_INVOKED:
5904                         priv->ieee->privacy_invoked=value;
5905                         break;
5906
5907                 case IPW2100_PARAM_AUTH_ALGS:
5908                         ret = ipw2100_wpa_set_auth_algs(priv, value);
5909                         break;
5910
5911                 case IPW2100_PARAM_IEEE_802_1X:
5912                         priv->ieee->ieee802_1x=value;
5913                         break;
5914
5915                 default:
5916                         printk(KERN_ERR DRV_NAME ": %s: Unknown WPA param: %d\n",
5917                                             dev->name, name);
5918                         ret = -EOPNOTSUPP;
5919         }
5920
5921         return ret;
5922 }
5923
5924 static int ipw2100_wpa_mlme(struct net_device *dev, int command, int reason){
5925
5926         struct ipw2100_priv *priv = ieee80211_priv(dev);
5927         int ret=0;
5928
5929         switch(command){
5930                 case IPW2100_MLME_STA_DEAUTH:
5931                         // silently ignore
5932                         break;
5933
5934                 case IPW2100_MLME_STA_DISASSOC:
5935                         ipw2100_disassociate_bssid(priv);
5936                         break;
5937
5938                 default:
5939                         printk(KERN_ERR DRV_NAME ": %s: Unknown MLME request: %d\n",
5940                                             dev->name, command);
5941                         ret = -EOPNOTSUPP;
5942         }
5943
5944         return ret;
5945 }
5946
5947
5948 void ipw2100_wpa_assoc_frame(struct ipw2100_priv *priv,
5949                              char *wpa_ie, int wpa_ie_len){
5950
5951         struct ipw2100_wpa_assoc_frame frame;
5952
5953         frame.fixed_ie_mask = 0;
5954
5955         /* copy WPA IE */
5956         memcpy(frame.var_ie, wpa_ie, wpa_ie_len);
5957         frame.var_ie_len = wpa_ie_len;
5958
5959         /* make sure WPA is enabled */
5960         ipw2100_wpa_enable(priv, 1);
5961         ipw2100_set_wpa_ie(priv, &frame, 0);
5962 }
5963
5964
5965 static int ipw2100_wpa_set_wpa_ie(struct net_device *dev,
5966                                 struct ipw2100_param *param, int plen){
5967
5968         struct ipw2100_priv *priv = ieee80211_priv(dev);
5969         struct ieee80211_device *ieee = priv->ieee;
5970         u8 *buf;
5971
5972         if (! ieee->wpa_enabled)
5973             return -EOPNOTSUPP;
5974
5975         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
5976            (param->u.wpa_ie.len &&
5977                 param->u.wpa_ie.data==NULL))
5978                 return -EINVAL;
5979
5980         if (param->u.wpa_ie.len){
5981                 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
5982                 if (buf == NULL)
5983                         return -ENOMEM;
5984
5985                 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
5986
5987                 kfree(ieee->wpa_ie);
5988                 ieee->wpa_ie = buf;
5989                 ieee->wpa_ie_len = param->u.wpa_ie.len;
5990
5991         } else {
5992                 kfree(ieee->wpa_ie);
5993                 ieee->wpa_ie = NULL;
5994                 ieee->wpa_ie_len = 0;
5995         }
5996
5997         ipw2100_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
5998
5999         return 0;
6000 }
6001
6002 /* implementation borrowed from hostap driver */
6003
6004 static int ipw2100_wpa_set_encryption(struct net_device *dev,
6005                                 struct ipw2100_param *param, int param_len){
6006
6007         int ret = 0;
6008         struct ipw2100_priv *priv = ieee80211_priv(dev);
6009         struct ieee80211_device *ieee = priv->ieee;
6010         struct ieee80211_crypto_ops *ops;
6011         struct ieee80211_crypt_data **crypt;
6012
6013         struct ieee80211_security sec = {
6014                 .flags = 0,
6015         };
6016
6017         param->u.crypt.err = 0;
6018         param->u.crypt.alg[IPW2100_CRYPT_ALG_NAME_LEN - 1] = '\0';
6019
6020         if (param_len !=
6021             (int) ((char *) param->u.crypt.key - (char *) param) +
6022             param->u.crypt.key_len){
6023                 IPW_DEBUG_INFO("Len mismatch %d, %d\n", param_len, param->u.crypt.key_len);
6024                 return -EINVAL;
6025         }
6026         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
6027             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
6028             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
6029                 if (param->u.crypt.idx >= WEP_KEYS)
6030                         return -EINVAL;
6031                 crypt = &ieee->crypt[param->u.crypt.idx];
6032         } else {
6033                 return -EINVAL;
6034         }
6035
6036         if (strcmp(param->u.crypt.alg, "none") == 0) {
6037                 if (crypt){
6038                         sec.enabled = 0;
6039                         sec.level = SEC_LEVEL_0;
6040                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
6041                         ieee80211_crypt_delayed_deinit(ieee, crypt);
6042                 }
6043                 goto done;
6044         }
6045         sec.enabled = 1;
6046         sec.flags |= SEC_ENABLED;
6047
6048         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6049         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
6050                 request_module("ieee80211_crypt_wep");
6051                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6052         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
6053                 request_module("ieee80211_crypt_tkip");
6054                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6055         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
6056                 request_module("ieee80211_crypt_ccmp");
6057                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6058         }
6059         if (ops == NULL) {
6060                 IPW_DEBUG_INFO("%s: unknown crypto alg '%s'\n",
6061                        dev->name, param->u.crypt.alg);
6062                 param->u.crypt.err = IPW2100_CRYPT_ERR_UNKNOWN_ALG;
6063                 ret = -EINVAL;
6064                 goto done;
6065         }
6066
6067         if (*crypt == NULL || (*crypt)->ops != ops) {
6068                 struct ieee80211_crypt_data *new_crypt;
6069
6070                 ieee80211_crypt_delayed_deinit(ieee, crypt);
6071
6072                 new_crypt = (struct ieee80211_crypt_data *)
6073                         kmalloc(sizeof(struct ieee80211_crypt_data), GFP_KERNEL);
6074                 if (new_crypt == NULL) {
6075                         ret = -ENOMEM;
6076                         goto done;
6077                 }
6078                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
6079                 new_crypt->ops = ops;
6080                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
6081                         new_crypt->priv = new_crypt->ops->init(param->u.crypt.idx);
6082
6083                 if (new_crypt->priv == NULL) {
6084                         kfree(new_crypt);
6085                         param->u.crypt.err =
6086                                 IPW2100_CRYPT_ERR_CRYPT_INIT_FAILED;
6087                         ret = -EINVAL;
6088                         goto done;
6089                 }
6090
6091                 *crypt = new_crypt;
6092         }
6093
6094         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
6095             (*crypt)->ops->set_key(param->u.crypt.key,
6096                                    param->u.crypt.key_len, param->u.crypt.seq,
6097                                    (*crypt)->priv) < 0) {
6098                 IPW_DEBUG_INFO("%s: key setting failed\n",
6099                        dev->name);
6100                 param->u.crypt.err = IPW2100_CRYPT_ERR_KEY_SET_FAILED;
6101                 ret = -EINVAL;
6102                 goto done;
6103         }
6104
6105         if (param->u.crypt.set_tx){
6106                 ieee->tx_keyidx = param->u.crypt.idx;
6107                 sec.active_key = param->u.crypt.idx;
6108                 sec.flags |= SEC_ACTIVE_KEY;
6109         }
6110
6111         if (ops->name != NULL){
6112
6113                 if (strcmp(ops->name, "WEP") == 0) {
6114                         memcpy(sec.keys[param->u.crypt.idx], param->u.crypt.key, param->u.crypt.key_len);
6115                         sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
6116                         sec.flags |= (1 << param->u.crypt.idx);
6117                         sec.flags |= SEC_LEVEL;
6118                         sec.level = SEC_LEVEL_1;
6119                 } else if (strcmp(ops->name, "TKIP") == 0) {
6120                         sec.flags |= SEC_LEVEL;
6121                         sec.level = SEC_LEVEL_2;
6122                 } else if (strcmp(ops->name, "CCMP") == 0) {
6123                         sec.flags |= SEC_LEVEL;
6124                         sec.level = SEC_LEVEL_3;
6125                 }
6126         }
6127  done:
6128         if (ieee->set_security)
6129                 ieee->set_security(ieee->dev, &sec);
6130
6131         /* Do not reset port if card is in Managed mode since resetting will
6132          * generate new IEEE 802.11 authentication which may end up in looping
6133          * with IEEE 802.1X.  If your hardware requires a reset after WEP
6134          * configuration (for example... Prism2), implement the reset_port in
6135          * the callbacks structures used to initialize the 802.11 stack. */
6136         if (ieee->reset_on_keychange &&
6137             ieee->iw_mode != IW_MODE_INFRA &&
6138             ieee->reset_port &&
6139             ieee->reset_port(dev)) {
6140                 IPW_DEBUG_INFO("%s: reset_port failed\n", dev->name);
6141                 param->u.crypt.err = IPW2100_CRYPT_ERR_CARD_CONF_FAILED;
6142                 return -EINVAL;
6143         }
6144
6145         return ret;
6146 }
6147
6148
6149 static int ipw2100_wpa_supplicant(struct net_device *dev, struct iw_point *p){
6150
6151         struct ipw2100_param *param;
6152         int ret=0;
6153
6154         IPW_DEBUG_IOCTL("wpa_supplicant: len=%d\n", p->length);
6155
6156         if (p->length < sizeof(struct ipw2100_param) || !p->pointer)
6157                 return -EINVAL;
6158
6159         param = (struct ipw2100_param *)kmalloc(p->length, GFP_KERNEL);
6160         if (param == NULL)
6161                 return -ENOMEM;
6162
6163         if (copy_from_user(param, p->pointer, p->length)){
6164                 kfree(param);
6165                 return -EFAULT;
6166         }
6167
6168         switch (param->cmd){
6169
6170         case IPW2100_CMD_SET_WPA_PARAM:
6171                 ret = ipw2100_wpa_set_param(dev, param->u.wpa_param.name,
6172                                             param->u.wpa_param.value);
6173                 break;
6174
6175         case IPW2100_CMD_SET_WPA_IE:
6176                 ret = ipw2100_wpa_set_wpa_ie(dev, param, p->length);
6177                 break;
6178
6179         case IPW2100_CMD_SET_ENCRYPTION:
6180                 ret = ipw2100_wpa_set_encryption(dev, param, p->length);
6181                 break;
6182
6183         case IPW2100_CMD_MLME:
6184                 ret = ipw2100_wpa_mlme(dev, param->u.mlme.command,
6185                                        param->u.mlme.reason_code);
6186                 break;
6187
6188         default:
6189                 printk(KERN_ERR DRV_NAME ": %s: Unknown WPA supplicant request: %d\n",
6190                                 dev->name, param->cmd);
6191                 ret = -EOPNOTSUPP;
6192
6193         }
6194
6195         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
6196                 ret = -EFAULT;
6197
6198         kfree(param);
6199         return ret;
6200 }
6201 #endif /* CONFIG_IEEE80211_WPA */
6202
6203 static int ipw2100_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
6204 {
6205 #ifdef CONFIG_IEEE80211_WPA
6206         struct iwreq *wrq = (struct iwreq *) rq;
6207         int ret=-1;
6208         switch (cmd){
6209             case IPW2100_IOCTL_WPA_SUPPLICANT:
6210                 ret = ipw2100_wpa_supplicant(dev, &wrq->u.data);
6211                 return ret;
6212
6213             default:
6214                 return -EOPNOTSUPP;
6215         }
6216
6217 #endif /* CONFIG_IEEE80211_WPA */
6218
6219         return -EOPNOTSUPP;
6220 }
6221
6222
6223 static void ipw_ethtool_get_drvinfo(struct net_device *dev,
6224                                     struct ethtool_drvinfo *info)
6225 {
6226         struct ipw2100_priv *priv = ieee80211_priv(dev);
6227         char fw_ver[64], ucode_ver[64];
6228
6229         strcpy(info->driver, DRV_NAME);
6230         strcpy(info->version, DRV_VERSION);
6231
6232         ipw2100_get_fwversion(priv, fw_ver, sizeof(fw_ver));
6233         ipw2100_get_ucodeversion(priv, ucode_ver, sizeof(ucode_ver));
6234
6235         snprintf(info->fw_version, sizeof(info->fw_version), "%s:%d:%s",
6236                  fw_ver, priv->eeprom_version, ucode_ver);
6237
6238         strcpy(info->bus_info, pci_name(priv->pci_dev));
6239 }
6240
6241 static u32 ipw2100_ethtool_get_link(struct net_device *dev)
6242 {
6243     struct ipw2100_priv *priv = ieee80211_priv(dev);
6244     return (priv->status & STATUS_ASSOCIATED) ? 1 : 0;
6245 }
6246
6247
6248 static struct ethtool_ops ipw2100_ethtool_ops = {
6249     .get_link        = ipw2100_ethtool_get_link,
6250     .get_drvinfo     = ipw_ethtool_get_drvinfo,
6251 };
6252
6253 static void ipw2100_hang_check(void *adapter)
6254 {
6255         struct ipw2100_priv *priv = adapter;
6256         unsigned long flags;
6257         u32 rtc = 0xa5a5a5a5;
6258         u32 len = sizeof(rtc);
6259         int restart = 0;
6260
6261         spin_lock_irqsave(&priv->low_lock, flags);
6262
6263         if (priv->fatal_error != 0) {
6264                 /* If fatal_error is set then we need to restart */
6265                 IPW_DEBUG_INFO("%s: Hardware fatal error detected.\n",
6266                                priv->net_dev->name);
6267
6268                 restart = 1;
6269         } else if (ipw2100_get_ordinal(priv, IPW_ORD_RTC_TIME, &rtc, &len) ||
6270                    (rtc == priv->last_rtc)) {
6271                 /* Check if firmware is hung */
6272                 IPW_DEBUG_INFO("%s: Firmware RTC stalled.\n",
6273                                priv->net_dev->name);
6274
6275                 restart = 1;
6276         }
6277
6278         if (restart) {
6279                 /* Kill timer */
6280                 priv->stop_hang_check = 1;
6281                 priv->hangs++;
6282
6283                 /* Restart the NIC */
6284                 schedule_reset(priv);
6285         }
6286
6287         priv->last_rtc = rtc;
6288
6289         if (!priv->stop_hang_check)
6290                 queue_delayed_work(priv->workqueue, &priv->hang_check, HZ / 2);
6291
6292         spin_unlock_irqrestore(&priv->low_lock, flags);
6293 }
6294
6295
6296 static void ipw2100_rf_kill(void *adapter)
6297 {
6298         struct ipw2100_priv *priv = adapter;
6299         unsigned long flags;
6300
6301         spin_lock_irqsave(&priv->low_lock, flags);
6302
6303         if (rf_kill_active(priv)) {
6304                 IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
6305                 if (!priv->stop_rf_kill)
6306                         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
6307                 goto exit_unlock;
6308         }
6309
6310         /* RF Kill is now disabled, so bring the device back up */
6311
6312         if (!(priv->status & STATUS_RF_KILL_MASK)) {
6313                 IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
6314                                   "device\n");
6315                 schedule_reset(priv);
6316         } else
6317                 IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
6318                                   "enabled\n");
6319
6320  exit_unlock:
6321         spin_unlock_irqrestore(&priv->low_lock, flags);
6322 }
6323
6324 static void ipw2100_irq_tasklet(struct ipw2100_priv *priv);
6325
6326 /* Look into using netdev destructor to shutdown ieee80211? */
6327
6328 static struct net_device *ipw2100_alloc_device(
6329         struct pci_dev *pci_dev,
6330         char *base_addr,
6331         unsigned long mem_start,
6332         unsigned long mem_len)
6333 {
6334         struct ipw2100_priv *priv;
6335         struct net_device *dev;
6336
6337         dev = alloc_ieee80211(sizeof(struct ipw2100_priv));
6338         if (!dev)
6339                 return NULL;
6340         priv = ieee80211_priv(dev);
6341         priv->ieee = netdev_priv(dev);
6342         priv->pci_dev = pci_dev;
6343         priv->net_dev = dev;
6344
6345         priv->ieee->hard_start_xmit = ipw2100_tx;
6346         priv->ieee->set_security = shim__set_security;
6347
6348         dev->open = ipw2100_open;
6349         dev->stop = ipw2100_close;
6350         dev->init = ipw2100_net_init;
6351         dev->do_ioctl = ipw2100_ioctl;
6352         dev->get_stats = ipw2100_stats;
6353         dev->ethtool_ops = &ipw2100_ethtool_ops;
6354         dev->tx_timeout = ipw2100_tx_timeout;
6355         dev->wireless_handlers = &ipw2100_wx_handler_def;
6356         dev->get_wireless_stats = ipw2100_wx_wireless_stats;
6357         dev->set_mac_address = ipw2100_set_address;
6358         dev->watchdog_timeo = 3*HZ;
6359         dev->irq = 0;
6360
6361         dev->base_addr = (unsigned long)base_addr;
6362         dev->mem_start = mem_start;
6363         dev->mem_end = dev->mem_start + mem_len - 1;
6364
6365         /* NOTE: We don't use the wireless_handlers hook
6366          * in dev as the system will start throwing WX requests
6367          * to us before we're actually initialized and it just
6368          * ends up causing problems.  So, we just handle
6369          * the WX extensions through the ipw2100_ioctl interface */
6370
6371
6372         /* memset() puts everything to 0, so we only have explicitely set
6373          * those values that need to be something else */
6374
6375         /* If power management is turned on, default to AUTO mode */
6376         priv->power_mode = IPW_POWER_AUTO;
6377
6378
6379
6380 #ifdef CONFIG_IEEE80211_WPA
6381         priv->ieee->wpa_enabled = 0;
6382         priv->ieee->tkip_countermeasures = 0;
6383         priv->ieee->drop_unencrypted = 0;
6384         priv->ieee->privacy_invoked = 0;
6385         priv->ieee->ieee802_1x = 1;
6386 #endif /* CONFIG_IEEE80211_WPA */
6387
6388         /* Set module parameters */
6389         switch (mode) {
6390         case 1:
6391                 priv->ieee->iw_mode = IW_MODE_ADHOC;
6392                 break;
6393 #ifdef CONFIG_IPW2100_MONITOR
6394         case 2:
6395                 priv->ieee->iw_mode = IW_MODE_MONITOR;
6396                 break;
6397 #endif
6398         default:
6399         case 0:
6400                 priv->ieee->iw_mode = IW_MODE_INFRA;
6401                 break;
6402         }
6403
6404         if (disable == 1)
6405                 priv->status |= STATUS_RF_KILL_SW;
6406
6407         if (channel != 0 &&
6408             ((channel >= REG_MIN_CHANNEL) &&
6409              (channel <= REG_MAX_CHANNEL))) {
6410                 priv->config |= CFG_STATIC_CHANNEL;
6411                 priv->channel = channel;
6412         }
6413
6414         if (associate)
6415                 priv->config |= CFG_ASSOCIATE;
6416
6417         priv->beacon_interval = DEFAULT_BEACON_INTERVAL;
6418         priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
6419         priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
6420         priv->rts_threshold = DEFAULT_RTS_THRESHOLD | RTS_DISABLED;
6421         priv->frag_threshold = DEFAULT_FTS | FRAG_DISABLED;
6422         priv->tx_power = IPW_TX_POWER_DEFAULT;
6423         priv->tx_rates = DEFAULT_TX_RATES;
6424
6425         strcpy(priv->nick, "ipw2100");
6426
6427         spin_lock_init(&priv->low_lock);
6428         sema_init(&priv->action_sem, 1);
6429         sema_init(&priv->adapter_sem, 1);
6430
6431         init_waitqueue_head(&priv->wait_command_queue);
6432
6433         netif_carrier_off(dev);
6434
6435         INIT_LIST_HEAD(&priv->msg_free_list);
6436         INIT_LIST_HEAD(&priv->msg_pend_list);
6437         INIT_STAT(&priv->msg_free_stat);
6438         INIT_STAT(&priv->msg_pend_stat);
6439
6440         INIT_LIST_HEAD(&priv->tx_free_list);
6441         INIT_LIST_HEAD(&priv->tx_pend_list);
6442         INIT_STAT(&priv->tx_free_stat);
6443         INIT_STAT(&priv->tx_pend_stat);
6444
6445         INIT_LIST_HEAD(&priv->fw_pend_list);
6446         INIT_STAT(&priv->fw_pend_stat);
6447
6448
6449 #ifdef CONFIG_SOFTWARE_SUSPEND2
6450         priv->workqueue = create_workqueue(DRV_NAME, 0);
6451 #else
6452         priv->workqueue = create_workqueue(DRV_NAME);
6453 #endif
6454         INIT_WORK(&priv->reset_work,
6455                   (void (*)(void *))ipw2100_reset_adapter, priv);
6456         INIT_WORK(&priv->security_work,
6457                   (void (*)(void *))ipw2100_security_work, priv);
6458         INIT_WORK(&priv->wx_event_work,
6459                   (void (*)(void *))ipw2100_wx_event_work, priv);
6460         INIT_WORK(&priv->hang_check, ipw2100_hang_check, priv);
6461         INIT_WORK(&priv->rf_kill, ipw2100_rf_kill, priv);
6462
6463         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
6464                      ipw2100_irq_tasklet, (unsigned long)priv);
6465
6466         /* NOTE:  We do not start the deferred work for status checks yet */
6467         priv->stop_rf_kill = 1;
6468         priv->stop_hang_check = 1;
6469
6470         return dev;
6471 }
6472
6473 static int ipw2100_pci_init_one(struct pci_dev *pci_dev,
6474                                 const struct pci_device_id *ent)
6475 {
6476         unsigned long mem_start, mem_len, mem_flags;
6477         char *base_addr = NULL;
6478         struct net_device *dev = NULL;
6479         struct ipw2100_priv *priv = NULL;
6480         int err = 0;
6481         int registered = 0;
6482         u32 val;
6483
6484         IPW_DEBUG_INFO("enter\n");
6485
6486         mem_start = pci_resource_start(pci_dev, 0);
6487         mem_len = pci_resource_len(pci_dev, 0);
6488         mem_flags = pci_resource_flags(pci_dev, 0);
6489
6490         if ((mem_flags & IORESOURCE_MEM) != IORESOURCE_MEM) {
6491                 IPW_DEBUG_INFO("weird - resource type is not memory\n");
6492                 err = -ENODEV;
6493                 goto fail;
6494         }
6495
6496         base_addr = ioremap_nocache(mem_start, mem_len);
6497         if (!base_addr) {
6498                 printk(KERN_WARNING DRV_NAME
6499                        "Error calling ioremap_nocache.\n");
6500                 err = -EIO;
6501                 goto fail;
6502         }
6503
6504         /* allocate and initialize our net_device */
6505         dev = ipw2100_alloc_device(pci_dev, base_addr, mem_start, mem_len);
6506         if (!dev) {
6507                 printk(KERN_WARNING DRV_NAME
6508                        "Error calling ipw2100_alloc_device.\n");
6509                 err = -ENOMEM;
6510                 goto fail;
6511         }
6512
6513         /* set up PCI mappings for device */
6514         err = pci_enable_device(pci_dev);
6515         if (err) {
6516                 printk(KERN_WARNING DRV_NAME
6517                        "Error calling pci_enable_device.\n");
6518                 return err;
6519         }
6520
6521         priv = ieee80211_priv(dev);
6522
6523         pci_set_master(pci_dev);
6524         pci_set_drvdata(pci_dev, priv);
6525
6526         err = pci_set_dma_mask(pci_dev, DMA_32BIT_MASK);
6527         if (err) {
6528                 printk(KERN_WARNING DRV_NAME
6529                        "Error calling pci_set_dma_mask.\n");
6530                 pci_disable_device(pci_dev);
6531                 return err;
6532         }
6533
6534         err = pci_request_regions(pci_dev, DRV_NAME);
6535         if (err) {
6536                 printk(KERN_WARNING DRV_NAME
6537                        "Error calling pci_request_regions.\n");
6538                 pci_disable_device(pci_dev);
6539                 return err;
6540         }
6541
6542         /* We disable the RETRY_TIMEOUT register (0x41) to keep
6543          * PCI Tx retries from interfering with C3 CPU state */
6544         pci_read_config_dword(pci_dev, 0x40, &val);
6545         if ((val & 0x0000ff00) != 0)
6546                 pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6547
6548         pci_set_power_state(pci_dev, PCI_D0);
6549
6550         if (!ipw2100_hw_is_adapter_in_system(dev)) {
6551                 printk(KERN_WARNING DRV_NAME
6552                        "Device not found via register read.\n");
6553                 err = -ENODEV;
6554                 goto fail;
6555         }
6556
6557         SET_NETDEV_DEV(dev, &pci_dev->dev);
6558
6559         /* Force interrupts to be shut off on the device */
6560         priv->status |= STATUS_INT_ENABLED;
6561         ipw2100_disable_interrupts(priv);
6562
6563         /* Allocate and initialize the Tx/Rx queues and lists */
6564         if (ipw2100_queues_allocate(priv)) {
6565                 printk(KERN_WARNING DRV_NAME
6566                        "Error calilng ipw2100_queues_allocate.\n");
6567                 err = -ENOMEM;
6568                 goto fail;
6569         }
6570         ipw2100_queues_initialize(priv);
6571
6572         err = request_irq(pci_dev->irq,
6573                           ipw2100_interrupt, SA_SHIRQ,
6574                           dev->name, priv);
6575         if (err) {
6576                 printk(KERN_WARNING DRV_NAME
6577                        "Error calling request_irq: %d.\n",
6578                        pci_dev->irq);
6579                 goto fail;
6580         }
6581         dev->irq = pci_dev->irq;
6582
6583         IPW_DEBUG_INFO("Attempting to register device...\n");
6584
6585         SET_MODULE_OWNER(dev);
6586
6587         printk(KERN_INFO DRV_NAME
6588                ": Detected Intel PRO/Wireless 2100 Network Connection\n");
6589
6590         /* Bring up the interface.  Pre 0.46, after we registered the
6591          * network device we would call ipw2100_up.  This introduced a race
6592          * condition with newer hotplug configurations (network was coming
6593          * up and making calls before the device was initialized).
6594          *
6595          * If we called ipw2100_up before we registered the device, then the
6596          * device name wasn't registered.  So, we instead use the net_dev->init
6597          * member to call a function that then just turns and calls ipw2100_up.
6598          * net_dev->init is called after name allocation but before the
6599          * notifier chain is called */
6600         down(&priv->action_sem);
6601         err = register_netdev(dev);
6602         if (err) {
6603                 printk(KERN_WARNING DRV_NAME
6604                        "Error calling register_netdev.\n");
6605                 goto fail_unlock;
6606         }
6607         registered = 1;
6608
6609         IPW_DEBUG_INFO("%s: Bound to %s\n", dev->name, pci_name(pci_dev));
6610
6611         /* perform this after register_netdev so that dev->name is set */
6612         sysfs_create_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6613         netif_carrier_off(dev);
6614
6615         /* If the RF Kill switch is disabled, go ahead and complete the
6616          * startup sequence */
6617         if (!(priv->status & STATUS_RF_KILL_MASK)) {
6618                 /* Enable the adapter - sends HOST_COMPLETE */
6619                 if (ipw2100_enable_adapter(priv)) {
6620                         printk(KERN_WARNING DRV_NAME
6621                                ": %s: failed in call to enable adapter.\n",
6622                                priv->net_dev->name);
6623                         ipw2100_hw_stop_adapter(priv);
6624                         err = -EIO;
6625                         goto fail_unlock;
6626                 }
6627
6628                 /* Start a scan . . . */
6629                 ipw2100_set_scan_options(priv);
6630                 ipw2100_start_scan(priv);
6631         }
6632
6633         IPW_DEBUG_INFO("exit\n");
6634
6635         priv->status |= STATUS_INITIALIZED;
6636
6637         up(&priv->action_sem);
6638
6639         return 0;
6640
6641  fail_unlock:
6642         up(&priv->action_sem);
6643
6644  fail:
6645         if (dev) {
6646                 if (registered)
6647                         unregister_netdev(dev);
6648
6649                 ipw2100_hw_stop_adapter(priv);
6650
6651                 ipw2100_disable_interrupts(priv);
6652
6653                 if (dev->irq)
6654                         free_irq(dev->irq, priv);
6655
6656                 ipw2100_kill_workqueue(priv);
6657
6658                 /* These are safe to call even if they weren't allocated */
6659                 ipw2100_queues_free(priv);
6660                 sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6661
6662                 free_ieee80211(dev);
6663                 pci_set_drvdata(pci_dev, NULL);
6664         }
6665
6666         if (base_addr)
6667                 iounmap((char*)base_addr);
6668
6669         pci_release_regions(pci_dev);
6670         pci_disable_device(pci_dev);
6671
6672         return err;
6673 }
6674
6675 static void __devexit ipw2100_pci_remove_one(struct pci_dev *pci_dev)
6676 {
6677         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6678         struct net_device *dev;
6679
6680         if (priv) {
6681                 down(&priv->action_sem);
6682
6683                 priv->status &= ~STATUS_INITIALIZED;
6684
6685                 dev = priv->net_dev;
6686                 sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6687
6688 #ifdef CONFIG_PM
6689                 if (ipw2100_firmware.version)
6690                         ipw2100_release_firmware(priv, &ipw2100_firmware);
6691 #endif
6692                 /* Take down the hardware */
6693                 ipw2100_down(priv);
6694
6695                 /* Release the semaphore so that the network subsystem can
6696                  * complete any needed calls into the driver... */
6697                 up(&priv->action_sem);
6698
6699                 /* Unregister the device first - this results in close()
6700                  * being called if the device is open.  If we free storage
6701                  * first, then close() will crash. */
6702                 unregister_netdev(dev);
6703
6704                 /* ipw2100_down will ensure that there is no more pending work
6705                  * in the workqueue's, so we can safely remove them now. */
6706                 ipw2100_kill_workqueue(priv);
6707
6708                 ipw2100_queues_free(priv);
6709
6710                 /* Free potential debugging firmware snapshot */
6711                 ipw2100_snapshot_free(priv);
6712
6713                 if (dev->irq)
6714                         free_irq(dev->irq, priv);
6715
6716                 if (dev->base_addr)
6717                         iounmap((unsigned char *)dev->base_addr);
6718
6719                 free_ieee80211(dev);
6720         }
6721
6722         pci_release_regions(pci_dev);
6723         pci_disable_device(pci_dev);
6724
6725         IPW_DEBUG_INFO("exit\n");
6726 }
6727
6728
6729 #ifdef CONFIG_PM
6730 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
6731 static int ipw2100_suspend(struct pci_dev *pci_dev, u32 state)
6732 #else
6733 static int ipw2100_suspend(struct pci_dev *pci_dev, pm_message_t state)
6734 #endif
6735 {
6736         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6737         struct net_device *dev = priv->net_dev;
6738
6739         IPW_DEBUG_INFO("%s: Going into suspend...\n",
6740                dev->name);
6741
6742         down(&priv->action_sem);
6743         if (priv->status & STATUS_INITIALIZED) {
6744                 /* Take down the device; powers it off, etc. */
6745                 ipw2100_down(priv);
6746         }
6747
6748         /* Remove the PRESENT state of the device */
6749         netif_device_detach(dev);
6750
6751         pci_save_state(pci_dev);
6752         pci_disable_device (pci_dev);
6753         pci_set_power_state(pci_dev, PCI_D3hot);
6754
6755         up(&priv->action_sem);
6756
6757         return 0;
6758 }
6759
6760 static int ipw2100_resume(struct pci_dev *pci_dev)
6761 {
6762         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6763         struct net_device *dev = priv->net_dev;
6764         u32 val;
6765
6766         if (IPW2100_PM_DISABLED)
6767                 return 0;
6768
6769         down(&priv->action_sem);
6770
6771         IPW_DEBUG_INFO("%s: Coming out of suspend...\n",
6772                dev->name);
6773
6774         pci_set_power_state(pci_dev, PCI_D0);
6775         pci_enable_device(pci_dev);
6776         pci_restore_state(pci_dev);
6777
6778         /*
6779          * Suspend/Resume resets the PCI configuration space, so we have to
6780          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
6781          * from interfering with C3 CPU state. pci_restore_state won't help
6782          * here since it only restores the first 64 bytes pci config header.
6783          */
6784         pci_read_config_dword(pci_dev, 0x40, &val);
6785         if ((val & 0x0000ff00) != 0)
6786                 pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6787
6788         /* Set the device back into the PRESENT state; this will also wake
6789          * the queue of needed */
6790         netif_device_attach(dev);
6791
6792         /* Bring the device back up */
6793         if (!(priv->status & STATUS_RF_KILL_SW))
6794                 ipw2100_up(priv, 0);
6795
6796         up(&priv->action_sem);
6797
6798         return 0;
6799 }
6800 #endif
6801
6802
6803 #define IPW2100_DEV_ID(x) { PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, x }
6804
6805 static struct pci_device_id ipw2100_pci_id_table[] __devinitdata = {
6806         IPW2100_DEV_ID(0x2520), /* IN 2100A mPCI 3A */
6807         IPW2100_DEV_ID(0x2521), /* IN 2100A mPCI 3B */
6808         IPW2100_DEV_ID(0x2524), /* IN 2100A mPCI 3B */
6809         IPW2100_DEV_ID(0x2525), /* IN 2100A mPCI 3B */
6810         IPW2100_DEV_ID(0x2526), /* IN 2100A mPCI Gen A3 */
6811         IPW2100_DEV_ID(0x2522), /* IN 2100 mPCI 3B */
6812         IPW2100_DEV_ID(0x2523), /* IN 2100 mPCI 3A */
6813         IPW2100_DEV_ID(0x2527), /* IN 2100 mPCI 3B */
6814         IPW2100_DEV_ID(0x2528), /* IN 2100 mPCI 3B */
6815         IPW2100_DEV_ID(0x2529), /* IN 2100 mPCI 3B */
6816         IPW2100_DEV_ID(0x252B), /* IN 2100 mPCI 3A */
6817         IPW2100_DEV_ID(0x252C), /* IN 2100 mPCI 3A */
6818         IPW2100_DEV_ID(0x252D), /* IN 2100 mPCI 3A */
6819
6820         IPW2100_DEV_ID(0x2550), /* IB 2100A mPCI 3B */
6821         IPW2100_DEV_ID(0x2551), /* IB 2100 mPCI 3B */
6822         IPW2100_DEV_ID(0x2553), /* IB 2100 mPCI 3B */
6823         IPW2100_DEV_ID(0x2554), /* IB 2100 mPCI 3B */
6824         IPW2100_DEV_ID(0x2555), /* IB 2100 mPCI 3B */
6825
6826         IPW2100_DEV_ID(0x2560), /* DE 2100A mPCI 3A */
6827         IPW2100_DEV_ID(0x2562), /* DE 2100A mPCI 3A */
6828         IPW2100_DEV_ID(0x2563), /* DE 2100A mPCI 3A */
6829         IPW2100_DEV_ID(0x2561), /* DE 2100 mPCI 3A */
6830         IPW2100_DEV_ID(0x2565), /* DE 2100 mPCI 3A */
6831         IPW2100_DEV_ID(0x2566), /* DE 2100 mPCI 3A */
6832         IPW2100_DEV_ID(0x2567), /* DE 2100 mPCI 3A */
6833
6834         IPW2100_DEV_ID(0x2570), /* GA 2100 mPCI 3B */
6835
6836         IPW2100_DEV_ID(0x2580), /* TO 2100A mPCI 3B */
6837         IPW2100_DEV_ID(0x2582), /* TO 2100A mPCI 3B */
6838         IPW2100_DEV_ID(0x2583), /* TO 2100A mPCI 3B */
6839         IPW2100_DEV_ID(0x2581), /* TO 2100 mPCI 3B */
6840         IPW2100_DEV_ID(0x2585), /* TO 2100 mPCI 3B */
6841         IPW2100_DEV_ID(0x2586), /* TO 2100 mPCI 3B */
6842         IPW2100_DEV_ID(0x2587), /* TO 2100 mPCI 3B */
6843
6844         IPW2100_DEV_ID(0x2590), /* SO 2100A mPCI 3B */
6845         IPW2100_DEV_ID(0x2592), /* SO 2100A mPCI 3B */
6846         IPW2100_DEV_ID(0x2591), /* SO 2100 mPCI 3B */
6847         IPW2100_DEV_ID(0x2593), /* SO 2100 mPCI 3B */
6848         IPW2100_DEV_ID(0x2596), /* SO 2100 mPCI 3B */
6849         IPW2100_DEV_ID(0x2598), /* SO 2100 mPCI 3B */
6850
6851         IPW2100_DEV_ID(0x25A0), /* HP 2100 mPCI 3B */
6852         {0,},
6853 };
6854
6855 MODULE_DEVICE_TABLE(pci, ipw2100_pci_id_table);
6856
6857 static struct pci_driver ipw2100_pci_driver = {
6858         .name = DRV_NAME,
6859         .id_table = ipw2100_pci_id_table,
6860         .probe = ipw2100_pci_init_one,
6861         .remove = __devexit_p(ipw2100_pci_remove_one),
6862 #ifdef CONFIG_PM
6863         .suspend = ipw2100_suspend,
6864         .resume = ipw2100_resume,
6865 #endif
6866 };
6867
6868
6869 /**
6870  * Initialize the ipw2100 driver/module
6871  *
6872  * @returns 0 if ok, < 0 errno node con error.
6873  *
6874  * Note: we cannot init the /proc stuff until the PCI driver is there,
6875  * or we risk an unlikely race condition on someone accessing
6876  * uninitialized data in the PCI dev struct through /proc.
6877  */
6878 static int __init ipw2100_init(void)
6879 {
6880         int ret;
6881
6882         printk(KERN_INFO DRV_NAME ": %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
6883         printk(KERN_INFO DRV_NAME ": %s\n", DRV_COPYRIGHT);
6884
6885 #ifdef CONFIG_IEEE80211_NOWEP
6886         IPW_DEBUG_INFO(DRV_NAME ": Compiled with WEP disabled.\n");
6887 #endif
6888
6889         ret = pci_module_init(&ipw2100_pci_driver);
6890
6891 #ifdef CONFIG_IPW_DEBUG
6892         ipw2100_debug_level = debug;
6893         driver_create_file(&ipw2100_pci_driver.driver,
6894                            &driver_attr_debug_level);
6895 #endif
6896
6897         return ret;
6898 }
6899
6900
6901 /**
6902  * Cleanup ipw2100 driver registration
6903  */
6904 static void __exit ipw2100_exit(void)
6905 {
6906         /* FIXME: IPG: check that we have no instances of the devices open */
6907 #ifdef CONFIG_IPW_DEBUG
6908         driver_remove_file(&ipw2100_pci_driver.driver,
6909                            &driver_attr_debug_level);
6910 #endif
6911         pci_unregister_driver(&ipw2100_pci_driver);
6912 }
6913
6914 module_init(ipw2100_init);
6915 module_exit(ipw2100_exit);
6916
6917 #define WEXT_USECHANNELS 1
6918
6919 static const long ipw2100_frequencies[] = {
6920         2412, 2417, 2422, 2427,
6921         2432, 2437, 2442, 2447,
6922         2452, 2457, 2462, 2467,
6923         2472, 2484
6924 };
6925
6926 #define FREQ_COUNT (sizeof(ipw2100_frequencies) / \
6927                     sizeof(ipw2100_frequencies[0]))
6928
6929 static const long ipw2100_rates_11b[] = {
6930         1000000,
6931         2000000,
6932         5500000,
6933         11000000
6934 };
6935
6936 #define RATE_COUNT (sizeof(ipw2100_rates_11b) / sizeof(ipw2100_rates_11b[0]))
6937
6938 static int ipw2100_wx_get_name(struct net_device *dev,
6939                                struct iw_request_info *info,
6940                                union iwreq_data *wrqu, char *extra)
6941 {
6942         /*
6943          * This can be called at any time.  No action lock required
6944          */
6945
6946         struct ipw2100_priv *priv = ieee80211_priv(dev);
6947         if (!(priv->status & STATUS_ASSOCIATED))
6948                 strcpy(wrqu->name, "unassociated");
6949         else
6950                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b");
6951
6952         IPW_DEBUG_WX("Name: %s\n", wrqu->name);
6953         return 0;
6954 }
6955
6956
6957 static int ipw2100_wx_set_freq(struct net_device *dev,
6958                                struct iw_request_info *info,
6959                                union iwreq_data *wrqu, char *extra)
6960 {
6961         struct ipw2100_priv *priv = ieee80211_priv(dev);
6962         struct iw_freq *fwrq = &wrqu->freq;
6963         int err = 0;
6964
6965         if (priv->ieee->iw_mode == IW_MODE_INFRA)
6966                 return -EOPNOTSUPP;
6967
6968         down(&priv->action_sem);
6969         if (!(priv->status & STATUS_INITIALIZED)) {
6970                 err = -EIO;
6971                 goto done;
6972         }
6973
6974         /* if setting by freq convert to channel */
6975         if (fwrq->e == 1) {
6976                 if ((fwrq->m >= (int) 2.412e8 &&
6977                      fwrq->m <= (int) 2.487e8)) {
6978                         int f = fwrq->m / 100000;
6979                         int c = 0;
6980
6981                         while ((c < REG_MAX_CHANNEL) &&
6982                                (f != ipw2100_frequencies[c]))
6983                                 c++;
6984
6985                         /* hack to fall through */
6986                         fwrq->e = 0;
6987                         fwrq->m = c + 1;
6988                 }
6989         }
6990
6991         if (fwrq->e > 0 || fwrq->m > 1000)
6992                 return -EOPNOTSUPP;
6993         else { /* Set the channel */
6994                 IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
6995                 err = ipw2100_set_channel(priv, fwrq->m, 0);
6996         }
6997
6998  done:
6999         up(&priv->action_sem);
7000         return err;
7001 }
7002
7003
7004 static int ipw2100_wx_get_freq(struct net_device *dev,
7005                                struct iw_request_info *info,
7006                                union iwreq_data *wrqu, char *extra)
7007 {
7008         /*
7009          * This can be called at any time.  No action lock required
7010          */
7011
7012         struct ipw2100_priv *priv = ieee80211_priv(dev);
7013
7014         wrqu->freq.e = 0;
7015
7016         /* If we are associated, trying to associate, or have a statically
7017          * configured CHANNEL then return that; otherwise return ANY */
7018         if (priv->config & CFG_STATIC_CHANNEL ||
7019             priv->status & STATUS_ASSOCIATED)
7020                 wrqu->freq.m = priv->channel;
7021         else
7022                 wrqu->freq.m = 0;
7023
7024         IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
7025         return 0;
7026
7027 }
7028
7029 static int ipw2100_wx_set_mode(struct net_device *dev,
7030                                struct iw_request_info *info,
7031                                union iwreq_data *wrqu, char *extra)
7032 {
7033         struct ipw2100_priv *priv = ieee80211_priv(dev);
7034         int err = 0;
7035
7036         IPW_DEBUG_WX("SET Mode -> %d \n", wrqu->mode);
7037
7038         if (wrqu->mode == priv->ieee->iw_mode)
7039                 return 0;
7040
7041         down(&priv->action_sem);
7042         if (!(priv->status & STATUS_INITIALIZED)) {
7043                 err = -EIO;
7044                 goto done;
7045         }
7046
7047         switch (wrqu->mode) {
7048 #ifdef CONFIG_IPW2100_MONITOR
7049         case IW_MODE_MONITOR:
7050                 err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7051                 break;
7052 #endif /* CONFIG_IPW2100_MONITOR */
7053         case IW_MODE_ADHOC:
7054                 err = ipw2100_switch_mode(priv, IW_MODE_ADHOC);
7055                 break;
7056         case IW_MODE_INFRA:
7057         case IW_MODE_AUTO:
7058         default:
7059                 err = ipw2100_switch_mode(priv, IW_MODE_INFRA);
7060                 break;
7061         }
7062
7063 done:
7064         up(&priv->action_sem);
7065         return err;
7066 }
7067
7068 static int ipw2100_wx_get_mode(struct net_device *dev,
7069                                struct iw_request_info *info,
7070                                union iwreq_data *wrqu, char *extra)
7071 {
7072         /*
7073          * This can be called at any time.  No action lock required
7074          */
7075
7076         struct ipw2100_priv *priv = ieee80211_priv(dev);
7077
7078         wrqu->mode = priv->ieee->iw_mode;
7079         IPW_DEBUG_WX("GET Mode -> %d\n", wrqu->mode);
7080
7081         return 0;
7082 }
7083
7084
7085 #define POWER_MODES 5
7086
7087 /* Values are in microsecond */
7088 static const s32 timeout_duration[POWER_MODES] = {
7089         350000,
7090         250000,
7091         75000,
7092         37000,
7093         25000,
7094 };
7095
7096 static const s32 period_duration[POWER_MODES] = {
7097         400000,
7098         700000,
7099         1000000,
7100         1000000,
7101         1000000
7102 };
7103
7104 static int ipw2100_wx_get_range(struct net_device *dev,
7105                                 struct iw_request_info *info,
7106                                 union iwreq_data *wrqu, char *extra)
7107 {
7108         /*
7109          * This can be called at any time.  No action lock required
7110          */
7111
7112         struct ipw2100_priv *priv = ieee80211_priv(dev);
7113         struct iw_range *range = (struct iw_range *)extra;
7114         u16 val;
7115         int i, level;
7116
7117         wrqu->data.length = sizeof(*range);
7118         memset(range, 0, sizeof(*range));
7119
7120         /* Let's try to keep this struct in the same order as in
7121          * linux/include/wireless.h
7122          */
7123
7124         /* TODO: See what values we can set, and remove the ones we can't
7125          * set, or fill them with some default data.
7126          */
7127
7128         /* ~5 Mb/s real (802.11b) */
7129         range->throughput = 5 * 1000 * 1000;
7130
7131 //      range->sensitivity;     /* signal level threshold range */
7132
7133         range->max_qual.qual = 100;
7134         /* TODO: Find real max RSSI and stick here */
7135         range->max_qual.level = 0;
7136         range->max_qual.noise = 0;
7137         range->max_qual.updated = 7; /* Updated all three */
7138
7139         range->avg_qual.qual = 70; /* > 8% missed beacons is 'bad' */
7140         /* TODO: Find real 'good' to 'bad' threshol value for RSSI */
7141         range->avg_qual.level = 20 + IPW2100_RSSI_TO_DBM;
7142         range->avg_qual.noise = 0;
7143         range->avg_qual.updated = 7; /* Updated all three */
7144
7145         range->num_bitrates = RATE_COUNT;
7146
7147         for (i = 0; i < RATE_COUNT && i < IW_MAX_BITRATES; i++) {
7148                 range->bitrate[i] = ipw2100_rates_11b[i];
7149         }
7150
7151         range->min_rts = MIN_RTS_THRESHOLD;
7152         range->max_rts = MAX_RTS_THRESHOLD;
7153         range->min_frag = MIN_FRAG_THRESHOLD;
7154         range->max_frag = MAX_FRAG_THRESHOLD;
7155
7156         range->min_pmp = period_duration[0];    /* Minimal PM period */
7157         range->max_pmp = period_duration[POWER_MODES-1];/* Maximal PM period */
7158         range->min_pmt = timeout_duration[POWER_MODES-1];       /* Minimal PM timeout */
7159         range->max_pmt = timeout_duration[0];/* Maximal PM timeout */
7160
7161         /* How to decode max/min PM period */
7162         range->pmp_flags = IW_POWER_PERIOD;
7163         /* How to decode max/min PM period */
7164         range->pmt_flags = IW_POWER_TIMEOUT;
7165         /* What PM options are supported */
7166         range->pm_capa = IW_POWER_TIMEOUT | IW_POWER_PERIOD;
7167
7168         range->encoding_size[0] = 5;
7169         range->encoding_size[1] = 13;           /* Different token sizes */
7170         range->num_encoding_sizes = 2;          /* Number of entry in the list */
7171         range->max_encoding_tokens = WEP_KEYS;  /* Max number of tokens */
7172 //      range->encoding_login_index;            /* token index for login token */
7173
7174         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7175                 range->txpower_capa = IW_TXPOW_DBM;
7176                 range->num_txpower = IW_MAX_TXPOWER;
7177                 for (i = 0, level = (IPW_TX_POWER_MAX_DBM * 16); i < IW_MAX_TXPOWER;
7178                      i++, level -= ((IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM) * 16) /
7179                              (IW_MAX_TXPOWER - 1))
7180                         range->txpower[i] = level / 16;
7181         } else {
7182                 range->txpower_capa = 0;
7183                 range->num_txpower = 0;
7184         }
7185
7186
7187         /* Set the Wireless Extension versions */
7188         range->we_version_compiled = WIRELESS_EXT;
7189         range->we_version_source = 16;
7190
7191 //      range->retry_capa;      /* What retry options are supported */
7192 //      range->retry_flags;     /* How to decode max/min retry limit */
7193 //      range->r_time_flags;    /* How to decode max/min retry life */
7194 //      range->min_retry;       /* Minimal number of retries */
7195 //      range->max_retry;       /* Maximal number of retries */
7196 //      range->min_r_time;      /* Minimal retry lifetime */
7197 //      range->max_r_time;      /* Maximal retry lifetime */
7198
7199         range->num_channels = FREQ_COUNT;
7200
7201         val = 0;
7202         for (i = 0; i < FREQ_COUNT; i++) {
7203                 // TODO: Include only legal frequencies for some countries
7204 //              if (local->channel_mask & (1 << i)) {
7205                         range->freq[val].i = i + 1;
7206                         range->freq[val].m = ipw2100_frequencies[i] * 100000;
7207                         range->freq[val].e = 1;
7208                         val++;
7209 //              }
7210                 if (val == IW_MAX_FREQUENCIES)
7211                 break;
7212         }
7213         range->num_frequency = val;
7214
7215         IPW_DEBUG_WX("GET Range\n");
7216
7217         return 0;
7218 }
7219
7220 static int ipw2100_wx_set_wap(struct net_device *dev,
7221                               struct iw_request_info *info,
7222                               union iwreq_data *wrqu, char *extra)
7223 {
7224         struct ipw2100_priv *priv = ieee80211_priv(dev);
7225         int err = 0;
7226
7227         static const unsigned char any[] = {
7228                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
7229         };
7230         static const unsigned char off[] = {
7231                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7232         };
7233
7234         // sanity checks
7235         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
7236                 return -EINVAL;
7237
7238         down(&priv->action_sem);
7239         if (!(priv->status & STATUS_INITIALIZED)) {
7240                 err = -EIO;
7241                 goto done;
7242         }
7243
7244         if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
7245             !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
7246                 /* we disable mandatory BSSID association */
7247                 IPW_DEBUG_WX("exit - disable mandatory BSSID\n");
7248                 priv->config &= ~CFG_STATIC_BSSID;
7249                 err = ipw2100_set_mandatory_bssid(priv, NULL, 0);
7250                 goto done;
7251         }
7252
7253         priv->config |= CFG_STATIC_BSSID;
7254         memcpy(priv->mandatory_bssid_mac, wrqu->ap_addr.sa_data, ETH_ALEN);
7255
7256         err = ipw2100_set_mandatory_bssid(priv, wrqu->ap_addr.sa_data, 0);
7257
7258         IPW_DEBUG_WX("SET BSSID -> %02X:%02X:%02X:%02X:%02X:%02X\n",
7259                      wrqu->ap_addr.sa_data[0] & 0xff,
7260                      wrqu->ap_addr.sa_data[1] & 0xff,
7261                      wrqu->ap_addr.sa_data[2] & 0xff,
7262                      wrqu->ap_addr.sa_data[3] & 0xff,
7263                      wrqu->ap_addr.sa_data[4] & 0xff,
7264                      wrqu->ap_addr.sa_data[5] & 0xff);
7265
7266  done:
7267         up(&priv->action_sem);
7268         return err;
7269 }
7270
7271 static int ipw2100_wx_get_wap(struct net_device *dev,
7272                               struct iw_request_info *info,
7273                               union iwreq_data *wrqu, char *extra)
7274 {
7275         /*
7276          * This can be called at any time.  No action lock required
7277          */
7278
7279         struct ipw2100_priv *priv = ieee80211_priv(dev);
7280
7281         /* If we are associated, trying to associate, or have a statically
7282          * configured BSSID then return that; otherwise return ANY */
7283         if (priv->config & CFG_STATIC_BSSID ||
7284             priv->status & STATUS_ASSOCIATED) {
7285                 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
7286                 memcpy(wrqu->ap_addr.sa_data, &priv->bssid, ETH_ALEN);
7287         } else
7288                 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
7289
7290         IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
7291                      MAC_ARG(wrqu->ap_addr.sa_data));
7292         return 0;
7293 }
7294
7295 static int ipw2100_wx_set_essid(struct net_device *dev,
7296                                 struct iw_request_info *info,
7297                                 union iwreq_data *wrqu, char *extra)
7298 {
7299         struct ipw2100_priv *priv = ieee80211_priv(dev);
7300         char *essid = ""; /* ANY */
7301         int length = 0;
7302         int err = 0;
7303
7304         down(&priv->action_sem);
7305         if (!(priv->status & STATUS_INITIALIZED)) {
7306                 err = -EIO;
7307                 goto done;
7308         }
7309
7310         if (wrqu->essid.flags && wrqu->essid.length) {
7311                 length = wrqu->essid.length - 1;
7312                 essid = extra;
7313         }
7314
7315         if (length == 0) {
7316                 IPW_DEBUG_WX("Setting ESSID to ANY\n");
7317                 priv->config &= ~CFG_STATIC_ESSID;
7318                 err = ipw2100_set_essid(priv, NULL, 0, 0);
7319                 goto done;
7320         }
7321
7322         length = min(length, IW_ESSID_MAX_SIZE);
7323
7324         priv->config |= CFG_STATIC_ESSID;
7325
7326         if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
7327                 IPW_DEBUG_WX("ESSID set to current ESSID.\n");
7328                 err = 0;
7329                 goto done;
7330         }
7331
7332         IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(essid, length),
7333                      length);
7334
7335         priv->essid_len = length;
7336         memcpy(priv->essid, essid, priv->essid_len);
7337
7338         err = ipw2100_set_essid(priv, essid, length, 0);
7339
7340  done:
7341         up(&priv->action_sem);
7342         return err;
7343 }
7344
7345 static int ipw2100_wx_get_essid(struct net_device *dev,
7346                                 struct iw_request_info *info,
7347                                 union iwreq_data *wrqu, char *extra)
7348 {
7349         /*
7350          * This can be called at any time.  No action lock required
7351          */
7352
7353         struct ipw2100_priv *priv = ieee80211_priv(dev);
7354
7355         /* If we are associated, trying to associate, or have a statically
7356          * configured ESSID then return that; otherwise return ANY */
7357         if (priv->config & CFG_STATIC_ESSID ||
7358             priv->status & STATUS_ASSOCIATED) {
7359                 IPW_DEBUG_WX("Getting essid: '%s'\n",
7360                              escape_essid(priv->essid, priv->essid_len));
7361                 memcpy(extra, priv->essid, priv->essid_len);
7362                 wrqu->essid.length = priv->essid_len;
7363                 wrqu->essid.flags = 1; /* active */
7364         } else {
7365                 IPW_DEBUG_WX("Getting essid: ANY\n");
7366                 wrqu->essid.length = 0;
7367                 wrqu->essid.flags = 0; /* active */
7368         }
7369
7370         return 0;
7371 }
7372
7373 static int ipw2100_wx_set_nick(struct net_device *dev,
7374                                struct iw_request_info *info,
7375                                union iwreq_data *wrqu, char *extra)
7376 {
7377         /*
7378          * This can be called at any time.  No action lock required
7379          */
7380
7381         struct ipw2100_priv *priv = ieee80211_priv(dev);
7382
7383         if (wrqu->data.length > IW_ESSID_MAX_SIZE)
7384                 return -E2BIG;
7385
7386         wrqu->data.length = min((size_t)wrqu->data.length, sizeof(priv->nick));
7387         memset(priv->nick, 0, sizeof(priv->nick));
7388         memcpy(priv->nick, extra,  wrqu->data.length);
7389
7390         IPW_DEBUG_WX("SET Nickname -> %s \n", priv->nick);
7391
7392         return 0;
7393 }
7394
7395 static int ipw2100_wx_get_nick(struct net_device *dev,
7396                                struct iw_request_info *info,
7397                                union iwreq_data *wrqu, char *extra)
7398 {
7399         /*
7400          * This can be called at any time.  No action lock required
7401          */
7402
7403         struct ipw2100_priv *priv = ieee80211_priv(dev);
7404
7405         wrqu->data.length = strlen(priv->nick) + 1;
7406         memcpy(extra, priv->nick, wrqu->data.length);
7407         wrqu->data.flags = 1; /* active */
7408
7409         IPW_DEBUG_WX("GET Nickname -> %s \n", extra);
7410
7411         return 0;
7412 }
7413
7414 static int ipw2100_wx_set_rate(struct net_device *dev,
7415                                struct iw_request_info *info,
7416                                union iwreq_data *wrqu, char *extra)
7417 {
7418         struct ipw2100_priv *priv = ieee80211_priv(dev);
7419         u32 target_rate = wrqu->bitrate.value;
7420         u32 rate;
7421         int err = 0;
7422
7423         down(&priv->action_sem);
7424         if (!(priv->status & STATUS_INITIALIZED)) {
7425                 err = -EIO;
7426                 goto done;
7427         }
7428
7429         rate = 0;
7430
7431         if (target_rate == 1000000 ||
7432             (!wrqu->bitrate.fixed && target_rate > 1000000))
7433                 rate |= TX_RATE_1_MBIT;
7434         if (target_rate == 2000000 ||
7435             (!wrqu->bitrate.fixed && target_rate > 2000000))
7436                 rate |= TX_RATE_2_MBIT;
7437         if (target_rate == 5500000 ||
7438             (!wrqu->bitrate.fixed && target_rate > 5500000))
7439                 rate |= TX_RATE_5_5_MBIT;
7440         if (target_rate == 11000000 ||
7441             (!wrqu->bitrate.fixed && target_rate > 11000000))
7442                 rate |= TX_RATE_11_MBIT;
7443         if (rate == 0)
7444                 rate = DEFAULT_TX_RATES;
7445
7446         err = ipw2100_set_tx_rates(priv, rate, 0);
7447
7448         IPW_DEBUG_WX("SET Rate -> %04X \n", rate);
7449  done:
7450         up(&priv->action_sem);
7451         return err;
7452 }
7453
7454
7455 static int ipw2100_wx_get_rate(struct net_device *dev,
7456                                struct iw_request_info *info,
7457                                union iwreq_data *wrqu, char *extra)
7458 {
7459         struct ipw2100_priv *priv = ieee80211_priv(dev);
7460         int val;
7461         int len = sizeof(val);
7462         int err = 0;
7463
7464         if (!(priv->status & STATUS_ENABLED) ||
7465             priv->status & STATUS_RF_KILL_MASK ||
7466             !(priv->status & STATUS_ASSOCIATED)) {
7467                 wrqu->bitrate.value = 0;
7468                 return 0;
7469         }
7470
7471         down(&priv->action_sem);
7472         if (!(priv->status & STATUS_INITIALIZED)) {
7473                 err = -EIO;
7474                 goto done;
7475         }
7476
7477         err = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE, &val, &len);
7478         if (err) {
7479                 IPW_DEBUG_WX("failed querying ordinals.\n");
7480                 return err;
7481         }
7482
7483         switch (val & TX_RATE_MASK) {
7484         case TX_RATE_1_MBIT:
7485                 wrqu->bitrate.value = 1000000;
7486                 break;
7487         case TX_RATE_2_MBIT:
7488                 wrqu->bitrate.value = 2000000;
7489                 break;
7490         case TX_RATE_5_5_MBIT:
7491                 wrqu->bitrate.value = 5500000;
7492                 break;
7493         case TX_RATE_11_MBIT:
7494                 wrqu->bitrate.value = 11000000;
7495                 break;
7496         default:
7497                 wrqu->bitrate.value = 0;
7498         }
7499
7500         IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
7501
7502  done:
7503         up(&priv->action_sem);
7504         return err;
7505 }
7506
7507 static int ipw2100_wx_set_rts(struct net_device *dev,
7508                               struct iw_request_info *info,
7509                               union iwreq_data *wrqu, char *extra)
7510 {
7511         struct ipw2100_priv *priv = ieee80211_priv(dev);
7512         int value, err;
7513
7514         /* Auto RTS not yet supported */
7515         if (wrqu->rts.fixed == 0)
7516                 return -EINVAL;
7517
7518         down(&priv->action_sem);
7519         if (!(priv->status & STATUS_INITIALIZED)) {
7520                 err = -EIO;
7521                 goto done;
7522         }
7523
7524         if (wrqu->rts.disabled)
7525                 value = priv->rts_threshold | RTS_DISABLED;
7526         else {
7527                 if (wrqu->rts.value < 1 ||
7528                     wrqu->rts.value > 2304) {
7529                         err = -EINVAL;
7530                         goto done;
7531                 }
7532                 value = wrqu->rts.value;
7533         }
7534
7535         err = ipw2100_set_rts_threshold(priv, value);
7536
7537         IPW_DEBUG_WX("SET RTS Threshold -> 0x%08X \n", value);
7538  done:
7539         up(&priv->action_sem);
7540         return err;
7541 }
7542
7543 static int ipw2100_wx_get_rts(struct net_device *dev,
7544                               struct iw_request_info *info,
7545                               union iwreq_data *wrqu, char *extra)
7546 {
7547         /*
7548          * This can be called at any time.  No action lock required
7549          */
7550
7551         struct ipw2100_priv *priv = ieee80211_priv(dev);
7552
7553         wrqu->rts.value = priv->rts_threshold & ~RTS_DISABLED;
7554         wrqu->rts.fixed = 1; /* no auto select */
7555
7556         /* If RTS is set to the default value, then it is disabled */
7557         wrqu->rts.disabled = (priv->rts_threshold & RTS_DISABLED) ? 1 : 0;
7558
7559         IPW_DEBUG_WX("GET RTS Threshold -> 0x%08X \n", wrqu->rts.value);
7560
7561         return 0;
7562 }
7563
7564 static int ipw2100_wx_set_txpow(struct net_device *dev,
7565                                 struct iw_request_info *info,
7566                                 union iwreq_data *wrqu, char *extra)
7567 {
7568         struct ipw2100_priv *priv = ieee80211_priv(dev);
7569         int err = 0, value;
7570
7571         if (priv->ieee->iw_mode != IW_MODE_ADHOC)
7572                 return -EINVAL;
7573
7574         if (wrqu->txpower.disabled == 1 || wrqu->txpower.fixed == 0)
7575                 value = IPW_TX_POWER_DEFAULT;
7576         else {
7577                 if (wrqu->txpower.value < IPW_TX_POWER_MIN_DBM ||
7578                     wrqu->txpower.value > IPW_TX_POWER_MAX_DBM)
7579                         return -EINVAL;
7580
7581                 value = (wrqu->txpower.value - IPW_TX_POWER_MIN_DBM) * 16 /
7582                         (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM);
7583         }
7584
7585         down(&priv->action_sem);
7586         if (!(priv->status & STATUS_INITIALIZED)) {
7587                 err = -EIO;
7588                 goto done;
7589         }
7590
7591         err = ipw2100_set_tx_power(priv, value);
7592
7593         IPW_DEBUG_WX("SET TX Power -> %d \n", value);
7594
7595  done:
7596         up(&priv->action_sem);
7597         return err;
7598 }
7599
7600 static int ipw2100_wx_get_txpow(struct net_device *dev,
7601                                 struct iw_request_info *info,
7602                                 union iwreq_data *wrqu, char *extra)
7603 {
7604         /*
7605          * This can be called at any time.  No action lock required
7606          */
7607
7608         struct ipw2100_priv *priv = ieee80211_priv(dev);
7609
7610         if (priv->ieee->iw_mode != IW_MODE_ADHOC) {
7611                 wrqu->power.disabled = 1;
7612                 return 0;
7613         }
7614
7615         if (priv->tx_power == IPW_TX_POWER_DEFAULT) {
7616                 wrqu->power.fixed = 0;
7617                 wrqu->power.value = IPW_TX_POWER_MAX_DBM;
7618                 wrqu->power.disabled = 1;
7619         } else {
7620                 wrqu->power.disabled = 0;
7621                 wrqu->power.fixed = 1;
7622                 wrqu->power.value =
7623                         (priv->tx_power *
7624                          (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM)) /
7625                         (IPW_TX_POWER_MAX - IPW_TX_POWER_MIN) +
7626                         IPW_TX_POWER_MIN_DBM;
7627         }
7628
7629         wrqu->power.flags = IW_TXPOW_DBM;
7630
7631         IPW_DEBUG_WX("GET TX Power -> %d \n", wrqu->power.value);
7632
7633         return 0;
7634 }
7635
7636 static int ipw2100_wx_set_frag(struct net_device *dev,
7637                                struct iw_request_info *info,
7638                                union iwreq_data *wrqu, char *extra)
7639 {
7640         /*
7641          * This can be called at any time.  No action lock required
7642          */
7643
7644         struct ipw2100_priv *priv = ieee80211_priv(dev);
7645
7646         if (!wrqu->frag.fixed)
7647                 return -EINVAL;
7648
7649         if (wrqu->frag.disabled) {
7650                 priv->frag_threshold |= FRAG_DISABLED;
7651                 priv->ieee->fts = DEFAULT_FTS;
7652         } else {
7653                 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
7654                     wrqu->frag.value > MAX_FRAG_THRESHOLD)
7655                         return -EINVAL;
7656
7657                 priv->ieee->fts = wrqu->frag.value & ~0x1;
7658                 priv->frag_threshold = priv->ieee->fts;
7659         }
7660
7661         IPW_DEBUG_WX("SET Frag Threshold -> %d \n", priv->ieee->fts);
7662
7663         return 0;
7664 }
7665
7666 static int ipw2100_wx_get_frag(struct net_device *dev,
7667                                struct iw_request_info *info,
7668                                union iwreq_data *wrqu, char *extra)
7669 {
7670         /*
7671          * This can be called at any time.  No action lock required
7672          */
7673
7674         struct ipw2100_priv *priv = ieee80211_priv(dev);
7675         wrqu->frag.value = priv->frag_threshold & ~FRAG_DISABLED;
7676         wrqu->frag.fixed = 0;   /* no auto select */
7677         wrqu->frag.disabled = (priv->frag_threshold & FRAG_DISABLED) ? 1 : 0;
7678
7679         IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);
7680
7681         return 0;
7682 }
7683
7684 static int ipw2100_wx_set_retry(struct net_device *dev,
7685                                 struct iw_request_info *info,
7686                                 union iwreq_data *wrqu, char *extra)
7687 {
7688         struct ipw2100_priv *priv = ieee80211_priv(dev);
7689         int err = 0;
7690
7691         if (wrqu->retry.flags & IW_RETRY_LIFETIME ||
7692             wrqu->retry.disabled)
7693                 return -EINVAL;
7694
7695         if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
7696                 return 0;
7697
7698         down(&priv->action_sem);
7699         if (!(priv->status & STATUS_INITIALIZED)) {
7700                 err = -EIO;
7701                 goto done;
7702         }
7703
7704         if (wrqu->retry.flags & IW_RETRY_MIN) {
7705                 err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7706                 IPW_DEBUG_WX("SET Short Retry Limit -> %d \n",
7707                        wrqu->retry.value);
7708                 goto done;
7709         }
7710
7711         if (wrqu->retry.flags & IW_RETRY_MAX) {
7712                 err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7713                 IPW_DEBUG_WX("SET Long Retry Limit -> %d \n",
7714                        wrqu->retry.value);
7715                 goto done;
7716         }
7717
7718         err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7719         if (!err)
7720                 err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7721
7722         IPW_DEBUG_WX("SET Both Retry Limits -> %d \n", wrqu->retry.value);
7723
7724  done:
7725         up(&priv->action_sem);
7726         return err;
7727 }
7728
7729 static int ipw2100_wx_get_retry(struct net_device *dev,
7730                                 struct iw_request_info *info,
7731                                 union iwreq_data *wrqu, char *extra)
7732 {
7733         /*
7734          * This can be called at any time.  No action lock required
7735          */
7736
7737         struct ipw2100_priv *priv = ieee80211_priv(dev);
7738
7739         wrqu->retry.disabled = 0; /* can't be disabled */
7740
7741         if ((wrqu->retry.flags & IW_RETRY_TYPE) ==
7742             IW_RETRY_LIFETIME)
7743                 return -EINVAL;
7744
7745         if (wrqu->retry.flags & IW_RETRY_MAX) {
7746                 wrqu->retry.flags = IW_RETRY_LIMIT & IW_RETRY_MAX;
7747                 wrqu->retry.value = priv->long_retry_limit;
7748         } else {
7749                 wrqu->retry.flags =
7750                     (priv->short_retry_limit !=
7751                      priv->long_retry_limit) ?
7752                     IW_RETRY_LIMIT & IW_RETRY_MIN : IW_RETRY_LIMIT;
7753
7754                 wrqu->retry.value = priv->short_retry_limit;
7755         }
7756
7757         IPW_DEBUG_WX("GET Retry -> %d \n", wrqu->retry.value);
7758
7759         return 0;
7760 }
7761
7762 static int ipw2100_wx_set_scan(struct net_device *dev,
7763                                struct iw_request_info *info,
7764                                union iwreq_data *wrqu, char *extra)
7765 {
7766         struct ipw2100_priv *priv = ieee80211_priv(dev);
7767         int err = 0;
7768
7769         down(&priv->action_sem);
7770         if (!(priv->status & STATUS_INITIALIZED)) {
7771                 err = -EIO;
7772                 goto done;
7773         }
7774
7775         IPW_DEBUG_WX("Initiating scan...\n");
7776         if (ipw2100_set_scan_options(priv) ||
7777             ipw2100_start_scan(priv)) {
7778                 IPW_DEBUG_WX("Start scan failed.\n");
7779
7780                 /* TODO: Mark a scan as pending so when hardware initialized
7781                  *       a scan starts */
7782         }
7783
7784  done:
7785         up(&priv->action_sem);
7786         return err;
7787 }
7788
7789 static int ipw2100_wx_get_scan(struct net_device *dev,
7790                                struct iw_request_info *info,
7791                                union iwreq_data *wrqu, char *extra)
7792 {
7793         /*
7794          * This can be called at any time.  No action lock required
7795          */
7796
7797         struct ipw2100_priv *priv = ieee80211_priv(dev);
7798         return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
7799 }
7800
7801
7802 /*
7803  * Implementation based on code in hostap-driver v0.1.3 hostap_ioctl.c
7804  */
7805 static int ipw2100_wx_set_encode(struct net_device *dev,
7806                                  struct iw_request_info *info,
7807                                  union iwreq_data *wrqu, char *key)
7808 {
7809         /*
7810          * No check of STATUS_INITIALIZED required
7811          */
7812
7813         struct ipw2100_priv *priv = ieee80211_priv(dev);
7814         return ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
7815 }
7816
7817 static int ipw2100_wx_get_encode(struct net_device *dev,
7818                                  struct iw_request_info *info,
7819                                  union iwreq_data *wrqu, char *key)
7820 {
7821         /*
7822          * This can be called at any time.  No action lock required
7823          */
7824
7825         struct ipw2100_priv *priv = ieee80211_priv(dev);
7826         return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
7827 }
7828
7829 static int ipw2100_wx_set_power(struct net_device *dev,
7830                                 struct iw_request_info *info,
7831                                 union iwreq_data *wrqu, char *extra)
7832 {
7833         struct ipw2100_priv *priv = ieee80211_priv(dev);
7834         int err = 0;
7835
7836         down(&priv->action_sem);
7837         if (!(priv->status & STATUS_INITIALIZED)) {
7838                 err = -EIO;
7839                 goto done;
7840         }
7841
7842         if (wrqu->power.disabled) {
7843                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
7844                 err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
7845                 IPW_DEBUG_WX("SET Power Management Mode -> off\n");
7846                 goto done;
7847         }
7848
7849         switch (wrqu->power.flags & IW_POWER_MODE) {
7850         case IW_POWER_ON:    /* If not specified */
7851         case IW_POWER_MODE:  /* If set all mask */
7852         case IW_POWER_ALL_R: /* If explicitely state all */
7853                 break;
7854         default: /* Otherwise we don't support it */
7855                 IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
7856                              wrqu->power.flags);
7857                 err = -EOPNOTSUPP;
7858                 goto done;
7859         }
7860
7861         /* If the user hasn't specified a power management mode yet, default
7862          * to BATTERY */
7863         priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
7864         err = ipw2100_set_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
7865
7866         IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n",
7867                      priv->power_mode);
7868
7869  done:
7870         up(&priv->action_sem);
7871         return err;
7872
7873 }
7874
7875 static int ipw2100_wx_get_power(struct net_device *dev,
7876                                 struct iw_request_info *info,
7877                                 union iwreq_data *wrqu, char *extra)
7878 {
7879         /*
7880          * This can be called at any time.  No action lock required
7881          */
7882
7883         struct ipw2100_priv *priv = ieee80211_priv(dev);
7884
7885         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7886                 wrqu->power.disabled = 1;
7887         } else {
7888                 wrqu->power.disabled = 0;
7889                 wrqu->power.flags = 0;
7890         }
7891
7892         IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
7893
7894         return 0;
7895 }
7896
7897
7898 /*
7899  *
7900  * IWPRIV handlers
7901  *
7902  */
7903 #ifdef CONFIG_IPW2100_MONITOR
7904 static int ipw2100_wx_set_promisc(struct net_device *dev,
7905                                   struct iw_request_info *info,
7906                                   union iwreq_data *wrqu, char *extra)
7907 {
7908         struct ipw2100_priv *priv = ieee80211_priv(dev);
7909         int *parms = (int *)extra;
7910         int enable = (parms[0] > 0);
7911         int err = 0;
7912
7913         down(&priv->action_sem);
7914         if (!(priv->status & STATUS_INITIALIZED)) {
7915                 err = -EIO;
7916                 goto done;
7917         }
7918
7919         if (enable) {
7920                 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
7921                         err = ipw2100_set_channel(priv, parms[1], 0);
7922                         goto done;
7923                 }
7924                 priv->channel = parms[1];
7925                 err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7926         } else {
7927                 if (priv->ieee->iw_mode == IW_MODE_MONITOR)
7928                         err = ipw2100_switch_mode(priv, priv->last_mode);
7929         }
7930  done:
7931         up(&priv->action_sem);
7932         return err;
7933 }
7934
7935 static int ipw2100_wx_reset(struct net_device *dev,
7936                             struct iw_request_info *info,
7937                             union iwreq_data *wrqu, char *extra)
7938 {
7939         struct ipw2100_priv *priv = ieee80211_priv(dev);
7940         if (priv->status & STATUS_INITIALIZED)
7941                 schedule_reset(priv);
7942         return 0;
7943 }
7944
7945 #endif
7946
7947 static int ipw2100_wx_set_powermode(struct net_device *dev,
7948                                     struct iw_request_info *info,
7949                                     union iwreq_data *wrqu, char *extra)
7950 {
7951         struct ipw2100_priv *priv = ieee80211_priv(dev);
7952         int err = 0, mode = *(int *)extra;
7953
7954         down(&priv->action_sem);
7955         if (!(priv->status & STATUS_INITIALIZED)) {
7956                 err = -EIO;
7957                 goto done;
7958         }
7959
7960         if ((mode < 1) || (mode > POWER_MODES))
7961                 mode = IPW_POWER_AUTO;
7962
7963         if (priv->power_mode != mode)
7964                 err = ipw2100_set_power_mode(priv, mode);
7965  done:
7966         up(&priv->action_sem);
7967         return err;
7968 }
7969
7970 #define MAX_POWER_STRING 80
7971 static int ipw2100_wx_get_powermode(struct net_device *dev,
7972                                     struct iw_request_info *info,
7973                                     union iwreq_data *wrqu, char *extra)
7974 {
7975         /*
7976          * This can be called at any time.  No action lock required
7977          */
7978
7979         struct ipw2100_priv *priv = ieee80211_priv(dev);
7980         int level = IPW_POWER_LEVEL(priv->power_mode);
7981         s32 timeout, period;
7982
7983         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7984                 snprintf(extra, MAX_POWER_STRING,
7985                          "Power save level: %d (Off)", level);
7986         } else {
7987                 switch (level) {
7988                 case IPW_POWER_MODE_CAM:
7989                         snprintf(extra, MAX_POWER_STRING,
7990                                  "Power save level: %d (None)", level);
7991                         break;
7992                 case IPW_POWER_AUTO:
7993                 snprintf(extra, MAX_POWER_STRING,
7994                          "Power save level: %d (Auto)", 0);
7995                         break;
7996                 default:
7997                         timeout = timeout_duration[level - 1] / 1000;
7998                         period = period_duration[level - 1] / 1000;
7999                         snprintf(extra, MAX_POWER_STRING,
8000                                  "Power save level: %d "
8001                                  "(Timeout %dms, Period %dms)",
8002                                  level, timeout, period);
8003                 }
8004         }
8005
8006         wrqu->data.length = strlen(extra) + 1;
8007
8008         return 0;
8009 }
8010
8011
8012 static int ipw2100_wx_set_preamble(struct net_device *dev,
8013                                    struct iw_request_info *info,
8014                                    union iwreq_data *wrqu, char *extra)
8015 {
8016         struct ipw2100_priv *priv = ieee80211_priv(dev);
8017         int err, mode = *(int *)extra;
8018
8019         down(&priv->action_sem);
8020         if (!(priv->status & STATUS_INITIALIZED)) {
8021                 err = -EIO;
8022                 goto done;
8023         }
8024
8025         if (mode == 1)
8026                 priv->config |= CFG_LONG_PREAMBLE;
8027         else if (mode == 0)
8028                 priv->config &= ~CFG_LONG_PREAMBLE;
8029         else {
8030                 err = -EINVAL;
8031                 goto done;
8032         }
8033
8034         err = ipw2100_system_config(priv, 0);
8035
8036 done:
8037         up(&priv->action_sem);
8038         return err;
8039 }
8040
8041 static int ipw2100_wx_get_preamble(struct net_device *dev,
8042                                     struct iw_request_info *info,
8043                                     union iwreq_data *wrqu, char *extra)
8044 {
8045         /*
8046          * This can be called at any time.  No action lock required
8047          */
8048
8049         struct ipw2100_priv *priv = ieee80211_priv(dev);
8050
8051         if (priv->config & CFG_LONG_PREAMBLE)
8052                 snprintf(wrqu->name, IFNAMSIZ, "long (1)");
8053         else
8054                 snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
8055
8056         return 0;
8057 }
8058
8059 static iw_handler ipw2100_wx_handlers[] =
8060 {
8061         NULL,                     /* SIOCSIWCOMMIT */
8062         ipw2100_wx_get_name,      /* SIOCGIWNAME */
8063         NULL,                     /* SIOCSIWNWID */
8064         NULL,                     /* SIOCGIWNWID */
8065         ipw2100_wx_set_freq,      /* SIOCSIWFREQ */
8066         ipw2100_wx_get_freq,      /* SIOCGIWFREQ */
8067         ipw2100_wx_set_mode,      /* SIOCSIWMODE */
8068         ipw2100_wx_get_mode,      /* SIOCGIWMODE */
8069         NULL,                     /* SIOCSIWSENS */
8070         NULL,                     /* SIOCGIWSENS */
8071         NULL,                     /* SIOCSIWRANGE */
8072         ipw2100_wx_get_range,     /* SIOCGIWRANGE */
8073         NULL,                     /* SIOCSIWPRIV */
8074         NULL,                     /* SIOCGIWPRIV */
8075         NULL,                     /* SIOCSIWSTATS */
8076         NULL,                     /* SIOCGIWSTATS */
8077         NULL,                     /* SIOCSIWSPY */
8078         NULL,                     /* SIOCGIWSPY */
8079         NULL,                     /* SIOCGIWTHRSPY */
8080         NULL,                     /* SIOCWIWTHRSPY */
8081         ipw2100_wx_set_wap,       /* SIOCSIWAP */
8082         ipw2100_wx_get_wap,       /* SIOCGIWAP */
8083         NULL,                     /* -- hole -- */
8084         NULL,                     /* SIOCGIWAPLIST -- deprecated */
8085         ipw2100_wx_set_scan,      /* SIOCSIWSCAN */
8086         ipw2100_wx_get_scan,      /* SIOCGIWSCAN */
8087         ipw2100_wx_set_essid,     /* SIOCSIWESSID */
8088         ipw2100_wx_get_essid,     /* SIOCGIWESSID */
8089         ipw2100_wx_set_nick,      /* SIOCSIWNICKN */
8090         ipw2100_wx_get_nick,      /* SIOCGIWNICKN */
8091         NULL,                     /* -- hole -- */
8092         NULL,                     /* -- hole -- */
8093         ipw2100_wx_set_rate,      /* SIOCSIWRATE */
8094         ipw2100_wx_get_rate,      /* SIOCGIWRATE */
8095         ipw2100_wx_set_rts,       /* SIOCSIWRTS */
8096         ipw2100_wx_get_rts,       /* SIOCGIWRTS */
8097         ipw2100_wx_set_frag,      /* SIOCSIWFRAG */
8098         ipw2100_wx_get_frag,      /* SIOCGIWFRAG */
8099         ipw2100_wx_set_txpow,     /* SIOCSIWTXPOW */
8100         ipw2100_wx_get_txpow,     /* SIOCGIWTXPOW */
8101         ipw2100_wx_set_retry,     /* SIOCSIWRETRY */
8102         ipw2100_wx_get_retry,     /* SIOCGIWRETRY */
8103         ipw2100_wx_set_encode,    /* SIOCSIWENCODE */
8104         ipw2100_wx_get_encode,    /* SIOCGIWENCODE */
8105         ipw2100_wx_set_power,     /* SIOCSIWPOWER */
8106         ipw2100_wx_get_power,     /* SIOCGIWPOWER */
8107 };
8108
8109 #define IPW2100_PRIV_SET_MONITOR        SIOCIWFIRSTPRIV
8110 #define IPW2100_PRIV_RESET              SIOCIWFIRSTPRIV+1
8111 #define IPW2100_PRIV_SET_POWER          SIOCIWFIRSTPRIV+2
8112 #define IPW2100_PRIV_GET_POWER          SIOCIWFIRSTPRIV+3
8113 #define IPW2100_PRIV_SET_LONGPREAMBLE   SIOCIWFIRSTPRIV+4
8114 #define IPW2100_PRIV_GET_LONGPREAMBLE   SIOCIWFIRSTPRIV+5
8115
8116 static const struct iw_priv_args ipw2100_private_args[] = {
8117
8118 #ifdef CONFIG_IPW2100_MONITOR
8119         {
8120                 IPW2100_PRIV_SET_MONITOR,
8121                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"
8122         },
8123         {
8124                 IPW2100_PRIV_RESET,
8125                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"
8126         },
8127 #endif /* CONFIG_IPW2100_MONITOR */
8128
8129         {
8130                 IPW2100_PRIV_SET_POWER,
8131                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_power"
8132         },
8133         {
8134                 IPW2100_PRIV_GET_POWER,
8135                 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_POWER_STRING, "get_power"
8136         },
8137         {
8138                 IPW2100_PRIV_SET_LONGPREAMBLE,
8139                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_preamble"
8140         },
8141         {
8142                 IPW2100_PRIV_GET_LONGPREAMBLE,
8143                 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "get_preamble"
8144         },
8145 };
8146
8147 static iw_handler ipw2100_private_handler[] = {
8148 #ifdef CONFIG_IPW2100_MONITOR
8149         ipw2100_wx_set_promisc,
8150         ipw2100_wx_reset,
8151 #else /* CONFIG_IPW2100_MONITOR */
8152         NULL,
8153         NULL,
8154 #endif /* CONFIG_IPW2100_MONITOR */
8155         ipw2100_wx_set_powermode,
8156         ipw2100_wx_get_powermode,
8157         ipw2100_wx_set_preamble,
8158         ipw2100_wx_get_preamble,
8159 };
8160
8161 static struct iw_handler_def ipw2100_wx_handler_def =
8162 {
8163         .standard = ipw2100_wx_handlers,
8164         .num_standard = sizeof(ipw2100_wx_handlers) / sizeof(iw_handler),
8165         .num_private = sizeof(ipw2100_private_handler) / sizeof(iw_handler),
8166         .num_private_args = sizeof(ipw2100_private_args) /
8167         sizeof(struct iw_priv_args),
8168         .private = (iw_handler *)ipw2100_private_handler,
8169         .private_args = (struct iw_priv_args *)ipw2100_private_args,
8170 };
8171
8172 /*
8173  * Get wireless statistics.
8174  * Called by /proc/net/wireless
8175  * Also called by SIOCGIWSTATS
8176  */
8177 static struct iw_statistics *ipw2100_wx_wireless_stats(struct net_device * dev)
8178 {
8179         enum {
8180                 POOR = 30,
8181                 FAIR = 60,
8182                 GOOD = 80,
8183                 VERY_GOOD = 90,
8184                 EXCELLENT = 95,
8185                 PERFECT = 100
8186         };
8187         int rssi_qual;
8188         int tx_qual;
8189         int beacon_qual;
8190
8191         struct ipw2100_priv *priv = ieee80211_priv(dev);
8192         struct iw_statistics *wstats;
8193         u32 rssi, quality, tx_retries, missed_beacons, tx_failures;
8194         u32 ord_len = sizeof(u32);
8195
8196         if (!priv)
8197                 return (struct iw_statistics *) NULL;
8198
8199         wstats = &priv->wstats;
8200
8201         /* if hw is disabled, then ipw2100_get_ordinal() can't be called.
8202          * ipw2100_wx_wireless_stats seems to be called before fw is
8203          * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
8204          * and associated; if not associcated, the values are all meaningless
8205          * anyway, so set them all to NULL and INVALID */
8206         if (!(priv->status & STATUS_ASSOCIATED)) {
8207                 wstats->miss.beacon = 0;
8208                 wstats->discard.retries = 0;
8209                 wstats->qual.qual = 0;
8210                 wstats->qual.level = 0;
8211                 wstats->qual.noise = 0;
8212                 wstats->qual.updated = 7;
8213                 wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
8214                         IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
8215                 return wstats;
8216         }
8217
8218         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_MISSED_BCNS,
8219                                 &missed_beacons, &ord_len))
8220                 goto fail_get_ordinal;
8221
8222         /* If we don't have a connection the quality and level is 0*/
8223         if (!(priv->status & STATUS_ASSOCIATED)) {
8224                 wstats->qual.qual = 0;
8225                 wstats->qual.level = 0;
8226         } else {
8227                 if (ipw2100_get_ordinal(priv, IPW_ORD_RSSI_AVG_CURR,
8228                                         &rssi, &ord_len))
8229                         goto fail_get_ordinal;
8230                 wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8231                 if (rssi < 10)
8232                         rssi_qual = rssi * POOR / 10;
8233                 else if (rssi < 15)
8234                         rssi_qual = (rssi - 10) * (FAIR - POOR) / 5 + POOR;
8235                 else if (rssi < 20)
8236                         rssi_qual = (rssi - 15) * (GOOD - FAIR) / 5 + FAIR;
8237                 else if (rssi < 30)
8238                         rssi_qual = (rssi - 20) * (VERY_GOOD - GOOD) /
8239                                 10 + GOOD;
8240                 else
8241                         rssi_qual = (rssi - 30) * (PERFECT - VERY_GOOD) /
8242                                 10 + VERY_GOOD;
8243
8244                 if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_RETRIES,
8245                                         &tx_retries, &ord_len))
8246                         goto fail_get_ordinal;
8247
8248                 if (tx_retries > 75)
8249                         tx_qual = (90 - tx_retries) * POOR / 15;
8250                 else if (tx_retries > 70)
8251                         tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
8252                 else if (tx_retries > 65)
8253                         tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
8254                 else if (tx_retries > 50)
8255                         tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
8256                                 15 + GOOD;
8257                 else
8258                         tx_qual = (50 - tx_retries) *
8259                                 (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
8260
8261                 if (missed_beacons > 50)
8262                         beacon_qual = (60 - missed_beacons) * POOR / 10;
8263                 else if (missed_beacons > 40)
8264                         beacon_qual = (50 - missed_beacons) * (FAIR - POOR) /
8265                                 10 + POOR;
8266                 else if (missed_beacons > 32)
8267                         beacon_qual = (40 - missed_beacons) * (GOOD - FAIR) /
8268                                 18 + FAIR;
8269                 else if (missed_beacons > 20)
8270                         beacon_qual = (32 - missed_beacons) *
8271                                 (VERY_GOOD - GOOD) / 20 + GOOD;
8272                 else
8273                         beacon_qual = (20 - missed_beacons) *
8274                                 (PERFECT - VERY_GOOD) / 20 + VERY_GOOD;
8275
8276                 quality = min(beacon_qual, min(tx_qual, rssi_qual));
8277
8278 #ifdef CONFIG_IPW_DEBUG
8279                 if (beacon_qual == quality)
8280                         IPW_DEBUG_WX("Quality clamped by Missed Beacons\n");
8281                 else if (tx_qual == quality)
8282                         IPW_DEBUG_WX("Quality clamped by Tx Retries\n");
8283                 else if (quality != 100)
8284                         IPW_DEBUG_WX("Quality clamped by Signal Strength\n");
8285                 else
8286                         IPW_DEBUG_WX("Quality not clamped.\n");
8287 #endif
8288
8289                 wstats->qual.qual = quality;
8290                 wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8291         }
8292
8293         wstats->qual.noise = 0;
8294         wstats->qual.updated = 7;
8295         wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
8296
8297         /* FIXME: this is percent and not a # */
8298         wstats->miss.beacon = missed_beacons;
8299
8300         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURES,
8301                                 &tx_failures, &ord_len))
8302                 goto fail_get_ordinal;
8303         wstats->discard.retries = tx_failures;
8304
8305         return wstats;
8306
8307  fail_get_ordinal:
8308         IPW_DEBUG_WX("failed querying ordinals.\n");
8309
8310         return (struct iw_statistics *) NULL;
8311 }
8312
8313 static void ipw2100_wx_event_work(struct ipw2100_priv *priv)
8314 {
8315         union iwreq_data wrqu;
8316         int len = ETH_ALEN;
8317
8318         if (priv->status & STATUS_STOPPING)
8319                 return;
8320
8321         down(&priv->action_sem);
8322
8323         IPW_DEBUG_WX("enter\n");
8324
8325         up(&priv->action_sem);
8326
8327         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
8328
8329         /* Fetch BSSID from the hardware */
8330         if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) ||
8331             priv->status & STATUS_RF_KILL_MASK ||
8332             ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
8333                                 &priv->bssid,  &len)) {
8334                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
8335         } else {
8336                 /* We now have the BSSID, so can finish setting to the full
8337                  * associated state */
8338                 memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
8339                 memcpy(&priv->ieee->bssid, priv->bssid, ETH_ALEN);
8340                 priv->status &= ~STATUS_ASSOCIATING;
8341                 priv->status |= STATUS_ASSOCIATED;
8342                 netif_carrier_on(priv->net_dev);
8343                 if (netif_queue_stopped(priv->net_dev)) {
8344                         IPW_DEBUG_INFO("Waking net queue.\n");
8345                         netif_wake_queue(priv->net_dev);
8346                 } else {
8347                         IPW_DEBUG_INFO("Starting net queue.\n");
8348                         netif_start_queue(priv->net_dev);
8349                 }
8350         }
8351
8352         if (!(priv->status & STATUS_ASSOCIATED)) {
8353                 IPW_DEBUG_WX("Configuring ESSID\n");
8354                 down(&priv->action_sem);
8355                 /* This is a disassociation event, so kick the firmware to
8356                  * look for another AP */
8357                 if (priv->config & CFG_STATIC_ESSID)
8358                         ipw2100_set_essid(priv, priv->essid, priv->essid_len, 0);
8359                 else
8360                         ipw2100_set_essid(priv, NULL, 0, 0);
8361                 up(&priv->action_sem);
8362         }
8363
8364         wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
8365 }
8366
8367 #define IPW2100_FW_MAJOR_VERSION 1
8368 #define IPW2100_FW_MINOR_VERSION 3
8369
8370 #define IPW2100_FW_MINOR(x) ((x & 0xff) >> 8)
8371 #define IPW2100_FW_MAJOR(x) (x & 0xff)
8372
8373 #define IPW2100_FW_VERSION ((IPW2100_FW_MINOR_VERSION << 8) | \
8374                              IPW2100_FW_MAJOR_VERSION)
8375
8376 #define IPW2100_FW_PREFIX "ipw2100-" __stringify(IPW2100_FW_MAJOR_VERSION) \
8377 "." __stringify(IPW2100_FW_MINOR_VERSION)
8378
8379 #define IPW2100_FW_NAME(x) IPW2100_FW_PREFIX "" x ".fw"
8380
8381
8382 /*
8383
8384 BINARY FIRMWARE HEADER FORMAT
8385
8386 offset      length   desc
8387 0           2        version
8388 2           2        mode == 0:BSS,1:IBSS,2:MONITOR
8389 4           4        fw_len
8390 8           4        uc_len
8391 C           fw_len   firmware data
8392 12 + fw_len uc_len   microcode data
8393
8394 */
8395
8396 struct ipw2100_fw_header {
8397         short version;
8398         short mode;
8399         unsigned int fw_size;
8400         unsigned int uc_size;
8401 } __attribute__ ((packed));
8402
8403
8404
8405 static int ipw2100_mod_firmware_load(struct ipw2100_fw *fw)
8406 {
8407         struct ipw2100_fw_header *h =
8408                 (struct ipw2100_fw_header *)fw->fw_entry->data;
8409
8410         if (IPW2100_FW_MAJOR(h->version) != IPW2100_FW_MAJOR_VERSION) {
8411                 printk(KERN_WARNING DRV_NAME ": Firmware image not compatible "
8412                        "(detected version id of %u). "
8413                        "See Documentation/networking/README.ipw2100\n",
8414                        h->version);
8415                 return 1;
8416         }
8417
8418         fw->version = h->version;
8419         fw->fw.data = fw->fw_entry->data + sizeof(struct ipw2100_fw_header);
8420         fw->fw.size = h->fw_size;
8421         fw->uc.data = fw->fw.data + h->fw_size;
8422         fw->uc.size = h->uc_size;
8423
8424         return 0;
8425 }
8426
8427
8428 static int ipw2100_get_firmware(struct ipw2100_priv *priv,
8429                                 struct ipw2100_fw *fw)
8430 {
8431         char *fw_name;
8432         int rc;
8433
8434         IPW_DEBUG_INFO("%s: Using hotplug firmware load.\n",
8435                priv->net_dev->name);
8436
8437         switch (priv->ieee->iw_mode) {
8438         case IW_MODE_ADHOC:
8439                 fw_name = IPW2100_FW_NAME("-i");
8440                 break;
8441 #ifdef CONFIG_IPW2100_MONITOR
8442         case IW_MODE_MONITOR:
8443                 fw_name = IPW2100_FW_NAME("-p");
8444                 break;
8445 #endif
8446         case IW_MODE_INFRA:
8447         default:
8448                 fw_name = IPW2100_FW_NAME("");
8449                 break;
8450         }
8451
8452         rc = request_firmware(&fw->fw_entry, fw_name, &priv->pci_dev->dev);
8453
8454         if (rc < 0) {
8455                 printk(KERN_ERR DRV_NAME ": "
8456                        "%s: Firmware '%s' not available or load failed.\n",
8457                        priv->net_dev->name, fw_name);
8458                 return rc;
8459         }
8460         IPW_DEBUG_INFO("firmware data %p size %zd\n", fw->fw_entry->data,
8461                            fw->fw_entry->size);
8462
8463         ipw2100_mod_firmware_load(fw);
8464
8465         return 0;
8466 }
8467
8468 static void ipw2100_release_firmware(struct ipw2100_priv *priv,
8469                                      struct ipw2100_fw *fw)
8470 {
8471         fw->version = 0;
8472         if (fw->fw_entry)
8473                 release_firmware(fw->fw_entry);
8474         fw->fw_entry = NULL;
8475 }
8476
8477
8478 static int ipw2100_get_fwversion(struct ipw2100_priv *priv, char *buf,
8479                                  size_t max)
8480 {
8481         char ver[MAX_FW_VERSION_LEN];
8482         u32 len = MAX_FW_VERSION_LEN;
8483         u32 tmp;
8484         int i;
8485         /* firmware version is an ascii string (max len of 14) */
8486         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_FW_VER_NUM,
8487                                 ver, &len))
8488                 return -EIO;
8489         tmp = max;
8490         if (len >= max)
8491                 len = max - 1;
8492         for (i = 0; i < len; i++)
8493                 buf[i] = ver[i];
8494         buf[i] = '\0';
8495         return tmp;
8496 }
8497
8498 static int ipw2100_get_ucodeversion(struct ipw2100_priv *priv, char *buf,
8499                                     size_t max)
8500 {
8501         u32 ver;
8502         u32 len = sizeof(ver);
8503         /* microcode version is a 32 bit integer */
8504         if (ipw2100_get_ordinal(priv, IPW_ORD_UCODE_VERSION,
8505                                 &ver, &len))
8506                 return -EIO;
8507         return snprintf(buf, max, "%08X", ver);
8508 }
8509
8510 /*
8511  * On exit, the firmware will have been freed from the fw list
8512  */
8513 static int ipw2100_fw_download(struct ipw2100_priv *priv,
8514                                struct ipw2100_fw *fw)
8515 {
8516         /* firmware is constructed of N contiguous entries, each entry is
8517          * structured as:
8518          *
8519          * offset    sie         desc
8520          * 0         4           address to write to
8521          * 4         2           length of data run
8522          * 6         length      data
8523          */
8524         unsigned int addr;
8525         unsigned short len;
8526
8527         const unsigned char *firmware_data = fw->fw.data;
8528         unsigned int firmware_data_left = fw->fw.size;
8529
8530         while (firmware_data_left > 0) {
8531                 addr = *(u32 *)(firmware_data);
8532                 firmware_data      += 4;
8533                 firmware_data_left -= 4;
8534
8535                 len = *(u16 *)(firmware_data);
8536                 firmware_data      += 2;
8537                 firmware_data_left -= 2;
8538
8539                 if (len > 32) {
8540                         printk(KERN_ERR DRV_NAME ": "
8541                                "Invalid firmware run-length of %d bytes\n",
8542                                len);
8543                         return -EINVAL;
8544                 }
8545
8546                 write_nic_memory(priv->net_dev, addr, len, firmware_data);
8547                 firmware_data      += len;
8548                 firmware_data_left -= len;
8549         }
8550
8551         return 0;
8552 }
8553
8554 struct symbol_alive_response {
8555         u8 cmd_id;
8556         u8 seq_num;
8557         u8 ucode_rev;
8558         u8 eeprom_valid;
8559         u16 valid_flags;
8560         u8 IEEE_addr[6];
8561         u16 flags;
8562         u16 pcb_rev;
8563         u16 clock_settle_time;  // 1us LSB
8564         u16 powerup_settle_time;        // 1us LSB
8565         u16 hop_settle_time;    // 1us LSB
8566         u8 date[3];             // month, day, year
8567         u8 time[2];             // hours, minutes
8568         u8 ucode_valid;
8569 };
8570
8571 static int ipw2100_ucode_download(struct ipw2100_priv *priv,
8572                                   struct ipw2100_fw *fw)
8573 {
8574         struct net_device *dev = priv->net_dev;
8575         const unsigned char *microcode_data = fw->uc.data;
8576         unsigned int microcode_data_left = fw->uc.size;
8577
8578         struct symbol_alive_response response;
8579         int i, j;
8580         u8 data;
8581
8582         /* Symbol control */
8583         write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8584         readl((void *)(dev->base_addr));
8585         write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8586         readl((void *)(dev->base_addr));
8587
8588         /* HW config */
8589         write_nic_byte(dev, 0x210014, 0x72);    /* fifo width =16 */
8590         readl((void *)(dev->base_addr));
8591         write_nic_byte(dev, 0x210014, 0x72);    /* fifo width =16 */
8592         readl((void *)(dev->base_addr));
8593
8594         /* EN_CS_ACCESS bit to reset control store pointer */
8595         write_nic_byte(dev, 0x210000, 0x40);
8596         readl((void *)(dev->base_addr));
8597         write_nic_byte(dev, 0x210000, 0x0);
8598         readl((void *)(dev->base_addr));
8599         write_nic_byte(dev, 0x210000, 0x40);
8600         readl((void *)(dev->base_addr));
8601
8602         /* copy microcode from buffer into Symbol */
8603
8604         while (microcode_data_left > 0) {
8605                 write_nic_byte(dev, 0x210010, *microcode_data++);
8606                 write_nic_byte(dev, 0x210010, *microcode_data++);
8607                 microcode_data_left -= 2;
8608         }
8609
8610         /* EN_CS_ACCESS bit to reset the control store pointer */
8611         write_nic_byte(dev, 0x210000, 0x0);
8612         readl((void *)(dev->base_addr));
8613
8614         /* Enable System (Reg 0)
8615          * first enable causes garbage in RX FIFO */
8616         write_nic_byte(dev, 0x210000, 0x0);
8617         readl((void *)(dev->base_addr));
8618         write_nic_byte(dev, 0x210000, 0x80);
8619         readl((void *)(dev->base_addr));
8620
8621         /* Reset External Baseband Reg */
8622         write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8623         readl((void *)(dev->base_addr));
8624         write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8625         readl((void *)(dev->base_addr));
8626
8627         /* HW Config (Reg 5) */
8628         write_nic_byte(dev, 0x210014, 0x72);    // fifo width =16
8629         readl((void *)(dev->base_addr));
8630         write_nic_byte(dev, 0x210014, 0x72);    // fifo width =16
8631         readl((void *)(dev->base_addr));
8632
8633         /* Enable System (Reg 0)
8634          * second enable should be OK */
8635         write_nic_byte(dev, 0x210000, 0x00);    // clear enable system
8636         readl((void *)(dev->base_addr));
8637         write_nic_byte(dev, 0x210000, 0x80);    // set enable system
8638
8639         /* check Symbol is enabled - upped this from 5 as it wasn't always
8640          * catching the update */
8641         for (i = 0; i < 10; i++) {
8642                 udelay(10);
8643
8644                 /* check Dino is enabled bit */
8645                 read_nic_byte(dev, 0x210000, &data);
8646                 if (data & 0x1)
8647                         break;
8648         }
8649
8650         if (i == 10) {
8651                 printk(KERN_ERR DRV_NAME ": %s: Error initializing Symbol\n",
8652                        dev->name);
8653                 return -EIO;
8654         }
8655
8656         /* Get Symbol alive response */
8657         for (i = 0; i < 30; i++) {
8658                 /* Read alive response structure */
8659                 for (j = 0;
8660                      j < (sizeof(struct symbol_alive_response) >> 1);
8661                      j++)
8662                         read_nic_word(dev, 0x210004,
8663                                       ((u16 *)&response) + j);
8664
8665                 if ((response.cmd_id == 1) &&
8666                     (response.ucode_valid == 0x1))
8667                         break;
8668                 udelay(10);
8669         }
8670
8671         if (i == 30) {
8672                 printk(KERN_ERR DRV_NAME ": %s: No response from Symbol - hw not alive\n",
8673                        dev->name);
8674                 printk_buf(IPW_DL_ERROR, (u8*)&response, sizeof(response));
8675                 return -EIO;
8676         }
8677
8678         return 0;
8679 }