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