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