[PATCH] pcmcia: remove references to pcmcia/version.h
[linux-2.6] / drivers / net / wireless / wl3501_cs.c
1 /*
2  * WL3501 Wireless LAN PCMCIA Card Driver for Linux
3  * Written originally for Linux 2.0.30 by Fox Chen, mhchen@golf.ccl.itri.org.tw
4  * Ported to 2.2, 2.4 & 2.5 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5  * Wireless extensions in 2.4 by Gustavo Niemeyer <niemeyer@conectiva.com>
6  *
7  * References used by Fox Chen while writing the original driver for 2.0.30:
8  *
9  *   1. WL24xx packet drivers (tooasm.asm)
10  *   2. Access Point Firmware Interface Specification for IEEE 802.11 SUTRO
11  *   3. IEEE 802.11
12  *   4. Linux network driver (/usr/src/linux/drivers/net)
13  *   5. ISA card driver - wl24.c
14  *   6. Linux PCMCIA skeleton driver - skeleton.c
15  *   7. Linux PCMCIA 3c589 network driver - 3c589_cs.c
16  *
17  * Tested with WL2400 firmware 1.2, Linux 2.0.30, and pcmcia-cs-2.9.12
18  *   1. Performance: about 165 Kbytes/sec in TCP/IP with Ad-Hoc mode.
19  *      rsh 192.168.1.3 "dd if=/dev/zero bs=1k count=1000" > /dev/null
20  *      (Specification 2M bits/sec. is about 250 Kbytes/sec., but we must deduct
21  *       ETHER/IP/UDP/TCP header, and acknowledgement overhead)
22  *
23  * Tested with Planet AP in 2.4.17, 184 Kbytes/s in UDP in Infrastructure mode,
24  * 173 Kbytes/s in TCP.
25  *
26  * Tested with Planet AP in 2.5.73-bk, 216 Kbytes/s in Infrastructure mode
27  * with a SMP machine (dual pentium 100), using pktgen, 432 pps (pkt_size = 60)
28  */
29 #undef REALLY_SLOW_IO   /* most systems can safely undef this */
30
31 #include <linux/config.h>
32 #include <linux/delay.h>
33 #include <linux/types.h>
34 #include <linux/ethtool.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
37 #include <linux/in.h>
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/fcntl.h>
41 #include <linux/if_arp.h>
42 #include <linux/ioport.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/wireless.h>
49
50 #include <net/iw_handler.h>
51
52 #include <pcmcia/cs_types.h>
53 #include <pcmcia/cs.h>
54 #include <pcmcia/cistpl.h>
55 #include <pcmcia/cisreg.h>
56 #include <pcmcia/ds.h>
57
58 #include <asm/io.h>
59 #include <asm/uaccess.h>
60 #include <asm/system.h>
61
62 #include "wl3501.h"
63
64 #ifndef __i386__
65 #define slow_down_io()
66 #endif
67
68 /* For rough constant delay */
69 #define WL3501_NOPLOOP(n) { int x = 0; while (x++ < n) slow_down_io(); }
70
71 /*
72  * All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If you do not
73  * define PCMCIA_DEBUG at all, all the debug code will be left out.  If you
74  * compile with PCMCIA_DEBUG=0, the debug code will be present but disabled --
75  * but it can then be enabled for specific modules at load time with a
76  * 'pc_debug=#' option to insmod.
77  */
78 #define PCMCIA_DEBUG 0
79 #ifdef PCMCIA_DEBUG
80 static int pc_debug = PCMCIA_DEBUG;
81 module_param(pc_debug, int, 0);
82 #define dprintk(n, format, args...) \
83         { if (pc_debug > (n)) \
84                 printk(KERN_INFO "%s: " format "\n", __FUNCTION__ , ##args); }
85 #else
86 #define dprintk(n, format, args...)
87 #endif
88
89 #define wl3501_outb(a, b) { outb(a, b); slow_down_io(); }
90 #define wl3501_outb_p(a, b) { outb_p(a, b); slow_down_io(); }
91 #define wl3501_outsb(a, b, c) { outsb(a, b, c); slow_down_io(); }
92
93 #define WL3501_RELEASE_TIMEOUT (25 * HZ)
94 #define WL3501_MAX_ADHOC_TRIES 16
95
96 #define WL3501_RESUME   0
97 #define WL3501_SUSPEND  1
98
99 /*
100  * The event() function is this driver's Card Services event handler.  It will
101  * be called by Card Services when an appropriate card status event is
102  * received. The config() and release() entry points are used to configure or
103  * release a socket, in response to card insertion and ejection events.  They
104  * are invoked from the wl24 event handler.
105  */
106 static void wl3501_config(dev_link_t *link);
107 static void wl3501_release(dev_link_t *link);
108 static int wl3501_event(event_t event, int pri, event_callback_args_t *args);
109
110 /*
111  * The dev_info variable is the "key" that is used to match up this
112  * device driver with appropriate cards, through the card configuration
113  * database.
114  */
115 static dev_info_t wl3501_dev_info = "wl3501_cs";
116
117 static int wl3501_chan2freq[] = {
118         [0]  = 2412, [1]  = 2417, [2]  = 2422, [3]  = 2427, [4] = 2432,
119         [5]  = 2437, [6]  = 2442, [7]  = 2447, [8]  = 2452, [9] = 2457,
120         [10] = 2462, [11] = 2467, [12] = 2472, [13] = 2477,
121 };
122
123 static const struct {
124         int reg_domain;
125         int min, max, deflt;
126 } iw_channel_table[] = {
127         {
128                 .reg_domain = IW_REG_DOMAIN_FCC,
129                 .min        = 1,
130                 .max        = 11,
131                 .deflt      = 1,
132         },
133         {
134                 .reg_domain = IW_REG_DOMAIN_DOC,
135                 .min        = 1,
136                 .max        = 11,
137                 .deflt      = 1,
138         },
139         {
140                 .reg_domain = IW_REG_DOMAIN_ETSI,
141                 .min        = 1,
142                 .max        = 13,
143                 .deflt      = 1,
144         },
145         {
146                 .reg_domain = IW_REG_DOMAIN_SPAIN,
147                 .min        = 10,
148                 .max        = 11,
149                 .deflt      = 10,
150         },
151         {
152                 .reg_domain = IW_REG_DOMAIN_FRANCE,
153                 .min        = 10,
154                 .max        = 13,
155                 .deflt      = 10,
156         },
157         {
158                 .reg_domain = IW_REG_DOMAIN_MKK,
159                 .min        = 14,
160                 .max        = 14,
161                 .deflt      = 14,
162         },
163         {
164                 .reg_domain = IW_REG_DOMAIN_MKK1,
165                 .min        = 1,
166                 .max        = 14,
167                 .deflt      = 1,
168         },
169         {
170                 .reg_domain = IW_REG_DOMAIN_ISRAEL,
171                 .min        = 3,
172                 .max        = 9,
173                 .deflt      = 9,
174         },
175 };
176
177 /**
178  * iw_valid_channel - validate channel in regulatory domain
179  * @reg_comain - regulatory domain
180  * @channel - channel to validate
181  *
182  * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
183  */
184 static int iw_valid_channel(int reg_domain, int channel)
185 {
186         int i, rc = 0;
187
188         for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
189                 if (reg_domain == iw_channel_table[i].reg_domain) {
190                         rc = channel >= iw_channel_table[i].min &&
191                              channel <= iw_channel_table[i].max;
192                         break;
193                 }
194         return rc;
195 }
196
197 /**
198  * iw_default_channel - get default channel for a regulatory domain
199  * @reg_comain - regulatory domain
200  *
201  * Returns the default channel for a regulatory domain
202  */
203 static int iw_default_channel(int reg_domain)
204 {
205         int i, rc = 1;
206
207         for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
208                 if (reg_domain == iw_channel_table[i].reg_domain) {
209                         rc = iw_channel_table[i].deflt;
210                         break;
211                 }
212         return rc;
213 }
214
215 static void iw_set_mgmt_info_element(enum iw_mgmt_info_element_ids id,
216                                      struct iw_mgmt_info_element *el,
217                                      void *value, int len)
218 {
219         el->id  = id;
220         el->len = len;
221         memcpy(el->data, value, len);
222 }
223
224 static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
225                                       struct iw_mgmt_info_element *from)
226 {
227         iw_set_mgmt_info_element(from->id, to, from->data, from->len);
228 }
229
230 /*
231  * A linked list of "instances" of the wl24 device.  Each actual PCMCIA card
232  * corresponds to one device instance, and is described by one dev_link_t
233  * structure (defined in ds.h).
234  *
235  * You may not want to use a linked list for this -- for example, the memory
236  * card driver uses an array of dev_link_t pointers, where minor device numbers
237  * are used to derive the corresponding array index.
238  */
239 static dev_link_t *wl3501_dev_list;
240
241 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
242 {
243         wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
244 }
245
246 /*
247  * Get Ethernet MAC addresss.
248  *
249  * WARNING: We switch to FPAGE0 and switc back again.
250  *          Making sure there is no other WL function beening called by ISR.
251  */
252 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
253 {
254         int base_addr = this->base_addr;
255
256         /* get MAC addr */
257         wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
258         wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
259         wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
260
261         /* wait for reading EEPROM */
262         WL3501_NOPLOOP(100);
263         this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
264         WL3501_NOPLOOP(100);
265         this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
266         WL3501_NOPLOOP(100);
267         this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
268         WL3501_NOPLOOP(100);
269         this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
270         WL3501_NOPLOOP(100);
271         this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
272         WL3501_NOPLOOP(100);
273         this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
274         WL3501_NOPLOOP(100);
275         this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
276         WL3501_NOPLOOP(100);
277         wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
278         wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
279         wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
280         WL3501_NOPLOOP(100);
281         this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
282         WL3501_NOPLOOP(100);
283         this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
284         /* switch to SRAM Page 0 (for safety) */
285         wl3501_switch_page(this, WL3501_BSS_SPAGE0);
286
287         /* The MAC addr should be 00:60:... */
288         return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
289 }
290
291 /**
292  * wl3501_set_to_wla - Move 'size' bytes from PC to card
293  * @dest: Card addressing space
294  * @src: PC addressing space
295  * @size: Bytes to move
296  *
297  * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
298  */
299 void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src, 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 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 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 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(dev_link_t *link)
1501 {
1502         dev_link_t **linkp;
1503
1504         /* Locate device structure */
1505         for (linkp = &wl3501_dev_list; *linkp; linkp = &(*linkp)->next)
1506                 if (*linkp == link)
1507                         break;
1508         if (!*linkp)
1509                 goto out;
1510
1511         /* If the device is currently configured and active, we won't actually
1512          * delete it yet.  Instead, it is marked so that when the release()
1513          * function is called, that will trigger a proper detach(). */
1514
1515         if (link->state & DEV_CONFIG) {
1516 #ifdef PCMCIA_DEBUG
1517                 printk(KERN_DEBUG "wl3501_cs: detach postponed, '%s' "
1518                        "still locked\n", link->dev->dev_name);
1519 #endif
1520                 goto out;
1521         }
1522
1523         /* Break the link with Card Services */
1524         if (link->handle)
1525                 pcmcia_deregister_client(link->handle);
1526
1527         /* Unlink device structure, free pieces */
1528         *linkp = link->next;
1529
1530         if (link->priv)
1531                 free_netdev(link->priv);
1532         kfree(link);
1533 out:
1534         return;
1535 }
1536
1537 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1538                            union iwreq_data *wrqu, char *extra)
1539 {
1540         strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1541         return 0;
1542 }
1543
1544 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1545                            union iwreq_data *wrqu, char *extra)
1546 {
1547         struct wl3501_card *this = dev->priv;
1548         int channel = wrqu->freq.m;
1549         int rc = -EINVAL;
1550
1551         if (iw_valid_channel(this->reg_domain, channel)) {
1552                 this->chan = channel;
1553                 rc = wl3501_reset(dev);
1554         }
1555         return rc;
1556 }
1557
1558 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1559                            union iwreq_data *wrqu, char *extra)
1560 {
1561         struct wl3501_card *this = dev->priv;
1562
1563         wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1564         wrqu->freq.e = 1;
1565         return 0;
1566 }
1567
1568 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1569                            union iwreq_data *wrqu, char *extra)
1570 {
1571         int rc = -EINVAL;
1572
1573         if (wrqu->mode == IW_MODE_INFRA ||
1574             wrqu->mode == IW_MODE_ADHOC ||
1575             wrqu->mode == IW_MODE_AUTO) {
1576                 struct wl3501_card *this = dev->priv;
1577
1578                 this->net_type = wrqu->mode;
1579                 rc = wl3501_reset(dev);
1580         }
1581         return rc;
1582 }
1583
1584 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1585                            union iwreq_data *wrqu, char *extra)
1586 {
1587         struct wl3501_card *this = dev->priv;
1588
1589         wrqu->mode = this->net_type;
1590         return 0;
1591 }
1592
1593 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1594                            union iwreq_data *wrqu, char *extra)
1595 {
1596         struct wl3501_card *this = dev->priv;
1597
1598         wrqu->sens.value = this->rssi;
1599         wrqu->sens.disabled = !wrqu->sens.value;
1600         wrqu->sens.fixed = 1;
1601         return 0;
1602 }
1603
1604 static int wl3501_get_range(struct net_device *dev,
1605                             struct iw_request_info *info,
1606                             union iwreq_data *wrqu, char *extra)
1607 {
1608         struct iw_range *range = (struct iw_range *)extra;
1609
1610         /* Set the length (very important for backward compatibility) */
1611         wrqu->data.length = sizeof(*range);
1612
1613         /* Set all the info we don't care or don't know about to zero */
1614         memset(range, 0, sizeof(*range));
1615
1616         /* Set the Wireless Extension versions */
1617         range->we_version_compiled      = WIRELESS_EXT;
1618         range->we_version_source        = 1;
1619         range->throughput               = 2 * 1000 * 1000;     /* ~2 Mb/s */
1620         /* FIXME: study the code to fill in more fields... */
1621         return 0;
1622 }
1623
1624 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1625                           union iwreq_data *wrqu, char *extra)
1626 {
1627         struct wl3501_card *this = dev->priv;
1628         static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1629         int rc = -EINVAL;
1630
1631         /* FIXME: we support other ARPHRDs...*/
1632         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1633                 goto out;
1634         if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1635                 /* FIXME: rescan? */
1636         } else
1637                 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1638                 /* FIXME: rescan? deassoc & scan? */
1639         rc = 0;
1640 out:
1641         return rc;
1642 }
1643
1644 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1645                           union iwreq_data *wrqu, char *extra)
1646 {
1647         struct wl3501_card *this = dev->priv;
1648
1649         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1650         memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1651         return 0;
1652 }
1653
1654 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1655                            union iwreq_data *wrqu, char *extra)
1656 {
1657         /*
1658          * FIXME: trigger scanning with a reset, yes, I'm lazy
1659          */
1660         return wl3501_reset(dev);
1661 }
1662
1663 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1664                            union iwreq_data *wrqu, char *extra)
1665 {
1666         struct wl3501_card *this = dev->priv;
1667         int i;
1668         char *current_ev = extra;
1669         struct iw_event iwe;
1670
1671         for (i = 0; i < this->bss_cnt; ++i) {
1672                 iwe.cmd                 = SIOCGIWAP;
1673                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1674                 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1675                 current_ev = iwe_stream_add_event(current_ev,
1676                                                   extra + IW_SCAN_MAX_DATA,
1677                                                   &iwe, IW_EV_ADDR_LEN);
1678                 iwe.cmd           = SIOCGIWESSID;
1679                 iwe.u.data.flags  = 1;
1680                 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1681                 current_ev = iwe_stream_add_point(current_ev,
1682                                                   extra + IW_SCAN_MAX_DATA,
1683                                                   &iwe,
1684                                                   this->bss_set[i].ssid.essid);
1685                 iwe.cmd    = SIOCGIWMODE;
1686                 iwe.u.mode = this->bss_set[i].bss_type;
1687                 current_ev = iwe_stream_add_event(current_ev,
1688                                                   extra + IW_SCAN_MAX_DATA,
1689                                                   &iwe, IW_EV_UINT_LEN);
1690                 iwe.cmd = SIOCGIWFREQ;
1691                 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1692                 iwe.u.freq.e = 0;
1693                 current_ev = iwe_stream_add_event(current_ev,
1694                                                   extra + IW_SCAN_MAX_DATA,
1695                                                   &iwe, IW_EV_FREQ_LEN);
1696                 iwe.cmd = SIOCGIWENCODE;
1697                 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1698                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1699                 else
1700                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1701                 iwe.u.data.length = 0;
1702                 current_ev = iwe_stream_add_point(current_ev,
1703                                                   extra + IW_SCAN_MAX_DATA,
1704                                                   &iwe, NULL);
1705         }
1706         /* Length of data */
1707         wrqu->data.length = (current_ev - extra);
1708         wrqu->data.flags = 0; /* FIXME: set properly these flags */
1709         return 0;
1710 }
1711
1712 static int wl3501_set_essid(struct net_device *dev,
1713                             struct iw_request_info *info,
1714                             union iwreq_data *wrqu, char *extra)
1715 {
1716         struct wl3501_card *this = dev->priv;
1717
1718         if (wrqu->data.flags) {
1719                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1720                                          &this->essid.el,
1721                                          extra, wrqu->data.length);
1722         } else { /* We accept any ESSID */
1723                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1724                                          &this->essid.el, "ANY", 3);
1725         }
1726         return wl3501_reset(dev);
1727 }
1728
1729 static int wl3501_get_essid(struct net_device *dev,
1730                             struct iw_request_info *info,
1731                             union iwreq_data *wrqu, char *extra)
1732 {
1733         struct wl3501_card *this = dev->priv;
1734         unsigned long flags;
1735
1736         spin_lock_irqsave(&this->lock, flags);
1737         wrqu->essid.flags  = 1;
1738         wrqu->essid.length = this->essid.el.len;
1739         memcpy(extra, this->essid.essid, this->essid.el.len);
1740         spin_unlock_irqrestore(&this->lock, flags);
1741         return 0;
1742 }
1743
1744 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1745                            union iwreq_data *wrqu, char *extra)
1746 {
1747         struct wl3501_card *this = dev->priv;
1748
1749         if (wrqu->data.length > sizeof(this->nick))
1750                 return -E2BIG;
1751         strlcpy(this->nick, extra, wrqu->data.length);
1752         return 0;
1753 }
1754
1755 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1756                            union iwreq_data *wrqu, char *extra)
1757 {
1758         struct wl3501_card *this = dev->priv;
1759
1760         strlcpy(extra, this->nick, 32);
1761         wrqu->data.length = strlen(extra);
1762         return 0;
1763 }
1764
1765 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1766                            union iwreq_data *wrqu, char *extra)
1767 {
1768         /*
1769          * FIXME: have to see from where to get this info, perhaps this card
1770          * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1771          * common with the Planet Access Points. -acme
1772          */
1773         wrqu->bitrate.value = 2000000;
1774         wrqu->bitrate.fixed = 1;
1775         return 0;
1776 }
1777
1778 static int wl3501_get_rts_threshold(struct net_device *dev,
1779                                     struct iw_request_info *info,
1780                                     union iwreq_data *wrqu, char *extra)
1781 {
1782         u16 threshold; /* size checked: it is u16 */
1783         struct wl3501_card *this = dev->priv;
1784         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1785                                       &threshold, sizeof(threshold));
1786         if (!rc) {
1787                 wrqu->rts.value = threshold;
1788                 wrqu->rts.disabled = threshold >= 2347;
1789                 wrqu->rts.fixed = 1;
1790         }
1791         return rc;
1792 }
1793
1794 static int wl3501_get_frag_threshold(struct net_device *dev,
1795                                      struct iw_request_info *info,
1796                                      union iwreq_data *wrqu, char *extra)
1797 {
1798         u16 threshold; /* size checked: it is u16 */
1799         struct wl3501_card *this = dev->priv;
1800         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1801                                       &threshold, sizeof(threshold));
1802         if (!rc) {
1803                 wrqu->frag.value = threshold;
1804                 wrqu->frag.disabled = threshold >= 2346;
1805                 wrqu->frag.fixed = 1;
1806         }
1807         return rc;
1808 }
1809
1810 static int wl3501_get_txpow(struct net_device *dev,
1811                             struct iw_request_info *info,
1812                             union iwreq_data *wrqu, char *extra)
1813 {
1814         u16 txpow;
1815         struct wl3501_card *this = dev->priv;
1816         int rc = wl3501_get_mib_value(this,
1817                                       WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1818                                       &txpow, sizeof(txpow));
1819         if (!rc) {
1820                 wrqu->txpower.value = txpow;
1821                 wrqu->txpower.disabled = 0;
1822                 /*
1823                  * From the MIB values I think this can be configurable,
1824                  * as it lists several tx power levels -acme
1825                  */
1826                 wrqu->txpower.fixed = 0;
1827                 wrqu->txpower.flags = IW_TXPOW_MWATT;
1828         }
1829         return rc;
1830 }
1831
1832 static int wl3501_get_retry(struct net_device *dev,
1833                             struct iw_request_info *info,
1834                             union iwreq_data *wrqu, char *extra)
1835 {
1836         u8 retry; /* size checked: it is u8 */
1837         struct wl3501_card *this = dev->priv;
1838         int rc = wl3501_get_mib_value(this,
1839                                       WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1840                                       &retry, sizeof(retry));
1841         if (rc)
1842                 goto out;
1843         if (wrqu->retry.flags & IW_RETRY_MAX) {
1844                 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1845                 goto set_value;
1846         }
1847         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1848                                   &retry, sizeof(retry));
1849         if (rc)
1850                 goto out;
1851         wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1852 set_value:
1853         wrqu->retry.value = retry;
1854         wrqu->retry.disabled = 0;
1855 out:
1856         return rc;
1857 }
1858
1859 static int wl3501_get_encode(struct net_device *dev,
1860                              struct iw_request_info *info,
1861                              union iwreq_data *wrqu, char *extra)
1862 {
1863         u8 implemented, restricted, keys[100], len_keys, tocopy;
1864         struct wl3501_card *this = dev->priv;
1865         int rc = wl3501_get_mib_value(this,
1866                                       WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1867                                       &implemented, sizeof(implemented));
1868         if (rc)
1869                 goto out;
1870         if (!implemented) {
1871                 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1872                 goto out;
1873         }
1874         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1875                                   &restricted, sizeof(restricted));
1876         if (rc)
1877                 goto out;
1878         wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1879                                             IW_ENCODE_OPEN;
1880         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1881                                   &len_keys, sizeof(len_keys));
1882         if (rc)
1883                 goto out;
1884         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1885                                   keys, len_keys);
1886         if (rc)
1887                 goto out;
1888         tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1889         tocopy = min_t(u8, tocopy, 100);
1890         wrqu->encoding.length = tocopy;
1891         memset(extra, 0, tocopy);
1892         memcpy(extra, keys, tocopy);
1893 out:
1894         return rc;
1895 }
1896
1897 static int wl3501_get_power(struct net_device *dev,
1898                             struct iw_request_info *info,
1899                             union iwreq_data *wrqu, char *extra)
1900 {
1901         u8 pwr_state;
1902         struct wl3501_card *this = dev->priv;
1903         int rc = wl3501_get_mib_value(this,
1904                                       WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1905                                       &pwr_state, sizeof(pwr_state));
1906         if (rc)
1907                 goto out;
1908         wrqu->power.disabled = !pwr_state;
1909         wrqu->power.flags = IW_POWER_ON;
1910 out:
1911         return rc;
1912 }
1913
1914 static const iw_handler wl3501_handler[] = {
1915         [SIOCGIWNAME    - SIOCIWFIRST] = wl3501_get_name,
1916         [SIOCSIWFREQ    - SIOCIWFIRST] = wl3501_set_freq,
1917         [SIOCGIWFREQ    - SIOCIWFIRST] = wl3501_get_freq,
1918         [SIOCSIWMODE    - SIOCIWFIRST] = wl3501_set_mode,
1919         [SIOCGIWMODE    - SIOCIWFIRST] = wl3501_get_mode,
1920         [SIOCGIWSENS    - SIOCIWFIRST] = wl3501_get_sens,
1921         [SIOCGIWRANGE   - SIOCIWFIRST] = wl3501_get_range,
1922         [SIOCSIWSPY     - SIOCIWFIRST] = iw_handler_set_spy,
1923         [SIOCGIWSPY     - SIOCIWFIRST] = iw_handler_get_spy,
1924         [SIOCSIWTHRSPY  - SIOCIWFIRST] = iw_handler_set_thrspy,
1925         [SIOCGIWTHRSPY  - SIOCIWFIRST] = iw_handler_get_thrspy,
1926         [SIOCSIWAP      - SIOCIWFIRST] = wl3501_set_wap,
1927         [SIOCGIWAP      - SIOCIWFIRST] = wl3501_get_wap,
1928         [SIOCSIWSCAN    - SIOCIWFIRST] = wl3501_set_scan,
1929         [SIOCGIWSCAN    - SIOCIWFIRST] = wl3501_get_scan,
1930         [SIOCSIWESSID   - SIOCIWFIRST] = wl3501_set_essid,
1931         [SIOCGIWESSID   - SIOCIWFIRST] = wl3501_get_essid,
1932         [SIOCSIWNICKN   - SIOCIWFIRST] = wl3501_set_nick,
1933         [SIOCGIWNICKN   - SIOCIWFIRST] = wl3501_get_nick,
1934         [SIOCGIWRATE    - SIOCIWFIRST] = wl3501_get_rate,
1935         [SIOCGIWRTS     - SIOCIWFIRST] = wl3501_get_rts_threshold,
1936         [SIOCGIWFRAG    - SIOCIWFIRST] = wl3501_get_frag_threshold,
1937         [SIOCGIWTXPOW   - SIOCIWFIRST] = wl3501_get_txpow,
1938         [SIOCGIWRETRY   - SIOCIWFIRST] = wl3501_get_retry,
1939         [SIOCGIWENCODE  - SIOCIWFIRST] = wl3501_get_encode,
1940         [SIOCGIWPOWER   - SIOCIWFIRST] = wl3501_get_power,
1941 };
1942
1943 static const struct iw_handler_def wl3501_handler_def = {
1944         .num_standard   = sizeof(wl3501_handler) / sizeof(iw_handler),
1945         .standard       = (iw_handler *)wl3501_handler,
1946         .spy_offset     = offsetof(struct wl3501_card, spy_data),
1947 };
1948
1949 /**
1950  * wl3501_attach - creates an "instance" of the driver
1951  *
1952  * Creates an "instance" of the driver, allocating local data structures for
1953  * one device.  The device is registered with Card Services.
1954  *
1955  * The dev_link structure is initialized, but we don't actually configure the
1956  * card at this point -- we wait until we receive a card insertion event.
1957  */
1958 static dev_link_t *wl3501_attach(void)
1959 {
1960         client_reg_t client_reg;
1961         dev_link_t *link;
1962         struct net_device *dev;
1963         int ret;
1964
1965         /* Initialize the dev_link_t structure */
1966         link = kmalloc(sizeof(*link), GFP_KERNEL);
1967         if (!link)
1968                 goto out;
1969         memset(link, 0, sizeof(struct dev_link_t));
1970
1971         /* The io structure describes IO port mapping */
1972         link->io.NumPorts1      = 16;
1973         link->io.Attributes1    = IO_DATA_PATH_WIDTH_8;
1974         link->io.IOAddrLines    = 5;
1975
1976         /* Interrupt setup */
1977         link->irq.Attributes    = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1978         link->irq.IRQInfo1      = IRQ_LEVEL_ID;
1979         link->irq.Handler = wl3501_interrupt;
1980
1981         /* General socket configuration */
1982         link->conf.Attributes   = CONF_ENABLE_IRQ;
1983         link->conf.Vcc          = 50;
1984         link->conf.IntType      = INT_MEMORY_AND_IO;
1985         link->conf.ConfigIndex  = 1;
1986         link->conf.Present      = PRESENT_OPTION;
1987
1988         dev = alloc_etherdev(sizeof(struct wl3501_card));
1989         if (!dev)
1990                 goto out_link;
1991         dev->open               = wl3501_open;
1992         dev->stop               = wl3501_close;
1993         dev->hard_start_xmit    = wl3501_hard_start_xmit;
1994         dev->tx_timeout         = wl3501_tx_timeout;
1995         dev->watchdog_timeo     = 5 * HZ;
1996         dev->get_stats          = wl3501_get_stats;
1997         dev->get_wireless_stats = wl3501_get_wireless_stats;
1998         dev->wireless_handlers  = (struct iw_handler_def *)&wl3501_handler_def;
1999         SET_ETHTOOL_OPS(dev, &ops);
2000         netif_stop_queue(dev);
2001         link->priv = link->irq.Instance = dev;
2002
2003         /* Register with Card Services */
2004         link->next               = wl3501_dev_list;
2005         wl3501_dev_list          = link;
2006         client_reg.dev_info      = &wl3501_dev_info;
2007         client_reg.Version       = 0x0210;
2008         client_reg.event_callback_args.client_data = link;
2009         ret = pcmcia_register_client(&link->handle, &client_reg);
2010         if (ret) {
2011                 cs_error(link->handle, RegisterClient, ret);
2012                 wl3501_detach(link);
2013                 link = NULL;
2014         }
2015 out:
2016         return link;
2017 out_link:
2018         kfree(link);
2019         link = NULL;
2020         goto out;
2021 }
2022
2023 #define CS_CHECK(fn, ret) \
2024 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
2025
2026 /**
2027  * wl3501_config - configure the PCMCIA socket and make eth device available
2028  * @link - FILL_IN
2029  *
2030  * wl3501_config() is scheduled to run after a CARD_INSERTION event is
2031  * received, to configure the PCMCIA socket, and to make the ethernet device
2032  * available to the system.
2033  */
2034 static void wl3501_config(dev_link_t *link)
2035 {
2036         tuple_t tuple;
2037         cisparse_t parse;
2038         client_handle_t handle = link->handle;
2039         struct net_device *dev = link->priv;
2040         int i = 0, j, last_fn, last_ret;
2041         unsigned char bf[64];
2042         struct wl3501_card *this;
2043
2044         /* This reads the card's CONFIG tuple to find its config registers. */
2045         tuple.Attributes        = 0;
2046         tuple.DesiredTuple      = CISTPL_CONFIG;
2047         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
2048         tuple.TupleData         = bf;
2049         tuple.TupleDataMax      = sizeof(bf);
2050         tuple.TupleOffset       = 0;
2051         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
2052         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
2053         link->conf.ConfigBase   = parse.config.base;
2054         link->conf.Present      = parse.config.rmask[0];
2055
2056         /* Configure card */
2057         link->state |= DEV_CONFIG;
2058
2059         /* Try allocating IO ports.  This tries a few fixed addresses.  If you
2060          * want, you can also read the card's config table to pick addresses --
2061          * see the serial driver for an example. */
2062
2063         for (j = 0x280; j < 0x400; j += 0x20) {
2064                 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
2065                  * 0x200-0x2ff, and so on, because this seems safer */
2066                 link->io.BasePort1 = j;
2067                 link->io.BasePort2 = link->io.BasePort1 + 0x10;
2068                 i = pcmcia_request_io(link->handle, &link->io);
2069                 if (i == CS_SUCCESS)
2070                         break;
2071         }
2072         if (i != CS_SUCCESS) {
2073                 cs_error(link->handle, RequestIO, i);
2074                 goto failed;
2075         }
2076
2077         /* Now allocate an interrupt line. Note that this does not actually
2078          * assign a handler to the interrupt. */
2079
2080         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
2081
2082         /* This actually configures the PCMCIA socket -- setting up the I/O
2083          * windows and the interrupt mapping.  */
2084
2085         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
2086
2087         dev->irq = link->irq.AssignedIRQ;
2088         dev->base_addr = link->io.BasePort1;
2089         SET_NETDEV_DEV(dev, &handle_to_dev(handle));
2090         if (register_netdev(dev)) {
2091                 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2092                 goto failed;
2093         }
2094
2095         SET_MODULE_OWNER(dev);
2096
2097         this = dev->priv;
2098         /*
2099          * At this point, the dev_node_t structure(s) should be initialized and
2100          * arranged in a linked list at link->dev.
2101          */
2102         link->dev = &this->node;
2103         link->state &= ~DEV_CONFIG_PENDING;
2104
2105         this->base_addr = dev->base_addr;
2106
2107         if (!wl3501_get_flash_mac_addr(this)) {
2108                 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2109                        dev->name);
2110                 goto failed;
2111         }
2112         strcpy(this->node.dev_name, dev->name);
2113
2114         /* print probe information */
2115         printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2116                dev->name, this->base_addr, (int)dev->irq);
2117         for (i = 0; i < 6; i++) {
2118                 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2119                 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2120         }
2121         printk("\n");
2122         /*
2123          * Initialize card parameters - added by jss
2124          */
2125         this->net_type          = IW_MODE_INFRA;
2126         this->bss_cnt           = 0;
2127         this->join_sta_bss      = 0;
2128         this->adhoc_times       = 0;
2129         iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2130                                  "ANY", 3);
2131         this->card_name[0]      = '\0';
2132         this->firmware_date[0]  = '\0';
2133         this->rssi              = 255;
2134         this->chan              = iw_default_channel(this->reg_domain);
2135         strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2136         spin_lock_init(&this->lock);
2137         init_waitqueue_head(&this->wait);
2138         netif_start_queue(dev);
2139         goto out;
2140 cs_failed:
2141         cs_error(link->handle, last_fn, last_ret);
2142 failed:
2143         wl3501_release(link);
2144 out:
2145         return;
2146 }
2147
2148 /**
2149  * wl3501_release - unregister the net, release PCMCIA configuration
2150  * @arg - link
2151  *
2152  * After a card is removed, wl3501_release() will unregister the net device,
2153  * and release the PCMCIA configuration.  If the device is still open, this
2154  * will be postponed until it is closed.
2155  */
2156 static void wl3501_release(dev_link_t *link)
2157 {
2158         struct net_device *dev = link->priv;
2159
2160         /* Unlink the device chain */
2161         if (link->dev) {
2162                 unregister_netdev(dev);
2163                 link->dev = NULL;
2164         }
2165
2166         /* Don't bother checking to see if these succeed or not */
2167         pcmcia_release_configuration(link->handle);
2168         pcmcia_release_io(link->handle, &link->io);
2169         pcmcia_release_irq(link->handle, &link->irq);
2170         link->state &= ~DEV_CONFIG;
2171 }
2172
2173 /**
2174  * wl3501_event - The card status event handler
2175  * @event - event
2176  * @pri - priority
2177  * @args - arguments for this event
2178  *
2179  * The card status event handler. Mostly, this schedules other stuff to run
2180  * after an event is received. A CARD_REMOVAL event also sets some flags to
2181  * discourage the net drivers from trying to talk to the card any more.
2182  *
2183  * When a CARD_REMOVAL event is received, we immediately set a flag to block
2184  * future accesses to this device. All the functions that actually access the
2185  * device should check this flag to make sure the card is still present.
2186  */
2187 static int wl3501_event(event_t event, int pri, event_callback_args_t *args)
2188 {
2189         dev_link_t *link = args->client_data;
2190         struct net_device *dev = link->priv;
2191
2192         switch (event) {
2193         case CS_EVENT_CARD_REMOVAL:
2194                 link->state &= ~DEV_PRESENT;
2195                 if (link->state & DEV_CONFIG) {
2196                         while (link->open > 0)
2197                                 wl3501_close(dev);
2198                         netif_device_detach(dev);
2199                         wl3501_release(link);
2200                 }
2201                 break;
2202         case CS_EVENT_CARD_INSERTION:
2203                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
2204                 wl3501_config(link);
2205                 break;
2206         case CS_EVENT_PM_SUSPEND:
2207                 link->state |= DEV_SUSPEND;
2208                 wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2209                 /* Fall through... */
2210         case CS_EVENT_RESET_PHYSICAL:
2211                 if (link->state & DEV_CONFIG) {
2212                         if (link->open)
2213                                 netif_device_detach(dev);
2214                         pcmcia_release_configuration(link->handle);
2215                 }
2216                 break;
2217         case CS_EVENT_PM_RESUME:
2218                 link->state &= ~DEV_SUSPEND;
2219                 wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2220                 /* Fall through... */
2221         case CS_EVENT_CARD_RESET:
2222                 if (link->state & DEV_CONFIG) {
2223                         pcmcia_request_configuration(link->handle, &link->conf);
2224                         if (link->open) {
2225                                 wl3501_reset(dev);
2226                                 netif_device_attach(dev);
2227                         }
2228                 }
2229                 break;
2230         }
2231         return 0;
2232 }
2233
2234 static struct pcmcia_device_id wl3501_ids[] = {
2235         PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2236         PCMCIA_DEVICE_NULL
2237 };
2238 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2239
2240 static struct pcmcia_driver wl3501_driver = {
2241         .owner          = THIS_MODULE,
2242         .drv            = {
2243                 .name   = "wl3501_cs",
2244         },
2245         .attach         = wl3501_attach,
2246         .event          = wl3501_event,
2247         .detach         = wl3501_detach,
2248         .id_table       = wl3501_ids,
2249 };
2250
2251 static int __init wl3501_init_module(void)
2252 {
2253         return pcmcia_register_driver(&wl3501_driver);
2254 }
2255
2256 static void __exit wl3501_exit_module(void)
2257 {
2258         dprintk(0, ": unloading");
2259         pcmcia_unregister_driver(&wl3501_driver);
2260         BUG_ON(wl3501_dev_list != NULL);
2261 }
2262
2263 module_init(wl3501_init_module);
2264 module_exit(wl3501_exit_module);
2265
2266 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2267               "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2268               "Gustavo Niemeyer <niemeyer@conectiva.com>");
2269 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2270 MODULE_LICENSE("GPL");