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