Linux Audio

Check our new training course

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