[PATCH] pcmcia: unify detach, REMOVAL_EVENT handlers into one remove callback
[safe/jmp/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(struct pcmcia_device *p_dev)
1502 {
1503         dev_link_t *link = dev_to_instance(p_dev);
1504         dev_link_t **linkp;
1505         struct net_device *dev = link->priv;
1506
1507         /* Locate device structure */
1508         for (linkp = &wl3501_dev_list; *linkp; linkp = &(*linkp)->next)
1509                 if (*linkp == link)
1510                         break;
1511         if (!*linkp)
1512                 goto out;
1513
1514         /* If the device is currently configured and active, we won't actually
1515          * delete it yet.  Instead, it is marked so that when the release()
1516          * function is called, that will trigger a proper detach(). */
1517
1518         if (link->state & DEV_CONFIG) {
1519                 while (link->open > 0)
1520                         wl3501_close(dev);
1521
1522                 netif_device_detach(dev);
1523                 wl3501_release(link);
1524         }
1525
1526         /* Unlink device structure, free pieces */
1527         *linkp = link->next;
1528
1529         if (link->priv)
1530                 free_netdev(link->priv);
1531         kfree(link);
1532 out:
1533         return;
1534 }
1535
1536 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1537                            union iwreq_data *wrqu, char *extra)
1538 {
1539         strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1540         return 0;
1541 }
1542
1543 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1544                            union iwreq_data *wrqu, char *extra)
1545 {
1546         struct wl3501_card *this = dev->priv;
1547         int channel = wrqu->freq.m;
1548         int rc = -EINVAL;
1549
1550         if (iw_valid_channel(this->reg_domain, channel)) {
1551                 this->chan = channel;
1552                 rc = wl3501_reset(dev);
1553         }
1554         return rc;
1555 }
1556
1557 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1558                            union iwreq_data *wrqu, char *extra)
1559 {
1560         struct wl3501_card *this = dev->priv;
1561
1562         wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1563         wrqu->freq.e = 1;
1564         return 0;
1565 }
1566
1567 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1568                            union iwreq_data *wrqu, char *extra)
1569 {
1570         int rc = -EINVAL;
1571
1572         if (wrqu->mode == IW_MODE_INFRA ||
1573             wrqu->mode == IW_MODE_ADHOC ||
1574             wrqu->mode == IW_MODE_AUTO) {
1575                 struct wl3501_card *this = dev->priv;
1576
1577                 this->net_type = wrqu->mode;
1578                 rc = wl3501_reset(dev);
1579         }
1580         return rc;
1581 }
1582
1583 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1584                            union iwreq_data *wrqu, char *extra)
1585 {
1586         struct wl3501_card *this = dev->priv;
1587
1588         wrqu->mode = this->net_type;
1589         return 0;
1590 }
1591
1592 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1593                            union iwreq_data *wrqu, char *extra)
1594 {
1595         struct wl3501_card *this = dev->priv;
1596
1597         wrqu->sens.value = this->rssi;
1598         wrqu->sens.disabled = !wrqu->sens.value;
1599         wrqu->sens.fixed = 1;
1600         return 0;
1601 }
1602
1603 static int wl3501_get_range(struct net_device *dev,
1604                             struct iw_request_info *info,
1605                             union iwreq_data *wrqu, char *extra)
1606 {
1607         struct iw_range *range = (struct iw_range *)extra;
1608
1609         /* Set the length (very important for backward compatibility) */
1610         wrqu->data.length = sizeof(*range);
1611
1612         /* Set all the info we don't care or don't know about to zero */
1613         memset(range, 0, sizeof(*range));
1614
1615         /* Set the Wireless Extension versions */
1616         range->we_version_compiled      = WIRELESS_EXT;
1617         range->we_version_source        = 1;
1618         range->throughput               = 2 * 1000 * 1000;     /* ~2 Mb/s */
1619         /* FIXME: study the code to fill in more fields... */
1620         return 0;
1621 }
1622
1623 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1624                           union iwreq_data *wrqu, char *extra)
1625 {
1626         struct wl3501_card *this = dev->priv;
1627         static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1628         int rc = -EINVAL;
1629
1630         /* FIXME: we support other ARPHRDs...*/
1631         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1632                 goto out;
1633         if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1634                 /* FIXME: rescan? */
1635         } else
1636                 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1637                 /* FIXME: rescan? deassoc & scan? */
1638         rc = 0;
1639 out:
1640         return rc;
1641 }
1642
1643 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1644                           union iwreq_data *wrqu, char *extra)
1645 {
1646         struct wl3501_card *this = dev->priv;
1647
1648         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1649         memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1650         return 0;
1651 }
1652
1653 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1654                            union iwreq_data *wrqu, char *extra)
1655 {
1656         /*
1657          * FIXME: trigger scanning with a reset, yes, I'm lazy
1658          */
1659         return wl3501_reset(dev);
1660 }
1661
1662 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1663                            union iwreq_data *wrqu, char *extra)
1664 {
1665         struct wl3501_card *this = dev->priv;
1666         int i;
1667         char *current_ev = extra;
1668         struct iw_event iwe;
1669
1670         for (i = 0; i < this->bss_cnt; ++i) {
1671                 iwe.cmd                 = SIOCGIWAP;
1672                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1673                 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1674                 current_ev = iwe_stream_add_event(current_ev,
1675                                                   extra + IW_SCAN_MAX_DATA,
1676                                                   &iwe, IW_EV_ADDR_LEN);
1677                 iwe.cmd           = SIOCGIWESSID;
1678                 iwe.u.data.flags  = 1;
1679                 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1680                 current_ev = iwe_stream_add_point(current_ev,
1681                                                   extra + IW_SCAN_MAX_DATA,
1682                                                   &iwe,
1683                                                   this->bss_set[i].ssid.essid);
1684                 iwe.cmd    = SIOCGIWMODE;
1685                 iwe.u.mode = this->bss_set[i].bss_type;
1686                 current_ev = iwe_stream_add_event(current_ev,
1687                                                   extra + IW_SCAN_MAX_DATA,
1688                                                   &iwe, IW_EV_UINT_LEN);
1689                 iwe.cmd = SIOCGIWFREQ;
1690                 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1691                 iwe.u.freq.e = 0;
1692                 current_ev = iwe_stream_add_event(current_ev,
1693                                                   extra + IW_SCAN_MAX_DATA,
1694                                                   &iwe, IW_EV_FREQ_LEN);
1695                 iwe.cmd = SIOCGIWENCODE;
1696                 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1697                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1698                 else
1699                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1700                 iwe.u.data.length = 0;
1701                 current_ev = iwe_stream_add_point(current_ev,
1702                                                   extra + IW_SCAN_MAX_DATA,
1703                                                   &iwe, NULL);
1704         }
1705         /* Length of data */
1706         wrqu->data.length = (current_ev - extra);
1707         wrqu->data.flags = 0; /* FIXME: set properly these flags */
1708         return 0;
1709 }
1710
1711 static int wl3501_set_essid(struct net_device *dev,
1712                             struct iw_request_info *info,
1713                             union iwreq_data *wrqu, char *extra)
1714 {
1715         struct wl3501_card *this = dev->priv;
1716
1717         if (wrqu->data.flags) {
1718                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1719                                          &this->essid.el,
1720                                          extra, wrqu->data.length);
1721         } else { /* We accept any ESSID */
1722                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1723                                          &this->essid.el, "ANY", 3);
1724         }
1725         return wl3501_reset(dev);
1726 }
1727
1728 static int wl3501_get_essid(struct net_device *dev,
1729                             struct iw_request_info *info,
1730                             union iwreq_data *wrqu, char *extra)
1731 {
1732         struct wl3501_card *this = dev->priv;
1733         unsigned long flags;
1734
1735         spin_lock_irqsave(&this->lock, flags);
1736         wrqu->essid.flags  = 1;
1737         wrqu->essid.length = this->essid.el.len;
1738         memcpy(extra, this->essid.essid, this->essid.el.len);
1739         spin_unlock_irqrestore(&this->lock, flags);
1740         return 0;
1741 }
1742
1743 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1744                            union iwreq_data *wrqu, char *extra)
1745 {
1746         struct wl3501_card *this = dev->priv;
1747
1748         if (wrqu->data.length > sizeof(this->nick))
1749                 return -E2BIG;
1750         strlcpy(this->nick, extra, wrqu->data.length);
1751         return 0;
1752 }
1753
1754 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1755                            union iwreq_data *wrqu, char *extra)
1756 {
1757         struct wl3501_card *this = dev->priv;
1758
1759         strlcpy(extra, this->nick, 32);
1760         wrqu->data.length = strlen(extra);
1761         return 0;
1762 }
1763
1764 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1765                            union iwreq_data *wrqu, char *extra)
1766 {
1767         /*
1768          * FIXME: have to see from where to get this info, perhaps this card
1769          * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1770          * common with the Planet Access Points. -acme
1771          */
1772         wrqu->bitrate.value = 2000000;
1773         wrqu->bitrate.fixed = 1;
1774         return 0;
1775 }
1776
1777 static int wl3501_get_rts_threshold(struct net_device *dev,
1778                                     struct iw_request_info *info,
1779                                     union iwreq_data *wrqu, char *extra)
1780 {
1781         u16 threshold; /* size checked: it is u16 */
1782         struct wl3501_card *this = dev->priv;
1783         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1784                                       &threshold, sizeof(threshold));
1785         if (!rc) {
1786                 wrqu->rts.value = threshold;
1787                 wrqu->rts.disabled = threshold >= 2347;
1788                 wrqu->rts.fixed = 1;
1789         }
1790         return rc;
1791 }
1792
1793 static int wl3501_get_frag_threshold(struct net_device *dev,
1794                                      struct iw_request_info *info,
1795                                      union iwreq_data *wrqu, char *extra)
1796 {
1797         u16 threshold; /* size checked: it is u16 */
1798         struct wl3501_card *this = dev->priv;
1799         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1800                                       &threshold, sizeof(threshold));
1801         if (!rc) {
1802                 wrqu->frag.value = threshold;
1803                 wrqu->frag.disabled = threshold >= 2346;
1804                 wrqu->frag.fixed = 1;
1805         }
1806         return rc;
1807 }
1808
1809 static int wl3501_get_txpow(struct net_device *dev,
1810                             struct iw_request_info *info,
1811                             union iwreq_data *wrqu, char *extra)
1812 {
1813         u16 txpow;
1814         struct wl3501_card *this = dev->priv;
1815         int rc = wl3501_get_mib_value(this,
1816                                       WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1817                                       &txpow, sizeof(txpow));
1818         if (!rc) {
1819                 wrqu->txpower.value = txpow;
1820                 wrqu->txpower.disabled = 0;
1821                 /*
1822                  * From the MIB values I think this can be configurable,
1823                  * as it lists several tx power levels -acme
1824                  */
1825                 wrqu->txpower.fixed = 0;
1826                 wrqu->txpower.flags = IW_TXPOW_MWATT;
1827         }
1828         return rc;
1829 }
1830
1831 static int wl3501_get_retry(struct net_device *dev,
1832                             struct iw_request_info *info,
1833                             union iwreq_data *wrqu, char *extra)
1834 {
1835         u8 retry; /* size checked: it is u8 */
1836         struct wl3501_card *this = dev->priv;
1837         int rc = wl3501_get_mib_value(this,
1838                                       WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1839                                       &retry, sizeof(retry));
1840         if (rc)
1841                 goto out;
1842         if (wrqu->retry.flags & IW_RETRY_MAX) {
1843                 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1844                 goto set_value;
1845         }
1846         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1847                                   &retry, sizeof(retry));
1848         if (rc)
1849                 goto out;
1850         wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1851 set_value:
1852         wrqu->retry.value = retry;
1853         wrqu->retry.disabled = 0;
1854 out:
1855         return rc;
1856 }
1857
1858 static int wl3501_get_encode(struct net_device *dev,
1859                              struct iw_request_info *info,
1860                              union iwreq_data *wrqu, char *extra)
1861 {
1862         u8 implemented, restricted, keys[100], len_keys, tocopy;
1863         struct wl3501_card *this = dev->priv;
1864         int rc = wl3501_get_mib_value(this,
1865                                       WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1866                                       &implemented, sizeof(implemented));
1867         if (rc)
1868                 goto out;
1869         if (!implemented) {
1870                 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1871                 goto out;
1872         }
1873         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1874                                   &restricted, sizeof(restricted));
1875         if (rc)
1876                 goto out;
1877         wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1878                                             IW_ENCODE_OPEN;
1879         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1880                                   &len_keys, sizeof(len_keys));
1881         if (rc)
1882                 goto out;
1883         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1884                                   keys, len_keys);
1885         if (rc)
1886                 goto out;
1887         tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1888         tocopy = min_t(u8, tocopy, 100);
1889         wrqu->encoding.length = tocopy;
1890         memset(extra, 0, tocopy);
1891         memcpy(extra, keys, tocopy);
1892 out:
1893         return rc;
1894 }
1895
1896 static int wl3501_get_power(struct net_device *dev,
1897                             struct iw_request_info *info,
1898                             union iwreq_data *wrqu, char *extra)
1899 {
1900         u8 pwr_state;
1901         struct wl3501_card *this = dev->priv;
1902         int rc = wl3501_get_mib_value(this,
1903                                       WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1904                                       &pwr_state, sizeof(pwr_state));
1905         if (rc)
1906                 goto out;
1907         wrqu->power.disabled = !pwr_state;
1908         wrqu->power.flags = IW_POWER_ON;
1909 out:
1910         return rc;
1911 }
1912
1913 static const iw_handler wl3501_handler[] = {
1914         [SIOCGIWNAME    - SIOCIWFIRST] = wl3501_get_name,
1915         [SIOCSIWFREQ    - SIOCIWFIRST] = wl3501_set_freq,
1916         [SIOCGIWFREQ    - SIOCIWFIRST] = wl3501_get_freq,
1917         [SIOCSIWMODE    - SIOCIWFIRST] = wl3501_set_mode,
1918         [SIOCGIWMODE    - SIOCIWFIRST] = wl3501_get_mode,
1919         [SIOCGIWSENS    - SIOCIWFIRST] = wl3501_get_sens,
1920         [SIOCGIWRANGE   - SIOCIWFIRST] = wl3501_get_range,
1921         [SIOCSIWSPY     - SIOCIWFIRST] = iw_handler_set_spy,
1922         [SIOCGIWSPY     - SIOCIWFIRST] = iw_handler_get_spy,
1923         [SIOCSIWTHRSPY  - SIOCIWFIRST] = iw_handler_set_thrspy,
1924         [SIOCGIWTHRSPY  - SIOCIWFIRST] = iw_handler_get_thrspy,
1925         [SIOCSIWAP      - SIOCIWFIRST] = wl3501_set_wap,
1926         [SIOCGIWAP      - SIOCIWFIRST] = wl3501_get_wap,
1927         [SIOCSIWSCAN    - SIOCIWFIRST] = wl3501_set_scan,
1928         [SIOCGIWSCAN    - SIOCIWFIRST] = wl3501_get_scan,
1929         [SIOCSIWESSID   - SIOCIWFIRST] = wl3501_set_essid,
1930         [SIOCGIWESSID   - SIOCIWFIRST] = wl3501_get_essid,
1931         [SIOCSIWNICKN   - SIOCIWFIRST] = wl3501_set_nick,
1932         [SIOCGIWNICKN   - SIOCIWFIRST] = wl3501_get_nick,
1933         [SIOCGIWRATE    - SIOCIWFIRST] = wl3501_get_rate,
1934         [SIOCGIWRTS     - SIOCIWFIRST] = wl3501_get_rts_threshold,
1935         [SIOCGIWFRAG    - SIOCIWFIRST] = wl3501_get_frag_threshold,
1936         [SIOCGIWTXPOW   - SIOCIWFIRST] = wl3501_get_txpow,
1937         [SIOCGIWRETRY   - SIOCIWFIRST] = wl3501_get_retry,
1938         [SIOCGIWENCODE  - SIOCIWFIRST] = wl3501_get_encode,
1939         [SIOCGIWPOWER   - SIOCIWFIRST] = wl3501_get_power,
1940 };
1941
1942 static const struct iw_handler_def wl3501_handler_def = {
1943         .num_standard   = sizeof(wl3501_handler) / sizeof(iw_handler),
1944         .standard       = (iw_handler *)wl3501_handler,
1945         .get_wireless_stats = wl3501_get_wireless_stats,
1946 };
1947
1948 /**
1949  * wl3501_attach - creates an "instance" of the driver
1950  *
1951  * Creates an "instance" of the driver, allocating local data structures for
1952  * one device.  The device is registered with Card Services.
1953  *
1954  * The dev_link structure is initialized, but we don't actually configure the
1955  * card at this point -- we wait until we receive a card insertion event.
1956  */
1957 static dev_link_t *wl3501_attach(void)
1958 {
1959         client_reg_t client_reg;
1960         dev_link_t *link;
1961         struct net_device *dev;
1962         struct wl3501_card *this;
1963         int ret;
1964
1965         /* Initialize the dev_link_t structure */
1966         link = kzalloc(sizeof(*link), GFP_KERNEL);
1967         if (!link)
1968                 goto out;
1969
1970         /* The io structure describes IO port mapping */
1971         link->io.NumPorts1      = 16;
1972         link->io.Attributes1    = IO_DATA_PATH_WIDTH_8;
1973         link->io.IOAddrLines    = 5;
1974
1975         /* Interrupt setup */
1976         link->irq.Attributes    = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1977         link->irq.IRQInfo1      = IRQ_LEVEL_ID;
1978         link->irq.Handler = wl3501_interrupt;
1979
1980         /* General socket configuration */
1981         link->conf.Attributes   = CONF_ENABLE_IRQ;
1982         link->conf.Vcc          = 50;
1983         link->conf.IntType      = INT_MEMORY_AND_IO;
1984         link->conf.ConfigIndex  = 1;
1985         link->conf.Present      = PRESENT_OPTION;
1986
1987         dev = alloc_etherdev(sizeof(struct wl3501_card));
1988         if (!dev)
1989                 goto out_link;
1990         dev->open               = wl3501_open;
1991         dev->stop               = wl3501_close;
1992         dev->hard_start_xmit    = wl3501_hard_start_xmit;
1993         dev->tx_timeout         = wl3501_tx_timeout;
1994         dev->watchdog_timeo     = 5 * HZ;
1995         dev->get_stats          = wl3501_get_stats;
1996         this = dev->priv;
1997         this->wireless_data.spy_data = &this->spy_data;
1998         dev->wireless_data      = &this->wireless_data;
1999         dev->wireless_handlers  = (struct iw_handler_def *)&wl3501_handler_def;
2000         SET_ETHTOOL_OPS(dev, &ops);
2001         netif_stop_queue(dev);
2002         link->priv = link->irq.Instance = dev;
2003
2004         /* Register with Card Services */
2005         link->next               = wl3501_dev_list;
2006         wl3501_dev_list          = link;
2007         client_reg.dev_info      = &wl3501_dev_info;
2008         client_reg.Version       = 0x0210;
2009         client_reg.event_callback_args.client_data = link;
2010         ret = pcmcia_register_client(&link->handle, &client_reg);
2011         if (ret) {
2012                 cs_error(link->handle, RegisterClient, ret);
2013                 wl3501_detach(link->handle);
2014                 link = NULL;
2015         }
2016 out:
2017         return link;
2018 out_link:
2019         kfree(link);
2020         link = NULL;
2021         goto out;
2022 }
2023
2024 #define CS_CHECK(fn, ret) \
2025 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
2026
2027 /**
2028  * wl3501_config - configure the PCMCIA socket and make eth device available
2029  * @link - FILL_IN
2030  *
2031  * wl3501_config() is scheduled to run after a CARD_INSERTION event is
2032  * received, to configure the PCMCIA socket, and to make the ethernet device
2033  * available to the system.
2034  */
2035 static void wl3501_config(dev_link_t *link)
2036 {
2037         tuple_t tuple;
2038         cisparse_t parse;
2039         client_handle_t handle = link->handle;
2040         struct net_device *dev = link->priv;
2041         int i = 0, j, last_fn, last_ret;
2042         unsigned char bf[64];
2043         struct wl3501_card *this;
2044
2045         /* This reads the card's CONFIG tuple to find its config registers. */
2046         tuple.Attributes        = 0;
2047         tuple.DesiredTuple      = CISTPL_CONFIG;
2048         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
2049         tuple.TupleData         = bf;
2050         tuple.TupleDataMax      = sizeof(bf);
2051         tuple.TupleOffset       = 0;
2052         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
2053         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
2054         link->conf.ConfigBase   = parse.config.base;
2055         link->conf.Present      = parse.config.rmask[0];
2056
2057         /* Configure card */
2058         link->state |= DEV_CONFIG;
2059
2060         /* Try allocating IO ports.  This tries a few fixed addresses.  If you
2061          * want, you can also read the card's config table to pick addresses --
2062          * see the serial driver for an example. */
2063
2064         for (j = 0x280; j < 0x400; j += 0x20) {
2065                 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
2066                  * 0x200-0x2ff, and so on, because this seems safer */
2067                 link->io.BasePort1 = j;
2068                 link->io.BasePort2 = link->io.BasePort1 + 0x10;
2069                 i = pcmcia_request_io(link->handle, &link->io);
2070                 if (i == CS_SUCCESS)
2071                         break;
2072         }
2073         if (i != CS_SUCCESS) {
2074                 cs_error(link->handle, RequestIO, i);
2075                 goto failed;
2076         }
2077
2078         /* Now allocate an interrupt line. Note that this does not actually
2079          * assign a handler to the interrupt. */
2080
2081         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
2082
2083         /* This actually configures the PCMCIA socket -- setting up the I/O
2084          * windows and the interrupt mapping.  */
2085
2086         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
2087
2088         dev->irq = link->irq.AssignedIRQ;
2089         dev->base_addr = link->io.BasePort1;
2090         SET_NETDEV_DEV(dev, &handle_to_dev(handle));
2091         if (register_netdev(dev)) {
2092                 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2093                 goto failed;
2094         }
2095
2096         SET_MODULE_OWNER(dev);
2097
2098         this = dev->priv;
2099         /*
2100          * At this point, the dev_node_t structure(s) should be initialized and
2101          * arranged in a linked list at link->dev.
2102          */
2103         link->dev = &this->node;
2104         link->state &= ~DEV_CONFIG_PENDING;
2105
2106         this->base_addr = dev->base_addr;
2107
2108         if (!wl3501_get_flash_mac_addr(this)) {
2109                 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2110                        dev->name);
2111                 goto failed;
2112         }
2113         strcpy(this->node.dev_name, dev->name);
2114
2115         /* print probe information */
2116         printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2117                dev->name, this->base_addr, (int)dev->irq);
2118         for (i = 0; i < 6; i++) {
2119                 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2120                 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2121         }
2122         printk("\n");
2123         /*
2124          * Initialize card parameters - added by jss
2125          */
2126         this->net_type          = IW_MODE_INFRA;
2127         this->bss_cnt           = 0;
2128         this->join_sta_bss      = 0;
2129         this->adhoc_times       = 0;
2130         iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2131                                  "ANY", 3);
2132         this->card_name[0]      = '\0';
2133         this->firmware_date[0]  = '\0';
2134         this->rssi              = 255;
2135         this->chan              = iw_default_channel(this->reg_domain);
2136         strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2137         spin_lock_init(&this->lock);
2138         init_waitqueue_head(&this->wait);
2139         netif_start_queue(dev);
2140         goto out;
2141 cs_failed:
2142         cs_error(link->handle, last_fn, last_ret);
2143 failed:
2144         wl3501_release(link);
2145 out:
2146         return;
2147 }
2148
2149 /**
2150  * wl3501_release - unregister the net, release PCMCIA configuration
2151  * @arg - link
2152  *
2153  * After a card is removed, wl3501_release() will unregister the net device,
2154  * and release the PCMCIA configuration.  If the device is still open, this
2155  * will be postponed until it is closed.
2156  */
2157 static void wl3501_release(dev_link_t *link)
2158 {
2159         struct net_device *dev = link->priv;
2160
2161         /* Unlink the device chain */
2162         if (link->dev) {
2163                 unregister_netdev(dev);
2164                 link->dev = NULL;
2165         }
2166
2167         /* Don't bother checking to see if these succeed or not */
2168         pcmcia_release_configuration(link->handle);
2169         pcmcia_release_io(link->handle, &link->io);
2170         pcmcia_release_irq(link->handle, &link->irq);
2171         link->state &= ~DEV_CONFIG;
2172 }
2173
2174 static int wl3501_suspend(struct pcmcia_device *p_dev)
2175 {
2176         dev_link_t *link = dev_to_instance(p_dev);
2177         struct net_device *dev = link->priv;
2178
2179         link->state |= DEV_SUSPEND;
2180
2181         wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2182         if (link->state & DEV_CONFIG) {
2183                 if (link->open)
2184                         netif_device_detach(dev);
2185                 pcmcia_release_configuration(link->handle);
2186         }
2187
2188         return 0;
2189 }
2190
2191 static int wl3501_resume(struct pcmcia_device *p_dev)
2192 {
2193         dev_link_t *link = dev_to_instance(p_dev);
2194         struct net_device *dev = link->priv;
2195
2196         wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2197         if (link->state & DEV_CONFIG) {
2198                 pcmcia_request_configuration(link->handle, &link->conf);
2199                 if (link->open) {
2200                         wl3501_reset(dev);
2201                         netif_device_attach(dev);
2202                 }
2203         }
2204
2205         return 0;
2206 }
2207
2208
2209 /**
2210  * wl3501_event - The card status event handler
2211  * @event - event
2212  * @pri - priority
2213  * @args - arguments for this event
2214  *
2215  * The card status event handler. Mostly, this schedules other stuff to run
2216  * after an event is received. A CARD_REMOVAL event also sets some flags to
2217  * discourage the net drivers from trying to talk to the card any more.
2218  *
2219  * When a CARD_REMOVAL event is received, we immediately set a flag to block
2220  * future accesses to this device. All the functions that actually access the
2221  * device should check this flag to make sure the card is still present.
2222  */
2223 static int wl3501_event(event_t event, int pri, event_callback_args_t *args)
2224 {
2225         dev_link_t *link = args->client_data;
2226
2227         switch (event) {
2228         case CS_EVENT_CARD_INSERTION:
2229                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
2230                 wl3501_config(link);
2231                 break;
2232         }
2233         return 0;
2234 }
2235
2236 static struct pcmcia_device_id wl3501_ids[] = {
2237         PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2238         PCMCIA_DEVICE_NULL
2239 };
2240 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2241
2242 static struct pcmcia_driver wl3501_driver = {
2243         .owner          = THIS_MODULE,
2244         .drv            = {
2245                 .name   = "wl3501_cs",
2246         },
2247         .attach         = wl3501_attach,
2248         .event          = wl3501_event,
2249         .remove         = wl3501_detach,
2250         .id_table       = wl3501_ids,
2251         .suspend        = wl3501_suspend,
2252         .resume         = wl3501_resume,
2253 };
2254
2255 static int __init wl3501_init_module(void)
2256 {
2257         return pcmcia_register_driver(&wl3501_driver);
2258 }
2259
2260 static void __exit wl3501_exit_module(void)
2261 {
2262         dprintk(0, ": unloading");
2263         pcmcia_unregister_driver(&wl3501_driver);
2264         BUG_ON(wl3501_dev_list != NULL);
2265 }
2266
2267 module_init(wl3501_init_module);
2268 module_exit(wl3501_exit_module);
2269
2270 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2271               "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2272               "Gustavo Niemeyer <niemeyer@conectiva.com>");
2273 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2274 MODULE_LICENSE("GPL");