Linux Audio

Check our new training course

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