Merge branch 'x86' of git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile...
[pandora-kernel.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/ethtool.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/in.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/fcntl.h>
39 #include <linux/if_arp.h>
40 #include <linux/ioport.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/slab.h>
45 #include <linux/string.h>
46 #include <linux/wireless.h>
47 #include <linux/ieee80211.h>
48
49 #include <net/iw_handler.h>
50
51 #include <pcmcia/cistpl.h>
52 #include <pcmcia/cisreg.h>
53 #include <pcmcia/ds.h>
54
55 #include <asm/io.h>
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58
59 #include "wl3501.h"
60
61 #ifndef __i386__
62 #define slow_down_io()
63 #endif
64
65 /* For rough constant delay */
66 #define WL3501_NOPLOOP(n) { int x = 0; while (x++ < n) slow_down_io(); }
67
68
69
70 #define wl3501_outb(a, b) { outb(a, b); slow_down_io(); }
71 #define wl3501_outb_p(a, b) { outb_p(a, b); slow_down_io(); }
72 #define wl3501_outsb(a, b, c) { outsb(a, b, c); slow_down_io(); }
73
74 #define WL3501_RELEASE_TIMEOUT (25 * HZ)
75 #define WL3501_MAX_ADHOC_TRIES 16
76
77 #define WL3501_RESUME   0
78 #define WL3501_SUSPEND  1
79
80 static int wl3501_config(struct pcmcia_device *link);
81 static void wl3501_release(struct pcmcia_device *link);
82
83 static const struct {
84         int reg_domain;
85         int min, max, deflt;
86 } iw_channel_table[] = {
87         {
88                 .reg_domain = IW_REG_DOMAIN_FCC,
89                 .min        = 1,
90                 .max        = 11,
91                 .deflt      = 1,
92         },
93         {
94                 .reg_domain = IW_REG_DOMAIN_DOC,
95                 .min        = 1,
96                 .max        = 11,
97                 .deflt      = 1,
98         },
99         {
100                 .reg_domain = IW_REG_DOMAIN_ETSI,
101                 .min        = 1,
102                 .max        = 13,
103                 .deflt      = 1,
104         },
105         {
106                 .reg_domain = IW_REG_DOMAIN_SPAIN,
107                 .min        = 10,
108                 .max        = 11,
109                 .deflt      = 10,
110         },
111         {
112                 .reg_domain = IW_REG_DOMAIN_FRANCE,
113                 .min        = 10,
114                 .max        = 13,
115                 .deflt      = 10,
116         },
117         {
118                 .reg_domain = IW_REG_DOMAIN_MKK,
119                 .min        = 14,
120                 .max        = 14,
121                 .deflt      = 14,
122         },
123         {
124                 .reg_domain = IW_REG_DOMAIN_MKK1,
125                 .min        = 1,
126                 .max        = 14,
127                 .deflt      = 1,
128         },
129         {
130                 .reg_domain = IW_REG_DOMAIN_ISRAEL,
131                 .min        = 3,
132                 .max        = 9,
133                 .deflt      = 9,
134         },
135 };
136
137 /**
138  * iw_valid_channel - validate channel in regulatory domain
139  * @reg_comain - regulatory domain
140  * @channel - channel to validate
141  *
142  * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
143  */
144 static int iw_valid_channel(int reg_domain, int channel)
145 {
146         int i, rc = 0;
147
148         for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
149                 if (reg_domain == iw_channel_table[i].reg_domain) {
150                         rc = channel >= iw_channel_table[i].min &&
151                              channel <= iw_channel_table[i].max;
152                         break;
153                 }
154         return rc;
155 }
156
157 /**
158  * iw_default_channel - get default channel for a regulatory domain
159  * @reg_comain - regulatory domain
160  *
161  * Returns the default channel for a regulatory domain
162  */
163 static int iw_default_channel(int reg_domain)
164 {
165         int i, rc = 1;
166
167         for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
168                 if (reg_domain == iw_channel_table[i].reg_domain) {
169                         rc = iw_channel_table[i].deflt;
170                         break;
171                 }
172         return rc;
173 }
174
175 static void iw_set_mgmt_info_element(enum iw_mgmt_info_element_ids id,
176                                      struct iw_mgmt_info_element *el,
177                                      void *value, int len)
178 {
179         el->id  = id;
180         el->len = len;
181         memcpy(el->data, value, len);
182 }
183
184 static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
185                                       struct iw_mgmt_info_element *from)
186 {
187         iw_set_mgmt_info_element(from->id, to, from->data, from->len);
188 }
189
190 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
191 {
192         wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
193 }
194
195 /*
196  * Get Ethernet MAC addresss.
197  *
198  * WARNING: We switch to FPAGE0 and switc back again.
199  *          Making sure there is no other WL function beening called by ISR.
200  */
201 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
202 {
203         int base_addr = this->base_addr;
204
205         /* get MAC addr */
206         wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
207         wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
208         wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
209
210         /* wait for reading EEPROM */
211         WL3501_NOPLOOP(100);
212         this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
213         WL3501_NOPLOOP(100);
214         this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
215         WL3501_NOPLOOP(100);
216         this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
217         WL3501_NOPLOOP(100);
218         this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
219         WL3501_NOPLOOP(100);
220         this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
221         WL3501_NOPLOOP(100);
222         this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
223         WL3501_NOPLOOP(100);
224         this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
225         WL3501_NOPLOOP(100);
226         wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
227         wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
228         wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
229         WL3501_NOPLOOP(100);
230         this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
231         WL3501_NOPLOOP(100);
232         this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
233         /* switch to SRAM Page 0 (for safety) */
234         wl3501_switch_page(this, WL3501_BSS_SPAGE0);
235
236         /* The MAC addr should be 00:60:... */
237         return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
238 }
239
240 /**
241  * wl3501_set_to_wla - Move 'size' bytes from PC to card
242  * @dest: Card addressing space
243  * @src: PC addressing space
244  * @size: Bytes to move
245  *
246  * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
247  */
248 static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
249                               int size)
250 {
251         /* switch to SRAM Page 0 */
252         wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
253                                                    WL3501_BSS_SPAGE0);
254         /* set LMAL and LMAH */
255         wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
256         wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
257
258         /* rep out to Port A */
259         wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
260 }
261
262 /**
263  * wl3501_get_from_wla - Move 'size' bytes from card to PC
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 = Postion 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_get_mib_value(struct wl3501_card *this, u8 index,
385                                 void *bf, int size)
386 {
387         struct wl3501_get_req sig = {
388                 .sig_id     = WL3501_SIG_GET_REQ,
389                 .mib_attrib = index,
390         };
391         unsigned long flags;
392         int rc = -EIO;
393
394         spin_lock_irqsave(&this->lock, flags);
395         if (wl3501_esbq_req_test(this)) {
396                 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
397                 if (ptr) {
398                         wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
399                         wl3501_esbq_req(this, &ptr);
400                         this->sig_get_confirm.mib_status = 255;
401                         spin_unlock_irqrestore(&this->lock, flags);
402                         rc = wait_event_interruptible(this->wait,
403                                 this->sig_get_confirm.mib_status != 255);
404                         if (!rc)
405                                 memcpy(bf, this->sig_get_confirm.mib_value,
406                                        size);
407                         goto out;
408                 }
409         }
410         spin_unlock_irqrestore(&this->lock, flags);
411 out:
412         return rc;
413 }
414
415 static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
416 {
417         struct wl3501_pwr_mgmt_req sig = {
418                 .sig_id         = WL3501_SIG_PWR_MGMT_REQ,
419                 .pwr_save       = suspend,
420                 .wake_up        = !suspend,
421                 .receive_dtims  = 10,
422         };
423         unsigned long flags;
424         int rc = -EIO;
425
426         spin_lock_irqsave(&this->lock, flags);
427         if (wl3501_esbq_req_test(this)) {
428                 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
429                 if (ptr) {
430                         wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
431                         wl3501_esbq_req(this, &ptr);
432                         this->sig_pwr_mgmt_confirm.status = 255;
433                         spin_unlock_irqrestore(&this->lock, flags);
434                         rc = wait_event_interruptible(this->wait,
435                                 this->sig_pwr_mgmt_confirm.status != 255);
436                         printk(KERN_INFO "%s: %s status=%d\n", __func__,
437                                suspend ? "suspend" : "resume",
438                                this->sig_pwr_mgmt_confirm.status);
439                         goto out;
440                 }
441         }
442         spin_unlock_irqrestore(&this->lock, flags);
443 out:
444         return rc;
445 }
446
447 /**
448  * wl3501_send_pkt - Send a packet.
449  * @this - card
450  *
451  * Send a packet.
452  *
453  * data = Ethernet raw frame.  (e.g. data[0] - data[5] is Dest MAC Addr,
454  *                                   data[6] - data[11] is Src MAC Addr)
455  * Ref: IEEE 802.11
456  */
457 static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
458 {
459         u16 bf, sig_bf, next, tmplen, pktlen;
460         struct wl3501_md_req sig = {
461                 .sig_id = WL3501_SIG_MD_REQ,
462         };
463         u8 *pdata = (char *)data;
464         int rc = -EIO;
465
466         if (wl3501_esbq_req_test(this)) {
467                 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
468                 rc = -ENOMEM;
469                 if (!sig_bf)    /* No free buffer available */
470                         goto out;
471                 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
472                 if (!bf) {
473                         /* No free buffer available */
474                         wl3501_free_tx_buffer(this, sig_bf);
475                         goto out;
476                 }
477                 rc = 0;
478                 memcpy(&sig.daddr[0], pdata, 12);
479                 pktlen = len - 12;
480                 pdata += 12;
481                 sig.data = bf;
482                 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
483                         u8 addr4[ETH_ALEN] = {
484                                 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
485                         };
486
487                         wl3501_set_to_wla(this, bf + 2 +
488                                           offsetof(struct wl3501_tx_hdr, addr4),
489                                           addr4, sizeof(addr4));
490                         sig.size = pktlen + 24 + 4 + 6;
491                         if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
492                                 tmplen = 254 - sizeof(struct wl3501_tx_hdr);
493                                 pktlen -= tmplen;
494                         } else {
495                                 tmplen = pktlen;
496                                 pktlen = 0;
497                         }
498                         wl3501_set_to_wla(this,
499                                           bf + 2 + sizeof(struct wl3501_tx_hdr),
500                                           pdata, tmplen);
501                         pdata += tmplen;
502                         wl3501_get_from_wla(this, bf, &next, sizeof(next));
503                         bf = next;
504                 } else {
505                         sig.size = pktlen + 24 + 4 - 2;
506                         pdata += 2;
507                         pktlen -= 2;
508                         if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
509                                 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
510                                 pktlen -= tmplen;
511                         } else {
512                                 tmplen = pktlen;
513                                 pktlen = 0;
514                         }
515                         wl3501_set_to_wla(this, bf + 2 +
516                                           offsetof(struct wl3501_tx_hdr, addr4),
517                                           pdata, tmplen);
518                         pdata += tmplen;
519                         wl3501_get_from_wla(this, bf, &next, sizeof(next));
520                         bf = next;
521                 }
522                 while (pktlen > 0) {
523                         if (pktlen > 254) {
524                                 tmplen = 254;
525                                 pktlen -= 254;
526                         } else {
527                                 tmplen = pktlen;
528                                 pktlen = 0;
529                         }
530                         wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
531                         pdata += tmplen;
532                         wl3501_get_from_wla(this, bf, &next, sizeof(next));
533                         bf = next;
534                 }
535                 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
536                 wl3501_esbq_req(this, &sig_bf);
537         }
538 out:
539         return rc;
540 }
541
542 static int wl3501_mgmt_resync(struct wl3501_card *this)
543 {
544         struct wl3501_resync_req sig = {
545                 .sig_id = WL3501_SIG_RESYNC_REQ,
546         };
547
548         return wl3501_esbq_exec(this, &sig, sizeof(sig));
549 }
550
551 static inline int wl3501_fw_bss_type(struct wl3501_card *this)
552 {
553         return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
554                                                  WL3501_NET_TYPE_ADHOC;
555 }
556
557 static inline int wl3501_fw_cap_info(struct wl3501_card *this)
558 {
559         return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
560                                                  WL3501_MGMT_CAPABILITY_IBSS;
561 }
562
563 static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
564 {
565         struct wl3501_scan_req sig = {
566                 .sig_id         = WL3501_SIG_SCAN_REQ,
567                 .scan_type      = WL3501_SCAN_TYPE_ACTIVE,
568                 .probe_delay    = 0x10,
569                 .min_chan_time  = chan_time,
570                 .max_chan_time  = chan_time,
571                 .bss_type       = wl3501_fw_bss_type(this),
572         };
573
574         this->bss_cnt = this->join_sta_bss = 0;
575         return wl3501_esbq_exec(this, &sig, sizeof(sig));
576 }
577
578 static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
579 {
580         struct wl3501_join_req sig = {
581                 .sig_id           = WL3501_SIG_JOIN_REQ,
582                 .timeout          = 10,
583                 .ds_pset = {
584                         .el = {
585                                 .id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
586                                 .len = 1,
587                         },
588                         .chan   = this->chan,
589                 },
590         };
591
592         memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
593         return wl3501_esbq_exec(this, &sig, sizeof(sig));
594 }
595
596 static int wl3501_mgmt_start(struct wl3501_card *this)
597 {
598         struct wl3501_start_req sig = {
599                 .sig_id                 = WL3501_SIG_START_REQ,
600                 .beacon_period          = 400,
601                 .dtim_period            = 1,
602                 .ds_pset = {
603                         .el = {
604                                 .id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
605                                 .len = 1,
606                         },
607                         .chan   = this->chan,
608                 },
609                 .bss_basic_rset = {
610                         .el = {
611                                 .id     = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
612                                 .len = 2,
613                         },
614                         .data_rate_labels = {
615                                 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
616                                       IW_MGMT_RATE_LABEL_1MBIT,
617                                 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
618                                       IW_MGMT_RATE_LABEL_2MBIT,
619                         },
620                 },
621                 .operational_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                 .ibss_pset              = {
634                         .el = {
635                                 .id      = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
636                                 .len     = 2,
637                         },
638                         .atim_window = 10,
639                 },
640                 .bss_type               = wl3501_fw_bss_type(this),
641                 .cap_info               = wl3501_fw_cap_info(this),
642         };
643
644         iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
645         iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
646         return wl3501_esbq_exec(this, &sig, sizeof(sig));
647 }
648
649 static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
650 {
651         u16 i = 0;
652         int matchflag = 0;
653         struct wl3501_scan_confirm sig;
654
655         pr_debug("entry");
656         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
657         if (sig.status == WL3501_STATUS_SUCCESS) {
658                 pr_debug("success");
659                 if ((this->net_type == IW_MODE_INFRA &&
660                      (sig.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
661                     (this->net_type == IW_MODE_ADHOC &&
662                      (sig.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
663                     this->net_type == IW_MODE_AUTO) {
664                         if (!this->essid.el.len)
665                                 matchflag = 1;
666                         else if (this->essid.el.len == 3 &&
667                                  !memcmp(this->essid.essid, "ANY", 3))
668                                 matchflag = 1;
669                         else if (this->essid.el.len != sig.ssid.el.len)
670                                 matchflag = 0;
671                         else if (memcmp(this->essid.essid, sig.ssid.essid,
672                                         this->essid.el.len))
673                                 matchflag = 0;
674                         else
675                                 matchflag = 1;
676                         if (matchflag) {
677                                 for (i = 0; i < this->bss_cnt; i++) {
678                                         if (!memcmp(this->bss_set[i].bssid,
679                                                     sig.bssid, ETH_ALEN)) {
680                                                 matchflag = 0;
681                                                 break;
682                                         }
683                                 }
684                         }
685                         if (matchflag && (i < 20)) {
686                                 memcpy(&this->bss_set[i].beacon_period,
687                                        &sig.beacon_period, 73);
688                                 this->bss_cnt++;
689                                 this->rssi = sig.rssi;
690                         }
691                 }
692         } else if (sig.status == WL3501_STATUS_TIMEOUT) {
693                 pr_debug("timeout");
694                 this->join_sta_bss = 0;
695                 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
696                         if (!wl3501_mgmt_join(this, i))
697                                 break;
698                 this->join_sta_bss = i;
699                 if (this->join_sta_bss == this->bss_cnt) {
700                         if (this->net_type == IW_MODE_INFRA)
701                                 wl3501_mgmt_scan(this, 100);
702                         else {
703                                 this->adhoc_times++;
704                                 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
705                                         wl3501_mgmt_start(this);
706                                 else
707                                         wl3501_mgmt_scan(this, 100);
708                         }
709                 }
710         }
711 }
712
713 /**
714  * wl3501_block_interrupt - Mask interrupt from SUTRO
715  * @this - card
716  *
717  * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
718  * Return: 1 if interrupt is originally enabled
719  */
720 static int wl3501_block_interrupt(struct wl3501_card *this)
721 {
722         u8 old = inb(this->base_addr + WL3501_NIC_GCR);
723         u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
724                         WL3501_GCR_ENECINT));
725
726         wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
727         return old & WL3501_GCR_ENECINT;
728 }
729
730 /**
731  * wl3501_unblock_interrupt - Enable interrupt from SUTRO
732  * @this - card
733  *
734  * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
735  * Return: 1 if interrupt is originally enabled
736  */
737 static int wl3501_unblock_interrupt(struct wl3501_card *this)
738 {
739         u8 old = inb(this->base_addr + WL3501_NIC_GCR);
740         u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
741                   WL3501_GCR_ENECINT;
742
743         wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
744         return old & WL3501_GCR_ENECINT;
745 }
746
747 /**
748  * wl3501_receive - Receive data from Receive Queue.
749  *
750  * Receive data from Receive Queue.
751  *
752  * @this: card
753  * @bf: address of host
754  * @size: size of buffer.
755  */
756 static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
757 {
758         u16 next_addr, next_addr1;
759         u8 *data = bf + 12;
760
761         size -= 12;
762         wl3501_get_from_wla(this, this->start_seg + 2,
763                             &next_addr, sizeof(next_addr));
764         if (size > WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr)) {
765                 wl3501_get_from_wla(this,
766                                     this->start_seg +
767                                         sizeof(struct wl3501_rx_hdr), data,
768                                     WL3501_BLKSZ -
769                                         sizeof(struct wl3501_rx_hdr));
770                 size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
771                 data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
772         } else {
773                 wl3501_get_from_wla(this,
774                                     this->start_seg +
775                                         sizeof(struct wl3501_rx_hdr),
776                                     data, size);
777                 size = 0;
778         }
779         while (size > 0) {
780                 if (size > WL3501_BLKSZ - 5) {
781                         wl3501_get_from_wla(this, next_addr + 5, data,
782                                             WL3501_BLKSZ - 5);
783                         size -= WL3501_BLKSZ - 5;
784                         data += WL3501_BLKSZ - 5;
785                         wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
786                                             sizeof(next_addr1));
787                         next_addr = next_addr1;
788                 } else {
789                         wl3501_get_from_wla(this, next_addr + 5, data, size);
790                         size = 0;
791                 }
792         }
793         return 0;
794 }
795
796 static void wl3501_esbq_req_free(struct wl3501_card *this)
797 {
798         u8 tmp;
799         u16 addr;
800
801         if (this->esbq_req_head == this->esbq_req_tail)
802                 goto out;
803         wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
804         if (!(tmp & 0x80))
805                 goto out;
806         wl3501_get_from_wla(this, this->esbq_req_tail, &addr, sizeof(addr));
807         wl3501_free_tx_buffer(this, addr);
808         this->esbq_req_tail += 4;
809         if (this->esbq_req_tail >= this->esbq_req_end)
810                 this->esbq_req_tail = this->esbq_req_start;
811 out:
812         return;
813 }
814
815 static int wl3501_esbq_confirm(struct wl3501_card *this)
816 {
817         u8 tmp;
818
819         wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
820         return tmp & 0x80;
821 }
822
823 static void wl3501_online(struct net_device *dev)
824 {
825         struct wl3501_card *this = netdev_priv(dev);
826
827         printk(KERN_INFO "%s: Wireless LAN online. BSSID: %pM\n",
828                dev->name, this->bssid);
829         netif_wake_queue(dev);
830 }
831
832 static void wl3501_esbq_confirm_done(struct wl3501_card *this)
833 {
834         u8 tmp = 0;
835
836         wl3501_set_to_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
837         this->esbq_confirm += 4;
838         if (this->esbq_confirm >= this->esbq_confirm_end)
839                 this->esbq_confirm = this->esbq_confirm_start;
840 }
841
842 static int wl3501_mgmt_auth(struct wl3501_card *this)
843 {
844         struct wl3501_auth_req sig = {
845                 .sig_id  = WL3501_SIG_AUTH_REQ,
846                 .type    = WL3501_SYS_TYPE_OPEN,
847                 .timeout = 1000,
848         };
849
850         pr_debug("entry");
851         memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
852         return wl3501_esbq_exec(this, &sig, sizeof(sig));
853 }
854
855 static int wl3501_mgmt_association(struct wl3501_card *this)
856 {
857         struct wl3501_assoc_req sig = {
858                 .sig_id          = WL3501_SIG_ASSOC_REQ,
859                 .timeout         = 1000,
860                 .listen_interval = 5,
861                 .cap_info        = this->cap_info,
862         };
863
864         pr_debug("entry");
865         memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
866         return wl3501_esbq_exec(this, &sig, sizeof(sig));
867 }
868
869 static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
870 {
871         struct wl3501_card *this = netdev_priv(dev);
872         struct wl3501_join_confirm sig;
873
874         pr_debug("entry");
875         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
876         if (sig.status == WL3501_STATUS_SUCCESS) {
877                 if (this->net_type == IW_MODE_INFRA) {
878                         if (this->join_sta_bss < this->bss_cnt) {
879                                 const int i = this->join_sta_bss;
880                                 memcpy(this->bssid,
881                                        this->bss_set[i].bssid, ETH_ALEN);
882                                 this->chan = this->bss_set[i].ds_pset.chan;
883                                 iw_copy_mgmt_info_element(&this->keep_essid.el,
884                                                      &this->bss_set[i].ssid.el);
885                                 wl3501_mgmt_auth(this);
886                         }
887                 } else {
888                         const int i = this->join_sta_bss;
889
890                         memcpy(&this->bssid, &this->bss_set[i].bssid, ETH_ALEN);
891                         this->chan = this->bss_set[i].ds_pset.chan;
892                         iw_copy_mgmt_info_element(&this->keep_essid.el,
893                                                   &this->bss_set[i].ssid.el);
894                         wl3501_online(dev);
895                 }
896         } else {
897                 int i;
898                 this->join_sta_bss++;
899                 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
900                         if (!wl3501_mgmt_join(this, i))
901                                 break;
902                 this->join_sta_bss = i;
903                 if (this->join_sta_bss == this->bss_cnt) {
904                         if (this->net_type == IW_MODE_INFRA)
905                                 wl3501_mgmt_scan(this, 100);
906                         else {
907                                 this->adhoc_times++;
908                                 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
909                                         wl3501_mgmt_start(this);
910                                 else
911                                         wl3501_mgmt_scan(this, 100);
912                         }
913                 }
914         }
915 }
916
917 static inline void wl3501_alarm_interrupt(struct net_device *dev,
918                                           struct wl3501_card *this)
919 {
920         if (this->net_type == IW_MODE_INFRA) {
921                 printk(KERN_INFO "Wireless LAN offline\n");
922                 netif_stop_queue(dev);
923                 wl3501_mgmt_resync(this);
924         }
925 }
926
927 static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
928                                                struct wl3501_card *this,
929                                                u16 addr)
930 {
931         struct wl3501_md_confirm sig;
932
933         pr_debug("entry");
934         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
935         wl3501_free_tx_buffer(this, sig.data);
936         if (netif_queue_stopped(dev))
937                 netif_wake_queue(dev);
938 }
939
940 static inline void wl3501_md_ind_interrupt(struct net_device *dev,
941                                            struct wl3501_card *this, u16 addr)
942 {
943         struct wl3501_md_ind sig;
944         struct sk_buff *skb;
945         u8 rssi, addr4[ETH_ALEN];
946         u16 pkt_len;
947
948         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
949         this->start_seg = sig.data;
950         wl3501_get_from_wla(this,
951                             sig.data + offsetof(struct wl3501_rx_hdr, rssi),
952                             &rssi, sizeof(rssi));
953         this->rssi = rssi <= 63 ? (rssi * 100) / 64 : 255;
954
955         wl3501_get_from_wla(this,
956                             sig.data +
957                                 offsetof(struct wl3501_rx_hdr, addr4),
958                             &addr4, sizeof(addr4));
959         if (!(addr4[0] == 0xAA && addr4[1] == 0xAA &&
960               addr4[2] == 0x03 && addr4[4] == 0x00)) {
961                 printk(KERN_INFO "Insupported packet type!\n");
962                 return;
963         }
964         pkt_len = sig.size + 12 - 24 - 4 - 6;
965
966         skb = dev_alloc_skb(pkt_len + 5);
967
968         if (!skb) {
969                 printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
970                        dev->name, pkt_len);
971                 dev->stats.rx_dropped++;
972         } else {
973                 skb->dev = dev;
974                 skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
975                 skb_copy_to_linear_data(skb, (unsigned char *)&sig.daddr, 12);
976                 wl3501_receive(this, skb->data, pkt_len);
977                 skb_put(skb, pkt_len);
978                 skb->protocol   = eth_type_trans(skb, dev);
979                 dev->stats.rx_packets++;
980                 dev->stats.rx_bytes += skb->len;
981                 netif_rx(skb);
982         }
983 }
984
985 static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
986                                                 u16 addr, void *sig, int size)
987 {
988         pr_debug("entry");
989         wl3501_get_from_wla(this, addr, &this->sig_get_confirm,
990                             sizeof(this->sig_get_confirm));
991         wake_up(&this->wait);
992 }
993
994 static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
995                                                   struct wl3501_card *this,
996                                                   u16 addr)
997 {
998         struct wl3501_start_confirm sig;
999
1000         pr_debug("entry");
1001         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1002         if (sig.status == WL3501_STATUS_SUCCESS)
1003                 netif_wake_queue(dev);
1004 }
1005
1006 static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
1007                                                   u16 addr)
1008 {
1009         struct wl3501_card *this = netdev_priv(dev);
1010         struct wl3501_assoc_confirm sig;
1011
1012         pr_debug("entry");
1013         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1014
1015         if (sig.status == WL3501_STATUS_SUCCESS)
1016                 wl3501_online(dev);
1017 }
1018
1019 static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
1020                                                  u16 addr)
1021 {
1022         struct wl3501_auth_confirm sig;
1023
1024         pr_debug("entry");
1025         wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1026
1027         if (sig.status == WL3501_STATUS_SUCCESS)
1028                 wl3501_mgmt_association(this);
1029         else
1030                 wl3501_mgmt_resync(this);
1031 }
1032
1033 static inline void wl3501_rx_interrupt(struct net_device *dev)
1034 {
1035         int morepkts;
1036         u16 addr;
1037         u8 sig_id;
1038         struct wl3501_card *this = netdev_priv(dev);
1039
1040         pr_debug("entry");
1041 loop:
1042         morepkts = 0;
1043         if (!wl3501_esbq_confirm(this))
1044                 goto free;
1045         wl3501_get_from_wla(this, this->esbq_confirm, &addr, sizeof(addr));
1046         wl3501_get_from_wla(this, addr + 2, &sig_id, sizeof(sig_id));
1047
1048         switch (sig_id) {
1049         case WL3501_SIG_DEAUTH_IND:
1050         case WL3501_SIG_DISASSOC_IND:
1051         case WL3501_SIG_ALARM:
1052                 wl3501_alarm_interrupt(dev, this);
1053                 break;
1054         case WL3501_SIG_MD_CONFIRM:
1055                 wl3501_md_confirm_interrupt(dev, this, addr);
1056                 break;
1057         case WL3501_SIG_MD_IND:
1058                 wl3501_md_ind_interrupt(dev, this, addr);
1059                 break;
1060         case WL3501_SIG_GET_CONFIRM:
1061                 wl3501_get_confirm_interrupt(this, addr,
1062                                              &this->sig_get_confirm,
1063                                              sizeof(this->sig_get_confirm));
1064                 break;
1065         case WL3501_SIG_PWR_MGMT_CONFIRM:
1066                 wl3501_get_confirm_interrupt(this, addr,
1067                                              &this->sig_pwr_mgmt_confirm,
1068                                             sizeof(this->sig_pwr_mgmt_confirm));
1069                 break;
1070         case WL3501_SIG_START_CONFIRM:
1071                 wl3501_start_confirm_interrupt(dev, this, addr);
1072                 break;
1073         case WL3501_SIG_SCAN_CONFIRM:
1074                 wl3501_mgmt_scan_confirm(this, addr);
1075                 break;
1076         case WL3501_SIG_JOIN_CONFIRM:
1077                 wl3501_mgmt_join_confirm(dev, addr);
1078                 break;
1079         case WL3501_SIG_ASSOC_CONFIRM:
1080                 wl3501_assoc_confirm_interrupt(dev, addr);
1081                 break;
1082         case WL3501_SIG_AUTH_CONFIRM:
1083                 wl3501_auth_confirm_interrupt(this, addr);
1084                 break;
1085         case WL3501_SIG_RESYNC_CONFIRM:
1086                 wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
1087                 break;
1088         }
1089         wl3501_esbq_confirm_done(this);
1090         morepkts = 1;
1091         /* free request if necessary */
1092 free:
1093         wl3501_esbq_req_free(this);
1094         if (morepkts)
1095                 goto loop;
1096 }
1097
1098 static inline void wl3501_ack_interrupt(struct wl3501_card *this)
1099 {
1100         wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
1101 }
1102
1103 /**
1104  * wl3501_interrupt - Hardware interrupt from card.
1105  * @irq - Interrupt number
1106  * @dev_id - net_device
1107  *
1108  * We must acknowledge the interrupt as soon as possible, and block the
1109  * interrupt from the same card immediately to prevent re-entry.
1110  *
1111  * Before accessing the Control_Status_Block, we must lock SUTRO first.
1112  * On the other hand, to prevent SUTRO from malfunctioning, we must
1113  * unlock the SUTRO as soon as possible.
1114  */
1115 static irqreturn_t wl3501_interrupt(int irq, void *dev_id)
1116 {
1117         struct net_device *dev = dev_id;
1118         struct wl3501_card *this;
1119
1120         this = netdev_priv(dev);
1121         spin_lock(&this->lock);
1122         wl3501_ack_interrupt(this);
1123         wl3501_block_interrupt(this);
1124         wl3501_rx_interrupt(dev);
1125         wl3501_unblock_interrupt(this);
1126         spin_unlock(&this->lock);
1127
1128         return IRQ_HANDLED;
1129 }
1130
1131 static int wl3501_reset_board(struct wl3501_card *this)
1132 {
1133         u8 tmp = 0;
1134         int i, rc = 0;
1135
1136         /* Coreset */
1137         wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1138         wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1139         wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1140
1141         /* Reset SRAM 0x480 to zero */
1142         wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1143
1144         /* Start up */
1145         wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1146
1147         WL3501_NOPLOOP(1024 * 50);
1148
1149         wl3501_unblock_interrupt(this); /* acme: was commented */
1150
1151         /* Polling Self_Test_Status */
1152         for (i = 0; i < 10000; i++) {
1153                 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1154
1155                 if (tmp == 'W') {
1156                         /* firmware complete all test successfully */
1157                         tmp = 'A';
1158                         wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1159                         goto out;
1160                 }
1161                 WL3501_NOPLOOP(10);
1162         }
1163         printk(KERN_WARNING "%s: failed to reset the board!\n", __func__);
1164         rc = -ENODEV;
1165 out:
1166         return rc;
1167 }
1168
1169 static int wl3501_init_firmware(struct wl3501_card *this)
1170 {
1171         u16 ptr, next;
1172         int rc = wl3501_reset_board(this);
1173
1174         if (rc)
1175                 goto fail;
1176         this->card_name[0] = '\0';
1177         wl3501_get_from_wla(this, 0x1a00,
1178                             this->card_name, sizeof(this->card_name));
1179         this->card_name[sizeof(this->card_name) - 1] = '\0';
1180         this->firmware_date[0] = '\0';
1181         wl3501_get_from_wla(this, 0x1a40,
1182                             this->firmware_date, sizeof(this->firmware_date));
1183         this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
1184         /* Switch to SRAM Page 0 */
1185         wl3501_switch_page(this, WL3501_BSS_SPAGE0);
1186         /* Read parameter from card */
1187         wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
1188         wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
1189         wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
1190         wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
1191         wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
1192         wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
1193         this->esbq_req_tail     = this->esbq_req_head = this->esbq_req_start;
1194         this->esbq_req_end     += this->esbq_req_start;
1195         this->esbq_confirm      = this->esbq_confirm_start;
1196         this->esbq_confirm_end += this->esbq_confirm_start;
1197         /* Initial Tx Buffer */
1198         this->tx_buffer_cnt = 1;
1199         ptr = this->tx_buffer_head;
1200         next = ptr + WL3501_BLKSZ;
1201         while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
1202                 this->tx_buffer_cnt++;
1203                 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1204                 ptr = next;
1205                 next = ptr + WL3501_BLKSZ;
1206         }
1207         rc = 0;
1208         next = 0;
1209         wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1210         this->tx_buffer_tail = ptr;
1211 out:
1212         return rc;
1213 fail:
1214         printk(KERN_WARNING "%s: failed!\n", __func__);
1215         goto out;
1216 }
1217
1218 static int wl3501_close(struct net_device *dev)
1219 {
1220         struct wl3501_card *this = netdev_priv(dev);
1221         int rc = -ENODEV;
1222         unsigned long flags;
1223         struct pcmcia_device *link;
1224         link = this->p_dev;
1225
1226         spin_lock_irqsave(&this->lock, flags);
1227         link->open--;
1228
1229         /* Stop wl3501_hard_start_xmit() from now on */
1230         netif_stop_queue(dev);
1231         wl3501_ack_interrupt(this);
1232
1233         /* Mask interrupts from the SUTRO */
1234         wl3501_block_interrupt(this);
1235
1236         rc = 0;
1237         printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1238         spin_unlock_irqrestore(&this->lock, flags);
1239         return rc;
1240 }
1241
1242 /**
1243  * wl3501_reset - Reset the SUTRO.
1244  * @dev - network device
1245  *
1246  * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1247  * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1248  * is running. It seems to be dangerous.
1249  */
1250 static int wl3501_reset(struct net_device *dev)
1251 {
1252         struct wl3501_card *this = netdev_priv(dev);
1253         int rc = -ENODEV;
1254
1255         wl3501_block_interrupt(this);
1256
1257         if (wl3501_init_firmware(this)) {
1258                 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1259                        dev->name);
1260                 /* Free IRQ, and mark IRQ as unused */
1261                 free_irq(dev->irq, dev);
1262                 goto out;
1263         }
1264
1265         /*
1266          * Queue has to be started only when the Card is Started
1267          */
1268         netif_stop_queue(dev);
1269         this->adhoc_times = 0;
1270         wl3501_ack_interrupt(this);
1271         wl3501_unblock_interrupt(this);
1272         wl3501_mgmt_scan(this, 100);
1273         pr_debug("%s: device reset", dev->name);
1274         rc = 0;
1275 out:
1276         return rc;
1277 }
1278
1279 static void wl3501_tx_timeout(struct net_device *dev)
1280 {
1281         struct wl3501_card *this = netdev_priv(dev);
1282         struct net_device_stats *stats = &dev->stats;
1283         unsigned long flags;
1284         int rc;
1285
1286         stats->tx_errors++;
1287         spin_lock_irqsave(&this->lock, flags);
1288         rc = wl3501_reset(dev);
1289         spin_unlock_irqrestore(&this->lock, flags);
1290         if (rc)
1291                 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1292                        dev->name, rc);
1293         else {
1294                 dev->trans_start = jiffies; /* prevent tx timeout */
1295                 netif_wake_queue(dev);
1296         }
1297 }
1298
1299 /*
1300  * Return : 0 - OK
1301  *          1 - Could not transmit (dev_queue_xmit will queue it)
1302  *              and try to sent it later
1303  */
1304 static netdev_tx_t wl3501_hard_start_xmit(struct sk_buff *skb,
1305                                                 struct net_device *dev)
1306 {
1307         int enabled, rc;
1308         struct wl3501_card *this = netdev_priv(dev);
1309         unsigned long flags;
1310
1311         spin_lock_irqsave(&this->lock, flags);
1312         enabled = wl3501_block_interrupt(this);
1313         rc = wl3501_send_pkt(this, skb->data, skb->len);
1314         if (enabled)
1315                 wl3501_unblock_interrupt(this);
1316         if (rc) {
1317                 ++dev->stats.tx_dropped;
1318                 netif_stop_queue(dev);
1319         } else {
1320                 ++dev->stats.tx_packets;
1321                 dev->stats.tx_bytes += skb->len;
1322                 kfree_skb(skb);
1323
1324                 if (this->tx_buffer_cnt < 2)
1325                         netif_stop_queue(dev);
1326         }
1327         spin_unlock_irqrestore(&this->lock, flags);
1328         return NETDEV_TX_OK;
1329 }
1330
1331 static int wl3501_open(struct net_device *dev)
1332 {
1333         int rc = -ENODEV;
1334         struct wl3501_card *this = netdev_priv(dev);
1335         unsigned long flags;
1336         struct pcmcia_device *link;
1337         link = this->p_dev;
1338
1339         spin_lock_irqsave(&this->lock, flags);
1340         if (!pcmcia_dev_present(link))
1341                 goto out;
1342         netif_device_attach(dev);
1343         link->open++;
1344
1345         /* Initial WL3501 firmware */
1346         pr_debug("%s: Initialize WL3501 firmware...", dev->name);
1347         if (wl3501_init_firmware(this))
1348                 goto fail;
1349         /* Initial device variables */
1350         this->adhoc_times = 0;
1351         /* Acknowledge Interrupt, for cleaning last state */
1352         wl3501_ack_interrupt(this);
1353
1354         /* Enable interrupt from card after all */
1355         wl3501_unblock_interrupt(this);
1356         wl3501_mgmt_scan(this, 100);
1357         rc = 0;
1358         pr_debug("%s: WL3501 opened", dev->name);
1359         printk(KERN_INFO "%s: Card Name: %s\n"
1360                          "%s: Firmware Date: %s\n",
1361                          dev->name, this->card_name,
1362                          dev->name, this->firmware_date);
1363 out:
1364         spin_unlock_irqrestore(&this->lock, flags);
1365         return rc;
1366 fail:
1367         printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1368         goto out;
1369 }
1370
1371 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1372 {
1373         struct wl3501_card *this = netdev_priv(dev);
1374         struct iw_statistics *wstats = &this->wstats;
1375         u32 value; /* size checked: it is u32 */
1376
1377         memset(wstats, 0, sizeof(*wstats));
1378         wstats->status = netif_running(dev);
1379         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1380                                   &value, sizeof(value)))
1381                 wstats->discard.code += value;
1382         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1383                                   &value, sizeof(value)))
1384                 wstats->discard.code += value;
1385         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1386                                   &value, sizeof(value)))
1387                 wstats->discard.code += value;
1388         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1389                                   &value, sizeof(value)))
1390                 wstats->discard.retries = value;
1391         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1392                                   &value, sizeof(value)))
1393                 wstats->discard.misc += value;
1394         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1395                                   &value, sizeof(value)))
1396                 wstats->discard.misc += value;
1397         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1398                                   &value, sizeof(value)))
1399                 wstats->discard.misc += value;
1400         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1401                                   &value, sizeof(value)))
1402                 wstats->discard.misc += value;
1403         return wstats;
1404 }
1405
1406 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1407 {
1408         strlcpy(info->driver, "wl3501_cs", sizeof(info->driver));
1409 }
1410
1411 static const struct ethtool_ops ops = {
1412         .get_drvinfo = wl3501_get_drvinfo
1413 };
1414
1415 /**
1416  * wl3501_detach - deletes a driver "instance"
1417  * @link - FILL_IN
1418  *
1419  * This deletes a driver "instance". The device is de-registered with Card
1420  * Services. If it has been released, all local data structures are freed.
1421  * Otherwise, the structures will be freed when the device is released.
1422  */
1423 static void wl3501_detach(struct pcmcia_device *link)
1424 {
1425         struct net_device *dev = link->priv;
1426
1427         /* If the device is currently configured and active, we won't actually
1428          * delete it yet.  Instead, it is marked so that when the release()
1429          * function is called, that will trigger a proper detach(). */
1430
1431         while (link->open > 0)
1432                 wl3501_close(dev);
1433
1434         netif_device_detach(dev);
1435         wl3501_release(link);
1436
1437         unregister_netdev(dev);
1438
1439         if (link->priv)
1440                 free_netdev(link->priv);
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 = ieee80211_dsss_chan_to_freq(this->chan) * 100000;
1470         wrqu->freq.e = 1;
1471         return 0;
1472 }
1473
1474 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1475                            union iwreq_data *wrqu, char *extra)
1476 {
1477         int rc = -EINVAL;
1478
1479         if (wrqu->mode == IW_MODE_INFRA ||
1480             wrqu->mode == IW_MODE_ADHOC ||
1481             wrqu->mode == IW_MODE_AUTO) {
1482                 struct wl3501_card *this = netdev_priv(dev);
1483
1484                 this->net_type = wrqu->mode;
1485                 rc = wl3501_reset(dev);
1486         }
1487         return rc;
1488 }
1489
1490 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1491                            union iwreq_data *wrqu, char *extra)
1492 {
1493         struct wl3501_card *this = netdev_priv(dev);
1494
1495         wrqu->mode = this->net_type;
1496         return 0;
1497 }
1498
1499 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1500                            union iwreq_data *wrqu, char *extra)
1501 {
1502         struct wl3501_card *this = netdev_priv(dev);
1503
1504         wrqu->sens.value = this->rssi;
1505         wrqu->sens.disabled = !wrqu->sens.value;
1506         wrqu->sens.fixed = 1;
1507         return 0;
1508 }
1509
1510 static int wl3501_get_range(struct net_device *dev,
1511                             struct iw_request_info *info,
1512                             union iwreq_data *wrqu, char *extra)
1513 {
1514         struct iw_range *range = (struct iw_range *)extra;
1515
1516         /* Set the length (very important for backward compatibility) */
1517         wrqu->data.length = sizeof(*range);
1518
1519         /* Set all the info we don't care or don't know about to zero */
1520         memset(range, 0, sizeof(*range));
1521
1522         /* Set the Wireless Extension versions */
1523         range->we_version_compiled      = WIRELESS_EXT;
1524         range->we_version_source        = 1;
1525         range->throughput               = 2 * 1000 * 1000;     /* ~2 Mb/s */
1526         /* FIXME: study the code to fill in more fields... */
1527         return 0;
1528 }
1529
1530 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1531                           union iwreq_data *wrqu, char *extra)
1532 {
1533         struct wl3501_card *this = netdev_priv(dev);
1534         static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
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 (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
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].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].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].ssid.essid);
1591                 iwe.cmd    = SIOCGIWMODE;
1592                 iwe.u.mode = this->bss_set[i].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].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].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(u8, 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_change_mtu         = eth_change_mtu,
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         SET_ETHTOOL_OPS(dev, &ops);
1891         netif_stop_queue(dev);
1892         p_dev->priv = dev;
1893
1894         return wl3501_config(p_dev);
1895 out_link:
1896         return -ENOMEM;
1897 }
1898
1899 static int wl3501_config(struct pcmcia_device *link)
1900 {
1901         struct net_device *dev = link->priv;
1902         int i = 0, j, ret;
1903         struct wl3501_card *this;
1904
1905         /* Try allocating IO ports.  This tries a few fixed addresses.  If you
1906          * want, you can also read the card's config table to pick addresses --
1907          * see the serial driver for an example. */
1908         link->io_lines = 5;
1909
1910         for (j = 0x280; j < 0x400; j += 0x20) {
1911                 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
1912                  * 0x200-0x2ff, and so on, because this seems safer */
1913                 link->resource[0]->start = j;
1914                 link->resource[1]->start = link->resource[0]->start + 0x10;
1915                 i = pcmcia_request_io(link);
1916                 if (i == 0)
1917                         break;
1918         }
1919         if (i != 0)
1920                 goto failed;
1921
1922         /* Now allocate an interrupt line. Note that this does not actually
1923          * assign a handler to the interrupt. */
1924
1925         ret = pcmcia_request_irq(link, wl3501_interrupt);
1926         if (ret)
1927                 goto failed;
1928
1929         ret = pcmcia_enable_device(link);
1930         if (ret)
1931                 goto failed;
1932
1933         dev->irq = link->irq;
1934         dev->base_addr = link->resource[0]->start;
1935         SET_NETDEV_DEV(dev, &link->dev);
1936         if (register_netdev(dev)) {
1937                 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
1938                 goto failed;
1939         }
1940
1941         this = netdev_priv(dev);
1942
1943         this->base_addr = dev->base_addr;
1944
1945         if (!wl3501_get_flash_mac_addr(this)) {
1946                 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
1947                        dev->name);
1948                 unregister_netdev(dev);
1949                 goto failed;
1950         }
1951
1952         for (i = 0; i < 6; i++)
1953                 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
1954
1955         /* print probe information */
1956         printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, "
1957                "MAC addr in flash ROM:%pM\n",
1958                dev->name, this->base_addr, (int)dev->irq,
1959                dev->dev_addr);
1960         /*
1961          * Initialize card parameters - added by jss
1962          */
1963         this->net_type          = IW_MODE_INFRA;
1964         this->bss_cnt           = 0;
1965         this->join_sta_bss      = 0;
1966         this->adhoc_times       = 0;
1967         iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
1968                                  "ANY", 3);
1969         this->card_name[0]      = '\0';
1970         this->firmware_date[0]  = '\0';
1971         this->rssi              = 255;
1972         this->chan              = iw_default_channel(this->reg_domain);
1973         strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
1974         spin_lock_init(&this->lock);
1975         init_waitqueue_head(&this->wait);
1976         netif_start_queue(dev);
1977         return 0;
1978
1979 failed:
1980         wl3501_release(link);
1981         return -ENODEV;
1982 }
1983
1984 static void wl3501_release(struct pcmcia_device *link)
1985 {
1986         pcmcia_disable_device(link);
1987 }
1988
1989 static int wl3501_suspend(struct pcmcia_device *link)
1990 {
1991         struct net_device *dev = link->priv;
1992
1993         wl3501_pwr_mgmt(netdev_priv(dev), WL3501_SUSPEND);
1994         if (link->open)
1995                 netif_device_detach(dev);
1996
1997         return 0;
1998 }
1999
2000 static int wl3501_resume(struct pcmcia_device *link)
2001 {
2002         struct net_device *dev = link->priv;
2003
2004         wl3501_pwr_mgmt(netdev_priv(dev), WL3501_RESUME);
2005         if (link->open) {
2006                 wl3501_reset(dev);
2007                 netif_device_attach(dev);
2008         }
2009
2010         return 0;
2011 }
2012
2013
2014 static struct pcmcia_device_id wl3501_ids[] = {
2015         PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2016         PCMCIA_DEVICE_NULL
2017 };
2018 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2019
2020 static struct pcmcia_driver wl3501_driver = {
2021         .owner          = THIS_MODULE,
2022         .name           = "wl3501_cs",
2023         .probe          = wl3501_probe,
2024         .remove         = wl3501_detach,
2025         .id_table       = wl3501_ids,
2026         .suspend        = wl3501_suspend,
2027         .resume         = wl3501_resume,
2028 };
2029
2030 static int __init wl3501_init_module(void)
2031 {
2032         return pcmcia_register_driver(&wl3501_driver);
2033 }
2034
2035 static void __exit wl3501_exit_module(void)
2036 {
2037         pcmcia_unregister_driver(&wl3501_driver);
2038 }
2039
2040 module_init(wl3501_init_module);
2041 module_exit(wl3501_exit_module);
2042
2043 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2044               "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2045               "Gustavo Niemeyer <niemeyer@conectiva.com>");
2046 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2047 MODULE_LICENSE("GPL");