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