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