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