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