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
1150 * We must acknowledge the interrupt as soon as possible, and block the
1151 * interrupt from the same card immediately to prevent re-entry.
1153 * Before accessing the Control_Status_Block, we must lock SUTRO first.
1154 * On the other hand, to prevent SUTRO from malfunctioning, we must
1155 * unlock the SUTRO as soon as possible.
1157 static irqreturn_t wl3501_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1159 struct net_device *dev = (struct net_device *)dev_id;
1160 struct wl3501_card *this;
1166 spin_lock(&this->lock);
1167 wl3501_ack_interrupt(this);
1168 wl3501_block_interrupt(this);
1169 wl3501_rx_interrupt(dev);
1170 wl3501_unblock_interrupt(this);
1171 spin_unlock(&this->lock);
1173 return IRQ_RETVAL(handled);
1176 printk(KERN_ERR "%s: irq %d for unknown device.\n", __FUNCTION__, irq);
1180 static int wl3501_reset_board(struct wl3501_card *this)
1186 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1187 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1188 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1190 /* Reset SRAM 0x480 to zero */
1191 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1194 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1196 WL3501_NOPLOOP(1024 * 50);
1198 wl3501_unblock_interrupt(this); /* acme: was commented */
1200 /* Polling Self_Test_Status */
1201 for (i = 0; i < 10000; i++) {
1202 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1205 /* firmware complete all test successfully */
1207 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1212 printk(KERN_WARNING "%s: failed to reset the board!\n", __FUNCTION__);
1218 static int wl3501_init_firmware(struct wl3501_card *this)
1221 int rc = wl3501_reset_board(this);
1225 this->card_name[0] = '\0';
1226 wl3501_get_from_wla(this, 0x1a00,
1227 this->card_name, sizeof(this->card_name));
1228 this->card_name[sizeof(this->card_name) - 1] = '\0';
1229 this->firmware_date[0] = '\0';
1230 wl3501_get_from_wla(this, 0x1a40,
1231 this->firmware_date, sizeof(this->firmware_date));
1232 this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
1233 /* Switch to SRAM Page 0 */
1234 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
1235 /* Read parameter from card */
1236 wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
1237 wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
1238 wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
1239 wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
1240 wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
1241 wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
1242 this->esbq_req_tail = this->esbq_req_head = this->esbq_req_start;
1243 this->esbq_req_end += this->esbq_req_start;
1244 this->esbq_confirm = this->esbq_confirm_start;
1245 this->esbq_confirm_end += this->esbq_confirm_start;
1246 /* Initial Tx Buffer */
1247 this->tx_buffer_cnt = 1;
1248 ptr = this->tx_buffer_head;
1249 next = ptr + WL3501_BLKSZ;
1250 while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
1251 this->tx_buffer_cnt++;
1252 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1254 next = ptr + WL3501_BLKSZ;
1258 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1259 this->tx_buffer_tail = ptr;
1263 printk(KERN_WARNING "%s: failed!\n", __FUNCTION__);
1267 static int wl3501_close(struct net_device *dev)
1269 struct wl3501_card *this = dev->priv;
1271 unsigned long flags;
1272 struct pcmcia_device *link;
1275 spin_lock_irqsave(&this->lock, flags);
1278 /* Stop wl3501_hard_start_xmit() from now on */
1279 netif_stop_queue(dev);
1280 wl3501_ack_interrupt(this);
1282 /* Mask interrupts from the SUTRO */
1283 wl3501_block_interrupt(this);
1286 printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1287 spin_unlock_irqrestore(&this->lock, flags);
1292 * wl3501_reset - Reset the SUTRO.
1293 * @dev - network device
1295 * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1296 * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1297 * is running. It seems to be dangerous.
1299 static int wl3501_reset(struct net_device *dev)
1301 struct wl3501_card *this = dev->priv;
1304 wl3501_block_interrupt(this);
1306 if (wl3501_init_firmware(this)) {
1307 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1309 /* Free IRQ, and mark IRQ as unused */
1310 free_irq(dev->irq, dev);
1315 * Queue has to be started only when the Card is Started
1317 netif_stop_queue(dev);
1318 this->adhoc_times = 0;
1319 wl3501_ack_interrupt(this);
1320 wl3501_unblock_interrupt(this);
1321 wl3501_mgmt_scan(this, 100);
1322 dprintk(1, "%s: device reset", dev->name);
1328 static void wl3501_tx_timeout(struct net_device *dev)
1330 struct wl3501_card *this = dev->priv;
1331 struct net_device_stats *stats = &this->stats;
1332 unsigned long flags;
1336 spin_lock_irqsave(&this->lock, flags);
1337 rc = wl3501_reset(dev);
1338 spin_unlock_irqrestore(&this->lock, flags);
1340 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1343 dev->trans_start = jiffies;
1344 netif_wake_queue(dev);
1350 * 1 - Could not transmit (dev_queue_xmit will queue it)
1351 * and try to sent it later
1353 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1356 struct wl3501_card *this = dev->priv;
1357 unsigned long flags;
1359 spin_lock_irqsave(&this->lock, flags);
1360 enabled = wl3501_block_interrupt(this);
1361 dev->trans_start = jiffies;
1362 rc = wl3501_send_pkt(this, skb->data, skb->len);
1364 wl3501_unblock_interrupt(this);
1366 ++this->stats.tx_dropped;
1367 netif_stop_queue(dev);
1369 ++this->stats.tx_packets;
1370 this->stats.tx_bytes += skb->len;
1373 if (this->tx_buffer_cnt < 2)
1374 netif_stop_queue(dev);
1376 spin_unlock_irqrestore(&this->lock, flags);
1380 static int wl3501_open(struct net_device *dev)
1383 struct wl3501_card *this = dev->priv;
1384 unsigned long flags;
1385 struct pcmcia_device *link;
1388 spin_lock_irqsave(&this->lock, flags);
1389 if (!pcmcia_dev_present(link))
1391 netif_device_attach(dev);
1394 /* Initial WL3501 firmware */
1395 dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1396 if (wl3501_init_firmware(this))
1398 /* Initial device variables */
1399 this->adhoc_times = 0;
1400 /* Acknowledge Interrupt, for cleaning last state */
1401 wl3501_ack_interrupt(this);
1403 /* Enable interrupt from card after all */
1404 wl3501_unblock_interrupt(this);
1405 wl3501_mgmt_scan(this, 100);
1407 dprintk(1, "%s: WL3501 opened", dev->name);
1408 printk(KERN_INFO "%s: Card Name: %s\n"
1409 "%s: Firmware Date: %s\n",
1410 dev->name, this->card_name,
1411 dev->name, this->firmware_date);
1413 spin_unlock_irqrestore(&this->lock, flags);
1416 printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1420 static struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1422 struct wl3501_card *this = dev->priv;
1424 return &this->stats;
1427 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1429 struct wl3501_card *this = dev->priv;
1430 struct iw_statistics *wstats = &this->wstats;
1431 u32 value; /* size checked: it is u32 */
1433 memset(wstats, 0, sizeof(*wstats));
1434 wstats->status = netif_running(dev);
1435 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1436 &value, sizeof(value)))
1437 wstats->discard.code += value;
1438 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1439 &value, sizeof(value)))
1440 wstats->discard.code += value;
1441 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1442 &value, sizeof(value)))
1443 wstats->discard.code += value;
1444 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1445 &value, sizeof(value)))
1446 wstats->discard.retries = value;
1447 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1448 &value, sizeof(value)))
1449 wstats->discard.misc += value;
1450 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1451 &value, sizeof(value)))
1452 wstats->discard.misc += value;
1453 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1454 &value, sizeof(value)))
1455 wstats->discard.misc += value;
1456 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1457 &value, sizeof(value)))
1458 wstats->discard.misc += value;
1462 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1464 strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
1467 static struct ethtool_ops ops = {
1468 .get_drvinfo = wl3501_get_drvinfo
1472 * wl3501_detach - deletes a driver "instance"
1475 * This deletes a driver "instance". The device is de-registered with Card
1476 * Services. If it has been released, all local data structures are freed.
1477 * Otherwise, the structures will be freed when the device is released.
1479 static void wl3501_detach(struct pcmcia_device *link)
1481 struct net_device *dev = link->priv;
1483 /* If the device is currently configured and active, we won't actually
1484 * delete it yet. Instead, it is marked so that when the release()
1485 * function is called, that will trigger a proper detach(). */
1487 while (link->open > 0)
1490 netif_device_detach(dev);
1491 wl3501_release(link);
1494 free_netdev(link->priv);
1499 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1500 union iwreq_data *wrqu, char *extra)
1502 strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1506 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1507 union iwreq_data *wrqu, char *extra)
1509 struct wl3501_card *this = dev->priv;
1510 int channel = wrqu->freq.m;
1513 if (iw_valid_channel(this->reg_domain, channel)) {
1514 this->chan = channel;
1515 rc = wl3501_reset(dev);
1520 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1521 union iwreq_data *wrqu, char *extra)
1523 struct wl3501_card *this = dev->priv;
1525 wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1530 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1531 union iwreq_data *wrqu, char *extra)
1535 if (wrqu->mode == IW_MODE_INFRA ||
1536 wrqu->mode == IW_MODE_ADHOC ||
1537 wrqu->mode == IW_MODE_AUTO) {
1538 struct wl3501_card *this = dev->priv;
1540 this->net_type = wrqu->mode;
1541 rc = wl3501_reset(dev);
1546 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1547 union iwreq_data *wrqu, char *extra)
1549 struct wl3501_card *this = dev->priv;
1551 wrqu->mode = this->net_type;
1555 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1556 union iwreq_data *wrqu, char *extra)
1558 struct wl3501_card *this = dev->priv;
1560 wrqu->sens.value = this->rssi;
1561 wrqu->sens.disabled = !wrqu->sens.value;
1562 wrqu->sens.fixed = 1;
1566 static int wl3501_get_range(struct net_device *dev,
1567 struct iw_request_info *info,
1568 union iwreq_data *wrqu, char *extra)
1570 struct iw_range *range = (struct iw_range *)extra;
1572 /* Set the length (very important for backward compatibility) */
1573 wrqu->data.length = sizeof(*range);
1575 /* Set all the info we don't care or don't know about to zero */
1576 memset(range, 0, sizeof(*range));
1578 /* Set the Wireless Extension versions */
1579 range->we_version_compiled = WIRELESS_EXT;
1580 range->we_version_source = 1;
1581 range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
1582 /* FIXME: study the code to fill in more fields... */
1586 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1587 union iwreq_data *wrqu, char *extra)
1589 struct wl3501_card *this = dev->priv;
1590 static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1593 /* FIXME: we support other ARPHRDs...*/
1594 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1596 if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1597 /* FIXME: rescan? */
1599 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1600 /* FIXME: rescan? deassoc & scan? */
1606 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1607 union iwreq_data *wrqu, char *extra)
1609 struct wl3501_card *this = dev->priv;
1611 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1612 memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1616 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1617 union iwreq_data *wrqu, char *extra)
1620 * FIXME: trigger scanning with a reset, yes, I'm lazy
1622 return wl3501_reset(dev);
1625 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1626 union iwreq_data *wrqu, char *extra)
1628 struct wl3501_card *this = dev->priv;
1630 char *current_ev = extra;
1631 struct iw_event iwe;
1633 for (i = 0; i < this->bss_cnt; ++i) {
1634 iwe.cmd = SIOCGIWAP;
1635 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1636 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1637 current_ev = iwe_stream_add_event(current_ev,
1638 extra + IW_SCAN_MAX_DATA,
1639 &iwe, IW_EV_ADDR_LEN);
1640 iwe.cmd = SIOCGIWESSID;
1641 iwe.u.data.flags = 1;
1642 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1643 current_ev = iwe_stream_add_point(current_ev,
1644 extra + IW_SCAN_MAX_DATA,
1646 this->bss_set[i].ssid.essid);
1647 iwe.cmd = SIOCGIWMODE;
1648 iwe.u.mode = this->bss_set[i].bss_type;
1649 current_ev = iwe_stream_add_event(current_ev,
1650 extra + IW_SCAN_MAX_DATA,
1651 &iwe, IW_EV_UINT_LEN);
1652 iwe.cmd = SIOCGIWFREQ;
1653 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1655 current_ev = iwe_stream_add_event(current_ev,
1656 extra + IW_SCAN_MAX_DATA,
1657 &iwe, IW_EV_FREQ_LEN);
1658 iwe.cmd = SIOCGIWENCODE;
1659 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1660 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1662 iwe.u.data.flags = IW_ENCODE_DISABLED;
1663 iwe.u.data.length = 0;
1664 current_ev = iwe_stream_add_point(current_ev,
1665 extra + IW_SCAN_MAX_DATA,
1668 /* Length of data */
1669 wrqu->data.length = (current_ev - extra);
1670 wrqu->data.flags = 0; /* FIXME: set properly these flags */
1674 static int wl3501_set_essid(struct net_device *dev,
1675 struct iw_request_info *info,
1676 union iwreq_data *wrqu, char *extra)
1678 struct wl3501_card *this = dev->priv;
1680 if (wrqu->data.flags) {
1681 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1683 extra, wrqu->data.length);
1684 } else { /* We accept any ESSID */
1685 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1686 &this->essid.el, "ANY", 3);
1688 return wl3501_reset(dev);
1691 static int wl3501_get_essid(struct net_device *dev,
1692 struct iw_request_info *info,
1693 union iwreq_data *wrqu, char *extra)
1695 struct wl3501_card *this = dev->priv;
1696 unsigned long flags;
1698 spin_lock_irqsave(&this->lock, flags);
1699 wrqu->essid.flags = 1;
1700 wrqu->essid.length = this->essid.el.len;
1701 memcpy(extra, this->essid.essid, this->essid.el.len);
1702 spin_unlock_irqrestore(&this->lock, flags);
1706 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1707 union iwreq_data *wrqu, char *extra)
1709 struct wl3501_card *this = dev->priv;
1711 if (wrqu->data.length > sizeof(this->nick))
1713 strlcpy(this->nick, extra, wrqu->data.length);
1717 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1718 union iwreq_data *wrqu, char *extra)
1720 struct wl3501_card *this = dev->priv;
1722 strlcpy(extra, this->nick, 32);
1723 wrqu->data.length = strlen(extra);
1727 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1728 union iwreq_data *wrqu, char *extra)
1731 * FIXME: have to see from where to get this info, perhaps this card
1732 * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1733 * common with the Planet Access Points. -acme
1735 wrqu->bitrate.value = 2000000;
1736 wrqu->bitrate.fixed = 1;
1740 static int wl3501_get_rts_threshold(struct net_device *dev,
1741 struct iw_request_info *info,
1742 union iwreq_data *wrqu, char *extra)
1744 u16 threshold; /* size checked: it is u16 */
1745 struct wl3501_card *this = dev->priv;
1746 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1747 &threshold, sizeof(threshold));
1749 wrqu->rts.value = threshold;
1750 wrqu->rts.disabled = threshold >= 2347;
1751 wrqu->rts.fixed = 1;
1756 static int wl3501_get_frag_threshold(struct net_device *dev,
1757 struct iw_request_info *info,
1758 union iwreq_data *wrqu, char *extra)
1760 u16 threshold; /* size checked: it is u16 */
1761 struct wl3501_card *this = dev->priv;
1762 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1763 &threshold, sizeof(threshold));
1765 wrqu->frag.value = threshold;
1766 wrqu->frag.disabled = threshold >= 2346;
1767 wrqu->frag.fixed = 1;
1772 static int wl3501_get_txpow(struct net_device *dev,
1773 struct iw_request_info *info,
1774 union iwreq_data *wrqu, char *extra)
1777 struct wl3501_card *this = dev->priv;
1778 int rc = wl3501_get_mib_value(this,
1779 WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1780 &txpow, sizeof(txpow));
1782 wrqu->txpower.value = txpow;
1783 wrqu->txpower.disabled = 0;
1785 * From the MIB values I think this can be configurable,
1786 * as it lists several tx power levels -acme
1788 wrqu->txpower.fixed = 0;
1789 wrqu->txpower.flags = IW_TXPOW_MWATT;
1794 static int wl3501_get_retry(struct net_device *dev,
1795 struct iw_request_info *info,
1796 union iwreq_data *wrqu, char *extra)
1798 u8 retry; /* size checked: it is u8 */
1799 struct wl3501_card *this = dev->priv;
1800 int rc = wl3501_get_mib_value(this,
1801 WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1802 &retry, sizeof(retry));
1805 if (wrqu->retry.flags & IW_RETRY_MAX) {
1806 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1809 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1810 &retry, sizeof(retry));
1813 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1815 wrqu->retry.value = retry;
1816 wrqu->retry.disabled = 0;
1821 static int wl3501_get_encode(struct net_device *dev,
1822 struct iw_request_info *info,
1823 union iwreq_data *wrqu, char *extra)
1825 u8 implemented, restricted, keys[100], len_keys, tocopy;
1826 struct wl3501_card *this = dev->priv;
1827 int rc = wl3501_get_mib_value(this,
1828 WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1829 &implemented, sizeof(implemented));
1833 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1836 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1837 &restricted, sizeof(restricted));
1840 wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1842 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1843 &len_keys, sizeof(len_keys));
1846 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1850 tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1851 tocopy = min_t(u8, tocopy, 100);
1852 wrqu->encoding.length = tocopy;
1853 memset(extra, 0, tocopy);
1854 memcpy(extra, keys, tocopy);
1859 static int wl3501_get_power(struct net_device *dev,
1860 struct iw_request_info *info,
1861 union iwreq_data *wrqu, char *extra)
1864 struct wl3501_card *this = dev->priv;
1865 int rc = wl3501_get_mib_value(this,
1866 WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1867 &pwr_state, sizeof(pwr_state));
1870 wrqu->power.disabled = !pwr_state;
1871 wrqu->power.flags = IW_POWER_ON;
1876 static const iw_handler wl3501_handler[] = {
1877 [SIOCGIWNAME - SIOCIWFIRST] = wl3501_get_name,
1878 [SIOCSIWFREQ - SIOCIWFIRST] = wl3501_set_freq,
1879 [SIOCGIWFREQ - SIOCIWFIRST] = wl3501_get_freq,
1880 [SIOCSIWMODE - SIOCIWFIRST] = wl3501_set_mode,
1881 [SIOCGIWMODE - SIOCIWFIRST] = wl3501_get_mode,
1882 [SIOCGIWSENS - SIOCIWFIRST] = wl3501_get_sens,
1883 [SIOCGIWRANGE - SIOCIWFIRST] = wl3501_get_range,
1884 [SIOCSIWSPY - SIOCIWFIRST] = iw_handler_set_spy,
1885 [SIOCGIWSPY - SIOCIWFIRST] = iw_handler_get_spy,
1886 [SIOCSIWTHRSPY - SIOCIWFIRST] = iw_handler_set_thrspy,
1887 [SIOCGIWTHRSPY - SIOCIWFIRST] = iw_handler_get_thrspy,
1888 [SIOCSIWAP - SIOCIWFIRST] = wl3501_set_wap,
1889 [SIOCGIWAP - SIOCIWFIRST] = wl3501_get_wap,
1890 [SIOCSIWSCAN - SIOCIWFIRST] = wl3501_set_scan,
1891 [SIOCGIWSCAN - SIOCIWFIRST] = wl3501_get_scan,
1892 [SIOCSIWESSID - SIOCIWFIRST] = wl3501_set_essid,
1893 [SIOCGIWESSID - SIOCIWFIRST] = wl3501_get_essid,
1894 [SIOCSIWNICKN - SIOCIWFIRST] = wl3501_set_nick,
1895 [SIOCGIWNICKN - SIOCIWFIRST] = wl3501_get_nick,
1896 [SIOCGIWRATE - SIOCIWFIRST] = wl3501_get_rate,
1897 [SIOCGIWRTS - SIOCIWFIRST] = wl3501_get_rts_threshold,
1898 [SIOCGIWFRAG - SIOCIWFIRST] = wl3501_get_frag_threshold,
1899 [SIOCGIWTXPOW - SIOCIWFIRST] = wl3501_get_txpow,
1900 [SIOCGIWRETRY - SIOCIWFIRST] = wl3501_get_retry,
1901 [SIOCGIWENCODE - SIOCIWFIRST] = wl3501_get_encode,
1902 [SIOCGIWPOWER - SIOCIWFIRST] = wl3501_get_power,
1905 static const struct iw_handler_def wl3501_handler_def = {
1906 .num_standard = sizeof(wl3501_handler) / sizeof(iw_handler),
1907 .standard = (iw_handler *)wl3501_handler,
1908 .get_wireless_stats = wl3501_get_wireless_stats,
1912 * wl3501_attach - creates an "instance" of the driver
1914 * Creates an "instance" of the driver, allocating local data structures for
1915 * one device. The device is registered with Card Services.
1917 * The dev_link structure is initialized, but we don't actually configure the
1918 * card at this point -- we wait until we receive a card insertion event.
1920 static int wl3501_probe(struct pcmcia_device *p_dev)
1922 struct net_device *dev;
1923 struct wl3501_card *this;
1925 /* The io structure describes IO port mapping */
1926 p_dev->io.NumPorts1 = 16;
1927 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1928 p_dev->io.IOAddrLines = 5;
1930 /* Interrupt setup */
1931 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1932 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
1933 p_dev->irq.Handler = wl3501_interrupt;
1935 /* General socket configuration */
1936 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
1937 p_dev->conf.IntType = INT_MEMORY_AND_IO;
1938 p_dev->conf.ConfigIndex = 1;
1939 p_dev->conf.Present = PRESENT_OPTION;
1941 dev = alloc_etherdev(sizeof(struct wl3501_card));
1944 dev->open = wl3501_open;
1945 dev->stop = wl3501_close;
1946 dev->hard_start_xmit = wl3501_hard_start_xmit;
1947 dev->tx_timeout = wl3501_tx_timeout;
1948 dev->watchdog_timeo = 5 * HZ;
1949 dev->get_stats = wl3501_get_stats;
1951 this->wireless_data.spy_data = &this->spy_data;
1952 this->p_dev = p_dev;
1953 dev->wireless_data = &this->wireless_data;
1954 dev->wireless_handlers = (struct iw_handler_def *)&wl3501_handler_def;
1955 SET_ETHTOOL_OPS(dev, &ops);
1956 netif_stop_queue(dev);
1957 p_dev->priv = p_dev->irq.Instance = dev;
1959 return wl3501_config(p_dev);
1964 #define CS_CHECK(fn, ret) \
1965 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1968 * wl3501_config - configure the PCMCIA socket and make eth device available
1971 * wl3501_config() is scheduled to run after a CARD_INSERTION event is
1972 * received, to configure the PCMCIA socket, and to make the ethernet device
1973 * available to the system.
1975 static int wl3501_config(struct pcmcia_device *link)
1979 struct net_device *dev = link->priv;
1980 int i = 0, j, last_fn, last_ret;
1981 unsigned char bf[64];
1982 struct wl3501_card *this;
1984 /* This reads the card's CONFIG tuple to find its config registers. */
1985 tuple.Attributes = 0;
1986 tuple.DesiredTuple = CISTPL_CONFIG;
1987 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
1988 tuple.TupleData = bf;
1989 tuple.TupleDataMax = sizeof(bf);
1990 tuple.TupleOffset = 0;
1991 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
1992 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
1993 link->conf.ConfigBase = parse.config.base;
1994 link->conf.Present = parse.config.rmask[0];
1996 /* Try allocating IO ports. This tries a few fixed addresses. If you
1997 * want, you can also read the card's config table to pick addresses --
1998 * see the serial driver for an example. */
2000 for (j = 0x280; j < 0x400; j += 0x20) {
2001 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
2002 * 0x200-0x2ff, and so on, because this seems safer */
2003 link->io.BasePort1 = j;
2004 link->io.BasePort2 = link->io.BasePort1 + 0x10;
2005 i = pcmcia_request_io(link, &link->io);
2006 if (i == CS_SUCCESS)
2009 if (i != CS_SUCCESS) {
2010 cs_error(link, RequestIO, i);
2014 /* Now allocate an interrupt line. Note that this does not actually
2015 * assign a handler to the interrupt. */
2017 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
2019 /* This actually configures the PCMCIA socket -- setting up the I/O
2020 * windows and the interrupt mapping. */
2022 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
2024 dev->irq = link->irq.AssignedIRQ;
2025 dev->base_addr = link->io.BasePort1;
2026 SET_NETDEV_DEV(dev, &handle_to_dev(link));
2027 if (register_netdev(dev)) {
2028 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2032 SET_MODULE_OWNER(dev);
2036 * At this point, the dev_node_t structure(s) should be initialized and
2037 * arranged in a linked list at link->dev_node.
2039 link->dev_node = &this->node;
2041 this->base_addr = dev->base_addr;
2043 if (!wl3501_get_flash_mac_addr(this)) {
2044 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2048 strcpy(this->node.dev_name, dev->name);
2050 /* print probe information */
2051 printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2052 dev->name, this->base_addr, (int)dev->irq);
2053 for (i = 0; i < 6; i++) {
2054 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2055 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2059 * Initialize card parameters - added by jss
2061 this->net_type = IW_MODE_INFRA;
2063 this->join_sta_bss = 0;
2064 this->adhoc_times = 0;
2065 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2067 this->card_name[0] = '\0';
2068 this->firmware_date[0] = '\0';
2070 this->chan = iw_default_channel(this->reg_domain);
2071 strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2072 spin_lock_init(&this->lock);
2073 init_waitqueue_head(&this->wait);
2074 netif_start_queue(dev);
2078 cs_error(link, last_fn, last_ret);
2080 wl3501_release(link);
2085 * wl3501_release - unregister the net, release PCMCIA configuration
2088 * After a card is removed, wl3501_release() will unregister the net device,
2089 * and release the PCMCIA configuration. If the device is still open, this
2090 * will be postponed until it is closed.
2092 static void wl3501_release(struct pcmcia_device *link)
2094 struct net_device *dev = link->priv;
2096 /* Unlink the device chain */
2098 unregister_netdev(dev);
2100 pcmcia_disable_device(link);
2103 static int wl3501_suspend(struct pcmcia_device *link)
2105 struct net_device *dev = link->priv;
2107 wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2109 netif_device_detach(dev);
2114 static int wl3501_resume(struct pcmcia_device *link)
2116 struct net_device *dev = link->priv;
2118 wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2121 netif_device_attach(dev);
2128 static struct pcmcia_device_id wl3501_ids[] = {
2129 PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2132 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2134 static struct pcmcia_driver wl3501_driver = {
2135 .owner = THIS_MODULE,
2137 .name = "wl3501_cs",
2139 .probe = wl3501_probe,
2140 .remove = wl3501_detach,
2141 .id_table = wl3501_ids,
2142 .suspend = wl3501_suspend,
2143 .resume = wl3501_resume,
2146 static int __init wl3501_init_module(void)
2148 return pcmcia_register_driver(&wl3501_driver);
2151 static void __exit wl3501_exit_module(void)
2153 pcmcia_unregister_driver(&wl3501_driver);
2156 module_init(wl3501_init_module);
2157 module_exit(wl3501_exit_module);
2159 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2160 "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2161 "Gustavo Niemeyer <niemeyer@conectiva.com>");
2162 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2163 MODULE_LICENSE("GPL");