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