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)
29 #undef REALLY_SLOW_IO /* most systems can safely undef this */
31 #include <linux/delay.h>
32 #include <linux/types.h>
33 #include <linux/ethtool.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/fcntl.h>
40 #include <linux/if_arp.h>
41 #include <linux/ioport.h>
42 #include <linux/netdevice.h>
43 #include <linux/etherdevice.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/wireless.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", __FUNCTION__ , ##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 int wl3501_chan2freq[] = {
116 [0] = 2412, [1] = 2417, [2] = 2422, [3] = 2427, [4] = 2432,
117 [5] = 2437, [6] = 2442, [7] = 2447, [8] = 2452, [9] = 2457,
118 [10] = 2462, [11] = 2467, [12] = 2472, [13] = 2477,
121 static const struct {
124 } iw_channel_table[] = {
126 .reg_domain = IW_REG_DOMAIN_FCC,
132 .reg_domain = IW_REG_DOMAIN_DOC,
138 .reg_domain = IW_REG_DOMAIN_ETSI,
144 .reg_domain = IW_REG_DOMAIN_SPAIN,
150 .reg_domain = IW_REG_DOMAIN_FRANCE,
156 .reg_domain = IW_REG_DOMAIN_MKK,
162 .reg_domain = IW_REG_DOMAIN_MKK1,
168 .reg_domain = IW_REG_DOMAIN_ISRAEL,
176 * iw_valid_channel - validate channel in regulatory domain
177 * @reg_comain - regulatory domain
178 * @channel - channel to validate
180 * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
182 static int iw_valid_channel(int reg_domain, int channel)
186 for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
187 if (reg_domain == iw_channel_table[i].reg_domain) {
188 rc = channel >= iw_channel_table[i].min &&
189 channel <= iw_channel_table[i].max;
196 * iw_default_channel - get default channel for a regulatory domain
197 * @reg_comain - regulatory domain
199 * Returns the default channel for a regulatory domain
201 static int iw_default_channel(int reg_domain)
205 for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
206 if (reg_domain == iw_channel_table[i].reg_domain) {
207 rc = iw_channel_table[i].deflt;
213 static void iw_set_mgmt_info_element(enum iw_mgmt_info_element_ids id,
214 struct iw_mgmt_info_element *el,
215 void *value, int len)
219 memcpy(el->data, value, len);
222 static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
223 struct iw_mgmt_info_element *from)
225 iw_set_mgmt_info_element(from->id, to, from->data, from->len);
228 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
230 wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
234 * Get Ethernet MAC addresss.
236 * WARNING: We switch to FPAGE0 and switc back again.
237 * Making sure there is no other WL function beening called by ISR.
239 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
241 int base_addr = this->base_addr;
244 wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
245 wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
246 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
248 /* wait for reading EEPROM */
250 this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
252 this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
254 this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
256 this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
258 this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
260 this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
262 this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
264 wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
265 wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
266 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
268 this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
270 this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
271 /* switch to SRAM Page 0 (for safety) */
272 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
274 /* The MAC addr should be 00:60:... */
275 return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
279 * wl3501_set_to_wla - Move 'size' bytes from PC to card
280 * @dest: Card addressing space
281 * @src: PC addressing space
282 * @size: Bytes to move
284 * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
286 static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
289 /* switch to SRAM Page 0 */
290 wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
292 /* set LMAL and LMAH */
293 wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
294 wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
296 /* rep out to Port A */
297 wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
301 * wl3501_get_from_wla - Move 'size' bytes from card to PC
302 * @src: Card addressing space
303 * @dest: PC addressing space
304 * @size: Bytes to move
306 * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
308 static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
311 /* switch to SRAM Page 0 */
312 wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
314 /* set LMAL and LMAH */
315 wl3501_outb(src & 0xff, this->base_addr + WL3501_NIC_LMAL);
316 wl3501_outb((src >> 8) & 0x7f, this->base_addr + WL3501_NIC_LMAH);
318 /* rep get from Port A */
319 insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
323 * Get/Allocate a free Tx Data Buffer
325 * *--------------*-----------------*----------------------------------*
326 * | PLCP | MAC Header | DST SRC Data ... |
327 * | (24 bytes) | (30 bytes) | (6) (6) (Ethernet Row Data) |
328 * *--------------*-----------------*----------------------------------*
329 * \ \- IEEE 802.11 -/ \-------------- len --------------/
330 * \-struct wl3501_80211_tx_hdr--/ \-------- Ethernet Frame -------/
332 * Return = Postion in Card
334 static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
336 u16 next, blk_cnt = 0, zero = 0;
337 u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
340 if (full_len > this->tx_buffer_cnt * 254)
342 ret = this->tx_buffer_head;
348 wl3501_get_from_wla(this, this->tx_buffer_head, &next,
351 wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
353 this->tx_buffer_head = next;
355 /* if buffer is not enough */
356 if (!next && full_len) {
357 this->tx_buffer_head = ret;
362 this->tx_buffer_cnt -= blk_cnt;
368 * Free an allocated Tx Buffer. ptr must be correct position.
370 static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
372 /* check if all space is not free */
373 if (!this->tx_buffer_head)
374 this->tx_buffer_head = ptr;
376 wl3501_set_to_wla(this, this->tx_buffer_tail,
381 this->tx_buffer_cnt++;
382 wl3501_get_from_wla(this, ptr, &next, sizeof(next));
383 this->tx_buffer_tail = ptr;
388 static int wl3501_esbq_req_test(struct wl3501_card *this)
392 wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
396 static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
400 wl3501_set_to_wla(this, this->esbq_req_head, ptr, 2);
401 wl3501_set_to_wla(this, this->esbq_req_head + 2, &tmp, sizeof(tmp));
402 this->esbq_req_head += 4;
403 if (this->esbq_req_head >= this->esbq_req_end)
404 this->esbq_req_head = this->esbq_req_start;
407 static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
411 if (wl3501_esbq_req_test(this)) {
412 u16 ptr = wl3501_get_tx_buffer(this, sig_size);
414 wl3501_set_to_wla(this, ptr, sig, sig_size);
415 wl3501_esbq_req(this, &ptr);
422 static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
425 struct wl3501_get_req sig = {
426 .sig_id = WL3501_SIG_GET_REQ,
432 spin_lock_irqsave(&this->lock, flags);
433 if (wl3501_esbq_req_test(this)) {
434 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
436 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
437 wl3501_esbq_req(this, &ptr);
438 this->sig_get_confirm.mib_status = 255;
439 spin_unlock_irqrestore(&this->lock, flags);
440 rc = wait_event_interruptible(this->wait,
441 this->sig_get_confirm.mib_status != 255);
443 memcpy(bf, this->sig_get_confirm.mib_value,
448 spin_unlock_irqrestore(&this->lock, flags);
453 static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
455 struct wl3501_pwr_mgmt_req sig = {
456 .sig_id = WL3501_SIG_PWR_MGMT_REQ,
464 spin_lock_irqsave(&this->lock, flags);
465 if (wl3501_esbq_req_test(this)) {
466 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
468 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
469 wl3501_esbq_req(this, &ptr);
470 this->sig_pwr_mgmt_confirm.status = 255;
471 spin_unlock_irqrestore(&this->lock, flags);
472 rc = wait_event_interruptible(this->wait,
473 this->sig_pwr_mgmt_confirm.status != 255);
474 printk(KERN_INFO "%s: %s status=%d\n", __FUNCTION__,
475 suspend ? "suspend" : "resume",
476 this->sig_pwr_mgmt_confirm.status);
480 spin_unlock_irqrestore(&this->lock, flags);
486 * wl3501_send_pkt - Send a packet.
491 * data = Ethernet raw frame. (e.g. data[0] - data[5] is Dest MAC Addr,
492 * data[6] - data[11] is Src MAC Addr)
495 static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
497 u16 bf, sig_bf, next, tmplen, pktlen;
498 struct wl3501_md_req sig = {
499 .sig_id = WL3501_SIG_MD_REQ,
501 u8 *pdata = (char *)data;
504 if (wl3501_esbq_req_test(this)) {
505 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
507 if (!sig_bf) /* No free buffer available */
509 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
511 /* No free buffer available */
512 wl3501_free_tx_buffer(this, sig_bf);
516 memcpy(&sig.daddr[0], pdata, 12);
520 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
521 u8 addr4[ETH_ALEN] = {
522 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
525 wl3501_set_to_wla(this, bf + 2 +
526 offsetof(struct wl3501_tx_hdr, addr4),
527 addr4, sizeof(addr4));
528 sig.size = pktlen + 24 + 4 + 6;
529 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
530 tmplen = 254 - sizeof(struct wl3501_tx_hdr);
536 wl3501_set_to_wla(this,
537 bf + 2 + sizeof(struct wl3501_tx_hdr),
540 wl3501_get_from_wla(this, bf, &next, sizeof(next));
543 sig.size = pktlen + 24 + 4 - 2;
546 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
547 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
553 wl3501_set_to_wla(this, bf + 2 +
554 offsetof(struct wl3501_tx_hdr, addr4),
557 wl3501_get_from_wla(this, bf, &next, sizeof(next));
568 wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
570 wl3501_get_from_wla(this, bf, &next, sizeof(next));
573 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
574 wl3501_esbq_req(this, &sig_bf);
580 static int wl3501_mgmt_resync(struct wl3501_card *this)
582 struct wl3501_resync_req sig = {
583 .sig_id = WL3501_SIG_RESYNC_REQ,
586 return wl3501_esbq_exec(this, &sig, sizeof(sig));
589 static inline int wl3501_fw_bss_type(struct wl3501_card *this)
591 return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
592 WL3501_NET_TYPE_ADHOC;
595 static inline int wl3501_fw_cap_info(struct wl3501_card *this)
597 return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
598 WL3501_MGMT_CAPABILITY_IBSS;
601 static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
603 struct wl3501_scan_req sig = {
604 .sig_id = WL3501_SIG_SCAN_REQ,
605 .scan_type = WL3501_SCAN_TYPE_ACTIVE,
607 .min_chan_time = chan_time,
608 .max_chan_time = chan_time,
609 .bss_type = wl3501_fw_bss_type(this),
612 this->bss_cnt = this->join_sta_bss = 0;
613 return wl3501_esbq_exec(this, &sig, sizeof(sig));
616 static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
618 struct wl3501_join_req sig = {
619 .sig_id = WL3501_SIG_JOIN_REQ,
623 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
630 memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
631 return wl3501_esbq_exec(this, &sig, sizeof(sig));
634 static int wl3501_mgmt_start(struct wl3501_card *this)
636 struct wl3501_start_req sig = {
637 .sig_id = WL3501_SIG_START_REQ,
638 .beacon_period = 400,
642 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
649 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
652 .data_rate_labels = {
653 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
654 IW_MGMT_RATE_LABEL_1MBIT,
655 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
656 IW_MGMT_RATE_LABEL_2MBIT,
659 .operational_rset = {
661 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
664 .data_rate_labels = {
665 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
666 IW_MGMT_RATE_LABEL_1MBIT,
667 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
668 IW_MGMT_RATE_LABEL_2MBIT,
673 .id = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
678 .bss_type = wl3501_fw_bss_type(this),
679 .cap_info = wl3501_fw_cap_info(this),
682 iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
683 iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
684 return wl3501_esbq_exec(this, &sig, sizeof(sig));
687 static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
691 struct wl3501_scan_confirm sig;
694 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
695 if (sig.status == WL3501_STATUS_SUCCESS) {
696 dprintk(3, "success");
697 if ((this->net_type == IW_MODE_INFRA &&
698 (sig.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
699 (this->net_type == IW_MODE_ADHOC &&
700 (sig.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
701 this->net_type == IW_MODE_AUTO) {
702 if (!this->essid.el.len)
704 else if (this->essid.el.len == 3 &&
705 !memcmp(this->essid.essid, "ANY", 3))
707 else if (this->essid.el.len != sig.ssid.el.len)
709 else if (memcmp(this->essid.essid, sig.ssid.essid,
715 for (i = 0; i < this->bss_cnt; i++) {
716 if (!memcmp(this->bss_set[i].bssid,
717 sig.bssid, ETH_ALEN)) {
723 if (matchflag && (i < 20)) {
724 memcpy(&this->bss_set[i].beacon_period,
725 &sig.beacon_period, 73);
727 this->rssi = sig.rssi;
730 } else if (sig.status == WL3501_STATUS_TIMEOUT) {
731 dprintk(3, "timeout");
732 this->join_sta_bss = 0;
733 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
734 if (!wl3501_mgmt_join(this, i))
736 this->join_sta_bss = i;
737 if (this->join_sta_bss == this->bss_cnt) {
738 if (this->net_type == IW_MODE_INFRA)
739 wl3501_mgmt_scan(this, 100);
742 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
743 wl3501_mgmt_start(this);
745 wl3501_mgmt_scan(this, 100);
752 * wl3501_block_interrupt - Mask interrupt from SUTRO
755 * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
756 * Return: 1 if interrupt is originally enabled
758 static int wl3501_block_interrupt(struct wl3501_card *this)
760 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
761 u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
762 WL3501_GCR_ENECINT));
764 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
765 return old & WL3501_GCR_ENECINT;
769 * wl3501_unblock_interrupt - Enable interrupt from SUTRO
772 * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
773 * Return: 1 if interrupt is originally enabled
775 static int wl3501_unblock_interrupt(struct wl3501_card *this)
777 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
778 u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
781 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
782 return old & WL3501_GCR_ENECINT;
786 * wl3501_receive - Receive data from Receive Queue.
788 * Receive data from Receive Queue.
791 * @bf: address of host
792 * @size: size of buffer.
794 static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
796 u16 next_addr, next_addr1;
800 wl3501_get_from_wla(this, this->start_seg + 2,
801 &next_addr, sizeof(next_addr));
802 if (size > WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr)) {
803 wl3501_get_from_wla(this,
805 sizeof(struct wl3501_rx_hdr), data,
807 sizeof(struct wl3501_rx_hdr));
808 size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
809 data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
811 wl3501_get_from_wla(this,
813 sizeof(struct wl3501_rx_hdr),
818 if (size > WL3501_BLKSZ - 5) {
819 wl3501_get_from_wla(this, next_addr + 5, data,
821 size -= WL3501_BLKSZ - 5;
822 data += WL3501_BLKSZ - 5;
823 wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
825 next_addr = next_addr1;
827 wl3501_get_from_wla(this, next_addr + 5, data, size);
834 static void wl3501_esbq_req_free(struct wl3501_card *this)
839 if (this->esbq_req_head == this->esbq_req_tail)
841 wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
844 wl3501_get_from_wla(this, this->esbq_req_tail, &addr, sizeof(addr));
845 wl3501_free_tx_buffer(this, addr);
846 this->esbq_req_tail += 4;
847 if (this->esbq_req_tail >= this->esbq_req_end)
848 this->esbq_req_tail = this->esbq_req_start;
853 static int wl3501_esbq_confirm(struct wl3501_card *this)
857 wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
861 static void wl3501_online(struct net_device *dev)
863 struct wl3501_card *this = dev->priv;
865 printk(KERN_INFO "%s: Wireless LAN online. BSSID: "
866 "%02X %02X %02X %02X %02X %02X\n", dev->name,
867 this->bssid[0], this->bssid[1], this->bssid[2],
868 this->bssid[3], this->bssid[4], this->bssid[5]);
869 netif_wake_queue(dev);
872 static void wl3501_esbq_confirm_done(struct wl3501_card *this)
876 wl3501_set_to_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
877 this->esbq_confirm += 4;
878 if (this->esbq_confirm >= this->esbq_confirm_end)
879 this->esbq_confirm = this->esbq_confirm_start;
882 static int wl3501_mgmt_auth(struct wl3501_card *this)
884 struct wl3501_auth_req sig = {
885 .sig_id = WL3501_SIG_AUTH_REQ,
886 .type = WL3501_SYS_TYPE_OPEN,
891 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
892 return wl3501_esbq_exec(this, &sig, sizeof(sig));
895 static int wl3501_mgmt_association(struct wl3501_card *this)
897 struct wl3501_assoc_req sig = {
898 .sig_id = WL3501_SIG_ASSOC_REQ,
900 .listen_interval = 5,
901 .cap_info = this->cap_info,
905 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
906 return wl3501_esbq_exec(this, &sig, sizeof(sig));
909 static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
911 struct wl3501_card *this = dev->priv;
912 struct wl3501_join_confirm sig;
915 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
916 if (sig.status == WL3501_STATUS_SUCCESS) {
917 if (this->net_type == IW_MODE_INFRA) {
918 if (this->join_sta_bss < this->bss_cnt) {
919 const int i = this->join_sta_bss;
921 this->bss_set[i].bssid, ETH_ALEN);
922 this->chan = this->bss_set[i].ds_pset.chan;
923 iw_copy_mgmt_info_element(&this->keep_essid.el,
924 &this->bss_set[i].ssid.el);
925 wl3501_mgmt_auth(this);
928 const int i = this->join_sta_bss;
930 memcpy(&this->bssid, &this->bss_set[i].bssid, ETH_ALEN);
931 this->chan = this->bss_set[i].ds_pset.chan;
932 iw_copy_mgmt_info_element(&this->keep_essid.el,
933 &this->bss_set[i].ssid.el);
938 this->join_sta_bss++;
939 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
940 if (!wl3501_mgmt_join(this, i))
942 this->join_sta_bss = i;
943 if (this->join_sta_bss == this->bss_cnt) {
944 if (this->net_type == IW_MODE_INFRA)
945 wl3501_mgmt_scan(this, 100);
948 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
949 wl3501_mgmt_start(this);
951 wl3501_mgmt_scan(this, 100);
957 static inline void wl3501_alarm_interrupt(struct net_device *dev,
958 struct wl3501_card *this)
960 if (this->net_type == IW_MODE_INFRA) {
961 printk(KERN_INFO "Wireless LAN offline\n");
962 netif_stop_queue(dev);
963 wl3501_mgmt_resync(this);
967 static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
968 struct wl3501_card *this,
971 struct wl3501_md_confirm sig;
974 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
975 wl3501_free_tx_buffer(this, sig.data);
976 if (netif_queue_stopped(dev))
977 netif_wake_queue(dev);
980 static inline void wl3501_md_ind_interrupt(struct net_device *dev,
981 struct wl3501_card *this, u16 addr)
983 struct wl3501_md_ind sig;
985 u8 rssi, addr4[ETH_ALEN];
988 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
989 this->start_seg = sig.data;
990 wl3501_get_from_wla(this,
991 sig.data + offsetof(struct wl3501_rx_hdr, rssi),
992 &rssi, sizeof(rssi));
993 this->rssi = rssi <= 63 ? (rssi * 100) / 64 : 255;
995 wl3501_get_from_wla(this,
997 offsetof(struct wl3501_rx_hdr, addr4),
998 &addr4, sizeof(addr4));
999 if (!(addr4[0] == 0xAA && addr4[1] == 0xAA &&
1000 addr4[2] == 0x03 && addr4[4] == 0x00)) {
1001 printk(KERN_INFO "Insupported packet type!\n");
1004 pkt_len = sig.size + 12 - 24 - 4 - 6;
1006 skb = dev_alloc_skb(pkt_len + 5);
1009 printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
1010 dev->name, pkt_len);
1011 this->stats.rx_dropped++;
1014 skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
1015 eth_copy_and_sum(skb, (unsigned char *)&sig.daddr, 12, 0);
1016 wl3501_receive(this, skb->data, pkt_len);
1017 skb_put(skb, pkt_len);
1018 skb->protocol = eth_type_trans(skb, dev);
1019 dev->last_rx = jiffies;
1020 this->stats.rx_packets++;
1021 this->stats.rx_bytes += skb->len;
1026 static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
1027 u16 addr, void *sig, int size)
1029 dprintk(3, "entry");
1030 wl3501_get_from_wla(this, addr, &this->sig_get_confirm,
1031 sizeof(this->sig_get_confirm));
1032 wake_up(&this->wait);
1035 static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
1036 struct wl3501_card *this,
1039 struct wl3501_start_confirm sig;
1041 dprintk(3, "entry");
1042 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1043 if (sig.status == WL3501_STATUS_SUCCESS)
1044 netif_wake_queue(dev);
1047 static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
1050 struct wl3501_card *this = dev->priv;
1051 struct wl3501_assoc_confirm sig;
1053 dprintk(3, "entry");
1054 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1056 if (sig.status == WL3501_STATUS_SUCCESS)
1060 static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
1063 struct wl3501_auth_confirm sig;
1065 dprintk(3, "entry");
1066 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1068 if (sig.status == WL3501_STATUS_SUCCESS)
1069 wl3501_mgmt_association(this);
1071 wl3501_mgmt_resync(this);
1074 static inline void wl3501_rx_interrupt(struct net_device *dev)
1079 struct wl3501_card *this = dev->priv;
1081 dprintk(3, "entry");
1084 if (!wl3501_esbq_confirm(this))
1086 wl3501_get_from_wla(this, this->esbq_confirm, &addr, sizeof(addr));
1087 wl3501_get_from_wla(this, addr + 2, &sig_id, sizeof(sig_id));
1090 case WL3501_SIG_DEAUTH_IND:
1091 case WL3501_SIG_DISASSOC_IND:
1092 case WL3501_SIG_ALARM:
1093 wl3501_alarm_interrupt(dev, this);
1095 case WL3501_SIG_MD_CONFIRM:
1096 wl3501_md_confirm_interrupt(dev, this, addr);
1098 case WL3501_SIG_MD_IND:
1099 wl3501_md_ind_interrupt(dev, this, addr);
1101 case WL3501_SIG_GET_CONFIRM:
1102 wl3501_get_confirm_interrupt(this, addr,
1103 &this->sig_get_confirm,
1104 sizeof(this->sig_get_confirm));
1106 case WL3501_SIG_PWR_MGMT_CONFIRM:
1107 wl3501_get_confirm_interrupt(this, addr,
1108 &this->sig_pwr_mgmt_confirm,
1109 sizeof(this->sig_pwr_mgmt_confirm));
1111 case WL3501_SIG_START_CONFIRM:
1112 wl3501_start_confirm_interrupt(dev, this, addr);
1114 case WL3501_SIG_SCAN_CONFIRM:
1115 wl3501_mgmt_scan_confirm(this, addr);
1117 case WL3501_SIG_JOIN_CONFIRM:
1118 wl3501_mgmt_join_confirm(dev, addr);
1120 case WL3501_SIG_ASSOC_CONFIRM:
1121 wl3501_assoc_confirm_interrupt(dev, addr);
1123 case WL3501_SIG_AUTH_CONFIRM:
1124 wl3501_auth_confirm_interrupt(this, addr);
1126 case WL3501_SIG_RESYNC_CONFIRM:
1127 wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
1130 wl3501_esbq_confirm_done(this);
1132 /* free request if necessary */
1134 wl3501_esbq_req_free(this);
1139 static inline void wl3501_ack_interrupt(struct wl3501_card *this)
1141 wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
1145 * wl3501_interrupt - Hardware interrupt from card.
1146 * @irq - Interrupt number
1147 * @dev_id - net_device
1149 * We must acknowledge the interrupt as soon as possible, and block the
1150 * interrupt from the same card immediately to prevent re-entry.
1152 * Before accessing the Control_Status_Block, we must lock SUTRO first.
1153 * On the other hand, to prevent SUTRO from malfunctioning, we must
1154 * unlock the SUTRO as soon as possible.
1156 static irqreturn_t wl3501_interrupt(int irq, void *dev_id)
1158 struct net_device *dev = dev_id;
1159 struct wl3501_card *this;
1161 this = netdev_priv(dev);
1162 spin_lock(&this->lock);
1163 wl3501_ack_interrupt(this);
1164 wl3501_block_interrupt(this);
1165 wl3501_rx_interrupt(dev);
1166 wl3501_unblock_interrupt(this);
1167 spin_unlock(&this->lock);
1172 static int wl3501_reset_board(struct wl3501_card *this)
1178 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1179 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1180 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1182 /* Reset SRAM 0x480 to zero */
1183 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1186 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1188 WL3501_NOPLOOP(1024 * 50);
1190 wl3501_unblock_interrupt(this); /* acme: was commented */
1192 /* Polling Self_Test_Status */
1193 for (i = 0; i < 10000; i++) {
1194 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1197 /* firmware complete all test successfully */
1199 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1204 printk(KERN_WARNING "%s: failed to reset the board!\n", __FUNCTION__);
1210 static int wl3501_init_firmware(struct wl3501_card *this)
1213 int rc = wl3501_reset_board(this);
1217 this->card_name[0] = '\0';
1218 wl3501_get_from_wla(this, 0x1a00,
1219 this->card_name, sizeof(this->card_name));
1220 this->card_name[sizeof(this->card_name) - 1] = '\0';
1221 this->firmware_date[0] = '\0';
1222 wl3501_get_from_wla(this, 0x1a40,
1223 this->firmware_date, sizeof(this->firmware_date));
1224 this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
1225 /* Switch to SRAM Page 0 */
1226 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
1227 /* Read parameter from card */
1228 wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
1229 wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
1230 wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
1231 wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
1232 wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
1233 wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
1234 this->esbq_req_tail = this->esbq_req_head = this->esbq_req_start;
1235 this->esbq_req_end += this->esbq_req_start;
1236 this->esbq_confirm = this->esbq_confirm_start;
1237 this->esbq_confirm_end += this->esbq_confirm_start;
1238 /* Initial Tx Buffer */
1239 this->tx_buffer_cnt = 1;
1240 ptr = this->tx_buffer_head;
1241 next = ptr + WL3501_BLKSZ;
1242 while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
1243 this->tx_buffer_cnt++;
1244 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1246 next = ptr + WL3501_BLKSZ;
1250 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1251 this->tx_buffer_tail = ptr;
1255 printk(KERN_WARNING "%s: failed!\n", __FUNCTION__);
1259 static int wl3501_close(struct net_device *dev)
1261 struct wl3501_card *this = dev->priv;
1263 unsigned long flags;
1264 struct pcmcia_device *link;
1267 spin_lock_irqsave(&this->lock, flags);
1270 /* Stop wl3501_hard_start_xmit() from now on */
1271 netif_stop_queue(dev);
1272 wl3501_ack_interrupt(this);
1274 /* Mask interrupts from the SUTRO */
1275 wl3501_block_interrupt(this);
1278 printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1279 spin_unlock_irqrestore(&this->lock, flags);
1284 * wl3501_reset - Reset the SUTRO.
1285 * @dev - network device
1287 * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1288 * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1289 * is running. It seems to be dangerous.
1291 static int wl3501_reset(struct net_device *dev)
1293 struct wl3501_card *this = dev->priv;
1296 wl3501_block_interrupt(this);
1298 if (wl3501_init_firmware(this)) {
1299 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1301 /* Free IRQ, and mark IRQ as unused */
1302 free_irq(dev->irq, dev);
1307 * Queue has to be started only when the Card is Started
1309 netif_stop_queue(dev);
1310 this->adhoc_times = 0;
1311 wl3501_ack_interrupt(this);
1312 wl3501_unblock_interrupt(this);
1313 wl3501_mgmt_scan(this, 100);
1314 dprintk(1, "%s: device reset", dev->name);
1320 static void wl3501_tx_timeout(struct net_device *dev)
1322 struct wl3501_card *this = dev->priv;
1323 struct net_device_stats *stats = &this->stats;
1324 unsigned long flags;
1328 spin_lock_irqsave(&this->lock, flags);
1329 rc = wl3501_reset(dev);
1330 spin_unlock_irqrestore(&this->lock, flags);
1332 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1335 dev->trans_start = jiffies;
1336 netif_wake_queue(dev);
1342 * 1 - Could not transmit (dev_queue_xmit will queue it)
1343 * and try to sent it later
1345 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1348 struct wl3501_card *this = dev->priv;
1349 unsigned long flags;
1351 spin_lock_irqsave(&this->lock, flags);
1352 enabled = wl3501_block_interrupt(this);
1353 dev->trans_start = jiffies;
1354 rc = wl3501_send_pkt(this, skb->data, skb->len);
1356 wl3501_unblock_interrupt(this);
1358 ++this->stats.tx_dropped;
1359 netif_stop_queue(dev);
1361 ++this->stats.tx_packets;
1362 this->stats.tx_bytes += skb->len;
1365 if (this->tx_buffer_cnt < 2)
1366 netif_stop_queue(dev);
1368 spin_unlock_irqrestore(&this->lock, flags);
1372 static int wl3501_open(struct net_device *dev)
1375 struct wl3501_card *this = dev->priv;
1376 unsigned long flags;
1377 struct pcmcia_device *link;
1380 spin_lock_irqsave(&this->lock, flags);
1381 if (!pcmcia_dev_present(link))
1383 netif_device_attach(dev);
1386 /* Initial WL3501 firmware */
1387 dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1388 if (wl3501_init_firmware(this))
1390 /* Initial device variables */
1391 this->adhoc_times = 0;
1392 /* Acknowledge Interrupt, for cleaning last state */
1393 wl3501_ack_interrupt(this);
1395 /* Enable interrupt from card after all */
1396 wl3501_unblock_interrupt(this);
1397 wl3501_mgmt_scan(this, 100);
1399 dprintk(1, "%s: WL3501 opened", dev->name);
1400 printk(KERN_INFO "%s: Card Name: %s\n"
1401 "%s: Firmware Date: %s\n",
1402 dev->name, this->card_name,
1403 dev->name, this->firmware_date);
1405 spin_unlock_irqrestore(&this->lock, flags);
1408 printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1412 static struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1414 struct wl3501_card *this = dev->priv;
1416 return &this->stats;
1419 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1421 struct wl3501_card *this = dev->priv;
1422 struct iw_statistics *wstats = &this->wstats;
1423 u32 value; /* size checked: it is u32 */
1425 memset(wstats, 0, sizeof(*wstats));
1426 wstats->status = netif_running(dev);
1427 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1428 &value, sizeof(value)))
1429 wstats->discard.code += value;
1430 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1431 &value, sizeof(value)))
1432 wstats->discard.code += value;
1433 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1434 &value, sizeof(value)))
1435 wstats->discard.code += value;
1436 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1437 &value, sizeof(value)))
1438 wstats->discard.retries = value;
1439 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1440 &value, sizeof(value)))
1441 wstats->discard.misc += value;
1442 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1443 &value, sizeof(value)))
1444 wstats->discard.misc += value;
1445 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1446 &value, sizeof(value)))
1447 wstats->discard.misc += value;
1448 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1449 &value, sizeof(value)))
1450 wstats->discard.misc += value;
1454 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1456 strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
1459 static const struct ethtool_ops ops = {
1460 .get_drvinfo = wl3501_get_drvinfo
1464 * wl3501_detach - deletes a driver "instance"
1467 * This deletes a driver "instance". The device is de-registered with Card
1468 * Services. If it has been released, all local data structures are freed.
1469 * Otherwise, the structures will be freed when the device is released.
1471 static void wl3501_detach(struct pcmcia_device *link)
1473 struct net_device *dev = link->priv;
1475 /* If the device is currently configured and active, we won't actually
1476 * delete it yet. Instead, it is marked so that when the release()
1477 * function is called, that will trigger a proper detach(). */
1479 while (link->open > 0)
1482 netif_device_detach(dev);
1483 wl3501_release(link);
1486 free_netdev(link->priv);
1491 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1492 union iwreq_data *wrqu, char *extra)
1494 strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1498 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1499 union iwreq_data *wrqu, char *extra)
1501 struct wl3501_card *this = dev->priv;
1502 int channel = wrqu->freq.m;
1505 if (iw_valid_channel(this->reg_domain, channel)) {
1506 this->chan = channel;
1507 rc = wl3501_reset(dev);
1512 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1513 union iwreq_data *wrqu, char *extra)
1515 struct wl3501_card *this = dev->priv;
1517 wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1522 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1523 union iwreq_data *wrqu, char *extra)
1527 if (wrqu->mode == IW_MODE_INFRA ||
1528 wrqu->mode == IW_MODE_ADHOC ||
1529 wrqu->mode == IW_MODE_AUTO) {
1530 struct wl3501_card *this = dev->priv;
1532 this->net_type = wrqu->mode;
1533 rc = wl3501_reset(dev);
1538 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1539 union iwreq_data *wrqu, char *extra)
1541 struct wl3501_card *this = dev->priv;
1543 wrqu->mode = this->net_type;
1547 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1548 union iwreq_data *wrqu, char *extra)
1550 struct wl3501_card *this = dev->priv;
1552 wrqu->sens.value = this->rssi;
1553 wrqu->sens.disabled = !wrqu->sens.value;
1554 wrqu->sens.fixed = 1;
1558 static int wl3501_get_range(struct net_device *dev,
1559 struct iw_request_info *info,
1560 union iwreq_data *wrqu, char *extra)
1562 struct iw_range *range = (struct iw_range *)extra;
1564 /* Set the length (very important for backward compatibility) */
1565 wrqu->data.length = sizeof(*range);
1567 /* Set all the info we don't care or don't know about to zero */
1568 memset(range, 0, sizeof(*range));
1570 /* Set the Wireless Extension versions */
1571 range->we_version_compiled = WIRELESS_EXT;
1572 range->we_version_source = 1;
1573 range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
1574 /* FIXME: study the code to fill in more fields... */
1578 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1579 union iwreq_data *wrqu, char *extra)
1581 struct wl3501_card *this = dev->priv;
1582 static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1585 /* FIXME: we support other ARPHRDs...*/
1586 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1588 if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1589 /* FIXME: rescan? */
1591 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1592 /* FIXME: rescan? deassoc & scan? */
1598 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1599 union iwreq_data *wrqu, char *extra)
1601 struct wl3501_card *this = dev->priv;
1603 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1604 memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1608 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1609 union iwreq_data *wrqu, char *extra)
1612 * FIXME: trigger scanning with a reset, yes, I'm lazy
1614 return wl3501_reset(dev);
1617 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1618 union iwreq_data *wrqu, char *extra)
1620 struct wl3501_card *this = dev->priv;
1622 char *current_ev = extra;
1623 struct iw_event iwe;
1625 for (i = 0; i < this->bss_cnt; ++i) {
1626 iwe.cmd = SIOCGIWAP;
1627 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1628 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1629 current_ev = iwe_stream_add_event(current_ev,
1630 extra + IW_SCAN_MAX_DATA,
1631 &iwe, IW_EV_ADDR_LEN);
1632 iwe.cmd = SIOCGIWESSID;
1633 iwe.u.data.flags = 1;
1634 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1635 current_ev = iwe_stream_add_point(current_ev,
1636 extra + IW_SCAN_MAX_DATA,
1638 this->bss_set[i].ssid.essid);
1639 iwe.cmd = SIOCGIWMODE;
1640 iwe.u.mode = this->bss_set[i].bss_type;
1641 current_ev = iwe_stream_add_event(current_ev,
1642 extra + IW_SCAN_MAX_DATA,
1643 &iwe, IW_EV_UINT_LEN);
1644 iwe.cmd = SIOCGIWFREQ;
1645 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1647 current_ev = iwe_stream_add_event(current_ev,
1648 extra + IW_SCAN_MAX_DATA,
1649 &iwe, IW_EV_FREQ_LEN);
1650 iwe.cmd = SIOCGIWENCODE;
1651 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1652 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1654 iwe.u.data.flags = IW_ENCODE_DISABLED;
1655 iwe.u.data.length = 0;
1656 current_ev = iwe_stream_add_point(current_ev,
1657 extra + IW_SCAN_MAX_DATA,
1660 /* Length of data */
1661 wrqu->data.length = (current_ev - extra);
1662 wrqu->data.flags = 0; /* FIXME: set properly these flags */
1666 static int wl3501_set_essid(struct net_device *dev,
1667 struct iw_request_info *info,
1668 union iwreq_data *wrqu, char *extra)
1670 struct wl3501_card *this = dev->priv;
1672 if (wrqu->data.flags) {
1673 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1675 extra, wrqu->data.length);
1676 } else { /* We accept any ESSID */
1677 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1678 &this->essid.el, "ANY", 3);
1680 return wl3501_reset(dev);
1683 static int wl3501_get_essid(struct net_device *dev,
1684 struct iw_request_info *info,
1685 union iwreq_data *wrqu, char *extra)
1687 struct wl3501_card *this = dev->priv;
1688 unsigned long flags;
1690 spin_lock_irqsave(&this->lock, flags);
1691 wrqu->essid.flags = 1;
1692 wrqu->essid.length = this->essid.el.len;
1693 memcpy(extra, this->essid.essid, this->essid.el.len);
1694 spin_unlock_irqrestore(&this->lock, flags);
1698 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1699 union iwreq_data *wrqu, char *extra)
1701 struct wl3501_card *this = dev->priv;
1703 if (wrqu->data.length > sizeof(this->nick))
1705 strlcpy(this->nick, extra, wrqu->data.length);
1709 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1710 union iwreq_data *wrqu, char *extra)
1712 struct wl3501_card *this = dev->priv;
1714 strlcpy(extra, this->nick, 32);
1715 wrqu->data.length = strlen(extra);
1719 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1720 union iwreq_data *wrqu, char *extra)
1723 * FIXME: have to see from where to get this info, perhaps this card
1724 * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1725 * common with the Planet Access Points. -acme
1727 wrqu->bitrate.value = 2000000;
1728 wrqu->bitrate.fixed = 1;
1732 static int wl3501_get_rts_threshold(struct net_device *dev,
1733 struct iw_request_info *info,
1734 union iwreq_data *wrqu, char *extra)
1736 u16 threshold; /* size checked: it is u16 */
1737 struct wl3501_card *this = dev->priv;
1738 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1739 &threshold, sizeof(threshold));
1741 wrqu->rts.value = threshold;
1742 wrqu->rts.disabled = threshold >= 2347;
1743 wrqu->rts.fixed = 1;
1748 static int wl3501_get_frag_threshold(struct net_device *dev,
1749 struct iw_request_info *info,
1750 union iwreq_data *wrqu, char *extra)
1752 u16 threshold; /* size checked: it is u16 */
1753 struct wl3501_card *this = dev->priv;
1754 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1755 &threshold, sizeof(threshold));
1757 wrqu->frag.value = threshold;
1758 wrqu->frag.disabled = threshold >= 2346;
1759 wrqu->frag.fixed = 1;
1764 static int wl3501_get_txpow(struct net_device *dev,
1765 struct iw_request_info *info,
1766 union iwreq_data *wrqu, char *extra)
1769 struct wl3501_card *this = dev->priv;
1770 int rc = wl3501_get_mib_value(this,
1771 WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1772 &txpow, sizeof(txpow));
1774 wrqu->txpower.value = txpow;
1775 wrqu->txpower.disabled = 0;
1777 * From the MIB values I think this can be configurable,
1778 * as it lists several tx power levels -acme
1780 wrqu->txpower.fixed = 0;
1781 wrqu->txpower.flags = IW_TXPOW_MWATT;
1786 static int wl3501_get_retry(struct net_device *dev,
1787 struct iw_request_info *info,
1788 union iwreq_data *wrqu, char *extra)
1790 u8 retry; /* size checked: it is u8 */
1791 struct wl3501_card *this = dev->priv;
1792 int rc = wl3501_get_mib_value(this,
1793 WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1794 &retry, sizeof(retry));
1797 if (wrqu->retry.flags & IW_RETRY_LONG) {
1798 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1801 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1802 &retry, sizeof(retry));
1805 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
1807 wrqu->retry.value = retry;
1808 wrqu->retry.disabled = 0;
1813 static int wl3501_get_encode(struct net_device *dev,
1814 struct iw_request_info *info,
1815 union iwreq_data *wrqu, char *extra)
1817 u8 implemented, restricted, keys[100], len_keys, tocopy;
1818 struct wl3501_card *this = dev->priv;
1819 int rc = wl3501_get_mib_value(this,
1820 WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1821 &implemented, sizeof(implemented));
1825 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1828 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1829 &restricted, sizeof(restricted));
1832 wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1834 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1835 &len_keys, sizeof(len_keys));
1838 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1842 tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1843 tocopy = min_t(u8, tocopy, 100);
1844 wrqu->encoding.length = tocopy;
1845 memset(extra, 0, tocopy);
1846 memcpy(extra, keys, tocopy);
1851 static int wl3501_get_power(struct net_device *dev,
1852 struct iw_request_info *info,
1853 union iwreq_data *wrqu, char *extra)
1856 struct wl3501_card *this = dev->priv;
1857 int rc = wl3501_get_mib_value(this,
1858 WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1859 &pwr_state, sizeof(pwr_state));
1862 wrqu->power.disabled = !pwr_state;
1863 wrqu->power.flags = IW_POWER_ON;
1868 static const iw_handler wl3501_handler[] = {
1869 [SIOCGIWNAME - SIOCIWFIRST] = wl3501_get_name,
1870 [SIOCSIWFREQ - SIOCIWFIRST] = wl3501_set_freq,
1871 [SIOCGIWFREQ - SIOCIWFIRST] = wl3501_get_freq,
1872 [SIOCSIWMODE - SIOCIWFIRST] = wl3501_set_mode,
1873 [SIOCGIWMODE - SIOCIWFIRST] = wl3501_get_mode,
1874 [SIOCGIWSENS - SIOCIWFIRST] = wl3501_get_sens,
1875 [SIOCGIWRANGE - SIOCIWFIRST] = wl3501_get_range,
1876 [SIOCSIWSPY - SIOCIWFIRST] = iw_handler_set_spy,
1877 [SIOCGIWSPY - SIOCIWFIRST] = iw_handler_get_spy,
1878 [SIOCSIWTHRSPY - SIOCIWFIRST] = iw_handler_set_thrspy,
1879 [SIOCGIWTHRSPY - SIOCIWFIRST] = iw_handler_get_thrspy,
1880 [SIOCSIWAP - SIOCIWFIRST] = wl3501_set_wap,
1881 [SIOCGIWAP - SIOCIWFIRST] = wl3501_get_wap,
1882 [SIOCSIWSCAN - SIOCIWFIRST] = wl3501_set_scan,
1883 [SIOCGIWSCAN - SIOCIWFIRST] = wl3501_get_scan,
1884 [SIOCSIWESSID - SIOCIWFIRST] = wl3501_set_essid,
1885 [SIOCGIWESSID - SIOCIWFIRST] = wl3501_get_essid,
1886 [SIOCSIWNICKN - SIOCIWFIRST] = wl3501_set_nick,
1887 [SIOCGIWNICKN - SIOCIWFIRST] = wl3501_get_nick,
1888 [SIOCGIWRATE - SIOCIWFIRST] = wl3501_get_rate,
1889 [SIOCGIWRTS - SIOCIWFIRST] = wl3501_get_rts_threshold,
1890 [SIOCGIWFRAG - SIOCIWFIRST] = wl3501_get_frag_threshold,
1891 [SIOCGIWTXPOW - SIOCIWFIRST] = wl3501_get_txpow,
1892 [SIOCGIWRETRY - SIOCIWFIRST] = wl3501_get_retry,
1893 [SIOCGIWENCODE - SIOCIWFIRST] = wl3501_get_encode,
1894 [SIOCGIWPOWER - SIOCIWFIRST] = wl3501_get_power,
1897 static const struct iw_handler_def wl3501_handler_def = {
1898 .num_standard = sizeof(wl3501_handler) / sizeof(iw_handler),
1899 .standard = (iw_handler *)wl3501_handler,
1900 .get_wireless_stats = wl3501_get_wireless_stats,
1904 * wl3501_attach - creates an "instance" of the driver
1906 * Creates an "instance" of the driver, allocating local data structures for
1907 * one device. The device is registered with Card Services.
1909 * The dev_link structure is initialized, but we don't actually configure the
1910 * card at this point -- we wait until we receive a card insertion event.
1912 static int wl3501_probe(struct pcmcia_device *p_dev)
1914 struct net_device *dev;
1915 struct wl3501_card *this;
1917 /* The io structure describes IO port mapping */
1918 p_dev->io.NumPorts1 = 16;
1919 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1920 p_dev->io.IOAddrLines = 5;
1922 /* Interrupt setup */
1923 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1924 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
1925 p_dev->irq.Handler = wl3501_interrupt;
1927 /* General socket configuration */
1928 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
1929 p_dev->conf.IntType = INT_MEMORY_AND_IO;
1930 p_dev->conf.ConfigIndex = 1;
1932 dev = alloc_etherdev(sizeof(struct wl3501_card));
1935 dev->open = wl3501_open;
1936 dev->stop = wl3501_close;
1937 dev->hard_start_xmit = wl3501_hard_start_xmit;
1938 dev->tx_timeout = wl3501_tx_timeout;
1939 dev->watchdog_timeo = 5 * HZ;
1940 dev->get_stats = wl3501_get_stats;
1942 this->wireless_data.spy_data = &this->spy_data;
1943 this->p_dev = p_dev;
1944 dev->wireless_data = &this->wireless_data;
1945 dev->wireless_handlers = (struct iw_handler_def *)&wl3501_handler_def;
1946 SET_ETHTOOL_OPS(dev, &ops);
1947 netif_stop_queue(dev);
1948 p_dev->priv = p_dev->irq.Instance = dev;
1950 return wl3501_config(p_dev);
1955 #define CS_CHECK(fn, ret) \
1956 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1959 * wl3501_config - configure the PCMCIA socket and make eth device available
1962 * wl3501_config() is scheduled to run after a CARD_INSERTION event is
1963 * received, to configure the PCMCIA socket, and to make the ethernet device
1964 * available to the system.
1966 static int wl3501_config(struct pcmcia_device *link)
1968 struct net_device *dev = link->priv;
1969 int i = 0, j, last_fn, last_ret;
1970 struct wl3501_card *this;
1972 /* Try allocating IO ports. This tries a few fixed addresses. If you
1973 * want, you can also read the card's config table to pick addresses --
1974 * see the serial driver for an example. */
1976 for (j = 0x280; j < 0x400; j += 0x20) {
1977 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
1978 * 0x200-0x2ff, and so on, because this seems safer */
1979 link->io.BasePort1 = j;
1980 link->io.BasePort2 = link->io.BasePort1 + 0x10;
1981 i = pcmcia_request_io(link, &link->io);
1982 if (i == CS_SUCCESS)
1985 if (i != CS_SUCCESS) {
1986 cs_error(link, RequestIO, i);
1990 /* Now allocate an interrupt line. Note that this does not actually
1991 * assign a handler to the interrupt. */
1993 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
1995 /* This actually configures the PCMCIA socket -- setting up the I/O
1996 * windows and the interrupt mapping. */
1998 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
2000 dev->irq = link->irq.AssignedIRQ;
2001 dev->base_addr = link->io.BasePort1;
2002 SET_NETDEV_DEV(dev, &handle_to_dev(link));
2003 if (register_netdev(dev)) {
2004 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2008 SET_MODULE_OWNER(dev);
2012 * At this point, the dev_node_t structure(s) should be initialized and
2013 * arranged in a linked list at link->dev_node.
2015 link->dev_node = &this->node;
2017 this->base_addr = dev->base_addr;
2019 if (!wl3501_get_flash_mac_addr(this)) {
2020 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2024 strcpy(this->node.dev_name, dev->name);
2026 /* print probe information */
2027 printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2028 dev->name, this->base_addr, (int)dev->irq);
2029 for (i = 0; i < 6; i++) {
2030 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2031 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2035 * Initialize card parameters - added by jss
2037 this->net_type = IW_MODE_INFRA;
2039 this->join_sta_bss = 0;
2040 this->adhoc_times = 0;
2041 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2043 this->card_name[0] = '\0';
2044 this->firmware_date[0] = '\0';
2046 this->chan = iw_default_channel(this->reg_domain);
2047 strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2048 spin_lock_init(&this->lock);
2049 init_waitqueue_head(&this->wait);
2050 netif_start_queue(dev);
2054 cs_error(link, last_fn, last_ret);
2056 wl3501_release(link);
2061 * wl3501_release - unregister the net, release PCMCIA configuration
2064 * After a card is removed, wl3501_release() will unregister the net device,
2065 * and release the PCMCIA configuration. If the device is still open, this
2066 * will be postponed until it is closed.
2068 static void wl3501_release(struct pcmcia_device *link)
2070 struct net_device *dev = link->priv;
2072 /* Unlink the device chain */
2074 unregister_netdev(dev);
2076 pcmcia_disable_device(link);
2079 static int wl3501_suspend(struct pcmcia_device *link)
2081 struct net_device *dev = link->priv;
2083 wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2085 netif_device_detach(dev);
2090 static int wl3501_resume(struct pcmcia_device *link)
2092 struct net_device *dev = link->priv;
2094 wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2097 netif_device_attach(dev);
2104 static struct pcmcia_device_id wl3501_ids[] = {
2105 PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2108 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2110 static struct pcmcia_driver wl3501_driver = {
2111 .owner = THIS_MODULE,
2113 .name = "wl3501_cs",
2115 .probe = wl3501_probe,
2116 .remove = wl3501_detach,
2117 .id_table = wl3501_ids,
2118 .suspend = wl3501_suspend,
2119 .resume = wl3501_resume,
2122 static int __init wl3501_init_module(void)
2124 return pcmcia_register_driver(&wl3501_driver);
2127 static void __exit wl3501_exit_module(void)
2129 pcmcia_unregister_driver(&wl3501_driver);
2132 module_init(wl3501_init_module);
2133 module_exit(wl3501_exit_module);
2135 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2136 "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2137 "Gustavo Niemeyer <niemeyer@conectiva.com>");
2138 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2139 MODULE_LICENSE("GPL");