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