OSDN Git Service

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