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>
7 * References used by Fox Chen while writing the original driver for 2.0.30:
9 * 1. WL24xx packet drivers (tooasm.asm)
10 * 2. Access Point Firmware Interface Specification for IEEE 802.11 SUTRO
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
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)
23 * Tested with Planet AP in 2.4.17, 184 Kbytes/s in UDP in Infrastructure mode,
24 * 173 Kbytes/s in TCP.
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)
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>
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>
49 #include <net/iw_handler.h>
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>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
64 #define slow_down_io()
67 /* For rough constant delay */
68 #define WL3501_NOPLOOP(n) { int x = 0; while (x++ < n) slow_down_io(); }
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.
77 #define PCMCIA_DEBUG 0
79 static int pc_debug = PCMCIA_DEBUG;
80 module_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); }
85 #define dprintk(n, format, args...)
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(); }
92 #define WL3501_RELEASE_TIMEOUT (25 * HZ)
93 #define WL3501_MAX_ADHOC_TRIES 16
95 #define WL3501_RESUME 0
96 #define WL3501_SUSPEND 1
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.
105 static int wl3501_config(struct pcmcia_device *link);
106 static void wl3501_release(struct pcmcia_device *link);
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
113 static dev_info_t wl3501_dev_info = "wl3501_cs";
115 static const struct {
118 } iw_channel_table[] = {
120 .reg_domain = IW_REG_DOMAIN_FCC,
126 .reg_domain = IW_REG_DOMAIN_DOC,
132 .reg_domain = IW_REG_DOMAIN_ETSI,
138 .reg_domain = IW_REG_DOMAIN_SPAIN,
144 .reg_domain = IW_REG_DOMAIN_FRANCE,
150 .reg_domain = IW_REG_DOMAIN_MKK,
156 .reg_domain = IW_REG_DOMAIN_MKK1,
162 .reg_domain = IW_REG_DOMAIN_ISRAEL,
170 * iw_valid_channel - validate channel in regulatory domain
171 * @reg_comain - regulatory domain
172 * @channel - channel to validate
174 * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
176 static int iw_valid_channel(int reg_domain, int channel)
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;
190 * iw_default_channel - get default channel for a regulatory domain
191 * @reg_comain - regulatory domain
193 * Returns the default channel for a regulatory domain
195 static int iw_default_channel(int reg_domain)
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;
207 static 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)
213 memcpy(el->data, value, len);
216 static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
217 struct iw_mgmt_info_element *from)
219 iw_set_mgmt_info_element(from->id, to, from->data, from->len);
222 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
224 wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
228 * Get Ethernet MAC addresss.
230 * WARNING: We switch to FPAGE0 and switc back again.
231 * Making sure there is no other WL function beening called by ISR.
233 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
235 int base_addr = this->base_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 */
242 /* wait for reading EEPROM */
244 this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
246 this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
248 this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
250 this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
252 this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
254 this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
256 this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
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);
262 this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
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);
268 /* The MAC addr should be 00:60:... */
269 return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
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
278 * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
280 static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
283 /* switch to SRAM Page 0 */
284 wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
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);
290 /* rep out to Port A */
291 wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
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
300 * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
302 static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
305 /* switch to SRAM Page 0 */
306 wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
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);
312 /* rep get from Port A */
313 insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
317 * Get/Allocate a free Tx Data Buffer
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 -------/
326 * Return = Postion in Card
328 static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
330 u16 next, blk_cnt = 0, zero = 0;
331 u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
334 if (full_len > this->tx_buffer_cnt * 254)
336 ret = this->tx_buffer_head;
342 wl3501_get_from_wla(this, this->tx_buffer_head, &next,
345 wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
347 this->tx_buffer_head = next;
349 /* if buffer is not enough */
350 if (!next && full_len) {
351 this->tx_buffer_head = ret;
356 this->tx_buffer_cnt -= blk_cnt;
362 * Free an allocated Tx Buffer. ptr must be correct position.
364 static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
366 /* check if all space is not free */
367 if (!this->tx_buffer_head)
368 this->tx_buffer_head = ptr;
370 wl3501_set_to_wla(this, this->tx_buffer_tail,
375 this->tx_buffer_cnt++;
376 wl3501_get_from_wla(this, ptr, &next, sizeof(next));
377 this->tx_buffer_tail = ptr;
382 static int wl3501_esbq_req_test(struct wl3501_card *this)
386 wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
390 static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
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;
401 static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
405 if (wl3501_esbq_req_test(this)) {
406 u16 ptr = wl3501_get_tx_buffer(this, sig_size);
408 wl3501_set_to_wla(this, ptr, sig, sig_size);
409 wl3501_esbq_req(this, &ptr);
416 static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
419 struct wl3501_get_req sig = {
420 .sig_id = WL3501_SIG_GET_REQ,
426 spin_lock_irqsave(&this->lock, flags);
427 if (wl3501_esbq_req_test(this)) {
428 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
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);
437 memcpy(bf, this->sig_get_confirm.mib_value,
442 spin_unlock_irqrestore(&this->lock, flags);
447 static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
449 struct wl3501_pwr_mgmt_req sig = {
450 .sig_id = WL3501_SIG_PWR_MGMT_REQ,
458 spin_lock_irqsave(&this->lock, flags);
459 if (wl3501_esbq_req_test(this)) {
460 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
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);
474 spin_unlock_irqrestore(&this->lock, flags);
480 * wl3501_send_pkt - Send a packet.
485 * data = Ethernet raw frame. (e.g. data[0] - data[5] is Dest MAC Addr,
486 * data[6] - data[11] is Src MAC Addr)
489 static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
491 u16 bf, sig_bf, next, tmplen, pktlen;
492 struct wl3501_md_req sig = {
493 .sig_id = WL3501_SIG_MD_REQ,
495 u8 *pdata = (char *)data;
498 if (wl3501_esbq_req_test(this)) {
499 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
501 if (!sig_bf) /* No free buffer available */
503 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
505 /* No free buffer available */
506 wl3501_free_tx_buffer(this, sig_bf);
510 memcpy(&sig.daddr[0], pdata, 12);
514 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
515 u8 addr4[ETH_ALEN] = {
516 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
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);
530 wl3501_set_to_wla(this,
531 bf + 2 + sizeof(struct wl3501_tx_hdr),
534 wl3501_get_from_wla(this, bf, &next, sizeof(next));
537 sig.size = pktlen + 24 + 4 - 2;
540 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
541 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
547 wl3501_set_to_wla(this, bf + 2 +
548 offsetof(struct wl3501_tx_hdr, addr4),
551 wl3501_get_from_wla(this, bf, &next, sizeof(next));
562 wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
564 wl3501_get_from_wla(this, bf, &next, sizeof(next));
567 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
568 wl3501_esbq_req(this, &sig_bf);
574 static int wl3501_mgmt_resync(struct wl3501_card *this)
576 struct wl3501_resync_req sig = {
577 .sig_id = WL3501_SIG_RESYNC_REQ,
580 return wl3501_esbq_exec(this, &sig, sizeof(sig));
583 static inline int wl3501_fw_bss_type(struct wl3501_card *this)
585 return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
586 WL3501_NET_TYPE_ADHOC;
589 static inline int wl3501_fw_cap_info(struct wl3501_card *this)
591 return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
592 WL3501_MGMT_CAPABILITY_IBSS;
595 static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
597 struct wl3501_scan_req sig = {
598 .sig_id = WL3501_SIG_SCAN_REQ,
599 .scan_type = WL3501_SCAN_TYPE_ACTIVE,
601 .min_chan_time = chan_time,
602 .max_chan_time = chan_time,
603 .bss_type = wl3501_fw_bss_type(this),
606 this->bss_cnt = this->join_sta_bss = 0;
607 return wl3501_esbq_exec(this, &sig, sizeof(sig));
610 static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
612 struct wl3501_join_req sig = {
613 .sig_id = WL3501_SIG_JOIN_REQ,
617 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
624 memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
625 return wl3501_esbq_exec(this, &sig, sizeof(sig));
628 static int wl3501_mgmt_start(struct wl3501_card *this)
630 struct wl3501_start_req sig = {
631 .sig_id = WL3501_SIG_START_REQ,
632 .beacon_period = 400,
636 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
643 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
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,
653 .operational_rset = {
655 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
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,
667 .id = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
672 .bss_type = wl3501_fw_bss_type(this),
673 .cap_info = wl3501_fw_cap_info(this),
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));
681 static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
685 struct wl3501_scan_confirm sig;
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)
698 else if (this->essid.el.len == 3 &&
699 !memcmp(this->essid.essid, "ANY", 3))
701 else if (this->essid.el.len != sig.ssid.el.len)
703 else if (memcmp(this->essid.essid, sig.ssid.essid,
709 for (i = 0; i < this->bss_cnt; i++) {
710 if (!memcmp(this->bss_set[i].bssid,
711 sig.bssid, ETH_ALEN)) {
717 if (matchflag && (i < 20)) {
718 memcpy(&this->bss_set[i].beacon_period,
719 &sig.beacon_period, 73);
721 this->rssi = sig.rssi;
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))
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);
736 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
737 wl3501_mgmt_start(this);
739 wl3501_mgmt_scan(this, 100);
746 * wl3501_block_interrupt - Mask interrupt from SUTRO
749 * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
750 * Return: 1 if interrupt is originally enabled
752 static int wl3501_block_interrupt(struct wl3501_card *this)
754 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
755 u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
756 WL3501_GCR_ENECINT));
758 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
759 return old & WL3501_GCR_ENECINT;
763 * wl3501_unblock_interrupt - Enable interrupt from SUTRO
766 * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
767 * Return: 1 if interrupt is originally enabled
769 static int wl3501_unblock_interrupt(struct wl3501_card *this)
771 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
772 u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
775 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
776 return old & WL3501_GCR_ENECINT;
780 * wl3501_receive - Receive data from Receive Queue.
782 * Receive data from Receive Queue.
785 * @bf: address of host
786 * @size: size of buffer.
788 static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
790 u16 next_addr, next_addr1;
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,
799 sizeof(struct wl3501_rx_hdr), data,
801 sizeof(struct wl3501_rx_hdr));
802 size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
803 data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
805 wl3501_get_from_wla(this,
807 sizeof(struct wl3501_rx_hdr),
812 if (size > WL3501_BLKSZ - 5) {
813 wl3501_get_from_wla(this, next_addr + 5, data,
815 size -= WL3501_BLKSZ - 5;
816 data += WL3501_BLKSZ - 5;
817 wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
819 next_addr = next_addr1;
821 wl3501_get_from_wla(this, next_addr + 5, data, size);
828 static void wl3501_esbq_req_free(struct wl3501_card *this)
833 if (this->esbq_req_head == this->esbq_req_tail)
835 wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
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;
847 static int wl3501_esbq_confirm(struct wl3501_card *this)
851 wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
855 static void wl3501_online(struct net_device *dev)
857 struct wl3501_card *this = netdev_priv(dev);
859 printk(KERN_INFO "%s: Wireless LAN online. BSSID: %pM\n",
860 dev->name, this->bssid);
861 netif_wake_queue(dev);
864 static void wl3501_esbq_confirm_done(struct wl3501_card *this)
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;
874 static int wl3501_mgmt_auth(struct wl3501_card *this)
876 struct wl3501_auth_req sig = {
877 .sig_id = WL3501_SIG_AUTH_REQ,
878 .type = WL3501_SYS_TYPE_OPEN,
883 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
884 return wl3501_esbq_exec(this, &sig, sizeof(sig));
887 static int wl3501_mgmt_association(struct wl3501_card *this)
889 struct wl3501_assoc_req sig = {
890 .sig_id = WL3501_SIG_ASSOC_REQ,
892 .listen_interval = 5,
893 .cap_info = this->cap_info,
897 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
898 return wl3501_esbq_exec(this, &sig, sizeof(sig));
901 static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
903 struct wl3501_card *this = netdev_priv(dev);
904 struct wl3501_join_confirm sig;
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;
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);
920 const int i = this->join_sta_bss;
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);
930 this->join_sta_bss++;
931 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
932 if (!wl3501_mgmt_join(this, i))
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);
940 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
941 wl3501_mgmt_start(this);
943 wl3501_mgmt_scan(this, 100);
949 static inline void wl3501_alarm_interrupt(struct net_device *dev,
950 struct wl3501_card *this)
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);
959 static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
960 struct wl3501_card *this,
963 struct wl3501_md_confirm sig;
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);
972 static inline void wl3501_md_ind_interrupt(struct net_device *dev,
973 struct wl3501_card *this, u16 addr)
975 struct wl3501_md_ind sig;
977 u8 rssi, addr4[ETH_ALEN];
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;
987 wl3501_get_from_wla(this,
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");
996 pkt_len = sig.size + 12 - 24 - 4 - 6;
998 skb = dev_alloc_skb(pkt_len + 5);
1001 printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
1002 dev->name, pkt_len);
1003 this->stats.rx_dropped++;
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 this->stats.rx_packets++;
1012 this->stats.rx_bytes += skb->len;
1017 static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
1018 u16 addr, void *sig, int size)
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);
1026 static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
1027 struct wl3501_card *this,
1030 struct wl3501_start_confirm sig;
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);
1038 static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
1041 struct wl3501_card *this = netdev_priv(dev);
1042 struct wl3501_assoc_confirm sig;
1044 dprintk(3, "entry");
1045 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1047 if (sig.status == WL3501_STATUS_SUCCESS)
1051 static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
1054 struct wl3501_auth_confirm sig;
1056 dprintk(3, "entry");
1057 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1059 if (sig.status == WL3501_STATUS_SUCCESS)
1060 wl3501_mgmt_association(this);
1062 wl3501_mgmt_resync(this);
1065 static inline void wl3501_rx_interrupt(struct net_device *dev)
1070 struct wl3501_card *this = netdev_priv(dev);
1072 dprintk(3, "entry");
1075 if (!wl3501_esbq_confirm(this))
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));
1081 case WL3501_SIG_DEAUTH_IND:
1082 case WL3501_SIG_DISASSOC_IND:
1083 case WL3501_SIG_ALARM:
1084 wl3501_alarm_interrupt(dev, this);
1086 case WL3501_SIG_MD_CONFIRM:
1087 wl3501_md_confirm_interrupt(dev, this, addr);
1089 case WL3501_SIG_MD_IND:
1090 wl3501_md_ind_interrupt(dev, this, addr);
1092 case WL3501_SIG_GET_CONFIRM:
1093 wl3501_get_confirm_interrupt(this, addr,
1094 &this->sig_get_confirm,
1095 sizeof(this->sig_get_confirm));
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));
1102 case WL3501_SIG_START_CONFIRM:
1103 wl3501_start_confirm_interrupt(dev, this, addr);
1105 case WL3501_SIG_SCAN_CONFIRM:
1106 wl3501_mgmt_scan_confirm(this, addr);
1108 case WL3501_SIG_JOIN_CONFIRM:
1109 wl3501_mgmt_join_confirm(dev, addr);
1111 case WL3501_SIG_ASSOC_CONFIRM:
1112 wl3501_assoc_confirm_interrupt(dev, addr);
1114 case WL3501_SIG_AUTH_CONFIRM:
1115 wl3501_auth_confirm_interrupt(this, addr);
1117 case WL3501_SIG_RESYNC_CONFIRM:
1118 wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
1121 wl3501_esbq_confirm_done(this);
1123 /* free request if necessary */
1125 wl3501_esbq_req_free(this);
1130 static inline void wl3501_ack_interrupt(struct wl3501_card *this)
1132 wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
1136 * wl3501_interrupt - Hardware interrupt from card.
1137 * @irq - Interrupt number
1138 * @dev_id - net_device
1140 * We must acknowledge the interrupt as soon as possible, and block the
1141 * interrupt from the same card immediately to prevent re-entry.
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.
1147 static irqreturn_t wl3501_interrupt(int irq, void *dev_id)
1149 struct net_device *dev = dev_id;
1150 struct wl3501_card *this;
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);
1163 static int wl3501_reset_board(struct wl3501_card *this)
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);
1173 /* Reset SRAM 0x480 to zero */
1174 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1177 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1179 WL3501_NOPLOOP(1024 * 50);
1181 wl3501_unblock_interrupt(this); /* acme: was commented */
1183 /* Polling Self_Test_Status */
1184 for (i = 0; i < 10000; i++) {
1185 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1188 /* firmware complete all test successfully */
1190 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1195 printk(KERN_WARNING "%s: failed to reset the board!\n", __func__);
1201 static int wl3501_init_firmware(struct wl3501_card *this)
1204 int rc = wl3501_reset_board(this);
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));
1237 next = ptr + WL3501_BLKSZ;
1241 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1242 this->tx_buffer_tail = ptr;
1246 printk(KERN_WARNING "%s: failed!\n", __func__);
1250 static int wl3501_close(struct net_device *dev)
1252 struct wl3501_card *this = netdev_priv(dev);
1254 unsigned long flags;
1255 struct pcmcia_device *link;
1258 spin_lock_irqsave(&this->lock, flags);
1261 /* Stop wl3501_hard_start_xmit() from now on */
1262 netif_stop_queue(dev);
1263 wl3501_ack_interrupt(this);
1265 /* Mask interrupts from the SUTRO */
1266 wl3501_block_interrupt(this);
1269 printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1270 spin_unlock_irqrestore(&this->lock, flags);
1275 * wl3501_reset - Reset the SUTRO.
1276 * @dev - network device
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.
1282 static int wl3501_reset(struct net_device *dev)
1284 struct wl3501_card *this = netdev_priv(dev);
1287 wl3501_block_interrupt(this);
1289 if (wl3501_init_firmware(this)) {
1290 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1292 /* Free IRQ, and mark IRQ as unused */
1293 free_irq(dev->irq, dev);
1298 * Queue has to be started only when the Card is Started
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);
1311 static void wl3501_tx_timeout(struct net_device *dev)
1313 struct wl3501_card *this = netdev_priv(dev);
1314 struct net_device_stats *stats = &this->stats;
1315 unsigned long flags;
1319 spin_lock_irqsave(&this->lock, flags);
1320 rc = wl3501_reset(dev);
1321 spin_unlock_irqrestore(&this->lock, flags);
1323 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1326 dev->trans_start = jiffies;
1327 netif_wake_queue(dev);
1333 * 1 - Could not transmit (dev_queue_xmit will queue it)
1334 * and try to sent it later
1336 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1339 struct wl3501_card *this = netdev_priv(dev);
1340 unsigned long flags;
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);
1347 wl3501_unblock_interrupt(this);
1349 ++this->stats.tx_dropped;
1350 netif_stop_queue(dev);
1352 ++this->stats.tx_packets;
1353 this->stats.tx_bytes += skb->len;
1356 if (this->tx_buffer_cnt < 2)
1357 netif_stop_queue(dev);
1359 spin_unlock_irqrestore(&this->lock, flags);
1363 static int wl3501_open(struct net_device *dev)
1366 struct wl3501_card *this = netdev_priv(dev);
1367 unsigned long flags;
1368 struct pcmcia_device *link;
1371 spin_lock_irqsave(&this->lock, flags);
1372 if (!pcmcia_dev_present(link))
1374 netif_device_attach(dev);
1377 /* Initial WL3501 firmware */
1378 dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1379 if (wl3501_init_firmware(this))
1381 /* Initial device variables */
1382 this->adhoc_times = 0;
1383 /* Acknowledge Interrupt, for cleaning last state */
1384 wl3501_ack_interrupt(this);
1386 /* Enable interrupt from card after all */
1387 wl3501_unblock_interrupt(this);
1388 wl3501_mgmt_scan(this, 100);
1390 dprintk(1, "%s: WL3501 opened", dev->name);
1391 printk(KERN_INFO "%s: Card Name: %s\n"
1392 "%s: Firmware Date: %s\n",
1393 dev->name, this->card_name,
1394 dev->name, this->firmware_date);
1396 spin_unlock_irqrestore(&this->lock, flags);
1399 printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1403 static struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1405 struct wl3501_card *this = netdev_priv(dev);
1407 return &this->stats;
1410 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1412 struct wl3501_card *this = netdev_priv(dev);
1413 struct iw_statistics *wstats = &this->wstats;
1414 u32 value; /* size checked: it is u32 */
1416 memset(wstats, 0, sizeof(*wstats));
1417 wstats->status = netif_running(dev);
1418 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1419 &value, sizeof(value)))
1420 wstats->discard.code += value;
1421 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1422 &value, sizeof(value)))
1423 wstats->discard.code += value;
1424 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1425 &value, sizeof(value)))
1426 wstats->discard.code += value;
1427 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1428 &value, sizeof(value)))
1429 wstats->discard.retries = value;
1430 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1431 &value, sizeof(value)))
1432 wstats->discard.misc += value;
1433 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1434 &value, sizeof(value)))
1435 wstats->discard.misc += value;
1436 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1437 &value, sizeof(value)))
1438 wstats->discard.misc += value;
1439 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1440 &value, sizeof(value)))
1441 wstats->discard.misc += value;
1445 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1447 strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
1450 static const struct ethtool_ops ops = {
1451 .get_drvinfo = wl3501_get_drvinfo
1455 * wl3501_detach - deletes a driver "instance"
1458 * This deletes a driver "instance". The device is de-registered with Card
1459 * Services. If it has been released, all local data structures are freed.
1460 * Otherwise, the structures will be freed when the device is released.
1462 static void wl3501_detach(struct pcmcia_device *link)
1464 struct net_device *dev = link->priv;
1466 /* If the device is currently configured and active, we won't actually
1467 * delete it yet. Instead, it is marked so that when the release()
1468 * function is called, that will trigger a proper detach(). */
1470 while (link->open > 0)
1473 netif_device_detach(dev);
1474 wl3501_release(link);
1477 free_netdev(link->priv);
1482 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1483 union iwreq_data *wrqu, char *extra)
1485 strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1489 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1490 union iwreq_data *wrqu, char *extra)
1492 struct wl3501_card *this = netdev_priv(dev);
1493 int channel = wrqu->freq.m;
1496 if (iw_valid_channel(this->reg_domain, channel)) {
1497 this->chan = channel;
1498 rc = wl3501_reset(dev);
1503 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1504 union iwreq_data *wrqu, char *extra)
1506 struct wl3501_card *this = netdev_priv(dev);
1508 wrqu->freq.m = ieee80211_dsss_chan_to_freq(this->chan) * 100000;
1513 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1514 union iwreq_data *wrqu, char *extra)
1518 if (wrqu->mode == IW_MODE_INFRA ||
1519 wrqu->mode == IW_MODE_ADHOC ||
1520 wrqu->mode == IW_MODE_AUTO) {
1521 struct wl3501_card *this = netdev_priv(dev);
1523 this->net_type = wrqu->mode;
1524 rc = wl3501_reset(dev);
1529 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1530 union iwreq_data *wrqu, char *extra)
1532 struct wl3501_card *this = netdev_priv(dev);
1534 wrqu->mode = this->net_type;
1538 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1539 union iwreq_data *wrqu, char *extra)
1541 struct wl3501_card *this = netdev_priv(dev);
1543 wrqu->sens.value = this->rssi;
1544 wrqu->sens.disabled = !wrqu->sens.value;
1545 wrqu->sens.fixed = 1;
1549 static int wl3501_get_range(struct net_device *dev,
1550 struct iw_request_info *info,
1551 union iwreq_data *wrqu, char *extra)
1553 struct iw_range *range = (struct iw_range *)extra;
1555 /* Set the length (very important for backward compatibility) */
1556 wrqu->data.length = sizeof(*range);
1558 /* Set all the info we don't care or don't know about to zero */
1559 memset(range, 0, sizeof(*range));
1561 /* Set the Wireless Extension versions */
1562 range->we_version_compiled = WIRELESS_EXT;
1563 range->we_version_source = 1;
1564 range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
1565 /* FIXME: study the code to fill in more fields... */
1569 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1570 union iwreq_data *wrqu, char *extra)
1572 struct wl3501_card *this = netdev_priv(dev);
1573 static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1576 /* FIXME: we support other ARPHRDs...*/
1577 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1579 if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1580 /* FIXME: rescan? */
1582 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1583 /* FIXME: rescan? deassoc & scan? */
1589 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1590 union iwreq_data *wrqu, char *extra)
1592 struct wl3501_card *this = netdev_priv(dev);
1594 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1595 memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1599 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1600 union iwreq_data *wrqu, char *extra)
1603 * FIXME: trigger scanning with a reset, yes, I'm lazy
1605 return wl3501_reset(dev);
1608 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1609 union iwreq_data *wrqu, char *extra)
1611 struct wl3501_card *this = netdev_priv(dev);
1613 char *current_ev = extra;
1614 struct iw_event iwe;
1616 for (i = 0; i < this->bss_cnt; ++i) {
1617 iwe.cmd = SIOCGIWAP;
1618 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1619 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1620 current_ev = iwe_stream_add_event(info, current_ev,
1621 extra + IW_SCAN_MAX_DATA,
1622 &iwe, IW_EV_ADDR_LEN);
1623 iwe.cmd = SIOCGIWESSID;
1624 iwe.u.data.flags = 1;
1625 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1626 current_ev = iwe_stream_add_point(info, current_ev,
1627 extra + IW_SCAN_MAX_DATA,
1629 this->bss_set[i].ssid.essid);
1630 iwe.cmd = SIOCGIWMODE;
1631 iwe.u.mode = this->bss_set[i].bss_type;
1632 current_ev = iwe_stream_add_event(info, current_ev,
1633 extra + IW_SCAN_MAX_DATA,
1634 &iwe, IW_EV_UINT_LEN);
1635 iwe.cmd = SIOCGIWFREQ;
1636 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1638 current_ev = iwe_stream_add_event(info, current_ev,
1639 extra + IW_SCAN_MAX_DATA,
1640 &iwe, IW_EV_FREQ_LEN);
1641 iwe.cmd = SIOCGIWENCODE;
1642 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1643 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1645 iwe.u.data.flags = IW_ENCODE_DISABLED;
1646 iwe.u.data.length = 0;
1647 current_ev = iwe_stream_add_point(info, current_ev,
1648 extra + IW_SCAN_MAX_DATA,
1651 /* Length of data */
1652 wrqu->data.length = (current_ev - extra);
1653 wrqu->data.flags = 0; /* FIXME: set properly these flags */
1657 static int wl3501_set_essid(struct net_device *dev,
1658 struct iw_request_info *info,
1659 union iwreq_data *wrqu, char *extra)
1661 struct wl3501_card *this = netdev_priv(dev);
1663 if (wrqu->data.flags) {
1664 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1666 extra, wrqu->data.length);
1667 } else { /* We accept any ESSID */
1668 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1669 &this->essid.el, "ANY", 3);
1671 return wl3501_reset(dev);
1674 static int wl3501_get_essid(struct net_device *dev,
1675 struct iw_request_info *info,
1676 union iwreq_data *wrqu, char *extra)
1678 struct wl3501_card *this = netdev_priv(dev);
1679 unsigned long flags;
1681 spin_lock_irqsave(&this->lock, flags);
1682 wrqu->essid.flags = 1;
1683 wrqu->essid.length = this->essid.el.len;
1684 memcpy(extra, this->essid.essid, this->essid.el.len);
1685 spin_unlock_irqrestore(&this->lock, flags);
1689 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1690 union iwreq_data *wrqu, char *extra)
1692 struct wl3501_card *this = netdev_priv(dev);
1694 if (wrqu->data.length > sizeof(this->nick))
1696 strlcpy(this->nick, extra, wrqu->data.length);
1700 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1701 union iwreq_data *wrqu, char *extra)
1703 struct wl3501_card *this = netdev_priv(dev);
1705 strlcpy(extra, this->nick, 32);
1706 wrqu->data.length = strlen(extra);
1710 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1711 union iwreq_data *wrqu, char *extra)
1714 * FIXME: have to see from where to get this info, perhaps this card
1715 * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1716 * common with the Planet Access Points. -acme
1718 wrqu->bitrate.value = 2000000;
1719 wrqu->bitrate.fixed = 1;
1723 static int wl3501_get_rts_threshold(struct net_device *dev,
1724 struct iw_request_info *info,
1725 union iwreq_data *wrqu, char *extra)
1727 u16 threshold; /* size checked: it is u16 */
1728 struct wl3501_card *this = netdev_priv(dev);
1729 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1730 &threshold, sizeof(threshold));
1732 wrqu->rts.value = threshold;
1733 wrqu->rts.disabled = threshold >= 2347;
1734 wrqu->rts.fixed = 1;
1739 static int wl3501_get_frag_threshold(struct net_device *dev,
1740 struct iw_request_info *info,
1741 union iwreq_data *wrqu, char *extra)
1743 u16 threshold; /* size checked: it is u16 */
1744 struct wl3501_card *this = netdev_priv(dev);
1745 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1746 &threshold, sizeof(threshold));
1748 wrqu->frag.value = threshold;
1749 wrqu->frag.disabled = threshold >= 2346;
1750 wrqu->frag.fixed = 1;
1755 static int wl3501_get_txpow(struct net_device *dev,
1756 struct iw_request_info *info,
1757 union iwreq_data *wrqu, char *extra)
1760 struct wl3501_card *this = netdev_priv(dev);
1761 int rc = wl3501_get_mib_value(this,
1762 WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1763 &txpow, sizeof(txpow));
1765 wrqu->txpower.value = txpow;
1766 wrqu->txpower.disabled = 0;
1768 * From the MIB values I think this can be configurable,
1769 * as it lists several tx power levels -acme
1771 wrqu->txpower.fixed = 0;
1772 wrqu->txpower.flags = IW_TXPOW_MWATT;
1777 static int wl3501_get_retry(struct net_device *dev,
1778 struct iw_request_info *info,
1779 union iwreq_data *wrqu, char *extra)
1781 u8 retry; /* size checked: it is u8 */
1782 struct wl3501_card *this = netdev_priv(dev);
1783 int rc = wl3501_get_mib_value(this,
1784 WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1785 &retry, sizeof(retry));
1788 if (wrqu->retry.flags & IW_RETRY_LONG) {
1789 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1792 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1793 &retry, sizeof(retry));
1796 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
1798 wrqu->retry.value = retry;
1799 wrqu->retry.disabled = 0;
1804 static int wl3501_get_encode(struct net_device *dev,
1805 struct iw_request_info *info,
1806 union iwreq_data *wrqu, char *extra)
1808 u8 implemented, restricted, keys[100], len_keys, tocopy;
1809 struct wl3501_card *this = netdev_priv(dev);
1810 int rc = wl3501_get_mib_value(this,
1811 WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1812 &implemented, sizeof(implemented));
1816 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1819 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1820 &restricted, sizeof(restricted));
1823 wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1825 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1826 &len_keys, sizeof(len_keys));
1829 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1833 tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1834 tocopy = min_t(u8, tocopy, 100);
1835 wrqu->encoding.length = tocopy;
1836 memcpy(extra, keys, tocopy);
1841 static int wl3501_get_power(struct net_device *dev,
1842 struct iw_request_info *info,
1843 union iwreq_data *wrqu, char *extra)
1846 struct wl3501_card *this = netdev_priv(dev);
1847 int rc = wl3501_get_mib_value(this,
1848 WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1849 &pwr_state, sizeof(pwr_state));
1852 wrqu->power.disabled = !pwr_state;
1853 wrqu->power.flags = IW_POWER_ON;
1858 static const iw_handler wl3501_handler[] = {
1859 [SIOCGIWNAME - SIOCIWFIRST] = wl3501_get_name,
1860 [SIOCSIWFREQ - SIOCIWFIRST] = wl3501_set_freq,
1861 [SIOCGIWFREQ - SIOCIWFIRST] = wl3501_get_freq,
1862 [SIOCSIWMODE - SIOCIWFIRST] = wl3501_set_mode,
1863 [SIOCGIWMODE - SIOCIWFIRST] = wl3501_get_mode,
1864 [SIOCGIWSENS - SIOCIWFIRST] = wl3501_get_sens,
1865 [SIOCGIWRANGE - SIOCIWFIRST] = wl3501_get_range,
1866 [SIOCSIWSPY - SIOCIWFIRST] = iw_handler_set_spy,
1867 [SIOCGIWSPY - SIOCIWFIRST] = iw_handler_get_spy,
1868 [SIOCSIWTHRSPY - SIOCIWFIRST] = iw_handler_set_thrspy,
1869 [SIOCGIWTHRSPY - SIOCIWFIRST] = iw_handler_get_thrspy,
1870 [SIOCSIWAP - SIOCIWFIRST] = wl3501_set_wap,
1871 [SIOCGIWAP - SIOCIWFIRST] = wl3501_get_wap,
1872 [SIOCSIWSCAN - SIOCIWFIRST] = wl3501_set_scan,
1873 [SIOCGIWSCAN - SIOCIWFIRST] = wl3501_get_scan,
1874 [SIOCSIWESSID - SIOCIWFIRST] = wl3501_set_essid,
1875 [SIOCGIWESSID - SIOCIWFIRST] = wl3501_get_essid,
1876 [SIOCSIWNICKN - SIOCIWFIRST] = wl3501_set_nick,
1877 [SIOCGIWNICKN - SIOCIWFIRST] = wl3501_get_nick,
1878 [SIOCGIWRATE - SIOCIWFIRST] = wl3501_get_rate,
1879 [SIOCGIWRTS - SIOCIWFIRST] = wl3501_get_rts_threshold,
1880 [SIOCGIWFRAG - SIOCIWFIRST] = wl3501_get_frag_threshold,
1881 [SIOCGIWTXPOW - SIOCIWFIRST] = wl3501_get_txpow,
1882 [SIOCGIWRETRY - SIOCIWFIRST] = wl3501_get_retry,
1883 [SIOCGIWENCODE - SIOCIWFIRST] = wl3501_get_encode,
1884 [SIOCGIWPOWER - SIOCIWFIRST] = wl3501_get_power,
1887 static const struct iw_handler_def wl3501_handler_def = {
1888 .num_standard = ARRAY_SIZE(wl3501_handler),
1889 .standard = (iw_handler *)wl3501_handler,
1890 .get_wireless_stats = wl3501_get_wireless_stats,
1894 * wl3501_attach - creates an "instance" of the driver
1896 * Creates an "instance" of the driver, allocating local data structures for
1897 * one device. The device is registered with Card Services.
1899 * The dev_link structure is initialized, but we don't actually configure the
1900 * card at this point -- we wait until we receive a card insertion event.
1902 static int wl3501_probe(struct pcmcia_device *p_dev)
1904 struct net_device *dev;
1905 struct wl3501_card *this;
1907 /* The io structure describes IO port mapping */
1908 p_dev->io.NumPorts1 = 16;
1909 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1910 p_dev->io.IOAddrLines = 5;
1912 /* Interrupt setup */
1913 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
1914 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
1915 p_dev->irq.Handler = wl3501_interrupt;
1917 /* General socket configuration */
1918 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
1919 p_dev->conf.IntType = INT_MEMORY_AND_IO;
1920 p_dev->conf.ConfigIndex = 1;
1922 dev = alloc_etherdev(sizeof(struct wl3501_card));
1925 dev->open = wl3501_open;
1926 dev->stop = wl3501_close;
1927 dev->hard_start_xmit = wl3501_hard_start_xmit;
1928 dev->tx_timeout = wl3501_tx_timeout;
1929 dev->watchdog_timeo = 5 * HZ;
1930 dev->get_stats = wl3501_get_stats;
1931 this = netdev_priv(dev);
1932 this->wireless_data.spy_data = &this->spy_data;
1933 this->p_dev = p_dev;
1934 dev->wireless_data = &this->wireless_data;
1935 dev->wireless_handlers = (struct iw_handler_def *)&wl3501_handler_def;
1936 SET_ETHTOOL_OPS(dev, &ops);
1937 netif_stop_queue(dev);
1938 p_dev->priv = p_dev->irq.Instance = dev;
1940 return wl3501_config(p_dev);
1945 #define CS_CHECK(fn, ret) \
1946 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1949 * wl3501_config - configure the PCMCIA socket and make eth device available
1952 * wl3501_config() is scheduled to run after a CARD_INSERTION event is
1953 * received, to configure the PCMCIA socket, and to make the ethernet device
1954 * available to the system.
1956 static int wl3501_config(struct pcmcia_device *link)
1958 struct net_device *dev = link->priv;
1959 int i = 0, j, last_fn, last_ret;
1960 struct wl3501_card *this;
1962 /* Try allocating IO ports. This tries a few fixed addresses. If you
1963 * want, you can also read the card's config table to pick addresses --
1964 * see the serial driver for an example. */
1966 for (j = 0x280; j < 0x400; j += 0x20) {
1967 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
1968 * 0x200-0x2ff, and so on, because this seems safer */
1969 link->io.BasePort1 = j;
1970 link->io.BasePort2 = link->io.BasePort1 + 0x10;
1971 i = pcmcia_request_io(link, &link->io);
1976 cs_error(link, RequestIO, i);
1980 /* Now allocate an interrupt line. Note that this does not actually
1981 * assign a handler to the interrupt. */
1983 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
1985 /* This actually configures the PCMCIA socket -- setting up the I/O
1986 * windows and the interrupt mapping. */
1988 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
1990 dev->irq = link->irq.AssignedIRQ;
1991 dev->base_addr = link->io.BasePort1;
1992 SET_NETDEV_DEV(dev, &handle_to_dev(link));
1993 if (register_netdev(dev)) {
1994 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
1998 this = netdev_priv(dev);
2000 * At this point, the dev_node_t structure(s) should be initialized and
2001 * arranged in a linked list at link->dev_node.
2003 link->dev_node = &this->node;
2005 this->base_addr = dev->base_addr;
2007 if (!wl3501_get_flash_mac_addr(this)) {
2008 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2012 strcpy(this->node.dev_name, dev->name);
2014 for (i = 0; i < 6; i++)
2015 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2017 /* print probe information */
2018 printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, "
2019 "MAC addr in flash ROM:%pM\n",
2020 dev->name, this->base_addr, (int)dev->irq,
2023 * Initialize card parameters - added by jss
2025 this->net_type = IW_MODE_INFRA;
2027 this->join_sta_bss = 0;
2028 this->adhoc_times = 0;
2029 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2031 this->card_name[0] = '\0';
2032 this->firmware_date[0] = '\0';
2034 this->chan = iw_default_channel(this->reg_domain);
2035 strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2036 spin_lock_init(&this->lock);
2037 init_waitqueue_head(&this->wait);
2038 netif_start_queue(dev);
2042 cs_error(link, last_fn, last_ret);
2044 wl3501_release(link);
2049 * wl3501_release - unregister the net, release PCMCIA configuration
2052 * After a card is removed, wl3501_release() will unregister the net device,
2053 * and release the PCMCIA configuration. If the device is still open, this
2054 * will be postponed until it is closed.
2056 static void wl3501_release(struct pcmcia_device *link)
2058 struct net_device *dev = link->priv;
2060 /* Unlink the device chain */
2062 unregister_netdev(dev);
2064 pcmcia_disable_device(link);
2067 static int wl3501_suspend(struct pcmcia_device *link)
2069 struct net_device *dev = link->priv;
2071 wl3501_pwr_mgmt(netdev_priv(dev), WL3501_SUSPEND);
2073 netif_device_detach(dev);
2078 static int wl3501_resume(struct pcmcia_device *link)
2080 struct net_device *dev = link->priv;
2082 wl3501_pwr_mgmt(netdev_priv(dev), WL3501_RESUME);
2085 netif_device_attach(dev);
2092 static struct pcmcia_device_id wl3501_ids[] = {
2093 PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2096 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2098 static struct pcmcia_driver wl3501_driver = {
2099 .owner = THIS_MODULE,
2101 .name = "wl3501_cs",
2103 .probe = wl3501_probe,
2104 .remove = wl3501_detach,
2105 .id_table = wl3501_ids,
2106 .suspend = wl3501_suspend,
2107 .resume = wl3501_resume,
2110 static int __init wl3501_init_module(void)
2112 return pcmcia_register_driver(&wl3501_driver);
2115 static void __exit wl3501_exit_module(void)
2117 pcmcia_unregister_driver(&wl3501_driver);
2120 module_init(wl3501_init_module);
2121 module_exit(wl3501_exit_module);
2123 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2124 "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2125 "Gustavo Niemeyer <niemeyer@conectiva.com>");
2126 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2127 MODULE_LICENSE("GPL");