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