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