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