Root/drivers/net/wireless/wl3501_cs.c

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

Archive Download this file



interactive