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