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