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