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