Linux Audio

Check our new training course

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