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