iwlwifi: allow user to set max rate
[linux-2.6] / drivers / net / wireless / wl3501_cs.c
1 /*
2  * WL3501 Wireless LAN PCMCIA Card Driver for Linux
3  * Written originally for Linux 2.0.30 by Fox Chen, mhchen@golf.ccl.itri.org.tw
4  * Ported to 2.2, 2.4 & 2.5 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5  * Wireless extensions in 2.4 by Gustavo Niemeyer <niemeyer@conectiva.com>
6  *
7  * References used by Fox Chen while writing the original driver for 2.0.30:
8  *
9  *   1. WL24xx packet drivers (tooasm.asm)
10  *   2. Access Point Firmware Interface Specification for IEEE 802.11 SUTRO
11  *   3. IEEE 802.11
12  *   4. Linux network driver (/usr/src/linux/drivers/net)
13  *   5. ISA card driver - wl24.c
14  *   6. Linux PCMCIA skeleton driver - skeleton.c
15  *   7. Linux PCMCIA 3c589 network driver - 3c589_cs.c
16  *
17  * Tested with WL2400 firmware 1.2, Linux 2.0.30, and pcmcia-cs-2.9.12
18  *   1. Performance: about 165 Kbytes/sec in TCP/IP with Ad-Hoc mode.
19  *      rsh 192.168.1.3 "dd if=/dev/zero bs=1k count=1000" > /dev/null
20  *      (Specification 2M bits/sec. is about 250 Kbytes/sec., but we must deduct
21  *       ETHER/IP/UDP/TCP header, and acknowledgement overhead)
22  *
23  * Tested with Planet AP in 2.4.17, 184 Kbytes/s in UDP in Infrastructure mode,
24  * 173 Kbytes/s in TCP.
25  *
26  * Tested with Planet AP in 2.5.73-bk, 216 Kbytes/s in Infrastructure mode
27  * with a SMP machine (dual pentium 100), using pktgen, 432 pps (pkt_size = 60)
28  */
29
30 #include <linux/delay.h>
31 #include <linux/types.h>
32 #include <linux/ethtool.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/in.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/fcntl.h>
39 #include <linux/if_arp.h>
40 #include <linux/ioport.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/slab.h>
45 #include <linux/string.h>
46 #include <linux/wireless.h>
47 #include <linux/ieee80211.h>
48
49 #include <net/iw_handler.h>
50
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/cisreg.h>
55 #include <pcmcia/ds.h>
56
57 #include <asm/io.h>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
60
61 #include "wl3501.h"
62
63 #ifndef __i386__
64 #define slow_down_io()
65 #endif
66
67 /* For rough constant delay */
68 #define WL3501_NOPLOOP(n) { int x = 0; while (x++ < n) slow_down_io(); }
69
70 /*
71  * All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If you do not
72  * define PCMCIA_DEBUG at all, all the debug code will be left out.  If you
73  * compile with PCMCIA_DEBUG=0, the debug code will be present but disabled --
74  * but it can then be enabled for specific modules at load time with a
75  * 'pc_debug=#' option to insmod.
76  */
77 #define PCMCIA_DEBUG 0
78 #ifdef PCMCIA_DEBUG
79 static int pc_debug = PCMCIA_DEBUG;
80 module_param(pc_debug, int, 0);
81 #define dprintk(n, format, args...) \
82         { if (pc_debug > (n)) \
83                 printk(KERN_INFO "%s: " format "\n", __func__ , ##args); }
84 #else
85 #define dprintk(n, format, args...)
86 #endif
87
88 #define wl3501_outb(a, b) { outb(a, b); slow_down_io(); }
89 #define wl3501_outb_p(a, b) { outb_p(a, b); slow_down_io(); }
90 #define wl3501_outsb(a, b, c) { outsb(a, b, c); slow_down_io(); }
91
92 #define WL3501_RELEASE_TIMEOUT (25 * HZ)
93 #define WL3501_MAX_ADHOC_TRIES 16
94
95 #define WL3501_RESUME   0
96 #define WL3501_SUSPEND  1
97
98 /*
99  * The event() function is this driver's Card Services event handler.  It will
100  * be called by Card Services when an appropriate card status event is
101  * received. The config() and release() entry points are used to configure or
102  * release a socket, in response to card insertion and ejection events.  They
103  * are invoked from the wl24 event handler.
104  */
105 static int wl3501_config(struct pcmcia_device *link);
106 static void wl3501_release(struct pcmcia_device *link);
107
108 /*
109  * The dev_info variable is the "key" that is used to match up this
110  * device driver with appropriate cards, through the card configuration
111  * database.
112  */
113 static dev_info_t wl3501_dev_info = "wl3501_cs";
114
115 static const struct {
116         int reg_domain;
117         int min, max, deflt;
118 } iw_channel_table[] = {
119         {
120                 .reg_domain = IW_REG_DOMAIN_FCC,
121                 .min        = 1,
122                 .max        = 11,
123                 .deflt      = 1,
124         },
125         {
126                 .reg_domain = IW_REG_DOMAIN_DOC,
127                 .min        = 1,
128                 .max        = 11,
129                 .deflt      = 1,
130         },
131         {
132                 .reg_domain = IW_REG_DOMAIN_ETSI,
133                 .min        = 1,
134                 .max        = 13,
135                 .deflt      = 1,
136         },
137         {
138                 .reg_domain = IW_REG_DOMAIN_SPAIN,
139                 .min        = 10,
140                 .max        = 11,
141                 .deflt      = 10,
142         },
143         {
144                 .reg_domain = IW_REG_DOMAIN_FRANCE,
145                 .min        = 10,
146                 .max        = 13,
147                 .deflt      = 10,
148         },
149         {
150                 .reg_domain = IW_REG_DOMAIN_MKK,
151                 .min        = 14,
152                 .max        = 14,
153                 .deflt      = 14,
154         },
155         {
156                 .reg_domain = IW_REG_DOMAIN_MKK1,
157                 .min        = 1,
158                 .max        = 14,
159                 .deflt      = 1,
160         },
161         {
162                 .reg_domain = IW_REG_DOMAIN_ISRAEL,
163                 .min        = 3,
164                 .max        = 9,
165                 .deflt      = 9,
166         },
167 };
168
169 /**
170  * iw_valid_channel - validate channel in regulatory domain
171  * @reg_comain - regulatory domain
172  * @channel - channel to validate
173  *
174  * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
175  */
176 static int iw_valid_channel(int reg_domain, int channel)
177 {
178         int i, rc = 0;
179
180         for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
181                 if (reg_domain == iw_channel_table[i].reg_domain) {
182                         rc = channel >= iw_channel_table[i].min &&
183                              channel <= iw_channel_table[i].max;
184                         break;
185                 }
186         return rc;
187 }
188
189 /**
190  * iw_default_channel - get default channel for a regulatory domain
191  * @reg_comain - regulatory domain
192  *
193  * Returns the default channel for a regulatory domain
194  */
195 static int iw_default_channel(int reg_domain)
196 {
197         int i, rc = 1;
198
199         for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
200                 if (reg_domain == iw_channel_table[i].reg_domain) {
201                         rc = iw_channel_table[i].deflt;
202                         break;
203                 }
204         return rc;
205 }
206
207 static void iw_set_mgmt_info_element(enum iw_mgmt_info_element_ids id,
208                                      struct iw_mgmt_info_element *el,
209                                      void *value, int len)
210 {
211         el->id  = id;
212         el->len = len;
213         memcpy(el->data, value, len);
214 }
215
216 static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
217                                       struct iw_mgmt_info_element *from)
218 {
219         iw_set_mgmt_info_element(from->id, to, from->data, from->len);
220 }
221
222 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
223 {
224         wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
225 }
226
227 /*
228  * Get Ethernet MAC addresss.
229  *
230  * WARNING: We switch to FPAGE0 and switc back again.
231  *          Making sure there is no other WL function beening called by ISR.
232  */
233 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
234 {
235         int base_addr = this->base_addr;
236
237         /* get MAC addr */
238         wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
239         wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
240         wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
241
242         /* wait for reading EEPROM */
243         WL3501_NOPLOOP(100);
244         this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
245         WL3501_NOPLOOP(100);
246         this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
247         WL3501_NOPLOOP(100);
248         this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
249         WL3501_NOPLOOP(100);
250         this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
251         WL3501_NOPLOOP(100);
252         this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
253         WL3501_NOPLOOP(100);
254         this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
255         WL3501_NOPLOOP(100);
256         this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
257         WL3501_NOPLOOP(100);
258         wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
259         wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
260         wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
261         WL3501_NOPLOOP(100);
262         this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
263         WL3501_NOPLOOP(100);
264         this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
265         /* switch to SRAM Page 0 (for safety) */
266         wl3501_switch_page(this, WL3501_BSS_SPAGE0);
267
268         /* The MAC addr should be 00:60:... */
269         return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
270 }
271
272 /**
273  * wl3501_set_to_wla - Move 'size' bytes from PC to card
274  * @dest: Card addressing space
275  * @src: PC addressing space
276  * @size: Bytes to move
277  *
278  * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
279  */
280 static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
281                               int size)
282 {
283         /* switch to SRAM Page 0 */
284         wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
285                                                    WL3501_BSS_SPAGE0);
286         /* set LMAL and LMAH */
287         wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
288         wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
289
290         /* rep out to Port A */
291         wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
292 }
293
294 /**
295  * wl3501_get_from_wla - Move 'size' bytes from card to PC
296  * @src: Card addressing space
297  * @dest: PC addressing space
298  * @size: Bytes to move
299  *
300  * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
301  */
302 static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
303                                 int size)
304 {
305         /* switch to SRAM Page 0 */
306         wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
307                                                   WL3501_BSS_SPAGE0);
308         /* set LMAL and LMAH */
309         wl3501_outb(src & 0xff, this->base_addr + WL3501_NIC_LMAL);
310         wl3501_outb((src >> 8) & 0x7f, this->base_addr + WL3501_NIC_LMAH);
311
312         /* rep get from Port A */
313         insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
314 }
315
316 /*
317  * Get/Allocate a free Tx Data Buffer
318  *
319  *  *--------------*-----------------*----------------------------------*
320  *  |    PLCP      |    MAC Header   |  DST  SRC         Data ...       |
321  *  |  (24 bytes)  |    (30 bytes)   |  (6)  (6)  (Ethernet Row Data)   |
322  *  *--------------*-----------------*----------------------------------*
323  *  \               \- IEEE 802.11 -/ \-------------- len --------------/
324  *   \-struct wl3501_80211_tx_hdr--/   \-------- Ethernet Frame -------/
325  *
326  * Return = Postion in Card
327  */
328 static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
329 {
330         u16 next, blk_cnt = 0, zero = 0;
331         u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
332         u16 ret = 0;
333
334         if (full_len > this->tx_buffer_cnt * 254)
335                 goto out;
336         ret = this->tx_buffer_head;
337         while (full_len) {
338                 if (full_len < 254)
339                         full_len = 0;
340                 else
341                         full_len -= 254;
342                 wl3501_get_from_wla(this, this->tx_buffer_head, &next,
343                                     sizeof(next));
344                 if (!full_len)
345                         wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
346                                           sizeof(zero));
347                 this->tx_buffer_head = next;
348                 blk_cnt++;
349                 /* if buffer is not enough */
350                 if (!next && full_len) {
351                         this->tx_buffer_head = ret;
352                         ret = 0;
353                         goto out;
354                 }
355         }
356         this->tx_buffer_cnt -= blk_cnt;
357 out:
358         return ret;
359 }
360
361 /*
362  * Free an allocated Tx Buffer. ptr must be correct position.
363  */
364 static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
365 {
366         /* check if all space is not free */
367         if (!this->tx_buffer_head)
368                 this->tx_buffer_head = ptr;
369         else
370                 wl3501_set_to_wla(this, this->tx_buffer_tail,
371                                   &ptr, sizeof(ptr));
372         while (ptr) {
373                 u16 next;
374
375                 this->tx_buffer_cnt++;
376                 wl3501_get_from_wla(this, ptr, &next, sizeof(next));
377                 this->tx_buffer_tail = ptr;
378                 ptr = next;
379         }
380 }
381
382 static int wl3501_esbq_req_test(struct wl3501_card *this)
383 {
384         u8 tmp;
385
386         wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
387         return tmp & 0x80;
388 }
389
390 static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
391 {
392         u16 tmp = 0;
393
394         wl3501_set_to_wla(this, this->esbq_req_head, ptr, 2);
395         wl3501_set_to_wla(this, this->esbq_req_head + 2, &tmp, sizeof(tmp));
396         this->esbq_req_head += 4;
397         if (this->esbq_req_head >= this->esbq_req_end)
398                 this->esbq_req_head = this->esbq_req_start;
399 }
400
401 static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
402 {
403         int rc = -EIO;
404
405         if (wl3501_esbq_req_test(this)) {
406                 u16 ptr = wl3501_get_tx_buffer(this, sig_size);
407                 if (ptr) {
408                         wl3501_set_to_wla(this, ptr, sig, sig_size);
409                         wl3501_esbq_req(this, &ptr);
410                         rc = 0;
411                 }
412         }
413         return rc;
414 }
415
416 static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
417                                 void *bf, int size)
418 {
419         struct wl3501_get_req sig = {
420                 .sig_id     = WL3501_SIG_GET_REQ,
421                 .mib_attrib = index,
422         };
423         unsigned long flags;
424         int rc = -EIO;
425
426         spin_lock_irqsave(&this->lock, flags);
427         if (wl3501_esbq_req_test(this)) {
428                 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
429                 if (ptr) {
430                         wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
431                         wl3501_esbq_req(this, &ptr);
432                         this->sig_get_confirm.mib_status = 255;
433                         spin_unlock_irqrestore(&this->lock, flags);
434                         rc = wait_event_interruptible(this->wait,
435                                 this->sig_get_confirm.mib_status != 255);
436                         if (!rc)
437                                 memcpy(bf, this->sig_get_confirm.mib_value,
438                                        size);
439                         goto out;
440                 }
441         }
442         spin_unlock_irqrestore(&this->lock, flags);
443 out:
444         return rc;
445 }
446
447 static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
448 {
449         struct wl3501_pwr_mgmt_req sig = {
450                 .sig_id         = WL3501_SIG_PWR_MGMT_REQ,
451                 .pwr_save       = suspend,
452                 .wake_up        = !suspend,
453                 .receive_dtims  = 10,
454         };
455         unsigned long flags;
456         int rc = -EIO;
457
458         spin_lock_irqsave(&this->lock, flags);
459         if (wl3501_esbq_req_test(this)) {
460                 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
461                 if (ptr) {
462                         wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
463                         wl3501_esbq_req(this, &ptr);
464                         this->sig_pwr_mgmt_confirm.status = 255;
465                         spin_unlock_irqrestore(&this->lock, flags);
466                         rc = wait_event_interruptible(this->wait,
467                                 this->sig_pwr_mgmt_confirm.status != 255);
468                         printk(KERN_INFO "%s: %s status=%d\n", __func__,
469                                suspend ? "suspend" : "resume",
470                                this->sig_pwr_mgmt_confirm.status);
471                         goto out;
472                 }
473         }
474         spin_unlock_irqrestore(&this->lock, flags);
475 out:
476         return rc;
477 }
478
479 /**
480  * wl3501_send_pkt - Send a packet.
481  * @this - card
482  *
483  * Send a packet.
484  *
485  * data = Ethernet raw frame.  (e.g. data[0] - data[5] is Dest MAC Addr,
486  *                                   data[6] - data[11] is Src MAC Addr)
487  * Ref: IEEE 802.11
488  */
489 static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
490 {
491         u16 bf, sig_bf, next, tmplen, pktlen;
492         struct wl3501_md_req sig = {
493                 .sig_id = WL3501_SIG_MD_REQ,
494         };
495         u8 *pdata = (char *)data;
496         int rc = -EIO;
497
498         if (wl3501_esbq_req_test(this)) {
499                 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
500                 rc = -ENOMEM;
501                 if (!sig_bf)    /* No free buffer available */
502                         goto out;
503                 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
504                 if (!bf) {
505                         /* No free buffer available */
506                         wl3501_free_tx_buffer(this, sig_bf);
507                         goto out;
508                 }
509                 rc = 0;
510                 memcpy(&sig.daddr[0], pdata, 12);
511                 pktlen = len - 12;
512                 pdata += 12;
513                 sig.data = bf;
514                 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
515                         u8 addr4[ETH_ALEN] = {
516                                 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
517                         };
518
519                         wl3501_set_to_wla(this, bf + 2 +
520                                           offsetof(struct wl3501_tx_hdr, addr4),
521                                           addr4, sizeof(addr4));
522                         sig.size = pktlen + 24 + 4 + 6;
523                         if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
524                                 tmplen = 254 - sizeof(struct wl3501_tx_hdr);
525                                 pktlen -= tmplen;
526                         } else {
527                                 tmplen = pktlen;
528                                 pktlen = 0;
529                         }
530                         wl3501_set_to_wla(this,
531                                           bf + 2 + sizeof(struct wl3501_tx_hdr),
532                                           pdata, tmplen);
533                         pdata += tmplen;
534                         wl3501_get_from_wla(this, bf, &next, sizeof(next));
535                         bf = next;
536                 } else {
537                         sig.size = pktlen + 24 + 4 - 2;
538                         pdata += 2;
539                         pktlen -= 2;
540                         if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
541                                 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
542                                 pktlen -= tmplen;
543                         } else {
544                                 tmplen = pktlen;
545                                 pktlen = 0;
546                         }
547                         wl3501_set_to_wla(this, bf + 2 +
548                                           offsetof(struct wl3501_tx_hdr, addr4),
549                                           pdata, tmplen);
550                         pdata += tmplen;
551                         wl3501_get_from_wla(this, bf, &next, sizeof(next));
552                         bf = next;
553                 }
554                 while (pktlen > 0) {
555                         if (pktlen > 254) {
556                                 tmplen = 254;
557                                 pktlen -= 254;
558                         } else {
559                                 tmplen = pktlen;
560                                 pktlen = 0;
561                         }
562                         wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
563                         pdata += tmplen;
564                         wl3501_get_from_wla(this, bf, &next, sizeof(next));
565                         bf = next;
566                 }
567                 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
568                 wl3501_esbq_req(this, &sig_bf);
569         }
570 out:
571         return rc;
572 }
573
574 static int wl3501_mgmt_resync(struct wl3501_card *this)
575 {
576         struct wl3501_resync_req sig = {
577                 .sig_id = WL3501_SIG_RESYNC_REQ,
578         };
579
580         return wl3501_esbq_exec(this, &sig, sizeof(sig));
581 }
582
583 static inline int wl3501_fw_bss_type(struct wl3501_card *this)
584 {
585         return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
586                                                  WL3501_NET_TYPE_ADHOC;
587 }
588
589 static inline int wl3501_fw_cap_info(struct wl3501_card *this)
590 {
591         return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
592                                                  WL3501_MGMT_CAPABILITY_IBSS;
593 }
594
595 static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
596 {
597         struct wl3501_scan_req sig = {
598                 .sig_id         = WL3501_SIG_SCAN_REQ,
599                 .scan_type      = WL3501_SCAN_TYPE_ACTIVE,
600                 .probe_delay    = 0x10,
601                 .min_chan_time  = chan_time,
602                 .max_chan_time  = chan_time,
603                 .bss_type       = wl3501_fw_bss_type(this),
604         };
605
606         this->bss_cnt = this->join_sta_bss = 0;
607         return wl3501_esbq_exec(this, &sig, sizeof(sig));
608 }
609
610 static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
611 {
612         struct wl3501_join_req sig = {
613                 .sig_id           = WL3501_SIG_JOIN_REQ,
614                 .timeout          = 10,
615                 .ds_pset = {
616                         .el = {
617                                 .id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
618                                 .len = 1,
619                         },
620                         .chan   = this->chan,
621                 },
622         };
623
624         memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
625         return wl3501_esbq_exec(this, &sig, sizeof(sig));
626 }
627
628 static int wl3501_mgmt_start(struct wl3501_card *this)
629 {
630         struct wl3501_start_req sig = {
631                 .sig_id                 = WL3501_SIG_START_REQ,
632                 .beacon_period          = 400,
633                 .dtim_period            = 1,
634                 .ds_pset = {
635                         .el = {
636                                 .id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
637                                 .len = 1,
638                         },
639                         .chan   = this->chan,
640                 },
641                 .bss_basic_rset = {
642                         .el = {
643                                 .id     = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
644                                 .len = 2,
645                         },
646                         .data_rate_labels = {
647                                 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
648                                       IW_MGMT_RATE_LABEL_1MBIT,
649                                 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
650                                       IW_MGMT_RATE_LABEL_2MBIT,
651                         },
652                 },
653                 .operational_rset       = {
654                         .el = {
655                                 .id     = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
656                                 .len = 2,
657                         },
658                         .data_rate_labels = {
659                                 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
660                                       IW_MGMT_RATE_LABEL_1MBIT,
661                                 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
662                                       IW_MGMT_RATE_LABEL_2MBIT,
663                         },
664                 },
665                 .ibss_pset              = {
666                         .el = {
667                                 .id      = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
668                                 .len     = 2,
669                         },
670                         .atim_window = 10,
671                 },
672                 .bss_type               = wl3501_fw_bss_type(this),
673                 .cap_info               = wl3501_fw_cap_info(this),
674         };
675
676         iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
677         iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
678         return wl3501_esbq_exec(this, &sig, sizeof(sig));
679 }
680
681 static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
682 {
683         u16 i = 0;
684         int matchflag = 0;
685         struct wl3501_scan_confirm sig;
686
687         dprintk(3, "entry");
688         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
689         if (sig.status == WL3501_STATUS_SUCCESS) {
690                 dprintk(3, "success");
691                 if ((this->net_type == IW_MODE_INFRA &&
692                      (sig.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
693                     (this->net_type == IW_MODE_ADHOC &&
694                      (sig.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
695                     this->net_type == IW_MODE_AUTO) {
696                         if (!this->essid.el.len)
697                                 matchflag = 1;
698                         else if (this->essid.el.len == 3 &&
699                                  !memcmp(this->essid.essid, "ANY", 3))
700                                 matchflag = 1;
701                         else if (this->essid.el.len != sig.ssid.el.len)
702                                 matchflag = 0;
703                         else if (memcmp(this->essid.essid, sig.ssid.essid,
704                                         this->essid.el.len))
705                                 matchflag = 0;
706                         else
707                                 matchflag = 1;
708                         if (matchflag) {
709                                 for (i = 0; i < this->bss_cnt; i++) {
710                                         if (!memcmp(this->bss_set[i].bssid,
711                                                     sig.bssid, ETH_ALEN)) {
712                                                 matchflag = 0;
713                                                 break;
714                                         }
715                                 }
716                         }
717                         if (matchflag && (i < 20)) {
718                                 memcpy(&this->bss_set[i].beacon_period,
719                                        &sig.beacon_period, 73);
720                                 this->bss_cnt++;
721                                 this->rssi = sig.rssi;
722                         }
723                 }
724         } else if (sig.status == WL3501_STATUS_TIMEOUT) {
725                 dprintk(3, "timeout");
726                 this->join_sta_bss = 0;
727                 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
728                         if (!wl3501_mgmt_join(this, i))
729                                 break;
730                 this->join_sta_bss = i;
731                 if (this->join_sta_bss == this->bss_cnt) {
732                         if (this->net_type == IW_MODE_INFRA)
733                                 wl3501_mgmt_scan(this, 100);
734                         else {
735                                 this->adhoc_times++;
736                                 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
737                                         wl3501_mgmt_start(this);
738                                 else
739                                         wl3501_mgmt_scan(this, 100);
740                         }
741                 }
742         }
743 }
744
745 /**
746  * wl3501_block_interrupt - Mask interrupt from SUTRO
747  * @this - card
748  *
749  * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
750  * Return: 1 if interrupt is originally enabled
751  */
752 static int wl3501_block_interrupt(struct wl3501_card *this)
753 {
754         u8 old = inb(this->base_addr + WL3501_NIC_GCR);
755         u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
756                         WL3501_GCR_ENECINT));
757
758         wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
759         return old & WL3501_GCR_ENECINT;
760 }
761
762 /**
763  * wl3501_unblock_interrupt - Enable interrupt from SUTRO
764  * @this - card
765  *
766  * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
767  * Return: 1 if interrupt is originally enabled
768  */
769 static int wl3501_unblock_interrupt(struct wl3501_card *this)
770 {
771         u8 old = inb(this->base_addr + WL3501_NIC_GCR);
772         u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
773                   WL3501_GCR_ENECINT;
774
775         wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
776         return old & WL3501_GCR_ENECINT;
777 }
778
779 /**
780  * wl3501_receive - Receive data from Receive Queue.
781  *
782  * Receive data from Receive Queue.
783  *
784  * @this: card
785  * @bf: address of host
786  * @size: size of buffer.
787  */
788 static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
789 {
790         u16 next_addr, next_addr1;
791         u8 *data = bf + 12;
792
793         size -= 12;
794         wl3501_get_from_wla(this, this->start_seg + 2,
795                             &next_addr, sizeof(next_addr));
796         if (size > WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr)) {
797                 wl3501_get_from_wla(this,
798                                     this->start_seg +
799                                         sizeof(struct wl3501_rx_hdr), data,
800                                     WL3501_BLKSZ -
801                                         sizeof(struct wl3501_rx_hdr));
802                 size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
803                 data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
804         } else {
805                 wl3501_get_from_wla(this,
806                                     this->start_seg +
807                                         sizeof(struct wl3501_rx_hdr),
808                                     data, size);
809                 size = 0;
810         }
811         while (size > 0) {
812                 if (size > WL3501_BLKSZ - 5) {
813                         wl3501_get_from_wla(this, next_addr + 5, data,
814                                             WL3501_BLKSZ - 5);
815                         size -= WL3501_BLKSZ - 5;
816                         data += WL3501_BLKSZ - 5;
817                         wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
818                                             sizeof(next_addr1));
819                         next_addr = next_addr1;
820                 } else {
821                         wl3501_get_from_wla(this, next_addr + 5, data, size);
822                         size = 0;
823                 }
824         }
825         return 0;
826 }
827
828 static void wl3501_esbq_req_free(struct wl3501_card *this)
829 {
830         u8 tmp;
831         u16 addr;
832
833         if (this->esbq_req_head == this->esbq_req_tail)
834                 goto out;
835         wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
836         if (!(tmp & 0x80))
837                 goto out;
838         wl3501_get_from_wla(this, this->esbq_req_tail, &addr, sizeof(addr));
839         wl3501_free_tx_buffer(this, addr);
840         this->esbq_req_tail += 4;
841         if (this->esbq_req_tail >= this->esbq_req_end)
842                 this->esbq_req_tail = this->esbq_req_start;
843 out:
844         return;
845 }
846
847 static int wl3501_esbq_confirm(struct wl3501_card *this)
848 {
849         u8 tmp;
850
851         wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
852         return tmp & 0x80;
853 }
854
855 static void wl3501_online(struct net_device *dev)
856 {
857         struct wl3501_card *this = netdev_priv(dev);
858
859         printk(KERN_INFO "%s: Wireless LAN online. BSSID: %pM\n",
860                dev->name, this->bssid);
861         netif_wake_queue(dev);
862 }
863
864 static void wl3501_esbq_confirm_done(struct wl3501_card *this)
865 {
866         u8 tmp = 0;
867
868         wl3501_set_to_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
869         this->esbq_confirm += 4;
870         if (this->esbq_confirm >= this->esbq_confirm_end)
871                 this->esbq_confirm = this->esbq_confirm_start;
872 }
873
874 static int wl3501_mgmt_auth(struct wl3501_card *this)
875 {
876         struct wl3501_auth_req sig = {
877                 .sig_id  = WL3501_SIG_AUTH_REQ,
878                 .type    = WL3501_SYS_TYPE_OPEN,
879                 .timeout = 1000,
880         };
881
882         dprintk(3, "entry");
883         memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
884         return wl3501_esbq_exec(this, &sig, sizeof(sig));
885 }
886
887 static int wl3501_mgmt_association(struct wl3501_card *this)
888 {
889         struct wl3501_assoc_req sig = {
890                 .sig_id          = WL3501_SIG_ASSOC_REQ,
891                 .timeout         = 1000,
892                 .listen_interval = 5,
893                 .cap_info        = this->cap_info,
894         };
895
896         dprintk(3, "entry");
897         memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
898         return wl3501_esbq_exec(this, &sig, sizeof(sig));
899 }
900
901 static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
902 {
903         struct wl3501_card *this = netdev_priv(dev);
904         struct wl3501_join_confirm sig;
905
906         dprintk(3, "entry");
907         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
908         if (sig.status == WL3501_STATUS_SUCCESS) {
909                 if (this->net_type == IW_MODE_INFRA) {
910                         if (this->join_sta_bss < this->bss_cnt) {
911                                 const int i = this->join_sta_bss;
912                                 memcpy(this->bssid,
913                                        this->bss_set[i].bssid, ETH_ALEN);
914                                 this->chan = this->bss_set[i].ds_pset.chan;
915                                 iw_copy_mgmt_info_element(&this->keep_essid.el,
916                                                      &this->bss_set[i].ssid.el);
917                                 wl3501_mgmt_auth(this);
918                         }
919                 } else {
920                         const int i = this->join_sta_bss;
921
922                         memcpy(&this->bssid, &this->bss_set[i].bssid, ETH_ALEN);
923                         this->chan = this->bss_set[i].ds_pset.chan;
924                         iw_copy_mgmt_info_element(&this->keep_essid.el,
925                                                   &this->bss_set[i].ssid.el);
926                         wl3501_online(dev);
927                 }
928         } else {
929                 int i;
930                 this->join_sta_bss++;
931                 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
932                         if (!wl3501_mgmt_join(this, i))
933                                 break;
934                 this->join_sta_bss = i;
935                 if (this->join_sta_bss == this->bss_cnt) {
936                         if (this->net_type == IW_MODE_INFRA)
937                                 wl3501_mgmt_scan(this, 100);
938                         else {
939                                 this->adhoc_times++;
940                                 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
941                                         wl3501_mgmt_start(this);
942                                 else
943                                         wl3501_mgmt_scan(this, 100);
944                         }
945                 }
946         }
947 }
948
949 static inline void wl3501_alarm_interrupt(struct net_device *dev,
950                                           struct wl3501_card *this)
951 {
952         if (this->net_type == IW_MODE_INFRA) {
953                 printk(KERN_INFO "Wireless LAN offline\n");
954                 netif_stop_queue(dev);
955                 wl3501_mgmt_resync(this);
956         }
957 }
958
959 static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
960                                                struct wl3501_card *this,
961                                                u16 addr)
962 {
963         struct wl3501_md_confirm sig;
964
965         dprintk(3, "entry");
966         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
967         wl3501_free_tx_buffer(this, sig.data);
968         if (netif_queue_stopped(dev))
969                 netif_wake_queue(dev);
970 }
971
972 static inline void wl3501_md_ind_interrupt(struct net_device *dev,
973                                            struct wl3501_card *this, u16 addr)
974 {
975         struct wl3501_md_ind sig;
976         struct sk_buff *skb;
977         u8 rssi, addr4[ETH_ALEN];
978         u16 pkt_len;
979
980         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
981         this->start_seg = sig.data;
982         wl3501_get_from_wla(this,
983                             sig.data + offsetof(struct wl3501_rx_hdr, rssi),
984                             &rssi, sizeof(rssi));
985         this->rssi = rssi <= 63 ? (rssi * 100) / 64 : 255;
986
987         wl3501_get_from_wla(this,
988                             sig.data +
989                                 offsetof(struct wl3501_rx_hdr, addr4),
990                             &addr4, sizeof(addr4));
991         if (!(addr4[0] == 0xAA && addr4[1] == 0xAA &&
992               addr4[2] == 0x03 && addr4[4] == 0x00)) {
993                 printk(KERN_INFO "Insupported packet type!\n");
994                 return;
995         }
996         pkt_len = sig.size + 12 - 24 - 4 - 6;
997
998         skb = dev_alloc_skb(pkt_len + 5);
999
1000         if (!skb) {
1001                 printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
1002                        dev->name, pkt_len);
1003                 this->stats.rx_dropped++;
1004         } else {
1005                 skb->dev = dev;
1006                 skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
1007                 skb_copy_to_linear_data(skb, (unsigned char *)&sig.daddr, 12);
1008                 wl3501_receive(this, skb->data, pkt_len);
1009                 skb_put(skb, pkt_len);
1010                 skb->protocol   = eth_type_trans(skb, dev);
1011                 this->stats.rx_packets++;
1012                 this->stats.rx_bytes += skb->len;
1013                 netif_rx(skb);
1014         }
1015 }
1016
1017 static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
1018                                                 u16 addr, void *sig, int size)
1019 {
1020         dprintk(3, "entry");
1021         wl3501_get_from_wla(this, addr, &this->sig_get_confirm,
1022                             sizeof(this->sig_get_confirm));
1023         wake_up(&this->wait);
1024 }
1025
1026 static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
1027                                                   struct wl3501_card *this,
1028                                                   u16 addr)
1029 {
1030         struct wl3501_start_confirm sig;
1031
1032         dprintk(3, "entry");
1033         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1034         if (sig.status == WL3501_STATUS_SUCCESS)
1035                 netif_wake_queue(dev);
1036 }
1037
1038 static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
1039                                                   u16 addr)
1040 {
1041         struct wl3501_card *this = netdev_priv(dev);
1042         struct wl3501_assoc_confirm sig;
1043
1044         dprintk(3, "entry");
1045         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1046
1047         if (sig.status == WL3501_STATUS_SUCCESS)
1048                 wl3501_online(dev);
1049 }
1050
1051 static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
1052                                                  u16 addr)
1053 {
1054         struct wl3501_auth_confirm sig;
1055
1056         dprintk(3, "entry");
1057         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1058
1059         if (sig.status == WL3501_STATUS_SUCCESS)
1060                 wl3501_mgmt_association(this);
1061         else
1062                 wl3501_mgmt_resync(this);
1063 }
1064
1065 static inline void wl3501_rx_interrupt(struct net_device *dev)
1066 {
1067         int morepkts;
1068         u16 addr;
1069         u8 sig_id;
1070         struct wl3501_card *this = netdev_priv(dev);
1071
1072         dprintk(3, "entry");
1073 loop:
1074         morepkts = 0;
1075         if (!wl3501_esbq_confirm(this))
1076                 goto free;
1077         wl3501_get_from_wla(this, this->esbq_confirm, &addr, sizeof(addr));
1078         wl3501_get_from_wla(this, addr + 2, &sig_id, sizeof(sig_id));
1079
1080         switch (sig_id) {
1081         case WL3501_SIG_DEAUTH_IND:
1082         case WL3501_SIG_DISASSOC_IND:
1083         case WL3501_SIG_ALARM:
1084                 wl3501_alarm_interrupt(dev, this);
1085                 break;
1086         case WL3501_SIG_MD_CONFIRM:
1087                 wl3501_md_confirm_interrupt(dev, this, addr);
1088                 break;
1089         case WL3501_SIG_MD_IND:
1090                 wl3501_md_ind_interrupt(dev, this, addr);
1091                 break;
1092         case WL3501_SIG_GET_CONFIRM:
1093                 wl3501_get_confirm_interrupt(this, addr,
1094                                              &this->sig_get_confirm,
1095                                              sizeof(this->sig_get_confirm));
1096                 break;
1097         case WL3501_SIG_PWR_MGMT_CONFIRM:
1098                 wl3501_get_confirm_interrupt(this, addr,
1099                                              &this->sig_pwr_mgmt_confirm,
1100                                             sizeof(this->sig_pwr_mgmt_confirm));
1101                 break;
1102         case WL3501_SIG_START_CONFIRM:
1103                 wl3501_start_confirm_interrupt(dev, this, addr);
1104                 break;
1105         case WL3501_SIG_SCAN_CONFIRM:
1106                 wl3501_mgmt_scan_confirm(this, addr);
1107                 break;
1108         case WL3501_SIG_JOIN_CONFIRM:
1109                 wl3501_mgmt_join_confirm(dev, addr);
1110                 break;
1111         case WL3501_SIG_ASSOC_CONFIRM:
1112                 wl3501_assoc_confirm_interrupt(dev, addr);
1113                 break;
1114         case WL3501_SIG_AUTH_CONFIRM:
1115                 wl3501_auth_confirm_interrupt(this, addr);
1116                 break;
1117         case WL3501_SIG_RESYNC_CONFIRM:
1118                 wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
1119                 break;
1120         }
1121         wl3501_esbq_confirm_done(this);
1122         morepkts = 1;
1123         /* free request if necessary */
1124 free:
1125         wl3501_esbq_req_free(this);
1126         if (morepkts)
1127                 goto loop;
1128 }
1129
1130 static inline void wl3501_ack_interrupt(struct wl3501_card *this)
1131 {
1132         wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
1133 }
1134
1135 /**
1136  * wl3501_interrupt - Hardware interrupt from card.
1137  * @irq - Interrupt number
1138  * @dev_id - net_device
1139  *
1140  * We must acknowledge the interrupt as soon as possible, and block the
1141  * interrupt from the same card immediately to prevent re-entry.
1142  *
1143  * Before accessing the Control_Status_Block, we must lock SUTRO first.
1144  * On the other hand, to prevent SUTRO from malfunctioning, we must
1145  * unlock the SUTRO as soon as possible.
1146  */
1147 static irqreturn_t wl3501_interrupt(int irq, void *dev_id)
1148 {
1149         struct net_device *dev = dev_id;
1150         struct wl3501_card *this;
1151
1152         this = netdev_priv(dev);
1153         spin_lock(&this->lock);
1154         wl3501_ack_interrupt(this);
1155         wl3501_block_interrupt(this);
1156         wl3501_rx_interrupt(dev);
1157         wl3501_unblock_interrupt(this);
1158         spin_unlock(&this->lock);
1159
1160         return IRQ_HANDLED;
1161 }
1162
1163 static int wl3501_reset_board(struct wl3501_card *this)
1164 {
1165         u8 tmp = 0;
1166         int i, rc = 0;
1167
1168         /* Coreset */
1169         wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1170         wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1171         wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1172
1173         /* Reset SRAM 0x480 to zero */
1174         wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1175
1176         /* Start up */
1177         wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1178
1179         WL3501_NOPLOOP(1024 * 50);
1180
1181         wl3501_unblock_interrupt(this); /* acme: was commented */
1182
1183         /* Polling Self_Test_Status */
1184         for (i = 0; i < 10000; i++) {
1185                 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1186
1187                 if (tmp == 'W') {
1188                         /* firmware complete all test successfully */
1189                         tmp = 'A';
1190                         wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1191                         goto out;
1192                 }
1193                 WL3501_NOPLOOP(10);
1194         }
1195         printk(KERN_WARNING "%s: failed to reset the board!\n", __func__);
1196         rc = -ENODEV;
1197 out:
1198         return rc;
1199 }
1200
1201 static int wl3501_init_firmware(struct wl3501_card *this)
1202 {
1203         u16 ptr, next;
1204         int rc = wl3501_reset_board(this);
1205
1206         if (rc)
1207                 goto fail;
1208         this->card_name[0] = '\0';
1209         wl3501_get_from_wla(this, 0x1a00,
1210                             this->card_name, sizeof(this->card_name));
1211         this->card_name[sizeof(this->card_name) - 1] = '\0';
1212         this->firmware_date[0] = '\0';
1213         wl3501_get_from_wla(this, 0x1a40,
1214                             this->firmware_date, sizeof(this->firmware_date));
1215         this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
1216         /* Switch to SRAM Page 0 */
1217         wl3501_switch_page(this, WL3501_BSS_SPAGE0);
1218         /* Read parameter from card */
1219         wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
1220         wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
1221         wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
1222         wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
1223         wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
1224         wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
1225         this->esbq_req_tail     = this->esbq_req_head = this->esbq_req_start;
1226         this->esbq_req_end     += this->esbq_req_start;
1227         this->esbq_confirm      = this->esbq_confirm_start;
1228         this->esbq_confirm_end += this->esbq_confirm_start;
1229         /* Initial Tx Buffer */
1230         this->tx_buffer_cnt = 1;
1231         ptr = this->tx_buffer_head;
1232         next = ptr + WL3501_BLKSZ;
1233         while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
1234                 this->tx_buffer_cnt++;
1235                 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1236                 ptr = next;
1237                 next = ptr + WL3501_BLKSZ;
1238         }
1239         rc = 0;
1240         next = 0;
1241         wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1242         this->tx_buffer_tail = ptr;
1243 out:
1244         return rc;
1245 fail:
1246         printk(KERN_WARNING "%s: failed!\n", __func__);
1247         goto out;
1248 }
1249
1250 static int wl3501_close(struct net_device *dev)
1251 {
1252         struct wl3501_card *this = netdev_priv(dev);
1253         int rc = -ENODEV;
1254         unsigned long flags;
1255         struct pcmcia_device *link;
1256         link = this->p_dev;
1257
1258         spin_lock_irqsave(&this->lock, flags);
1259         link->open--;
1260
1261         /* Stop wl3501_hard_start_xmit() from now on */
1262         netif_stop_queue(dev);
1263         wl3501_ack_interrupt(this);
1264
1265         /* Mask interrupts from the SUTRO */
1266         wl3501_block_interrupt(this);
1267
1268         rc = 0;
1269         printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1270         spin_unlock_irqrestore(&this->lock, flags);
1271         return rc;
1272 }
1273
1274 /**
1275  * wl3501_reset - Reset the SUTRO.
1276  * @dev - network device
1277  *
1278  * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1279  * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1280  * is running. It seems to be dangerous.
1281  */
1282 static int wl3501_reset(struct net_device *dev)
1283 {
1284         struct wl3501_card *this = netdev_priv(dev);
1285         int rc = -ENODEV;
1286
1287         wl3501_block_interrupt(this);
1288
1289         if (wl3501_init_firmware(this)) {
1290                 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1291                        dev->name);
1292                 /* Free IRQ, and mark IRQ as unused */
1293                 free_irq(dev->irq, dev);
1294                 goto out;
1295         }
1296
1297         /*
1298          * Queue has to be started only when the Card is Started
1299          */
1300         netif_stop_queue(dev);
1301         this->adhoc_times = 0;
1302         wl3501_ack_interrupt(this);
1303         wl3501_unblock_interrupt(this);
1304         wl3501_mgmt_scan(this, 100);
1305         dprintk(1, "%s: device reset", dev->name);
1306         rc = 0;
1307 out:
1308         return rc;
1309 }
1310
1311 static void wl3501_tx_timeout(struct net_device *dev)
1312 {
1313         struct wl3501_card *this = netdev_priv(dev);
1314         struct net_device_stats *stats = &this->stats;
1315         unsigned long flags;
1316         int rc;
1317
1318         stats->tx_errors++;
1319         spin_lock_irqsave(&this->lock, flags);
1320         rc = wl3501_reset(dev);
1321         spin_unlock_irqrestore(&this->lock, flags);
1322         if (rc)
1323                 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1324                        dev->name, rc);
1325         else {
1326                 dev->trans_start = jiffies;
1327                 netif_wake_queue(dev);
1328         }
1329 }
1330
1331 /*
1332  * Return : 0 - OK
1333  *          1 - Could not transmit (dev_queue_xmit will queue it)
1334  *              and try to sent it later
1335  */
1336 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1337 {
1338         int enabled, rc;
1339         struct wl3501_card *this = netdev_priv(dev);
1340         unsigned long flags;
1341
1342         spin_lock_irqsave(&this->lock, flags);
1343         enabled = wl3501_block_interrupt(this);
1344         dev->trans_start = jiffies;
1345         rc = wl3501_send_pkt(this, skb->data, skb->len);
1346         if (enabled)
1347                 wl3501_unblock_interrupt(this);
1348         if (rc) {
1349                 ++this->stats.tx_dropped;
1350                 netif_stop_queue(dev);
1351         } else {
1352                 ++this->stats.tx_packets;
1353                 this->stats.tx_bytes += skb->len;
1354                 kfree_skb(skb);
1355
1356                 if (this->tx_buffer_cnt < 2)
1357                         netif_stop_queue(dev);
1358         }
1359         spin_unlock_irqrestore(&this->lock, flags);
1360         return rc;
1361 }
1362
1363 static int wl3501_open(struct net_device *dev)
1364 {
1365         int rc = -ENODEV;
1366         struct wl3501_card *this = netdev_priv(dev);
1367         unsigned long flags;
1368         struct pcmcia_device *link;
1369         link = this->p_dev;
1370
1371         spin_lock_irqsave(&this->lock, flags);
1372         if (!pcmcia_dev_present(link))
1373                 goto out;
1374         netif_device_attach(dev);
1375         link->open++;
1376
1377         /* Initial WL3501 firmware */
1378         dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1379         if (wl3501_init_firmware(this))
1380                 goto fail;
1381         /* Initial device variables */
1382         this->adhoc_times = 0;
1383         /* Acknowledge Interrupt, for cleaning last state */
1384         wl3501_ack_interrupt(this);
1385
1386         /* Enable interrupt from card after all */
1387         wl3501_unblock_interrupt(this);
1388         wl3501_mgmt_scan(this, 100);
1389         rc = 0;
1390         dprintk(1, "%s: WL3501 opened", dev->name);
1391         printk(KERN_INFO "%s: Card Name: %s\n"
1392                          "%s: Firmware Date: %s\n",
1393                          dev->name, this->card_name,
1394                          dev->name, this->firmware_date);
1395 out:
1396         spin_unlock_irqrestore(&this->lock, flags);
1397         return rc;
1398 fail:
1399         printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1400         goto out;
1401 }
1402
1403 static struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1404 {
1405         struct wl3501_card *this = netdev_priv(dev);
1406
1407         return &this->stats;
1408 }
1409
1410 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1411 {
1412         struct wl3501_card *this = netdev_priv(dev);
1413         struct iw_statistics *wstats = &this->wstats;
1414         u32 value; /* size checked: it is u32 */
1415
1416         memset(wstats, 0, sizeof(*wstats));
1417         wstats->status = netif_running(dev);
1418         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1419                                   &value, sizeof(value)))
1420                 wstats->discard.code += value;
1421         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1422                                   &value, sizeof(value)))
1423                 wstats->discard.code += value;
1424         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1425                                   &value, sizeof(value)))
1426                 wstats->discard.code += value;
1427         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1428                                   &value, sizeof(value)))
1429                 wstats->discard.retries = value;
1430         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1431                                   &value, sizeof(value)))
1432                 wstats->discard.misc += value;
1433         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1434                                   &value, sizeof(value)))
1435                 wstats->discard.misc += value;
1436         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1437                                   &value, sizeof(value)))
1438                 wstats->discard.misc += value;
1439         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1440                                   &value, sizeof(value)))
1441                 wstats->discard.misc += value;
1442         return wstats;
1443 }
1444
1445 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1446 {
1447         strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
1448 }
1449
1450 static const struct ethtool_ops ops = {
1451         .get_drvinfo = wl3501_get_drvinfo
1452 };
1453
1454 /**
1455  * wl3501_detach - deletes a driver "instance"
1456  * @link - FILL_IN
1457  *
1458  * This deletes a driver "instance". The device is de-registered with Card
1459  * Services. If it has been released, all local data structures are freed.
1460  * Otherwise, the structures will be freed when the device is released.
1461  */
1462 static void wl3501_detach(struct pcmcia_device *link)
1463 {
1464         struct net_device *dev = link->priv;
1465
1466         /* If the device is currently configured and active, we won't actually
1467          * delete it yet.  Instead, it is marked so that when the release()
1468          * function is called, that will trigger a proper detach(). */
1469
1470         while (link->open > 0)
1471                 wl3501_close(dev);
1472
1473         netif_device_detach(dev);
1474         wl3501_release(link);
1475
1476         if (link->priv)
1477                 free_netdev(link->priv);
1478
1479         return;
1480 }
1481
1482 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1483                            union iwreq_data *wrqu, char *extra)
1484 {
1485         strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1486         return 0;
1487 }
1488
1489 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1490                            union iwreq_data *wrqu, char *extra)
1491 {
1492         struct wl3501_card *this = netdev_priv(dev);
1493         int channel = wrqu->freq.m;
1494         int rc = -EINVAL;
1495
1496         if (iw_valid_channel(this->reg_domain, channel)) {
1497                 this->chan = channel;
1498                 rc = wl3501_reset(dev);
1499         }
1500         return rc;
1501 }
1502
1503 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1504                            union iwreq_data *wrqu, char *extra)
1505 {
1506         struct wl3501_card *this = netdev_priv(dev);
1507
1508         wrqu->freq.m = ieee80211_dsss_chan_to_freq(this->chan) * 100000;
1509         wrqu->freq.e = 1;
1510         return 0;
1511 }
1512
1513 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1514                            union iwreq_data *wrqu, char *extra)
1515 {
1516         int rc = -EINVAL;
1517
1518         if (wrqu->mode == IW_MODE_INFRA ||
1519             wrqu->mode == IW_MODE_ADHOC ||
1520             wrqu->mode == IW_MODE_AUTO) {
1521                 struct wl3501_card *this = netdev_priv(dev);
1522
1523                 this->net_type = wrqu->mode;
1524                 rc = wl3501_reset(dev);
1525         }
1526         return rc;
1527 }
1528
1529 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1530                            union iwreq_data *wrqu, char *extra)
1531 {
1532         struct wl3501_card *this = netdev_priv(dev);
1533
1534         wrqu->mode = this->net_type;
1535         return 0;
1536 }
1537
1538 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1539                            union iwreq_data *wrqu, char *extra)
1540 {
1541         struct wl3501_card *this = netdev_priv(dev);
1542
1543         wrqu->sens.value = this->rssi;
1544         wrqu->sens.disabled = !wrqu->sens.value;
1545         wrqu->sens.fixed = 1;
1546         return 0;
1547 }
1548
1549 static int wl3501_get_range(struct net_device *dev,
1550                             struct iw_request_info *info,
1551                             union iwreq_data *wrqu, char *extra)
1552 {
1553         struct iw_range *range = (struct iw_range *)extra;
1554
1555         /* Set the length (very important for backward compatibility) */
1556         wrqu->data.length = sizeof(*range);
1557
1558         /* Set all the info we don't care or don't know about to zero */
1559         memset(range, 0, sizeof(*range));
1560
1561         /* Set the Wireless Extension versions */
1562         range->we_version_compiled      = WIRELESS_EXT;
1563         range->we_version_source        = 1;
1564         range->throughput               = 2 * 1000 * 1000;     /* ~2 Mb/s */
1565         /* FIXME: study the code to fill in more fields... */
1566         return 0;
1567 }
1568
1569 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1570                           union iwreq_data *wrqu, char *extra)
1571 {
1572         struct wl3501_card *this = netdev_priv(dev);
1573         static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1574         int rc = -EINVAL;
1575
1576         /* FIXME: we support other ARPHRDs...*/
1577         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1578                 goto out;
1579         if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1580                 /* FIXME: rescan? */
1581         } else
1582                 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1583                 /* FIXME: rescan? deassoc & scan? */
1584         rc = 0;
1585 out:
1586         return rc;
1587 }
1588
1589 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1590                           union iwreq_data *wrqu, char *extra)
1591 {
1592         struct wl3501_card *this = netdev_priv(dev);
1593
1594         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1595         memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1596         return 0;
1597 }
1598
1599 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1600                            union iwreq_data *wrqu, char *extra)
1601 {
1602         /*
1603          * FIXME: trigger scanning with a reset, yes, I'm lazy
1604          */
1605         return wl3501_reset(dev);
1606 }
1607
1608 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1609                            union iwreq_data *wrqu, char *extra)
1610 {
1611         struct wl3501_card *this = netdev_priv(dev);
1612         int i;
1613         char *current_ev = extra;
1614         struct iw_event iwe;
1615
1616         for (i = 0; i < this->bss_cnt; ++i) {
1617                 iwe.cmd                 = SIOCGIWAP;
1618                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1619                 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1620                 current_ev = iwe_stream_add_event(info, current_ev,
1621                                                   extra + IW_SCAN_MAX_DATA,
1622                                                   &iwe, IW_EV_ADDR_LEN);
1623                 iwe.cmd           = SIOCGIWESSID;
1624                 iwe.u.data.flags  = 1;
1625                 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1626                 current_ev = iwe_stream_add_point(info, current_ev,
1627                                                   extra + IW_SCAN_MAX_DATA,
1628                                                   &iwe,
1629                                                   this->bss_set[i].ssid.essid);
1630                 iwe.cmd    = SIOCGIWMODE;
1631                 iwe.u.mode = this->bss_set[i].bss_type;
1632                 current_ev = iwe_stream_add_event(info, current_ev,
1633                                                   extra + IW_SCAN_MAX_DATA,
1634                                                   &iwe, IW_EV_UINT_LEN);
1635                 iwe.cmd = SIOCGIWFREQ;
1636                 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1637                 iwe.u.freq.e = 0;
1638                 current_ev = iwe_stream_add_event(info, current_ev,
1639                                                   extra + IW_SCAN_MAX_DATA,
1640                                                   &iwe, IW_EV_FREQ_LEN);
1641                 iwe.cmd = SIOCGIWENCODE;
1642                 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1643                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1644                 else
1645                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1646                 iwe.u.data.length = 0;
1647                 current_ev = iwe_stream_add_point(info, current_ev,
1648                                                   extra + IW_SCAN_MAX_DATA,
1649                                                   &iwe, NULL);
1650         }
1651         /* Length of data */
1652         wrqu->data.length = (current_ev - extra);
1653         wrqu->data.flags = 0; /* FIXME: set properly these flags */
1654         return 0;
1655 }
1656
1657 static int wl3501_set_essid(struct net_device *dev,
1658                             struct iw_request_info *info,
1659                             union iwreq_data *wrqu, char *extra)
1660 {
1661         struct wl3501_card *this = netdev_priv(dev);
1662
1663         if (wrqu->data.flags) {
1664                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1665                                          &this->essid.el,
1666                                          extra, wrqu->data.length);
1667         } else { /* We accept any ESSID */
1668                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1669                                          &this->essid.el, "ANY", 3);
1670         }
1671         return wl3501_reset(dev);
1672 }
1673
1674 static int wl3501_get_essid(struct net_device *dev,
1675                             struct iw_request_info *info,
1676                             union iwreq_data *wrqu, char *extra)
1677 {
1678         struct wl3501_card *this = netdev_priv(dev);
1679         unsigned long flags;
1680
1681         spin_lock_irqsave(&this->lock, flags);
1682         wrqu->essid.flags  = 1;
1683         wrqu->essid.length = this->essid.el.len;
1684         memcpy(extra, this->essid.essid, this->essid.el.len);
1685         spin_unlock_irqrestore(&this->lock, flags);
1686         return 0;
1687 }
1688
1689 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1690                            union iwreq_data *wrqu, char *extra)
1691 {
1692         struct wl3501_card *this = netdev_priv(dev);
1693
1694         if (wrqu->data.length > sizeof(this->nick))
1695                 return -E2BIG;
1696         strlcpy(this->nick, extra, wrqu->data.length);
1697         return 0;
1698 }
1699
1700 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1701                            union iwreq_data *wrqu, char *extra)
1702 {
1703         struct wl3501_card *this = netdev_priv(dev);
1704
1705         strlcpy(extra, this->nick, 32);
1706         wrqu->data.length = strlen(extra);
1707         return 0;
1708 }
1709
1710 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1711                            union iwreq_data *wrqu, char *extra)
1712 {
1713         /*
1714          * FIXME: have to see from where to get this info, perhaps this card
1715          * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1716          * common with the Planet Access Points. -acme
1717          */
1718         wrqu->bitrate.value = 2000000;
1719         wrqu->bitrate.fixed = 1;
1720         return 0;
1721 }
1722
1723 static int wl3501_get_rts_threshold(struct net_device *dev,
1724                                     struct iw_request_info *info,
1725                                     union iwreq_data *wrqu, char *extra)
1726 {
1727         u16 threshold; /* size checked: it is u16 */
1728         struct wl3501_card *this = netdev_priv(dev);
1729         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1730                                       &threshold, sizeof(threshold));
1731         if (!rc) {
1732                 wrqu->rts.value = threshold;
1733                 wrqu->rts.disabled = threshold >= 2347;
1734                 wrqu->rts.fixed = 1;
1735         }
1736         return rc;
1737 }
1738
1739 static int wl3501_get_frag_threshold(struct net_device *dev,
1740                                      struct iw_request_info *info,
1741                                      union iwreq_data *wrqu, char *extra)
1742 {
1743         u16 threshold; /* size checked: it is u16 */
1744         struct wl3501_card *this = netdev_priv(dev);
1745         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1746                                       &threshold, sizeof(threshold));
1747         if (!rc) {
1748                 wrqu->frag.value = threshold;
1749                 wrqu->frag.disabled = threshold >= 2346;
1750                 wrqu->frag.fixed = 1;
1751         }
1752         return rc;
1753 }
1754
1755 static int wl3501_get_txpow(struct net_device *dev,
1756                             struct iw_request_info *info,
1757                             union iwreq_data *wrqu, char *extra)
1758 {
1759         u16 txpow;
1760         struct wl3501_card *this = netdev_priv(dev);
1761         int rc = wl3501_get_mib_value(this,
1762                                       WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1763                                       &txpow, sizeof(txpow));
1764         if (!rc) {
1765                 wrqu->txpower.value = txpow;
1766                 wrqu->txpower.disabled = 0;
1767                 /*
1768                  * From the MIB values I think this can be configurable,
1769                  * as it lists several tx power levels -acme
1770                  */
1771                 wrqu->txpower.fixed = 0;
1772                 wrqu->txpower.flags = IW_TXPOW_MWATT;
1773         }
1774         return rc;
1775 }
1776
1777 static int wl3501_get_retry(struct net_device *dev,
1778                             struct iw_request_info *info,
1779                             union iwreq_data *wrqu, char *extra)
1780 {
1781         u8 retry; /* size checked: it is u8 */
1782         struct wl3501_card *this = netdev_priv(dev);
1783         int rc = wl3501_get_mib_value(this,
1784                                       WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1785                                       &retry, sizeof(retry));
1786         if (rc)
1787                 goto out;
1788         if (wrqu->retry.flags & IW_RETRY_LONG) {
1789                 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1790                 goto set_value;
1791         }
1792         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1793                                   &retry, sizeof(retry));
1794         if (rc)
1795                 goto out;
1796         wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
1797 set_value:
1798         wrqu->retry.value = retry;
1799         wrqu->retry.disabled = 0;
1800 out:
1801         return rc;
1802 }
1803
1804 static int wl3501_get_encode(struct net_device *dev,
1805                              struct iw_request_info *info,
1806                              union iwreq_data *wrqu, char *extra)
1807 {
1808         u8 implemented, restricted, keys[100], len_keys, tocopy;
1809         struct wl3501_card *this = netdev_priv(dev);
1810         int rc = wl3501_get_mib_value(this,
1811                                       WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1812                                       &implemented, sizeof(implemented));
1813         if (rc)
1814                 goto out;
1815         if (!implemented) {
1816                 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1817                 goto out;
1818         }
1819         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1820                                   &restricted, sizeof(restricted));
1821         if (rc)
1822                 goto out;
1823         wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1824                                             IW_ENCODE_OPEN;
1825         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1826                                   &len_keys, sizeof(len_keys));
1827         if (rc)
1828                 goto out;
1829         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1830                                   keys, len_keys);
1831         if (rc)
1832                 goto out;
1833         tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1834         tocopy = min_t(u8, tocopy, 100);
1835         wrqu->encoding.length = tocopy;
1836         memcpy(extra, keys, tocopy);
1837 out:
1838         return rc;
1839 }
1840
1841 static int wl3501_get_power(struct net_device *dev,
1842                             struct iw_request_info *info,
1843                             union iwreq_data *wrqu, char *extra)
1844 {
1845         u8 pwr_state;
1846         struct wl3501_card *this = netdev_priv(dev);
1847         int rc = wl3501_get_mib_value(this,
1848                                       WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1849                                       &pwr_state, sizeof(pwr_state));
1850         if (rc)
1851                 goto out;
1852         wrqu->power.disabled = !pwr_state;
1853         wrqu->power.flags = IW_POWER_ON;
1854 out:
1855         return rc;
1856 }
1857
1858 static const iw_handler wl3501_handler[] = {
1859         [SIOCGIWNAME    - SIOCIWFIRST] = wl3501_get_name,
1860         [SIOCSIWFREQ    - SIOCIWFIRST] = wl3501_set_freq,
1861         [SIOCGIWFREQ    - SIOCIWFIRST] = wl3501_get_freq,
1862         [SIOCSIWMODE    - SIOCIWFIRST] = wl3501_set_mode,
1863         [SIOCGIWMODE    - SIOCIWFIRST] = wl3501_get_mode,
1864         [SIOCGIWSENS    - SIOCIWFIRST] = wl3501_get_sens,
1865         [SIOCGIWRANGE   - SIOCIWFIRST] = wl3501_get_range,
1866         [SIOCSIWSPY     - SIOCIWFIRST] = iw_handler_set_spy,
1867         [SIOCGIWSPY     - SIOCIWFIRST] = iw_handler_get_spy,
1868         [SIOCSIWTHRSPY  - SIOCIWFIRST] = iw_handler_set_thrspy,
1869         [SIOCGIWTHRSPY  - SIOCIWFIRST] = iw_handler_get_thrspy,
1870         [SIOCSIWAP      - SIOCIWFIRST] = wl3501_set_wap,
1871         [SIOCGIWAP      - SIOCIWFIRST] = wl3501_get_wap,
1872         [SIOCSIWSCAN    - SIOCIWFIRST] = wl3501_set_scan,
1873         [SIOCGIWSCAN    - SIOCIWFIRST] = wl3501_get_scan,
1874         [SIOCSIWESSID   - SIOCIWFIRST] = wl3501_set_essid,
1875         [SIOCGIWESSID   - SIOCIWFIRST] = wl3501_get_essid,
1876         [SIOCSIWNICKN   - SIOCIWFIRST] = wl3501_set_nick,
1877         [SIOCGIWNICKN   - SIOCIWFIRST] = wl3501_get_nick,
1878         [SIOCGIWRATE    - SIOCIWFIRST] = wl3501_get_rate,
1879         [SIOCGIWRTS     - SIOCIWFIRST] = wl3501_get_rts_threshold,
1880         [SIOCGIWFRAG    - SIOCIWFIRST] = wl3501_get_frag_threshold,
1881         [SIOCGIWTXPOW   - SIOCIWFIRST] = wl3501_get_txpow,
1882         [SIOCGIWRETRY   - SIOCIWFIRST] = wl3501_get_retry,
1883         [SIOCGIWENCODE  - SIOCIWFIRST] = wl3501_get_encode,
1884         [SIOCGIWPOWER   - SIOCIWFIRST] = wl3501_get_power,
1885 };
1886
1887 static const struct iw_handler_def wl3501_handler_def = {
1888         .num_standard   = ARRAY_SIZE(wl3501_handler),
1889         .standard       = (iw_handler *)wl3501_handler,
1890         .get_wireless_stats = wl3501_get_wireless_stats,
1891 };
1892
1893 /**
1894  * wl3501_attach - creates an "instance" of the driver
1895  *
1896  * Creates an "instance" of the driver, allocating local data structures for
1897  * one device.  The device is registered with Card Services.
1898  *
1899  * The dev_link structure is initialized, but we don't actually configure the
1900  * card at this point -- we wait until we receive a card insertion event.
1901  */
1902 static int wl3501_probe(struct pcmcia_device *p_dev)
1903 {
1904         struct net_device *dev;
1905         struct wl3501_card *this;
1906
1907         /* The io structure describes IO port mapping */
1908         p_dev->io.NumPorts1     = 16;
1909         p_dev->io.Attributes1   = IO_DATA_PATH_WIDTH_8;
1910         p_dev->io.IOAddrLines   = 5;
1911
1912         /* Interrupt setup */
1913         p_dev->irq.Attributes   = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
1914         p_dev->irq.IRQInfo1     = IRQ_LEVEL_ID;
1915         p_dev->irq.Handler = wl3501_interrupt;
1916
1917         /* General socket configuration */
1918         p_dev->conf.Attributes  = CONF_ENABLE_IRQ;
1919         p_dev->conf.IntType     = INT_MEMORY_AND_IO;
1920         p_dev->conf.ConfigIndex = 1;
1921
1922         dev = alloc_etherdev(sizeof(struct wl3501_card));
1923         if (!dev)
1924                 goto out_link;
1925         dev->open               = wl3501_open;
1926         dev->stop               = wl3501_close;
1927         dev->hard_start_xmit    = wl3501_hard_start_xmit;
1928         dev->tx_timeout         = wl3501_tx_timeout;
1929         dev->watchdog_timeo     = 5 * HZ;
1930         dev->get_stats          = wl3501_get_stats;
1931         this = netdev_priv(dev);
1932         this->wireless_data.spy_data = &this->spy_data;
1933         this->p_dev = p_dev;
1934         dev->wireless_data      = &this->wireless_data;
1935         dev->wireless_handlers  = (struct iw_handler_def *)&wl3501_handler_def;
1936         SET_ETHTOOL_OPS(dev, &ops);
1937         netif_stop_queue(dev);
1938         p_dev->priv = p_dev->irq.Instance = dev;
1939
1940         return wl3501_config(p_dev);
1941 out_link:
1942         return -ENOMEM;
1943 }
1944
1945 #define CS_CHECK(fn, ret) \
1946 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1947
1948 /**
1949  * wl3501_config - configure the PCMCIA socket and make eth device available
1950  * @link - FILL_IN
1951  *
1952  * wl3501_config() is scheduled to run after a CARD_INSERTION event is
1953  * received, to configure the PCMCIA socket, and to make the ethernet device
1954  * available to the system.
1955  */
1956 static int wl3501_config(struct pcmcia_device *link)
1957 {
1958         struct net_device *dev = link->priv;
1959         int i = 0, j, last_fn, last_ret;
1960         struct wl3501_card *this;
1961
1962         /* Try allocating IO ports.  This tries a few fixed addresses.  If you
1963          * want, you can also read the card's config table to pick addresses --
1964          * see the serial driver for an example. */
1965
1966         for (j = 0x280; j < 0x400; j += 0x20) {
1967                 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
1968                  * 0x200-0x2ff, and so on, because this seems safer */
1969                 link->io.BasePort1 = j;
1970                 link->io.BasePort2 = link->io.BasePort1 + 0x10;
1971                 i = pcmcia_request_io(link, &link->io);
1972                 if (i == 0)
1973                         break;
1974         }
1975         if (i != 0) {
1976                 cs_error(link, RequestIO, i);
1977                 goto failed;
1978         }
1979
1980         /* Now allocate an interrupt line. Note that this does not actually
1981          * assign a handler to the interrupt. */
1982
1983         CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
1984
1985         /* This actually configures the PCMCIA socket -- setting up the I/O
1986          * windows and the interrupt mapping.  */
1987
1988         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
1989
1990         dev->irq = link->irq.AssignedIRQ;
1991         dev->base_addr = link->io.BasePort1;
1992         SET_NETDEV_DEV(dev, &handle_to_dev(link));
1993         if (register_netdev(dev)) {
1994                 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
1995                 goto failed;
1996         }
1997
1998         this = netdev_priv(dev);
1999         /*
2000          * At this point, the dev_node_t structure(s) should be initialized and
2001          * arranged in a linked list at link->dev_node.
2002          */
2003         link->dev_node = &this->node;
2004
2005         this->base_addr = dev->base_addr;
2006
2007         if (!wl3501_get_flash_mac_addr(this)) {
2008                 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2009                        dev->name);
2010                 goto failed;
2011         }
2012         strcpy(this->node.dev_name, dev->name);
2013
2014         for (i = 0; i < 6; i++)
2015                 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2016
2017         /* print probe information */
2018         printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, "
2019                "MAC addr in flash ROM:%pM\n",
2020                dev->name, this->base_addr, (int)dev->irq,
2021                dev->dev_addr);
2022         /*
2023          * Initialize card parameters - added by jss
2024          */
2025         this->net_type          = IW_MODE_INFRA;
2026         this->bss_cnt           = 0;
2027         this->join_sta_bss      = 0;
2028         this->adhoc_times       = 0;
2029         iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2030                                  "ANY", 3);
2031         this->card_name[0]      = '\0';
2032         this->firmware_date[0]  = '\0';
2033         this->rssi              = 255;
2034         this->chan              = iw_default_channel(this->reg_domain);
2035         strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2036         spin_lock_init(&this->lock);
2037         init_waitqueue_head(&this->wait);
2038         netif_start_queue(dev);
2039         return 0;
2040
2041 cs_failed:
2042         cs_error(link, last_fn, last_ret);
2043 failed:
2044         wl3501_release(link);
2045         return -ENODEV;
2046 }
2047
2048 /**
2049  * wl3501_release - unregister the net, release PCMCIA configuration
2050  * @arg - link
2051  *
2052  * After a card is removed, wl3501_release() will unregister the net device,
2053  * and release the PCMCIA configuration.  If the device is still open, this
2054  * will be postponed until it is closed.
2055  */
2056 static void wl3501_release(struct pcmcia_device *link)
2057 {
2058         struct net_device *dev = link->priv;
2059
2060         /* Unlink the device chain */
2061         if (link->dev_node)
2062                 unregister_netdev(dev);
2063
2064         pcmcia_disable_device(link);
2065 }
2066
2067 static int wl3501_suspend(struct pcmcia_device *link)
2068 {
2069         struct net_device *dev = link->priv;
2070
2071         wl3501_pwr_mgmt(netdev_priv(dev), WL3501_SUSPEND);
2072         if (link->open)
2073                 netif_device_detach(dev);
2074
2075         return 0;
2076 }
2077
2078 static int wl3501_resume(struct pcmcia_device *link)
2079 {
2080         struct net_device *dev = link->priv;
2081
2082         wl3501_pwr_mgmt(netdev_priv(dev), WL3501_RESUME);
2083         if (link->open) {
2084                 wl3501_reset(dev);
2085                 netif_device_attach(dev);
2086         }
2087
2088         return 0;
2089 }
2090
2091
2092 static struct pcmcia_device_id wl3501_ids[] = {
2093         PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2094         PCMCIA_DEVICE_NULL
2095 };
2096 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2097
2098 static struct pcmcia_driver wl3501_driver = {
2099         .owner          = THIS_MODULE,
2100         .drv            = {
2101                 .name   = "wl3501_cs",
2102         },
2103         .probe          = wl3501_probe,
2104         .remove         = wl3501_detach,
2105         .id_table       = wl3501_ids,
2106         .suspend        = wl3501_suspend,
2107         .resume         = wl3501_resume,
2108 };
2109
2110 static int __init wl3501_init_module(void)
2111 {
2112         return pcmcia_register_driver(&wl3501_driver);
2113 }
2114
2115 static void __exit wl3501_exit_module(void)
2116 {
2117         pcmcia_unregister_driver(&wl3501_driver);
2118 }
2119
2120 module_init(wl3501_init_module);
2121 module_exit(wl3501_exit_module);
2122
2123 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2124               "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2125               "Gustavo Niemeyer <niemeyer@conectiva.com>");
2126 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2127 MODULE_LICENSE("GPL");