pcmcia: do not use io_req_t when calling pcmcia_request_io()
[deliverable/linux.git] / drivers / net / wireless / wl3501_cs.c
CommitLineData
1da177e4
LT
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 */
1da177e4 29
1da177e4
LT
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>
9ee677c2 47#include <linux/ieee80211.h>
1da177e4
LT
48
49#include <net/iw_handler.h>
50
1da177e4
LT
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
2caff147 69
1da177e4
LT
70
71#define wl3501_outb(a, b) { outb(a, b); slow_down_io(); }
72#define wl3501_outb_p(a, b) { outb_p(a, b); slow_down_io(); }
73#define wl3501_outsb(a, b, c) { outsb(a, b, c); slow_down_io(); }
74
75#define WL3501_RELEASE_TIMEOUT (25 * HZ)
76#define WL3501_MAX_ADHOC_TRIES 16
77
78#define WL3501_RESUME 0
79#define WL3501_SUSPEND 1
80
81/*
82 * The event() function is this driver's Card Services event handler. It will
83 * be called by Card Services when an appropriate card status event is
84 * received. The config() and release() entry points are used to configure or
85 * release a socket, in response to card insertion and ejection events. They
86 * are invoked from the wl24 event handler.
87 */
15b99ac1 88static int wl3501_config(struct pcmcia_device *link);
fba395ee 89static void wl3501_release(struct pcmcia_device *link);
1da177e4 90
1da177e4
LT
91static const struct {
92 int reg_domain;
93 int min, max, deflt;
94} iw_channel_table[] = {
95 {
96 .reg_domain = IW_REG_DOMAIN_FCC,
97 .min = 1,
98 .max = 11,
99 .deflt = 1,
100 },
101 {
102 .reg_domain = IW_REG_DOMAIN_DOC,
103 .min = 1,
104 .max = 11,
105 .deflt = 1,
106 },
107 {
108 .reg_domain = IW_REG_DOMAIN_ETSI,
109 .min = 1,
110 .max = 13,
111 .deflt = 1,
112 },
113 {
114 .reg_domain = IW_REG_DOMAIN_SPAIN,
115 .min = 10,
116 .max = 11,
117 .deflt = 10,
118 },
119 {
120 .reg_domain = IW_REG_DOMAIN_FRANCE,
121 .min = 10,
122 .max = 13,
123 .deflt = 10,
124 },
125 {
126 .reg_domain = IW_REG_DOMAIN_MKK,
127 .min = 14,
128 .max = 14,
129 .deflt = 14,
130 },
131 {
132 .reg_domain = IW_REG_DOMAIN_MKK1,
133 .min = 1,
134 .max = 14,
135 .deflt = 1,
136 },
137 {
138 .reg_domain = IW_REG_DOMAIN_ISRAEL,
139 .min = 3,
140 .max = 9,
141 .deflt = 9,
142 },
143};
144
145/**
146 * iw_valid_channel - validate channel in regulatory domain
147 * @reg_comain - regulatory domain
148 * @channel - channel to validate
149 *
150 * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
151 */
152static int iw_valid_channel(int reg_domain, int channel)
153{
154 int i, rc = 0;
155
156 for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
157 if (reg_domain == iw_channel_table[i].reg_domain) {
158 rc = channel >= iw_channel_table[i].min &&
159 channel <= iw_channel_table[i].max;
160 break;
161 }
162 return rc;
163}
164
165/**
166 * iw_default_channel - get default channel for a regulatory domain
167 * @reg_comain - regulatory domain
168 *
169 * Returns the default channel for a regulatory domain
170 */
171static int iw_default_channel(int reg_domain)
172{
173 int i, rc = 1;
174
175 for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
176 if (reg_domain == iw_channel_table[i].reg_domain) {
177 rc = iw_channel_table[i].deflt;
178 break;
179 }
180 return rc;
181}
182
183static void iw_set_mgmt_info_element(enum iw_mgmt_info_element_ids id,
184 struct iw_mgmt_info_element *el,
185 void *value, int len)
186{
187 el->id = id;
188 el->len = len;
189 memcpy(el->data, value, len);
190}
191
192static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
193 struct iw_mgmt_info_element *from)
194{
195 iw_set_mgmt_info_element(from->id, to, from->data, from->len);
196}
197
1da177e4
LT
198static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
199{
200 wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
201}
202
203/*
204 * Get Ethernet MAC addresss.
205 *
206 * WARNING: We switch to FPAGE0 and switc back again.
207 * Making sure there is no other WL function beening called by ISR.
208 */
209static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
210{
211 int base_addr = this->base_addr;
212
213 /* get MAC addr */
214 wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
215 wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
216 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
217
218 /* wait for reading EEPROM */
219 WL3501_NOPLOOP(100);
220 this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
221 WL3501_NOPLOOP(100);
222 this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
223 WL3501_NOPLOOP(100);
224 this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
225 WL3501_NOPLOOP(100);
226 this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
227 WL3501_NOPLOOP(100);
228 this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
229 WL3501_NOPLOOP(100);
230 this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
231 WL3501_NOPLOOP(100);
232 this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
233 WL3501_NOPLOOP(100);
234 wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
235 wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
236 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
237 WL3501_NOPLOOP(100);
238 this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
239 WL3501_NOPLOOP(100);
240 this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
241 /* switch to SRAM Page 0 (for safety) */
242 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
243
244 /* The MAC addr should be 00:60:... */
245 return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
246}
247
248/**
249 * wl3501_set_to_wla - Move 'size' bytes from PC to card
250 * @dest: Card addressing space
251 * @src: PC addressing space
252 * @size: Bytes to move
253 *
254 * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
255 */
ff1d2767
JM
256static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
257 int size)
1da177e4
LT
258{
259 /* switch to SRAM Page 0 */
260 wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
261 WL3501_BSS_SPAGE0);
262 /* set LMAL and LMAH */
263 wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
264 wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
265
266 /* rep out to Port A */
267 wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
268}
269
270/**
271 * wl3501_get_from_wla - Move 'size' bytes from card to PC
272 * @src: Card addressing space
273 * @dest: PC addressing space
274 * @size: Bytes to move
275 *
276 * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
277 */
ff1d2767
JM
278static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
279 int size)
1da177e4
LT
280{
281 /* switch to SRAM Page 0 */
282 wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
283 WL3501_BSS_SPAGE0);
284 /* set LMAL and LMAH */
285 wl3501_outb(src & 0xff, this->base_addr + WL3501_NIC_LMAL);
286 wl3501_outb((src >> 8) & 0x7f, this->base_addr + WL3501_NIC_LMAH);
287
288 /* rep get from Port A */
289 insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
290}
291
292/*
293 * Get/Allocate a free Tx Data Buffer
294 *
295 * *--------------*-----------------*----------------------------------*
296 * | PLCP | MAC Header | DST SRC Data ... |
297 * | (24 bytes) | (30 bytes) | (6) (6) (Ethernet Row Data) |
298 * *--------------*-----------------*----------------------------------*
299 * \ \- IEEE 802.11 -/ \-------------- len --------------/
300 * \-struct wl3501_80211_tx_hdr--/ \-------- Ethernet Frame -------/
301 *
302 * Return = Postion in Card
303 */
304static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
305{
306 u16 next, blk_cnt = 0, zero = 0;
307 u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
308 u16 ret = 0;
309
310 if (full_len > this->tx_buffer_cnt * 254)
311 goto out;
312 ret = this->tx_buffer_head;
313 while (full_len) {
314 if (full_len < 254)
315 full_len = 0;
316 else
317 full_len -= 254;
318 wl3501_get_from_wla(this, this->tx_buffer_head, &next,
319 sizeof(next));
320 if (!full_len)
321 wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
322 sizeof(zero));
323 this->tx_buffer_head = next;
324 blk_cnt++;
325 /* if buffer is not enough */
326 if (!next && full_len) {
327 this->tx_buffer_head = ret;
328 ret = 0;
329 goto out;
330 }
331 }
332 this->tx_buffer_cnt -= blk_cnt;
333out:
334 return ret;
335}
336
337/*
338 * Free an allocated Tx Buffer. ptr must be correct position.
339 */
340static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
341{
342 /* check if all space is not free */
343 if (!this->tx_buffer_head)
344 this->tx_buffer_head = ptr;
345 else
346 wl3501_set_to_wla(this, this->tx_buffer_tail,
347 &ptr, sizeof(ptr));
348 while (ptr) {
349 u16 next;
350
351 this->tx_buffer_cnt++;
352 wl3501_get_from_wla(this, ptr, &next, sizeof(next));
353 this->tx_buffer_tail = ptr;
354 ptr = next;
355 }
356}
357
358static int wl3501_esbq_req_test(struct wl3501_card *this)
359{
a2fb0ad3 360 u8 tmp = 0;
1da177e4
LT
361
362 wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
363 return tmp & 0x80;
364}
365
366static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
367{
368 u16 tmp = 0;
369
370 wl3501_set_to_wla(this, this->esbq_req_head, ptr, 2);
371 wl3501_set_to_wla(this, this->esbq_req_head + 2, &tmp, sizeof(tmp));
372 this->esbq_req_head += 4;
373 if (this->esbq_req_head >= this->esbq_req_end)
374 this->esbq_req_head = this->esbq_req_start;
375}
376
377static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
378{
379 int rc = -EIO;
380
381 if (wl3501_esbq_req_test(this)) {
382 u16 ptr = wl3501_get_tx_buffer(this, sig_size);
383 if (ptr) {
384 wl3501_set_to_wla(this, ptr, sig, sig_size);
385 wl3501_esbq_req(this, &ptr);
386 rc = 0;
387 }
388 }
389 return rc;
390}
391
392static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
393 void *bf, int size)
394{
395 struct wl3501_get_req sig = {
396 .sig_id = WL3501_SIG_GET_REQ,
397 .mib_attrib = index,
398 };
399 unsigned long flags;
400 int rc = -EIO;
401
402 spin_lock_irqsave(&this->lock, flags);
403 if (wl3501_esbq_req_test(this)) {
404 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
405 if (ptr) {
406 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
407 wl3501_esbq_req(this, &ptr);
408 this->sig_get_confirm.mib_status = 255;
409 spin_unlock_irqrestore(&this->lock, flags);
410 rc = wait_event_interruptible(this->wait,
411 this->sig_get_confirm.mib_status != 255);
412 if (!rc)
413 memcpy(bf, this->sig_get_confirm.mib_value,
414 size);
415 goto out;
416 }
417 }
418 spin_unlock_irqrestore(&this->lock, flags);
419out:
420 return rc;
421}
422
423static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
424{
425 struct wl3501_pwr_mgmt_req sig = {
426 .sig_id = WL3501_SIG_PWR_MGMT_REQ,
427 .pwr_save = suspend,
428 .wake_up = !suspend,
429 .receive_dtims = 10,
430 };
431 unsigned long flags;
432 int rc = -EIO;
433
434 spin_lock_irqsave(&this->lock, flags);
435 if (wl3501_esbq_req_test(this)) {
436 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
437 if (ptr) {
438 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
439 wl3501_esbq_req(this, &ptr);
440 this->sig_pwr_mgmt_confirm.status = 255;
441 spin_unlock_irqrestore(&this->lock, flags);
442 rc = wait_event_interruptible(this->wait,
443 this->sig_pwr_mgmt_confirm.status != 255);
c94c93da 444 printk(KERN_INFO "%s: %s status=%d\n", __func__,
1da177e4
LT
445 suspend ? "suspend" : "resume",
446 this->sig_pwr_mgmt_confirm.status);
447 goto out;
448 }
449 }
450 spin_unlock_irqrestore(&this->lock, flags);
451out:
452 return rc;
453}
454
455/**
456 * wl3501_send_pkt - Send a packet.
457 * @this - card
458 *
459 * Send a packet.
460 *
461 * data = Ethernet raw frame. (e.g. data[0] - data[5] is Dest MAC Addr,
462 * data[6] - data[11] is Src MAC Addr)
463 * Ref: IEEE 802.11
464 */
465static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
466{
467 u16 bf, sig_bf, next, tmplen, pktlen;
468 struct wl3501_md_req sig = {
469 .sig_id = WL3501_SIG_MD_REQ,
470 };
471 u8 *pdata = (char *)data;
472 int rc = -EIO;
473
474 if (wl3501_esbq_req_test(this)) {
475 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
476 rc = -ENOMEM;
477 if (!sig_bf) /* No free buffer available */
478 goto out;
479 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
480 if (!bf) {
481 /* No free buffer available */
482 wl3501_free_tx_buffer(this, sig_bf);
483 goto out;
484 }
485 rc = 0;
486 memcpy(&sig.daddr[0], pdata, 12);
487 pktlen = len - 12;
488 pdata += 12;
489 sig.data = bf;
490 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
491 u8 addr4[ETH_ALEN] = {
492 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
493 };
494
495 wl3501_set_to_wla(this, bf + 2 +
496 offsetof(struct wl3501_tx_hdr, addr4),
497 addr4, sizeof(addr4));
498 sig.size = pktlen + 24 + 4 + 6;
499 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
500 tmplen = 254 - sizeof(struct wl3501_tx_hdr);
501 pktlen -= tmplen;
502 } else {
503 tmplen = pktlen;
504 pktlen = 0;
505 }
506 wl3501_set_to_wla(this,
507 bf + 2 + sizeof(struct wl3501_tx_hdr),
508 pdata, tmplen);
509 pdata += tmplen;
510 wl3501_get_from_wla(this, bf, &next, sizeof(next));
511 bf = next;
512 } else {
513 sig.size = pktlen + 24 + 4 - 2;
514 pdata += 2;
515 pktlen -= 2;
516 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
517 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
518 pktlen -= tmplen;
519 } else {
520 tmplen = pktlen;
521 pktlen = 0;
522 }
523 wl3501_set_to_wla(this, bf + 2 +
524 offsetof(struct wl3501_tx_hdr, addr4),
525 pdata, tmplen);
526 pdata += tmplen;
527 wl3501_get_from_wla(this, bf, &next, sizeof(next));
528 bf = next;
529 }
530 while (pktlen > 0) {
531 if (pktlen > 254) {
532 tmplen = 254;
533 pktlen -= 254;
534 } else {
535 tmplen = pktlen;
536 pktlen = 0;
537 }
538 wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
539 pdata += tmplen;
540 wl3501_get_from_wla(this, bf, &next, sizeof(next));
541 bf = next;
542 }
543 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
544 wl3501_esbq_req(this, &sig_bf);
545 }
546out:
547 return rc;
548}
549
550static int wl3501_mgmt_resync(struct wl3501_card *this)
551{
552 struct wl3501_resync_req sig = {
553 .sig_id = WL3501_SIG_RESYNC_REQ,
554 };
555
556 return wl3501_esbq_exec(this, &sig, sizeof(sig));
557}
558
559static inline int wl3501_fw_bss_type(struct wl3501_card *this)
560{
561 return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
562 WL3501_NET_TYPE_ADHOC;
563}
564
565static inline int wl3501_fw_cap_info(struct wl3501_card *this)
566{
567 return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
568 WL3501_MGMT_CAPABILITY_IBSS;
569}
570
571static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
572{
573 struct wl3501_scan_req sig = {
574 .sig_id = WL3501_SIG_SCAN_REQ,
575 .scan_type = WL3501_SCAN_TYPE_ACTIVE,
576 .probe_delay = 0x10,
577 .min_chan_time = chan_time,
578 .max_chan_time = chan_time,
579 .bss_type = wl3501_fw_bss_type(this),
580 };
581
582 this->bss_cnt = this->join_sta_bss = 0;
583 return wl3501_esbq_exec(this, &sig, sizeof(sig));
584}
585
586static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
587{
588 struct wl3501_join_req sig = {
589 .sig_id = WL3501_SIG_JOIN_REQ,
590 .timeout = 10,
591 .ds_pset = {
592 .el = {
593 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
594 .len = 1,
595 },
596 .chan = this->chan,
597 },
598 };
599
600 memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
601 return wl3501_esbq_exec(this, &sig, sizeof(sig));
602}
603
604static int wl3501_mgmt_start(struct wl3501_card *this)
605{
606 struct wl3501_start_req sig = {
607 .sig_id = WL3501_SIG_START_REQ,
608 .beacon_period = 400,
609 .dtim_period = 1,
610 .ds_pset = {
611 .el = {
612 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
613 .len = 1,
614 },
615 .chan = this->chan,
616 },
617 .bss_basic_rset = {
618 .el = {
619 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
620 .len = 2,
621 },
622 .data_rate_labels = {
623 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
624 IW_MGMT_RATE_LABEL_1MBIT,
625 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
626 IW_MGMT_RATE_LABEL_2MBIT,
627 },
628 },
629 .operational_rset = {
630 .el = {
631 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
632 .len = 2,
633 },
634 .data_rate_labels = {
635 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
636 IW_MGMT_RATE_LABEL_1MBIT,
637 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
638 IW_MGMT_RATE_LABEL_2MBIT,
639 },
640 },
641 .ibss_pset = {
642 .el = {
643 .id = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
644 .len = 2,
645 },
646 .atim_window = 10,
647 },
648 .bss_type = wl3501_fw_bss_type(this),
649 .cap_info = wl3501_fw_cap_info(this),
650 };
651
652 iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
653 iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
654 return wl3501_esbq_exec(this, &sig, sizeof(sig));
655}
656
657static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
658{
659 u16 i = 0;
660 int matchflag = 0;
661 struct wl3501_scan_confirm sig;
662
2caff147 663 pr_debug("entry");
1da177e4
LT
664 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
665 if (sig.status == WL3501_STATUS_SUCCESS) {
2caff147 666 pr_debug("success");
1da177e4
LT
667 if ((this->net_type == IW_MODE_INFRA &&
668 (sig.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
669 (this->net_type == IW_MODE_ADHOC &&
670 (sig.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
671 this->net_type == IW_MODE_AUTO) {
672 if (!this->essid.el.len)
673 matchflag = 1;
674 else if (this->essid.el.len == 3 &&
675 !memcmp(this->essid.essid, "ANY", 3))
676 matchflag = 1;
677 else if (this->essid.el.len != sig.ssid.el.len)
678 matchflag = 0;
679 else if (memcmp(this->essid.essid, sig.ssid.essid,
680 this->essid.el.len))
681 matchflag = 0;
682 else
683 matchflag = 1;
684 if (matchflag) {
685 for (i = 0; i < this->bss_cnt; i++) {
686 if (!memcmp(this->bss_set[i].bssid,
687 sig.bssid, ETH_ALEN)) {
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) {
2caff147 701 pr_debug("timeout");
1da177e4
LT
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{
6dbc9c89 833 struct wl3501_card *this = netdev_priv(dev);
1da177e4 834
e174961c
JB
835 printk(KERN_INFO "%s: Wireless LAN online. BSSID: %pM\n",
836 dev->name, this->bssid);
1da177e4
LT
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
2caff147 858 pr_debug("entry");
1da177e4
LT
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
2caff147 872 pr_debug("entry");
1da177e4
LT
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{
6dbc9c89 879 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
880 struct wl3501_join_confirm sig;
881
2caff147 882 pr_debug("entry");
1da177e4
LT
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
2caff147 941 pr_debug("entry");
1da177e4
LT
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 "Insupported 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);
4255d411 979 dev->stats.rx_dropped++;
1da177e4
LT
980 } else {
981 skb->dev = dev;
982 skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
8c7b7faa 983 skb_copy_to_linear_data(skb, (unsigned char *)&sig.daddr, 12);
1da177e4
LT
984 wl3501_receive(this, skb->data, pkt_len);
985 skb_put(skb, pkt_len);
986 skb->protocol = eth_type_trans(skb, dev);
4255d411
SH
987 dev->stats.rx_packets++;
988 dev->stats.rx_bytes += skb->len;
1da177e4
LT
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{
2caff147 996 pr_debug("entry");
1da177e4
LT
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
2caff147 1008 pr_debug("entry");
1da177e4
LT
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{
6dbc9c89 1017 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1018 struct wl3501_assoc_confirm sig;
1019
2caff147 1020 pr_debug("entry");
1da177e4
LT
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
2caff147 1032 pr_debug("entry");
1da177e4
LT
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;
6dbc9c89 1046 struct wl3501_card *this = netdev_priv(dev);
1da177e4 1047
2caff147 1048 pr_debug("entry");
1da177e4
LT
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
1da177e4
LT
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 */
7d12e780 1123static irqreturn_t wl3501_interrupt(int irq, void *dev_id)
1da177e4 1124{
c31f28e7 1125 struct net_device *dev = dev_id;
1da177e4 1126 struct wl3501_card *this;
1da177e4 1127
c31f28e7 1128 this = netdev_priv(dev);
1da177e4
LT
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);
c31f28e7
JG
1135
1136 return IRQ_HANDLED;
1da177e4
LT
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 }
c94c93da 1171 printk(KERN_WARNING "%s: failed to reset the board!\n", __func__);
1da177e4
LT
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:
c94c93da 1222 printk(KERN_WARNING "%s: failed!\n", __func__);
1da177e4
LT
1223 goto out;
1224}
1225
1226static int wl3501_close(struct net_device *dev)
1227{
6dbc9c89 1228 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1229 int rc = -ENODEV;
1230 unsigned long flags;
fba395ee 1231 struct pcmcia_device *link;
fd238232 1232 link = this->p_dev;
1da177e4
LT
1233
1234 spin_lock_irqsave(&this->lock, flags);
1da177e4
LT
1235 link->open--;
1236
1237 /* Stop wl3501_hard_start_xmit() from now on */
1238 netif_stop_queue(dev);
1239 wl3501_ack_interrupt(this);
1240
1241 /* Mask interrupts from the SUTRO */
1242 wl3501_block_interrupt(this);
1243
1244 rc = 0;
1245 printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1da177e4
LT
1246 spin_unlock_irqrestore(&this->lock, flags);
1247 return rc;
1248}
1249
1250/**
1251 * wl3501_reset - Reset the SUTRO.
1252 * @dev - network device
1253 *
1254 * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1255 * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1256 * is running. It seems to be dangerous.
1257 */
1258static int wl3501_reset(struct net_device *dev)
1259{
6dbc9c89 1260 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1261 int rc = -ENODEV;
1262
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);
2caff147 1281 pr_debug("%s: device reset", dev->name);
1da177e4
LT
1282 rc = 0;
1283out:
1284 return rc;
1285}
1286
1287static void wl3501_tx_timeout(struct net_device *dev)
1288{
6dbc9c89 1289 struct wl3501_card *this = netdev_priv(dev);
4255d411 1290 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
1291 unsigned long flags;
1292 int rc;
1293
1294 stats->tx_errors++;
1295 spin_lock_irqsave(&this->lock, flags);
1296 rc = wl3501_reset(dev);
1297 spin_unlock_irqrestore(&this->lock, flags);
1298 if (rc)
1299 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1300 dev->name, rc);
1301 else {
1ae5dc34 1302 dev->trans_start = jiffies; /* prevent tx timeout */
1da177e4
LT
1303 netif_wake_queue(dev);
1304 }
1305}
1306
1307/*
1308 * Return : 0 - OK
1309 * 1 - Could not transmit (dev_queue_xmit will queue it)
1310 * and try to sent it later
1311 */
d0cf9c0d
SH
1312static netdev_tx_t wl3501_hard_start_xmit(struct sk_buff *skb,
1313 struct net_device *dev)
1da177e4
LT
1314{
1315 int enabled, rc;
6dbc9c89 1316 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1317 unsigned long flags;
1318
1319 spin_lock_irqsave(&this->lock, flags);
1320 enabled = wl3501_block_interrupt(this);
1da177e4
LT
1321 rc = wl3501_send_pkt(this, skb->data, skb->len);
1322 if (enabled)
1323 wl3501_unblock_interrupt(this);
1324 if (rc) {
4255d411 1325 ++dev->stats.tx_dropped;
1da177e4
LT
1326 netif_stop_queue(dev);
1327 } else {
4255d411
SH
1328 ++dev->stats.tx_packets;
1329 dev->stats.tx_bytes += skb->len;
1da177e4
LT
1330 kfree_skb(skb);
1331
1332 if (this->tx_buffer_cnt < 2)
1333 netif_stop_queue(dev);
1334 }
1335 spin_unlock_irqrestore(&this->lock, flags);
ec634fe3 1336 return NETDEV_TX_OK;
1da177e4
LT
1337}
1338
1339static int wl3501_open(struct net_device *dev)
1340{
1341 int rc = -ENODEV;
6dbc9c89 1342 struct wl3501_card *this = netdev_priv(dev);
1da177e4 1343 unsigned long flags;
fba395ee 1344 struct pcmcia_device *link;
fd238232 1345 link = this->p_dev;
1da177e4
LT
1346
1347 spin_lock_irqsave(&this->lock, flags);
9940ec36 1348 if (!pcmcia_dev_present(link))
1da177e4
LT
1349 goto out;
1350 netif_device_attach(dev);
1351 link->open++;
1352
1353 /* Initial WL3501 firmware */
2caff147 1354 pr_debug("%s: Initialize WL3501 firmware...", dev->name);
1da177e4
LT
1355 if (wl3501_init_firmware(this))
1356 goto fail;
1357 /* Initial device variables */
1358 this->adhoc_times = 0;
1359 /* Acknowledge Interrupt, for cleaning last state */
1360 wl3501_ack_interrupt(this);
1361
1362 /* Enable interrupt from card after all */
1363 wl3501_unblock_interrupt(this);
1364 wl3501_mgmt_scan(this, 100);
1365 rc = 0;
2caff147 1366 pr_debug("%s: WL3501 opened", dev->name);
1da177e4
LT
1367 printk(KERN_INFO "%s: Card Name: %s\n"
1368 "%s: Firmware Date: %s\n",
1369 dev->name, this->card_name,
1370 dev->name, this->firmware_date);
1371out:
1372 spin_unlock_irqrestore(&this->lock, flags);
1373 return rc;
1374fail:
1375 printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1376 goto out;
1377}
1378
ff1d2767 1379static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1da177e4 1380{
6dbc9c89 1381 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1382 struct iw_statistics *wstats = &this->wstats;
1383 u32 value; /* size checked: it is u32 */
1384
1385 memset(wstats, 0, sizeof(*wstats));
1386 wstats->status = netif_running(dev);
1387 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1388 &value, sizeof(value)))
1389 wstats->discard.code += value;
1390 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1391 &value, sizeof(value)))
1392 wstats->discard.code += value;
1393 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1394 &value, sizeof(value)))
1395 wstats->discard.code += value;
1396 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1397 &value, sizeof(value)))
1398 wstats->discard.retries = value;
1399 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1400 &value, sizeof(value)))
1401 wstats->discard.misc += value;
1402 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1403 &value, sizeof(value)))
1404 wstats->discard.misc += value;
1405 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1406 &value, sizeof(value)))
1407 wstats->discard.misc += value;
1408 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1409 &value, sizeof(value)))
1410 wstats->discard.misc += value;
1411 return wstats;
1412}
1413
1414static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1415{
ac8b4228 1416 strlcpy(info->driver, "wl3501_cs", sizeof(info->driver));
1da177e4
LT
1417}
1418
7282d491 1419static const struct ethtool_ops ops = {
1da177e4
LT
1420 .get_drvinfo = wl3501_get_drvinfo
1421};
1422
1423/**
1424 * wl3501_detach - deletes a driver "instance"
1425 * @link - FILL_IN
1426 *
1427 * This deletes a driver "instance". The device is de-registered with Card
1428 * Services. If it has been released, all local data structures are freed.
1429 * Otherwise, the structures will be freed when the device is released.
1430 */
fba395ee 1431static void wl3501_detach(struct pcmcia_device *link)
1da177e4 1432{
cc3b4866 1433 struct net_device *dev = link->priv;
1da177e4 1434
1da177e4
LT
1435 /* If the device is currently configured and active, we won't actually
1436 * delete it yet. Instead, it is marked so that when the release()
1437 * function is called, that will trigger a proper detach(). */
1438
e2d40963
DB
1439 while (link->open > 0)
1440 wl3501_close(dev);
1da177e4 1441
e2d40963
DB
1442 netif_device_detach(dev);
1443 wl3501_release(link);
1da177e4 1444
c7c2fa07
DB
1445 unregister_netdev(dev);
1446
1da177e4
LT
1447 if (link->priv)
1448 free_netdev(link->priv);
1da177e4
LT
1449}
1450
1451static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1452 union iwreq_data *wrqu, char *extra)
1453{
1454 strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1455 return 0;
1456}
1457
1458static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1459 union iwreq_data *wrqu, char *extra)
1460{
6dbc9c89 1461 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1462 int channel = wrqu->freq.m;
1463 int rc = -EINVAL;
1464
1465 if (iw_valid_channel(this->reg_domain, channel)) {
1466 this->chan = channel;
1467 rc = wl3501_reset(dev);
1468 }
1469 return rc;
1470}
1471
1472static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1473 union iwreq_data *wrqu, char *extra)
1474{
6dbc9c89 1475 struct wl3501_card *this = netdev_priv(dev);
1da177e4 1476
9ee677c2 1477 wrqu->freq.m = ieee80211_dsss_chan_to_freq(this->chan) * 100000;
1da177e4
LT
1478 wrqu->freq.e = 1;
1479 return 0;
1480}
1481
1482static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1483 union iwreq_data *wrqu, char *extra)
1484{
1485 int rc = -EINVAL;
1486
1487 if (wrqu->mode == IW_MODE_INFRA ||
1488 wrqu->mode == IW_MODE_ADHOC ||
1489 wrqu->mode == IW_MODE_AUTO) {
6dbc9c89 1490 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1491
1492 this->net_type = wrqu->mode;
1493 rc = wl3501_reset(dev);
1494 }
1495 return rc;
1496}
1497
1498static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1499 union iwreq_data *wrqu, char *extra)
1500{
6dbc9c89 1501 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1502
1503 wrqu->mode = this->net_type;
1504 return 0;
1505}
1506
1507static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1508 union iwreq_data *wrqu, char *extra)
1509{
6dbc9c89 1510 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1511
1512 wrqu->sens.value = this->rssi;
1513 wrqu->sens.disabled = !wrqu->sens.value;
1514 wrqu->sens.fixed = 1;
1515 return 0;
1516}
1517
1518static int wl3501_get_range(struct net_device *dev,
1519 struct iw_request_info *info,
1520 union iwreq_data *wrqu, char *extra)
1521{
1522 struct iw_range *range = (struct iw_range *)extra;
1523
1524 /* Set the length (very important for backward compatibility) */
1525 wrqu->data.length = sizeof(*range);
1526
1527 /* Set all the info we don't care or don't know about to zero */
1528 memset(range, 0, sizeof(*range));
1529
1530 /* Set the Wireless Extension versions */
1531 range->we_version_compiled = WIRELESS_EXT;
1532 range->we_version_source = 1;
1533 range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
1534 /* FIXME: study the code to fill in more fields... */
1535 return 0;
1536}
1537
1538static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1539 union iwreq_data *wrqu, char *extra)
1540{
6dbc9c89 1541 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1542 static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1543 int rc = -EINVAL;
1544
1545 /* FIXME: we support other ARPHRDs...*/
1546 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1547 goto out;
1548 if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1549 /* FIXME: rescan? */
1550 } else
1551 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1552 /* FIXME: rescan? deassoc & scan? */
1553 rc = 0;
1554out:
1555 return rc;
1556}
1557
1558static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1559 union iwreq_data *wrqu, char *extra)
1560{
6dbc9c89 1561 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1562
1563 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1564 memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1565 return 0;
1566}
1567
1568static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1569 union iwreq_data *wrqu, char *extra)
1570{
1571 /*
1572 * FIXME: trigger scanning with a reset, yes, I'm lazy
1573 */
1574 return wl3501_reset(dev);
1575}
1576
1577static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1578 union iwreq_data *wrqu, char *extra)
1579{
6dbc9c89 1580 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1581 int i;
1582 char *current_ev = extra;
1583 struct iw_event iwe;
1584
1585 for (i = 0; i < this->bss_cnt; ++i) {
1586 iwe.cmd = SIOCGIWAP;
1587 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1588 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
ccc58057 1589 current_ev = iwe_stream_add_event(info, current_ev,
1da177e4
LT
1590 extra + IW_SCAN_MAX_DATA,
1591 &iwe, IW_EV_ADDR_LEN);
1592 iwe.cmd = SIOCGIWESSID;
1593 iwe.u.data.flags = 1;
1594 iwe.u.data.length = this->bss_set[i].ssid.el.len;
ccc58057 1595 current_ev = iwe_stream_add_point(info, current_ev,
1da177e4
LT
1596 extra + IW_SCAN_MAX_DATA,
1597 &iwe,
1598 this->bss_set[i].ssid.essid);
1599 iwe.cmd = SIOCGIWMODE;
1600 iwe.u.mode = this->bss_set[i].bss_type;
ccc58057 1601 current_ev = iwe_stream_add_event(info, current_ev,
1da177e4
LT
1602 extra + IW_SCAN_MAX_DATA,
1603 &iwe, IW_EV_UINT_LEN);
1604 iwe.cmd = SIOCGIWFREQ;
1605 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1606 iwe.u.freq.e = 0;
ccc58057 1607 current_ev = iwe_stream_add_event(info, current_ev,
1da177e4
LT
1608 extra + IW_SCAN_MAX_DATA,
1609 &iwe, IW_EV_FREQ_LEN);
1610 iwe.cmd = SIOCGIWENCODE;
1611 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1612 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1613 else
1614 iwe.u.data.flags = IW_ENCODE_DISABLED;
1615 iwe.u.data.length = 0;
ccc58057 1616 current_ev = iwe_stream_add_point(info, current_ev,
1da177e4
LT
1617 extra + IW_SCAN_MAX_DATA,
1618 &iwe, NULL);
1619 }
1620 /* Length of data */
1621 wrqu->data.length = (current_ev - extra);
1622 wrqu->data.flags = 0; /* FIXME: set properly these flags */
1623 return 0;
1624}
1625
1626static int wl3501_set_essid(struct net_device *dev,
1627 struct iw_request_info *info,
1628 union iwreq_data *wrqu, char *extra)
1629{
6dbc9c89 1630 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1631
1632 if (wrqu->data.flags) {
1633 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1634 &this->essid.el,
1635 extra, wrqu->data.length);
1636 } else { /* We accept any ESSID */
1637 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1638 &this->essid.el, "ANY", 3);
1639 }
1640 return wl3501_reset(dev);
1641}
1642
1643static int wl3501_get_essid(struct net_device *dev,
1644 struct iw_request_info *info,
1645 union iwreq_data *wrqu, char *extra)
1646{
6dbc9c89 1647 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1648 unsigned long flags;
1649
1650 spin_lock_irqsave(&this->lock, flags);
1651 wrqu->essid.flags = 1;
1652 wrqu->essid.length = this->essid.el.len;
1653 memcpy(extra, this->essid.essid, this->essid.el.len);
1654 spin_unlock_irqrestore(&this->lock, flags);
1655 return 0;
1656}
1657
1658static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1659 union iwreq_data *wrqu, char *extra)
1660{
6dbc9c89 1661 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1662
1663 if (wrqu->data.length > sizeof(this->nick))
1664 return -E2BIG;
1665 strlcpy(this->nick, extra, wrqu->data.length);
1666 return 0;
1667}
1668
1669static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1670 union iwreq_data *wrqu, char *extra)
1671{
6dbc9c89 1672 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1673
1674 strlcpy(extra, this->nick, 32);
1675 wrqu->data.length = strlen(extra);
1676 return 0;
1677}
1678
1679static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1680 union iwreq_data *wrqu, char *extra)
1681{
1682 /*
1683 * FIXME: have to see from where to get this info, perhaps this card
1684 * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1685 * common with the Planet Access Points. -acme
1686 */
1687 wrqu->bitrate.value = 2000000;
1688 wrqu->bitrate.fixed = 1;
1689 return 0;
1690}
1691
1692static int wl3501_get_rts_threshold(struct net_device *dev,
1693 struct iw_request_info *info,
1694 union iwreq_data *wrqu, char *extra)
1695{
1696 u16 threshold; /* size checked: it is u16 */
6dbc9c89 1697 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1698 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1699 &threshold, sizeof(threshold));
1700 if (!rc) {
1701 wrqu->rts.value = threshold;
1702 wrqu->rts.disabled = threshold >= 2347;
1703 wrqu->rts.fixed = 1;
1704 }
1705 return rc;
1706}
1707
1708static int wl3501_get_frag_threshold(struct net_device *dev,
1709 struct iw_request_info *info,
1710 union iwreq_data *wrqu, char *extra)
1711{
1712 u16 threshold; /* size checked: it is u16 */
6dbc9c89 1713 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1714 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1715 &threshold, sizeof(threshold));
1716 if (!rc) {
1717 wrqu->frag.value = threshold;
1718 wrqu->frag.disabled = threshold >= 2346;
1719 wrqu->frag.fixed = 1;
1720 }
1721 return rc;
1722}
1723
1724static int wl3501_get_txpow(struct net_device *dev,
1725 struct iw_request_info *info,
1726 union iwreq_data *wrqu, char *extra)
1727{
1728 u16 txpow;
6dbc9c89 1729 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1730 int rc = wl3501_get_mib_value(this,
1731 WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1732 &txpow, sizeof(txpow));
1733 if (!rc) {
1734 wrqu->txpower.value = txpow;
1735 wrqu->txpower.disabled = 0;
1736 /*
1737 * From the MIB values I think this can be configurable,
1738 * as it lists several tx power levels -acme
1739 */
1740 wrqu->txpower.fixed = 0;
1741 wrqu->txpower.flags = IW_TXPOW_MWATT;
1742 }
1743 return rc;
1744}
1745
1746static int wl3501_get_retry(struct net_device *dev,
1747 struct iw_request_info *info,
1748 union iwreq_data *wrqu, char *extra)
1749{
1750 u8 retry; /* size checked: it is u8 */
6dbc9c89 1751 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1752 int rc = wl3501_get_mib_value(this,
1753 WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1754 &retry, sizeof(retry));
1755 if (rc)
1756 goto out;
de9621bc
JT
1757 if (wrqu->retry.flags & IW_RETRY_LONG) {
1758 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1da177e4
LT
1759 goto set_value;
1760 }
1761 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1762 &retry, sizeof(retry));
1763 if (rc)
1764 goto out;
de9621bc 1765 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
1da177e4
LT
1766set_value:
1767 wrqu->retry.value = retry;
1768 wrqu->retry.disabled = 0;
1769out:
1770 return rc;
1771}
1772
1773static int wl3501_get_encode(struct net_device *dev,
1774 struct iw_request_info *info,
1775 union iwreq_data *wrqu, char *extra)
1776{
1777 u8 implemented, restricted, keys[100], len_keys, tocopy;
6dbc9c89 1778 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1779 int rc = wl3501_get_mib_value(this,
1780 WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1781 &implemented, sizeof(implemented));
1782 if (rc)
1783 goto out;
1784 if (!implemented) {
1785 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1786 goto out;
1787 }
1788 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1789 &restricted, sizeof(restricted));
1790 if (rc)
1791 goto out;
1792 wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1793 IW_ENCODE_OPEN;
1794 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1795 &len_keys, sizeof(len_keys));
1796 if (rc)
1797 goto out;
1798 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1799 keys, len_keys);
1800 if (rc)
1801 goto out;
1802 tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1803 tocopy = min_t(u8, tocopy, 100);
1804 wrqu->encoding.length = tocopy;
1da177e4
LT
1805 memcpy(extra, keys, tocopy);
1806out:
1807 return rc;
1808}
1809
1810static int wl3501_get_power(struct net_device *dev,
1811 struct iw_request_info *info,
1812 union iwreq_data *wrqu, char *extra)
1813{
1814 u8 pwr_state;
6dbc9c89 1815 struct wl3501_card *this = netdev_priv(dev);
1da177e4
LT
1816 int rc = wl3501_get_mib_value(this,
1817 WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1818 &pwr_state, sizeof(pwr_state));
1819 if (rc)
1820 goto out;
1821 wrqu->power.disabled = !pwr_state;
1822 wrqu->power.flags = IW_POWER_ON;
1823out:
1824 return rc;
1825}
1826
1827static const iw_handler wl3501_handler[] = {
270020e8
JP
1828 IW_HANDLER(SIOCGIWNAME, wl3501_get_name),
1829 IW_HANDLER(SIOCSIWFREQ, wl3501_set_freq),
1830 IW_HANDLER(SIOCGIWFREQ, wl3501_get_freq),
1831 IW_HANDLER(SIOCSIWMODE, wl3501_set_mode),
1832 IW_HANDLER(SIOCGIWMODE, wl3501_get_mode),
1833 IW_HANDLER(SIOCGIWSENS, wl3501_get_sens),
1834 IW_HANDLER(SIOCGIWRANGE, wl3501_get_range),
1835 IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
1836 IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
1837 IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
1838 IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
1839 IW_HANDLER(SIOCSIWAP, wl3501_set_wap),
1840 IW_HANDLER(SIOCGIWAP, wl3501_get_wap),
1841 IW_HANDLER(SIOCSIWSCAN, wl3501_set_scan),
1842 IW_HANDLER(SIOCGIWSCAN, wl3501_get_scan),
1843 IW_HANDLER(SIOCSIWESSID, wl3501_set_essid),
1844 IW_HANDLER(SIOCGIWESSID, wl3501_get_essid),
1845 IW_HANDLER(SIOCSIWNICKN, wl3501_set_nick),
1846 IW_HANDLER(SIOCGIWNICKN, wl3501_get_nick),
1847 IW_HANDLER(SIOCGIWRATE, wl3501_get_rate),
1848 IW_HANDLER(SIOCGIWRTS, wl3501_get_rts_threshold),
1849 IW_HANDLER(SIOCGIWFRAG, wl3501_get_frag_threshold),
1850 IW_HANDLER(SIOCGIWTXPOW, wl3501_get_txpow),
1851 IW_HANDLER(SIOCGIWRETRY, wl3501_get_retry),
1852 IW_HANDLER(SIOCGIWENCODE, wl3501_get_encode),
1853 IW_HANDLER(SIOCGIWPOWER, wl3501_get_power),
1da177e4
LT
1854};
1855
1856static const struct iw_handler_def wl3501_handler_def = {
ff8ac609 1857 .num_standard = ARRAY_SIZE(wl3501_handler),
1da177e4 1858 .standard = (iw_handler *)wl3501_handler,
00b309f5 1859 .get_wireless_stats = wl3501_get_wireless_stats,
1da177e4
LT
1860};
1861
85a151b7
SH
1862static const struct net_device_ops wl3501_netdev_ops = {
1863 .ndo_open = wl3501_open,
1864 .ndo_stop = wl3501_close,
1865 .ndo_start_xmit = wl3501_hard_start_xmit,
1866 .ndo_tx_timeout = wl3501_tx_timeout,
1867 .ndo_change_mtu = eth_change_mtu,
1868 .ndo_set_mac_address = eth_mac_addr,
1869 .ndo_validate_addr = eth_validate_addr,
1870};
1871
1da177e4
LT
1872/**
1873 * wl3501_attach - creates an "instance" of the driver
1874 *
1875 * Creates an "instance" of the driver, allocating local data structures for
1876 * one device. The device is registered with Card Services.
1877 *
1878 * The dev_link structure is initialized, but we don't actually configure the
1879 * card at this point -- we wait until we receive a card insertion event.
1880 */
15b99ac1 1881static int wl3501_probe(struct pcmcia_device *p_dev)
1da177e4 1882{
1da177e4 1883 struct net_device *dev;
00b309f5 1884 struct wl3501_card *this;
1da177e4
LT
1885
1886 /* The io structure describes IO port mapping */
90abdc3b
DB
1887 p_dev->resource[0]->end = 16;
1888 p_dev->resource[0]->flags = IO_DATA_PATH_WIDTH_8;
1da177e4 1889
1da177e4 1890 /* General socket configuration */
fba395ee
DB
1891 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
1892 p_dev->conf.IntType = INT_MEMORY_AND_IO;
1893 p_dev->conf.ConfigIndex = 1;
1da177e4
LT
1894
1895 dev = alloc_etherdev(sizeof(struct wl3501_card));
1896 if (!dev)
1897 goto out_link;
4255d411
SH
1898
1899
85a151b7 1900 dev->netdev_ops = &wl3501_netdev_ops;
1da177e4 1901 dev->watchdog_timeo = 5 * HZ;
4255d411 1902
6dbc9c89 1903 this = netdev_priv(dev);
00b309f5 1904 this->wireless_data.spy_data = &this->spy_data;
fd238232 1905 this->p_dev = p_dev;
00b309f5 1906 dev->wireless_data = &this->wireless_data;
85a151b7 1907 dev->wireless_handlers = &wl3501_handler_def;
1da177e4
LT
1908 SET_ETHTOOL_OPS(dev, &ops);
1909 netif_stop_queue(dev);
5fa9167a 1910 p_dev->priv = dev;
1da177e4 1911
15b99ac1 1912 return wl3501_config(p_dev);
1da177e4 1913out_link:
f8cfa618 1914 return -ENOMEM;
1da177e4
LT
1915}
1916
1da177e4
LT
1917/**
1918 * wl3501_config - configure the PCMCIA socket and make eth device available
1919 * @link - FILL_IN
1920 *
1921 * wl3501_config() is scheduled to run after a CARD_INSERTION event is
1922 * received, to configure the PCMCIA socket, and to make the ethernet device
1923 * available to the system.
1924 */
15b99ac1 1925static int wl3501_config(struct pcmcia_device *link)
1da177e4 1926{
1da177e4 1927 struct net_device *dev = link->priv;
2caff147 1928 int i = 0, j, ret;
1da177e4
LT
1929 struct wl3501_card *this;
1930
1da177e4
LT
1931 /* Try allocating IO ports. This tries a few fixed addresses. If you
1932 * want, you can also read the card's config table to pick addresses --
1933 * see the serial driver for an example. */
90abdc3b 1934 link->io_lines = 5;
1da177e4
LT
1935
1936 for (j = 0x280; j < 0x400; j += 0x20) {
1937 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
1938 * 0x200-0x2ff, and so on, because this seems safer */
90abdc3b
DB
1939 link->resource[0]->start = j;
1940 link->resource[1]->start = link->resource[0]->start + 0x10;
1941 i = pcmcia_request_io(link);
4c89e88b 1942 if (i == 0)
1da177e4
LT
1943 break;
1944 }
2caff147 1945 if (i != 0)
1da177e4 1946 goto failed;
1da177e4
LT
1947
1948 /* Now allocate an interrupt line. Note that this does not actually
1949 * assign a handler to the interrupt. */
1950
eb14120f 1951 ret = pcmcia_request_irq(link, wl3501_interrupt);
2caff147
DB
1952 if (ret)
1953 goto failed;
1da177e4
LT
1954
1955 /* This actually configures the PCMCIA socket -- setting up the I/O
1956 * windows and the interrupt mapping. */
1957
2caff147
DB
1958 ret = pcmcia_request_configuration(link, &link->conf);
1959 if (ret)
1960 goto failed;
1da177e4 1961
eb14120f 1962 dev->irq = link->irq;
9a017a91 1963 dev->base_addr = link->resource[0]->start;
dd2e5a15 1964 SET_NETDEV_DEV(dev, &link->dev);
1da177e4
LT
1965 if (register_netdev(dev)) {
1966 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
1967 goto failed;
1968 }
1969
6dbc9c89 1970 this = netdev_priv(dev);
1da177e4
LT
1971
1972 this->base_addr = dev->base_addr;
1973
1974 if (!wl3501_get_flash_mac_addr(this)) {
1975 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
1976 dev->name);
c7c2fa07 1977 unregister_netdev(dev);
1da177e4
LT
1978 goto failed;
1979 }
1da177e4 1980
0795af57 1981 for (i = 0; i < 6; i++)
1da177e4 1982 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
0795af57
JP
1983
1984 /* print probe information */
1985 printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, "
e174961c 1986 "MAC addr in flash ROM:%pM\n",
0795af57 1987 dev->name, this->base_addr, (int)dev->irq,
e174961c 1988 dev->dev_addr);
1da177e4
LT
1989 /*
1990 * Initialize card parameters - added by jss
1991 */
1992 this->net_type = IW_MODE_INFRA;
1993 this->bss_cnt = 0;
1994 this->join_sta_bss = 0;
1995 this->adhoc_times = 0;
1996 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
1997 "ANY", 3);
1998 this->card_name[0] = '\0';
1999 this->firmware_date[0] = '\0';
2000 this->rssi = 255;
2001 this->chan = iw_default_channel(this->reg_domain);
2002 strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2003 spin_lock_init(&this->lock);
2004 init_waitqueue_head(&this->wait);
2005 netif_start_queue(dev);
15b99ac1
DB
2006 return 0;
2007
1da177e4
LT
2008failed:
2009 wl3501_release(link);
15b99ac1 2010 return -ENODEV;
1da177e4
LT
2011}
2012
2013/**
2014 * wl3501_release - unregister the net, release PCMCIA configuration
2015 * @arg - link
2016 *
2017 * After a card is removed, wl3501_release() will unregister the net device,
2018 * and release the PCMCIA configuration. If the device is still open, this
2019 * will be postponed until it is closed.
2020 */
fba395ee 2021static void wl3501_release(struct pcmcia_device *link)
1da177e4 2022{
fba395ee 2023 pcmcia_disable_device(link);
1da177e4
LT
2024}
2025
fba395ee 2026static int wl3501_suspend(struct pcmcia_device *link)
98e4c28b 2027{
98e4c28b
DB
2028 struct net_device *dev = link->priv;
2029
6dbc9c89 2030 wl3501_pwr_mgmt(netdev_priv(dev), WL3501_SUSPEND);
e2d40963 2031 if (link->open)
8661bb5b 2032 netif_device_detach(dev);
98e4c28b
DB
2033
2034 return 0;
2035}
2036
fba395ee 2037static int wl3501_resume(struct pcmcia_device *link)
98e4c28b 2038{
98e4c28b
DB
2039 struct net_device *dev = link->priv;
2040
6dbc9c89 2041 wl3501_pwr_mgmt(netdev_priv(dev), WL3501_RESUME);
e2d40963 2042 if (link->open) {
8661bb5b
DB
2043 wl3501_reset(dev);
2044 netif_device_attach(dev);
98e4c28b
DB
2045 }
2046
2047 return 0;
2048}
2049
2050
77b73f9b
DB
2051static struct pcmcia_device_id wl3501_ids[] = {
2052 PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2053 PCMCIA_DEVICE_NULL
2054};
2055MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2056
1da177e4 2057static struct pcmcia_driver wl3501_driver = {
1e212f36
DB
2058 .owner = THIS_MODULE,
2059 .drv = {
2060 .name = "wl3501_cs",
1da177e4 2061 },
15b99ac1 2062 .probe = wl3501_probe,
cc3b4866 2063 .remove = wl3501_detach,
77b73f9b 2064 .id_table = wl3501_ids,
98e4c28b
DB
2065 .suspend = wl3501_suspend,
2066 .resume = wl3501_resume,
1da177e4
LT
2067};
2068
2069static int __init wl3501_init_module(void)
2070{
2071 return pcmcia_register_driver(&wl3501_driver);
2072}
2073
2074static void __exit wl3501_exit_module(void)
2075{
1da177e4 2076 pcmcia_unregister_driver(&wl3501_driver);
1da177e4
LT
2077}
2078
2079module_init(wl3501_init_module);
2080module_exit(wl3501_exit_module);
2081
2082MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2083 "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2084 "Gustavo Niemeyer <niemeyer@conectiva.com>");
2085MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2086MODULE_LICENSE("GPL");
This page took 0.814941 seconds and 5 git commands to generate.