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