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