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>
7 * References used by Fox Chen while writing the original driver for 2.0.30:
9 * 1. WL24xx packet drivers (tooasm.asm)
10 * 2. Access Point Firmware Interface Specification for IEEE 802.11 SUTRO
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
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)
23 * Tested with Planet AP in 2.4.17, 184 Kbytes/s in UDP in Infrastructure mode,
24 * 173 Kbytes/s in TCP.
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)
29 #undef REALLY_SLOW_IO /* most systems can safely undef this */
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>
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>
50 #include <net/iw_handler.h>
52 #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>
59 #include <asm/uaccess.h>
60 #include <asm/system.h>
65 #define slow_down_io()
68 /* For rough constant delay */
69 #define WL3501_NOPLOOP(n) { int x = 0; while (x++ < n) slow_down_io(); }
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.
78 #define PCMCIA_DEBUG 0
80 static int pc_debug = PCMCIA_DEBUG;
81 module_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); }
86 #define dprintk(n, format, args...)
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(); }
93 #define WL3501_RELEASE_TIMEOUT (25 * HZ)
94 #define WL3501_MAX_ADHOC_TRIES 16
96 #define WL3501_RESUME 0
97 #define WL3501_SUSPEND 1
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.
106 static void wl3501_config(dev_link_t *link);
107 static void wl3501_release(dev_link_t *link);
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
114 static dev_info_t wl3501_dev_info = "wl3501_cs";
116 static 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,
122 static const struct {
125 } iw_channel_table[] = {
127 .reg_domain = IW_REG_DOMAIN_FCC,
133 .reg_domain = IW_REG_DOMAIN_DOC,
139 .reg_domain = IW_REG_DOMAIN_ETSI,
145 .reg_domain = IW_REG_DOMAIN_SPAIN,
151 .reg_domain = IW_REG_DOMAIN_FRANCE,
157 .reg_domain = IW_REG_DOMAIN_MKK,
163 .reg_domain = IW_REG_DOMAIN_MKK1,
169 .reg_domain = IW_REG_DOMAIN_ISRAEL,
177 * iw_valid_channel - validate channel in regulatory domain
178 * @reg_comain - regulatory domain
179 * @channel - channel to validate
181 * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
183 static int iw_valid_channel(int reg_domain, int channel)
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;
197 * iw_default_channel - get default channel for a regulatory domain
198 * @reg_comain - regulatory domain
200 * Returns the default channel for a regulatory domain
202 static int iw_default_channel(int reg_domain)
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;
214 static 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)
220 memcpy(el->data, value, len);
223 static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
224 struct iw_mgmt_info_element *from)
226 iw_set_mgmt_info_element(from->id, to, from->data, from->len);
230 * A linked list of "instances" of the wl24 device. Each actual PCMCIA card
231 * corresponds to one device instance, and is described by one dev_link_t
232 * structure (defined in ds.h).
234 * You may not want to use a linked list for this -- for example, the memory
235 * card driver uses an array of dev_link_t pointers, where minor device numbers
236 * are used to derive the corresponding array index.
238 static dev_link_t *wl3501_dev_list;
240 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
242 wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
246 * Get Ethernet MAC addresss.
248 * WARNING: We switch to FPAGE0 and switc back again.
249 * Making sure there is no other WL function beening called by ISR.
251 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
253 int base_addr = this->base_addr;
256 wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
257 wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
258 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
260 /* wait for reading EEPROM */
262 this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
264 this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
266 this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
268 this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
270 this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
272 this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
274 this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
276 wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
277 wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
278 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
280 this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
282 this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
283 /* switch to SRAM Page 0 (for safety) */
284 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
286 /* The MAC addr should be 00:60:... */
287 return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
291 * wl3501_set_to_wla - Move 'size' bytes from PC to card
292 * @dest: Card addressing space
293 * @src: PC addressing space
294 * @size: Bytes to move
296 * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
298 static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
301 /* switch to SRAM Page 0 */
302 wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
304 /* set LMAL and LMAH */
305 wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
306 wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
308 /* rep out to Port A */
309 wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
313 * wl3501_get_from_wla - Move 'size' bytes from card to PC
314 * @src: Card addressing space
315 * @dest: PC addressing space
316 * @size: Bytes to move
318 * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
320 static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
323 /* switch to SRAM Page 0 */
324 wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
326 /* set LMAL and LMAH */
327 wl3501_outb(src & 0xff, this->base_addr + WL3501_NIC_LMAL);
328 wl3501_outb((src >> 8) & 0x7f, this->base_addr + WL3501_NIC_LMAH);
330 /* rep get from Port A */
331 insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
335 * Get/Allocate a free Tx Data Buffer
337 * *--------------*-----------------*----------------------------------*
338 * | PLCP | MAC Header | DST SRC Data ... |
339 * | (24 bytes) | (30 bytes) | (6) (6) (Ethernet Row Data) |
340 * *--------------*-----------------*----------------------------------*
341 * \ \- IEEE 802.11 -/ \-------------- len --------------/
342 * \-struct wl3501_80211_tx_hdr--/ \-------- Ethernet Frame -------/
344 * Return = Postion in Card
346 static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
348 u16 next, blk_cnt = 0, zero = 0;
349 u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
352 if (full_len > this->tx_buffer_cnt * 254)
354 ret = this->tx_buffer_head;
360 wl3501_get_from_wla(this, this->tx_buffer_head, &next,
363 wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
365 this->tx_buffer_head = next;
367 /* if buffer is not enough */
368 if (!next && full_len) {
369 this->tx_buffer_head = ret;
374 this->tx_buffer_cnt -= blk_cnt;
380 * Free an allocated Tx Buffer. ptr must be correct position.
382 static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
384 /* check if all space is not free */
385 if (!this->tx_buffer_head)
386 this->tx_buffer_head = ptr;
388 wl3501_set_to_wla(this, this->tx_buffer_tail,
393 this->tx_buffer_cnt++;
394 wl3501_get_from_wla(this, ptr, &next, sizeof(next));
395 this->tx_buffer_tail = ptr;
400 static int wl3501_esbq_req_test(struct wl3501_card *this)
404 wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
408 static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
412 wl3501_set_to_wla(this, this->esbq_req_head, ptr, 2);
413 wl3501_set_to_wla(this, this->esbq_req_head + 2, &tmp, sizeof(tmp));
414 this->esbq_req_head += 4;
415 if (this->esbq_req_head >= this->esbq_req_end)
416 this->esbq_req_head = this->esbq_req_start;
419 static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
423 if (wl3501_esbq_req_test(this)) {
424 u16 ptr = wl3501_get_tx_buffer(this, sig_size);
426 wl3501_set_to_wla(this, ptr, sig, sig_size);
427 wl3501_esbq_req(this, &ptr);
434 static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
437 struct wl3501_get_req sig = {
438 .sig_id = WL3501_SIG_GET_REQ,
444 spin_lock_irqsave(&this->lock, flags);
445 if (wl3501_esbq_req_test(this)) {
446 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
448 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
449 wl3501_esbq_req(this, &ptr);
450 this->sig_get_confirm.mib_status = 255;
451 spin_unlock_irqrestore(&this->lock, flags);
452 rc = wait_event_interruptible(this->wait,
453 this->sig_get_confirm.mib_status != 255);
455 memcpy(bf, this->sig_get_confirm.mib_value,
460 spin_unlock_irqrestore(&this->lock, flags);
465 static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
467 struct wl3501_pwr_mgmt_req sig = {
468 .sig_id = WL3501_SIG_PWR_MGMT_REQ,
476 spin_lock_irqsave(&this->lock, flags);
477 if (wl3501_esbq_req_test(this)) {
478 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
480 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
481 wl3501_esbq_req(this, &ptr);
482 this->sig_pwr_mgmt_confirm.status = 255;
483 spin_unlock_irqrestore(&this->lock, flags);
484 rc = wait_event_interruptible(this->wait,
485 this->sig_pwr_mgmt_confirm.status != 255);
486 printk(KERN_INFO "%s: %s status=%d\n", __FUNCTION__,
487 suspend ? "suspend" : "resume",
488 this->sig_pwr_mgmt_confirm.status);
492 spin_unlock_irqrestore(&this->lock, flags);
498 * wl3501_send_pkt - Send a packet.
503 * data = Ethernet raw frame. (e.g. data[0] - data[5] is Dest MAC Addr,
504 * data[6] - data[11] is Src MAC Addr)
507 static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
509 u16 bf, sig_bf, next, tmplen, pktlen;
510 struct wl3501_md_req sig = {
511 .sig_id = WL3501_SIG_MD_REQ,
513 u8 *pdata = (char *)data;
516 if (wl3501_esbq_req_test(this)) {
517 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
519 if (!sig_bf) /* No free buffer available */
521 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
523 /* No free buffer available */
524 wl3501_free_tx_buffer(this, sig_bf);
528 memcpy(&sig.daddr[0], pdata, 12);
532 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
533 u8 addr4[ETH_ALEN] = {
534 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
537 wl3501_set_to_wla(this, bf + 2 +
538 offsetof(struct wl3501_tx_hdr, addr4),
539 addr4, sizeof(addr4));
540 sig.size = pktlen + 24 + 4 + 6;
541 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
542 tmplen = 254 - sizeof(struct wl3501_tx_hdr);
548 wl3501_set_to_wla(this,
549 bf + 2 + sizeof(struct wl3501_tx_hdr),
552 wl3501_get_from_wla(this, bf, &next, sizeof(next));
555 sig.size = pktlen + 24 + 4 - 2;
558 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
559 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
565 wl3501_set_to_wla(this, bf + 2 +
566 offsetof(struct wl3501_tx_hdr, addr4),
569 wl3501_get_from_wla(this, bf, &next, sizeof(next));
580 wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
582 wl3501_get_from_wla(this, bf, &next, sizeof(next));
585 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
586 wl3501_esbq_req(this, &sig_bf);
592 static int wl3501_mgmt_resync(struct wl3501_card *this)
594 struct wl3501_resync_req sig = {
595 .sig_id = WL3501_SIG_RESYNC_REQ,
598 return wl3501_esbq_exec(this, &sig, sizeof(sig));
601 static inline int wl3501_fw_bss_type(struct wl3501_card *this)
603 return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
604 WL3501_NET_TYPE_ADHOC;
607 static inline int wl3501_fw_cap_info(struct wl3501_card *this)
609 return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
610 WL3501_MGMT_CAPABILITY_IBSS;
613 static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
615 struct wl3501_scan_req sig = {
616 .sig_id = WL3501_SIG_SCAN_REQ,
617 .scan_type = WL3501_SCAN_TYPE_ACTIVE,
619 .min_chan_time = chan_time,
620 .max_chan_time = chan_time,
621 .bss_type = wl3501_fw_bss_type(this),
624 this->bss_cnt = this->join_sta_bss = 0;
625 return wl3501_esbq_exec(this, &sig, sizeof(sig));
628 static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
630 struct wl3501_join_req sig = {
631 .sig_id = WL3501_SIG_JOIN_REQ,
635 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
642 memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
643 return wl3501_esbq_exec(this, &sig, sizeof(sig));
646 static int wl3501_mgmt_start(struct wl3501_card *this)
648 struct wl3501_start_req sig = {
649 .sig_id = WL3501_SIG_START_REQ,
650 .beacon_period = 400,
654 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
661 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
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,
671 .operational_rset = {
673 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
676 .data_rate_labels = {
677 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
678 IW_MGMT_RATE_LABEL_1MBIT,
679 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
680 IW_MGMT_RATE_LABEL_2MBIT,
685 .id = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
690 .bss_type = wl3501_fw_bss_type(this),
691 .cap_info = wl3501_fw_cap_info(this),
694 iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
695 iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
696 return wl3501_esbq_exec(this, &sig, sizeof(sig));
699 static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
703 struct wl3501_scan_confirm sig;
706 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
707 if (sig.status == WL3501_STATUS_SUCCESS) {
708 dprintk(3, "success");
709 if ((this->net_type == IW_MODE_INFRA &&
710 (sig.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
711 (this->net_type == IW_MODE_ADHOC &&
712 (sig.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
713 this->net_type == IW_MODE_AUTO) {
714 if (!this->essid.el.len)
716 else if (this->essid.el.len == 3 &&
717 !memcmp(this->essid.essid, "ANY", 3))
719 else if (this->essid.el.len != sig.ssid.el.len)
721 else if (memcmp(this->essid.essid, sig.ssid.essid,
727 for (i = 0; i < this->bss_cnt; i++) {
728 if (!memcmp(this->bss_set[i].bssid,
729 sig.bssid, ETH_ALEN)) {
735 if (matchflag && (i < 20)) {
736 memcpy(&this->bss_set[i].beacon_period,
737 &sig.beacon_period, 73);
739 this->rssi = sig.rssi;
742 } else if (sig.status == WL3501_STATUS_TIMEOUT) {
743 dprintk(3, "timeout");
744 this->join_sta_bss = 0;
745 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
746 if (!wl3501_mgmt_join(this, i))
748 this->join_sta_bss = i;
749 if (this->join_sta_bss == this->bss_cnt) {
750 if (this->net_type == IW_MODE_INFRA)
751 wl3501_mgmt_scan(this, 100);
754 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
755 wl3501_mgmt_start(this);
757 wl3501_mgmt_scan(this, 100);
764 * wl3501_block_interrupt - Mask interrupt from SUTRO
767 * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
768 * Return: 1 if interrupt is originally enabled
770 static int wl3501_block_interrupt(struct wl3501_card *this)
772 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
773 u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
774 WL3501_GCR_ENECINT));
776 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
777 return old & WL3501_GCR_ENECINT;
781 * wl3501_unblock_interrupt - Enable interrupt from SUTRO
784 * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
785 * Return: 1 if interrupt is originally enabled
787 static int wl3501_unblock_interrupt(struct wl3501_card *this)
789 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
790 u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
793 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
794 return old & WL3501_GCR_ENECINT;
798 * wl3501_receive - Receive data from Receive Queue.
800 * Receive data from Receive Queue.
803 * @bf: address of host
804 * @size: size of buffer.
806 static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
808 u16 next_addr, next_addr1;
812 wl3501_get_from_wla(this, this->start_seg + 2,
813 &next_addr, sizeof(next_addr));
814 if (size > WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr)) {
815 wl3501_get_from_wla(this,
817 sizeof(struct wl3501_rx_hdr), data,
819 sizeof(struct wl3501_rx_hdr));
820 size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
821 data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
823 wl3501_get_from_wla(this,
825 sizeof(struct wl3501_rx_hdr),
830 if (size > WL3501_BLKSZ - 5) {
831 wl3501_get_from_wla(this, next_addr + 5, data,
833 size -= WL3501_BLKSZ - 5;
834 data += WL3501_BLKSZ - 5;
835 wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
837 next_addr = next_addr1;
839 wl3501_get_from_wla(this, next_addr + 5, data, size);
846 static void wl3501_esbq_req_free(struct wl3501_card *this)
851 if (this->esbq_req_head == this->esbq_req_tail)
853 wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
856 wl3501_get_from_wla(this, this->esbq_req_tail, &addr, sizeof(addr));
857 wl3501_free_tx_buffer(this, addr);
858 this->esbq_req_tail += 4;
859 if (this->esbq_req_tail >= this->esbq_req_end)
860 this->esbq_req_tail = this->esbq_req_start;
865 static int wl3501_esbq_confirm(struct wl3501_card *this)
869 wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
873 static void wl3501_online(struct net_device *dev)
875 struct wl3501_card *this = dev->priv;
877 printk(KERN_INFO "%s: Wireless LAN online. BSSID: "
878 "%02X %02X %02X %02X %02X %02X\n", dev->name,
879 this->bssid[0], this->bssid[1], this->bssid[2],
880 this->bssid[3], this->bssid[4], this->bssid[5]);
881 netif_wake_queue(dev);
884 static void wl3501_esbq_confirm_done(struct wl3501_card *this)
888 wl3501_set_to_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
889 this->esbq_confirm += 4;
890 if (this->esbq_confirm >= this->esbq_confirm_end)
891 this->esbq_confirm = this->esbq_confirm_start;
894 static int wl3501_mgmt_auth(struct wl3501_card *this)
896 struct wl3501_auth_req sig = {
897 .sig_id = WL3501_SIG_AUTH_REQ,
898 .type = WL3501_SYS_TYPE_OPEN,
903 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
904 return wl3501_esbq_exec(this, &sig, sizeof(sig));
907 static int wl3501_mgmt_association(struct wl3501_card *this)
909 struct wl3501_assoc_req sig = {
910 .sig_id = WL3501_SIG_ASSOC_REQ,
912 .listen_interval = 5,
913 .cap_info = this->cap_info,
917 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
918 return wl3501_esbq_exec(this, &sig, sizeof(sig));
921 static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
923 struct wl3501_card *this = dev->priv;
924 struct wl3501_join_confirm sig;
927 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
928 if (sig.status == WL3501_STATUS_SUCCESS) {
929 if (this->net_type == IW_MODE_INFRA) {
930 if (this->join_sta_bss < this->bss_cnt) {
931 const int i = this->join_sta_bss;
933 this->bss_set[i].bssid, ETH_ALEN);
934 this->chan = this->bss_set[i].ds_pset.chan;
935 iw_copy_mgmt_info_element(&this->keep_essid.el,
936 &this->bss_set[i].ssid.el);
937 wl3501_mgmt_auth(this);
940 const int i = this->join_sta_bss;
942 memcpy(&this->bssid, &this->bss_set[i].bssid, ETH_ALEN);
943 this->chan = this->bss_set[i].ds_pset.chan;
944 iw_copy_mgmt_info_element(&this->keep_essid.el,
945 &this->bss_set[i].ssid.el);
950 this->join_sta_bss++;
951 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
952 if (!wl3501_mgmt_join(this, i))
954 this->join_sta_bss = i;
955 if (this->join_sta_bss == this->bss_cnt) {
956 if (this->net_type == IW_MODE_INFRA)
957 wl3501_mgmt_scan(this, 100);
960 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
961 wl3501_mgmt_start(this);
963 wl3501_mgmt_scan(this, 100);
969 static inline void wl3501_alarm_interrupt(struct net_device *dev,
970 struct wl3501_card *this)
972 if (this->net_type == IW_MODE_INFRA) {
973 printk(KERN_INFO "Wireless LAN offline\n");
974 netif_stop_queue(dev);
975 wl3501_mgmt_resync(this);
979 static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
980 struct wl3501_card *this,
983 struct wl3501_md_confirm sig;
986 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
987 wl3501_free_tx_buffer(this, sig.data);
988 if (netif_queue_stopped(dev))
989 netif_wake_queue(dev);
992 static inline void wl3501_md_ind_interrupt(struct net_device *dev,
993 struct wl3501_card *this, u16 addr)
995 struct wl3501_md_ind sig;
997 u8 rssi, addr4[ETH_ALEN];
1000 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1001 this->start_seg = sig.data;
1002 wl3501_get_from_wla(this,
1003 sig.data + offsetof(struct wl3501_rx_hdr, rssi),
1004 &rssi, sizeof(rssi));
1005 this->rssi = rssi <= 63 ? (rssi * 100) / 64 : 255;
1007 wl3501_get_from_wla(this,
1009 offsetof(struct wl3501_rx_hdr, addr4),
1010 &addr4, sizeof(addr4));
1011 if (!(addr4[0] == 0xAA && addr4[1] == 0xAA &&
1012 addr4[2] == 0x03 && addr4[4] == 0x00)) {
1013 printk(KERN_INFO "Insupported packet type!\n");
1016 pkt_len = sig.size + 12 - 24 - 4 - 6;
1018 skb = dev_alloc_skb(pkt_len + 5);
1021 printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
1022 dev->name, pkt_len);
1023 this->stats.rx_dropped++;
1026 skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
1027 eth_copy_and_sum(skb, (unsigned char *)&sig.daddr, 12, 0);
1028 wl3501_receive(this, skb->data, pkt_len);
1029 skb_put(skb, pkt_len);
1030 skb->protocol = eth_type_trans(skb, dev);
1031 dev->last_rx = jiffies;
1032 this->stats.rx_packets++;
1033 this->stats.rx_bytes += skb->len;
1038 static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
1039 u16 addr, void *sig, int size)
1041 dprintk(3, "entry");
1042 wl3501_get_from_wla(this, addr, &this->sig_get_confirm,
1043 sizeof(this->sig_get_confirm));
1044 wake_up(&this->wait);
1047 static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
1048 struct wl3501_card *this,
1051 struct wl3501_start_confirm sig;
1053 dprintk(3, "entry");
1054 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1055 if (sig.status == WL3501_STATUS_SUCCESS)
1056 netif_wake_queue(dev);
1059 static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
1062 struct wl3501_card *this = dev->priv;
1063 struct wl3501_assoc_confirm sig;
1065 dprintk(3, "entry");
1066 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1068 if (sig.status == WL3501_STATUS_SUCCESS)
1072 static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
1075 struct wl3501_auth_confirm sig;
1077 dprintk(3, "entry");
1078 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1080 if (sig.status == WL3501_STATUS_SUCCESS)
1081 wl3501_mgmt_association(this);
1083 wl3501_mgmt_resync(this);
1086 static inline void wl3501_rx_interrupt(struct net_device *dev)
1091 struct wl3501_card *this = dev->priv;
1093 dprintk(3, "entry");
1096 if (!wl3501_esbq_confirm(this))
1098 wl3501_get_from_wla(this, this->esbq_confirm, &addr, sizeof(addr));
1099 wl3501_get_from_wla(this, addr + 2, &sig_id, sizeof(sig_id));
1102 case WL3501_SIG_DEAUTH_IND:
1103 case WL3501_SIG_DISASSOC_IND:
1104 case WL3501_SIG_ALARM:
1105 wl3501_alarm_interrupt(dev, this);
1107 case WL3501_SIG_MD_CONFIRM:
1108 wl3501_md_confirm_interrupt(dev, this, addr);
1110 case WL3501_SIG_MD_IND:
1111 wl3501_md_ind_interrupt(dev, this, addr);
1113 case WL3501_SIG_GET_CONFIRM:
1114 wl3501_get_confirm_interrupt(this, addr,
1115 &this->sig_get_confirm,
1116 sizeof(this->sig_get_confirm));
1118 case WL3501_SIG_PWR_MGMT_CONFIRM:
1119 wl3501_get_confirm_interrupt(this, addr,
1120 &this->sig_pwr_mgmt_confirm,
1121 sizeof(this->sig_pwr_mgmt_confirm));
1123 case WL3501_SIG_START_CONFIRM:
1124 wl3501_start_confirm_interrupt(dev, this, addr);
1126 case WL3501_SIG_SCAN_CONFIRM:
1127 wl3501_mgmt_scan_confirm(this, addr);
1129 case WL3501_SIG_JOIN_CONFIRM:
1130 wl3501_mgmt_join_confirm(dev, addr);
1132 case WL3501_SIG_ASSOC_CONFIRM:
1133 wl3501_assoc_confirm_interrupt(dev, addr);
1135 case WL3501_SIG_AUTH_CONFIRM:
1136 wl3501_auth_confirm_interrupt(this, addr);
1138 case WL3501_SIG_RESYNC_CONFIRM:
1139 wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
1142 wl3501_esbq_confirm_done(this);
1144 /* free request if necessary */
1146 wl3501_esbq_req_free(this);
1151 static inline void wl3501_ack_interrupt(struct wl3501_card *this)
1153 wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
1157 * wl3501_interrupt - Hardware interrupt from card.
1158 * @irq - Interrupt number
1159 * @dev_id - net_device
1162 * We must acknowledge the interrupt as soon as possible, and block the
1163 * interrupt from the same card immediately to prevent re-entry.
1165 * Before accessing the Control_Status_Block, we must lock SUTRO first.
1166 * On the other hand, to prevent SUTRO from malfunctioning, we must
1167 * unlock the SUTRO as soon as possible.
1169 static irqreturn_t wl3501_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1171 struct net_device *dev = (struct net_device *)dev_id;
1172 struct wl3501_card *this;
1178 spin_lock(&this->lock);
1179 wl3501_ack_interrupt(this);
1180 wl3501_block_interrupt(this);
1181 wl3501_rx_interrupt(dev);
1182 wl3501_unblock_interrupt(this);
1183 spin_unlock(&this->lock);
1185 return IRQ_RETVAL(handled);
1188 printk(KERN_ERR "%s: irq %d for unknown device.\n", __FUNCTION__, irq);
1192 static int wl3501_reset_board(struct wl3501_card *this)
1198 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1199 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1200 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1202 /* Reset SRAM 0x480 to zero */
1203 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1206 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1208 WL3501_NOPLOOP(1024 * 50);
1210 wl3501_unblock_interrupt(this); /* acme: was commented */
1212 /* Polling Self_Test_Status */
1213 for (i = 0; i < 10000; i++) {
1214 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1217 /* firmware complete all test successfully */
1219 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1224 printk(KERN_WARNING "%s: failed to reset the board!\n", __FUNCTION__);
1230 static int wl3501_init_firmware(struct wl3501_card *this)
1233 int rc = wl3501_reset_board(this);
1237 this->card_name[0] = '\0';
1238 wl3501_get_from_wla(this, 0x1a00,
1239 this->card_name, sizeof(this->card_name));
1240 this->card_name[sizeof(this->card_name) - 1] = '\0';
1241 this->firmware_date[0] = '\0';
1242 wl3501_get_from_wla(this, 0x1a40,
1243 this->firmware_date, sizeof(this->firmware_date));
1244 this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
1245 /* Switch to SRAM Page 0 */
1246 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
1247 /* Read parameter from card */
1248 wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
1249 wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
1250 wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
1251 wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
1252 wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
1253 wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
1254 this->esbq_req_tail = this->esbq_req_head = this->esbq_req_start;
1255 this->esbq_req_end += this->esbq_req_start;
1256 this->esbq_confirm = this->esbq_confirm_start;
1257 this->esbq_confirm_end += this->esbq_confirm_start;
1258 /* Initial Tx Buffer */
1259 this->tx_buffer_cnt = 1;
1260 ptr = this->tx_buffer_head;
1261 next = ptr + WL3501_BLKSZ;
1262 while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
1263 this->tx_buffer_cnt++;
1264 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1266 next = ptr + WL3501_BLKSZ;
1270 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1271 this->tx_buffer_tail = ptr;
1275 printk(KERN_WARNING "%s: failed!\n", __FUNCTION__);
1279 static int wl3501_close(struct net_device *dev)
1281 struct wl3501_card *this = dev->priv;
1283 unsigned long flags;
1286 spin_lock_irqsave(&this->lock, flags);
1287 /* Check if the device is in wl3501_dev_list */
1288 for (link = wl3501_dev_list; link; link = link->next)
1289 if (link->priv == dev)
1295 /* Stop wl3501_hard_start_xmit() from now on */
1296 netif_stop_queue(dev);
1297 wl3501_ack_interrupt(this);
1299 /* Mask interrupts from the SUTRO */
1300 wl3501_block_interrupt(this);
1303 printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1305 spin_unlock_irqrestore(&this->lock, flags);
1310 * wl3501_reset - Reset the SUTRO.
1311 * @dev - network device
1313 * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1314 * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1315 * is running. It seems to be dangerous.
1317 static int wl3501_reset(struct net_device *dev)
1319 struct wl3501_card *this = dev->priv;
1322 wl3501_block_interrupt(this);
1324 if (wl3501_init_firmware(this)) {
1325 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1327 /* Free IRQ, and mark IRQ as unused */
1328 free_irq(dev->irq, dev);
1333 * Queue has to be started only when the Card is Started
1335 netif_stop_queue(dev);
1336 this->adhoc_times = 0;
1337 wl3501_ack_interrupt(this);
1338 wl3501_unblock_interrupt(this);
1339 wl3501_mgmt_scan(this, 100);
1340 dprintk(1, "%s: device reset", dev->name);
1346 static void wl3501_tx_timeout(struct net_device *dev)
1348 struct wl3501_card *this = dev->priv;
1349 struct net_device_stats *stats = &this->stats;
1350 unsigned long flags;
1354 spin_lock_irqsave(&this->lock, flags);
1355 rc = wl3501_reset(dev);
1356 spin_unlock_irqrestore(&this->lock, flags);
1358 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1361 dev->trans_start = jiffies;
1362 netif_wake_queue(dev);
1368 * 1 - Could not transmit (dev_queue_xmit will queue it)
1369 * and try to sent it later
1371 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1374 struct wl3501_card *this = dev->priv;
1375 unsigned long flags;
1377 spin_lock_irqsave(&this->lock, flags);
1378 enabled = wl3501_block_interrupt(this);
1379 dev->trans_start = jiffies;
1380 rc = wl3501_send_pkt(this, skb->data, skb->len);
1382 wl3501_unblock_interrupt(this);
1384 ++this->stats.tx_dropped;
1385 netif_stop_queue(dev);
1387 ++this->stats.tx_packets;
1388 this->stats.tx_bytes += skb->len;
1391 if (this->tx_buffer_cnt < 2)
1392 netif_stop_queue(dev);
1394 spin_unlock_irqrestore(&this->lock, flags);
1398 static int wl3501_open(struct net_device *dev)
1401 struct wl3501_card *this = dev->priv;
1402 unsigned long flags;
1405 spin_lock_irqsave(&this->lock, flags);
1406 /* Check if the device is in wl3501_dev_list */
1407 for (link = wl3501_dev_list; link; link = link->next)
1408 if (link->priv == dev)
1412 netif_device_attach(dev);
1415 /* Initial WL3501 firmware */
1416 dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1417 if (wl3501_init_firmware(this))
1419 /* Initial device variables */
1420 this->adhoc_times = 0;
1421 /* Acknowledge Interrupt, for cleaning last state */
1422 wl3501_ack_interrupt(this);
1424 /* Enable interrupt from card after all */
1425 wl3501_unblock_interrupt(this);
1426 wl3501_mgmt_scan(this, 100);
1428 dprintk(1, "%s: WL3501 opened", dev->name);
1429 printk(KERN_INFO "%s: Card Name: %s\n"
1430 "%s: Firmware Date: %s\n",
1431 dev->name, this->card_name,
1432 dev->name, this->firmware_date);
1434 spin_unlock_irqrestore(&this->lock, flags);
1437 printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1441 static struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1443 struct wl3501_card *this = dev->priv;
1445 return &this->stats;
1448 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1450 struct wl3501_card *this = dev->priv;
1451 struct iw_statistics *wstats = &this->wstats;
1452 u32 value; /* size checked: it is u32 */
1454 memset(wstats, 0, sizeof(*wstats));
1455 wstats->status = netif_running(dev);
1456 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1457 &value, sizeof(value)))
1458 wstats->discard.code += value;
1459 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1460 &value, sizeof(value)))
1461 wstats->discard.code += value;
1462 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1463 &value, sizeof(value)))
1464 wstats->discard.code += value;
1465 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1466 &value, sizeof(value)))
1467 wstats->discard.retries = value;
1468 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1469 &value, sizeof(value)))
1470 wstats->discard.misc += value;
1471 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1472 &value, sizeof(value)))
1473 wstats->discard.misc += value;
1474 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1475 &value, sizeof(value)))
1476 wstats->discard.misc += value;
1477 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1478 &value, sizeof(value)))
1479 wstats->discard.misc += value;
1483 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1485 strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
1488 static struct ethtool_ops ops = {
1489 .get_drvinfo = wl3501_get_drvinfo
1493 * wl3501_detach - deletes a driver "instance"
1496 * This deletes a driver "instance". The device is de-registered with Card
1497 * Services. If it has been released, all local data structures are freed.
1498 * Otherwise, the structures will be freed when the device is released.
1500 static void wl3501_detach(struct pcmcia_device *p_dev)
1502 dev_link_t *link = dev_to_instance(p_dev);
1504 struct net_device *dev = link->priv;
1506 /* Locate device structure */
1507 for (linkp = &wl3501_dev_list; *linkp; linkp = &(*linkp)->next)
1513 /* If the device is currently configured and active, we won't actually
1514 * delete it yet. Instead, it is marked so that when the release()
1515 * function is called, that will trigger a proper detach(). */
1517 if (link->state & DEV_CONFIG) {
1518 while (link->open > 0)
1521 netif_device_detach(dev);
1522 wl3501_release(link);
1525 /* Unlink device structure, free pieces */
1526 *linkp = link->next;
1529 free_netdev(link->priv);
1535 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1536 union iwreq_data *wrqu, char *extra)
1538 strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1542 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1543 union iwreq_data *wrqu, char *extra)
1545 struct wl3501_card *this = dev->priv;
1546 int channel = wrqu->freq.m;
1549 if (iw_valid_channel(this->reg_domain, channel)) {
1550 this->chan = channel;
1551 rc = wl3501_reset(dev);
1556 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1557 union iwreq_data *wrqu, char *extra)
1559 struct wl3501_card *this = dev->priv;
1561 wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1566 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1567 union iwreq_data *wrqu, char *extra)
1571 if (wrqu->mode == IW_MODE_INFRA ||
1572 wrqu->mode == IW_MODE_ADHOC ||
1573 wrqu->mode == IW_MODE_AUTO) {
1574 struct wl3501_card *this = dev->priv;
1576 this->net_type = wrqu->mode;
1577 rc = wl3501_reset(dev);
1582 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1583 union iwreq_data *wrqu, char *extra)
1585 struct wl3501_card *this = dev->priv;
1587 wrqu->mode = this->net_type;
1591 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1592 union iwreq_data *wrqu, char *extra)
1594 struct wl3501_card *this = dev->priv;
1596 wrqu->sens.value = this->rssi;
1597 wrqu->sens.disabled = !wrqu->sens.value;
1598 wrqu->sens.fixed = 1;
1602 static int wl3501_get_range(struct net_device *dev,
1603 struct iw_request_info *info,
1604 union iwreq_data *wrqu, char *extra)
1606 struct iw_range *range = (struct iw_range *)extra;
1608 /* Set the length (very important for backward compatibility) */
1609 wrqu->data.length = sizeof(*range);
1611 /* Set all the info we don't care or don't know about to zero */
1612 memset(range, 0, sizeof(*range));
1614 /* Set the Wireless Extension versions */
1615 range->we_version_compiled = WIRELESS_EXT;
1616 range->we_version_source = 1;
1617 range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
1618 /* FIXME: study the code to fill in more fields... */
1622 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1623 union iwreq_data *wrqu, char *extra)
1625 struct wl3501_card *this = dev->priv;
1626 static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1629 /* FIXME: we support other ARPHRDs...*/
1630 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1632 if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1633 /* FIXME: rescan? */
1635 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1636 /* FIXME: rescan? deassoc & scan? */
1642 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1643 union iwreq_data *wrqu, char *extra)
1645 struct wl3501_card *this = dev->priv;
1647 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1648 memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1652 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1653 union iwreq_data *wrqu, char *extra)
1656 * FIXME: trigger scanning with a reset, yes, I'm lazy
1658 return wl3501_reset(dev);
1661 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1662 union iwreq_data *wrqu, char *extra)
1664 struct wl3501_card *this = dev->priv;
1666 char *current_ev = extra;
1667 struct iw_event iwe;
1669 for (i = 0; i < this->bss_cnt; ++i) {
1670 iwe.cmd = SIOCGIWAP;
1671 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1672 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1673 current_ev = iwe_stream_add_event(current_ev,
1674 extra + IW_SCAN_MAX_DATA,
1675 &iwe, IW_EV_ADDR_LEN);
1676 iwe.cmd = SIOCGIWESSID;
1677 iwe.u.data.flags = 1;
1678 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1679 current_ev = iwe_stream_add_point(current_ev,
1680 extra + IW_SCAN_MAX_DATA,
1682 this->bss_set[i].ssid.essid);
1683 iwe.cmd = SIOCGIWMODE;
1684 iwe.u.mode = this->bss_set[i].bss_type;
1685 current_ev = iwe_stream_add_event(current_ev,
1686 extra + IW_SCAN_MAX_DATA,
1687 &iwe, IW_EV_UINT_LEN);
1688 iwe.cmd = SIOCGIWFREQ;
1689 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1691 current_ev = iwe_stream_add_event(current_ev,
1692 extra + IW_SCAN_MAX_DATA,
1693 &iwe, IW_EV_FREQ_LEN);
1694 iwe.cmd = SIOCGIWENCODE;
1695 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1696 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1698 iwe.u.data.flags = IW_ENCODE_DISABLED;
1699 iwe.u.data.length = 0;
1700 current_ev = iwe_stream_add_point(current_ev,
1701 extra + IW_SCAN_MAX_DATA,
1704 /* Length of data */
1705 wrqu->data.length = (current_ev - extra);
1706 wrqu->data.flags = 0; /* FIXME: set properly these flags */
1710 static int wl3501_set_essid(struct net_device *dev,
1711 struct iw_request_info *info,
1712 union iwreq_data *wrqu, char *extra)
1714 struct wl3501_card *this = dev->priv;
1716 if (wrqu->data.flags) {
1717 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1719 extra, wrqu->data.length);
1720 } else { /* We accept any ESSID */
1721 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1722 &this->essid.el, "ANY", 3);
1724 return wl3501_reset(dev);
1727 static int wl3501_get_essid(struct net_device *dev,
1728 struct iw_request_info *info,
1729 union iwreq_data *wrqu, char *extra)
1731 struct wl3501_card *this = dev->priv;
1732 unsigned long flags;
1734 spin_lock_irqsave(&this->lock, flags);
1735 wrqu->essid.flags = 1;
1736 wrqu->essid.length = this->essid.el.len;
1737 memcpy(extra, this->essid.essid, this->essid.el.len);
1738 spin_unlock_irqrestore(&this->lock, flags);
1742 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1743 union iwreq_data *wrqu, char *extra)
1745 struct wl3501_card *this = dev->priv;
1747 if (wrqu->data.length > sizeof(this->nick))
1749 strlcpy(this->nick, extra, wrqu->data.length);
1753 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1754 union iwreq_data *wrqu, char *extra)
1756 struct wl3501_card *this = dev->priv;
1758 strlcpy(extra, this->nick, 32);
1759 wrqu->data.length = strlen(extra);
1763 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1764 union iwreq_data *wrqu, char *extra)
1767 * FIXME: have to see from where to get this info, perhaps this card
1768 * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1769 * common with the Planet Access Points. -acme
1771 wrqu->bitrate.value = 2000000;
1772 wrqu->bitrate.fixed = 1;
1776 static int wl3501_get_rts_threshold(struct net_device *dev,
1777 struct iw_request_info *info,
1778 union iwreq_data *wrqu, char *extra)
1780 u16 threshold; /* size checked: it is u16 */
1781 struct wl3501_card *this = dev->priv;
1782 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1783 &threshold, sizeof(threshold));
1785 wrqu->rts.value = threshold;
1786 wrqu->rts.disabled = threshold >= 2347;
1787 wrqu->rts.fixed = 1;
1792 static int wl3501_get_frag_threshold(struct net_device *dev,
1793 struct iw_request_info *info,
1794 union iwreq_data *wrqu, char *extra)
1796 u16 threshold; /* size checked: it is u16 */
1797 struct wl3501_card *this = dev->priv;
1798 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1799 &threshold, sizeof(threshold));
1801 wrqu->frag.value = threshold;
1802 wrqu->frag.disabled = threshold >= 2346;
1803 wrqu->frag.fixed = 1;
1808 static int wl3501_get_txpow(struct net_device *dev,
1809 struct iw_request_info *info,
1810 union iwreq_data *wrqu, char *extra)
1813 struct wl3501_card *this = dev->priv;
1814 int rc = wl3501_get_mib_value(this,
1815 WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1816 &txpow, sizeof(txpow));
1818 wrqu->txpower.value = txpow;
1819 wrqu->txpower.disabled = 0;
1821 * From the MIB values I think this can be configurable,
1822 * as it lists several tx power levels -acme
1824 wrqu->txpower.fixed = 0;
1825 wrqu->txpower.flags = IW_TXPOW_MWATT;
1830 static int wl3501_get_retry(struct net_device *dev,
1831 struct iw_request_info *info,
1832 union iwreq_data *wrqu, char *extra)
1834 u8 retry; /* size checked: it is u8 */
1835 struct wl3501_card *this = dev->priv;
1836 int rc = wl3501_get_mib_value(this,
1837 WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1838 &retry, sizeof(retry));
1841 if (wrqu->retry.flags & IW_RETRY_MAX) {
1842 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1845 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1846 &retry, sizeof(retry));
1849 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1851 wrqu->retry.value = retry;
1852 wrqu->retry.disabled = 0;
1857 static int wl3501_get_encode(struct net_device *dev,
1858 struct iw_request_info *info,
1859 union iwreq_data *wrqu, char *extra)
1861 u8 implemented, restricted, keys[100], len_keys, tocopy;
1862 struct wl3501_card *this = dev->priv;
1863 int rc = wl3501_get_mib_value(this,
1864 WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1865 &implemented, sizeof(implemented));
1869 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1872 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1873 &restricted, sizeof(restricted));
1876 wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1878 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1879 &len_keys, sizeof(len_keys));
1882 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1886 tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1887 tocopy = min_t(u8, tocopy, 100);
1888 wrqu->encoding.length = tocopy;
1889 memset(extra, 0, tocopy);
1890 memcpy(extra, keys, tocopy);
1895 static int wl3501_get_power(struct net_device *dev,
1896 struct iw_request_info *info,
1897 union iwreq_data *wrqu, char *extra)
1900 struct wl3501_card *this = dev->priv;
1901 int rc = wl3501_get_mib_value(this,
1902 WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1903 &pwr_state, sizeof(pwr_state));
1906 wrqu->power.disabled = !pwr_state;
1907 wrqu->power.flags = IW_POWER_ON;
1912 static const iw_handler wl3501_handler[] = {
1913 [SIOCGIWNAME - SIOCIWFIRST] = wl3501_get_name,
1914 [SIOCSIWFREQ - SIOCIWFIRST] = wl3501_set_freq,
1915 [SIOCGIWFREQ - SIOCIWFIRST] = wl3501_get_freq,
1916 [SIOCSIWMODE - SIOCIWFIRST] = wl3501_set_mode,
1917 [SIOCGIWMODE - SIOCIWFIRST] = wl3501_get_mode,
1918 [SIOCGIWSENS - SIOCIWFIRST] = wl3501_get_sens,
1919 [SIOCGIWRANGE - SIOCIWFIRST] = wl3501_get_range,
1920 [SIOCSIWSPY - SIOCIWFIRST] = iw_handler_set_spy,
1921 [SIOCGIWSPY - SIOCIWFIRST] = iw_handler_get_spy,
1922 [SIOCSIWTHRSPY - SIOCIWFIRST] = iw_handler_set_thrspy,
1923 [SIOCGIWTHRSPY - SIOCIWFIRST] = iw_handler_get_thrspy,
1924 [SIOCSIWAP - SIOCIWFIRST] = wl3501_set_wap,
1925 [SIOCGIWAP - SIOCIWFIRST] = wl3501_get_wap,
1926 [SIOCSIWSCAN - SIOCIWFIRST] = wl3501_set_scan,
1927 [SIOCGIWSCAN - SIOCIWFIRST] = wl3501_get_scan,
1928 [SIOCSIWESSID - SIOCIWFIRST] = wl3501_set_essid,
1929 [SIOCGIWESSID - SIOCIWFIRST] = wl3501_get_essid,
1930 [SIOCSIWNICKN - SIOCIWFIRST] = wl3501_set_nick,
1931 [SIOCGIWNICKN - SIOCIWFIRST] = wl3501_get_nick,
1932 [SIOCGIWRATE - SIOCIWFIRST] = wl3501_get_rate,
1933 [SIOCGIWRTS - SIOCIWFIRST] = wl3501_get_rts_threshold,
1934 [SIOCGIWFRAG - SIOCIWFIRST] = wl3501_get_frag_threshold,
1935 [SIOCGIWTXPOW - SIOCIWFIRST] = wl3501_get_txpow,
1936 [SIOCGIWRETRY - SIOCIWFIRST] = wl3501_get_retry,
1937 [SIOCGIWENCODE - SIOCIWFIRST] = wl3501_get_encode,
1938 [SIOCGIWPOWER - SIOCIWFIRST] = wl3501_get_power,
1941 static const struct iw_handler_def wl3501_handler_def = {
1942 .num_standard = sizeof(wl3501_handler) / sizeof(iw_handler),
1943 .standard = (iw_handler *)wl3501_handler,
1944 .get_wireless_stats = wl3501_get_wireless_stats,
1948 * wl3501_attach - creates an "instance" of the driver
1950 * Creates an "instance" of the driver, allocating local data structures for
1951 * one device. The device is registered with Card Services.
1953 * The dev_link structure is initialized, but we don't actually configure the
1954 * card at this point -- we wait until we receive a card insertion event.
1956 static int wl3501_attach(struct pcmcia_device *p_dev)
1959 struct net_device *dev;
1960 struct wl3501_card *this;
1962 /* Initialize the dev_link_t structure */
1963 link = kzalloc(sizeof(*link), GFP_KERNEL);
1967 /* The io structure describes IO port mapping */
1968 link->io.NumPorts1 = 16;
1969 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1970 link->io.IOAddrLines = 5;
1972 /* Interrupt setup */
1973 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1974 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
1975 link->irq.Handler = wl3501_interrupt;
1977 /* General socket configuration */
1978 link->conf.Attributes = CONF_ENABLE_IRQ;
1979 link->conf.Vcc = 50;
1980 link->conf.IntType = INT_MEMORY_AND_IO;
1981 link->conf.ConfigIndex = 1;
1982 link->conf.Present = PRESENT_OPTION;
1984 dev = alloc_etherdev(sizeof(struct wl3501_card));
1987 dev->open = wl3501_open;
1988 dev->stop = wl3501_close;
1989 dev->hard_start_xmit = wl3501_hard_start_xmit;
1990 dev->tx_timeout = wl3501_tx_timeout;
1991 dev->watchdog_timeo = 5 * HZ;
1992 dev->get_stats = wl3501_get_stats;
1994 this->wireless_data.spy_data = &this->spy_data;
1995 dev->wireless_data = &this->wireless_data;
1996 dev->wireless_handlers = (struct iw_handler_def *)&wl3501_handler_def;
1997 SET_ETHTOOL_OPS(dev, &ops);
1998 netif_stop_queue(dev);
1999 link->priv = link->irq.Instance = dev;
2001 link->handle = p_dev;
2002 p_dev->instance = link;
2004 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
2005 wl3501_config(link);
2014 #define CS_CHECK(fn, ret) \
2015 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
2018 * wl3501_config - configure the PCMCIA socket and make eth device available
2021 * wl3501_config() is scheduled to run after a CARD_INSERTION event is
2022 * received, to configure the PCMCIA socket, and to make the ethernet device
2023 * available to the system.
2025 static void wl3501_config(dev_link_t *link)
2029 client_handle_t handle = link->handle;
2030 struct net_device *dev = link->priv;
2031 int i = 0, j, last_fn, last_ret;
2032 unsigned char bf[64];
2033 struct wl3501_card *this;
2035 /* This reads the card's CONFIG tuple to find its config registers. */
2036 tuple.Attributes = 0;
2037 tuple.DesiredTuple = CISTPL_CONFIG;
2038 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
2039 tuple.TupleData = bf;
2040 tuple.TupleDataMax = sizeof(bf);
2041 tuple.TupleOffset = 0;
2042 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
2043 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
2044 link->conf.ConfigBase = parse.config.base;
2045 link->conf.Present = parse.config.rmask[0];
2047 /* Configure card */
2048 link->state |= DEV_CONFIG;
2050 /* Try allocating IO ports. This tries a few fixed addresses. If you
2051 * want, you can also read the card's config table to pick addresses --
2052 * see the serial driver for an example. */
2054 for (j = 0x280; j < 0x400; j += 0x20) {
2055 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
2056 * 0x200-0x2ff, and so on, because this seems safer */
2057 link->io.BasePort1 = j;
2058 link->io.BasePort2 = link->io.BasePort1 + 0x10;
2059 i = pcmcia_request_io(link->handle, &link->io);
2060 if (i == CS_SUCCESS)
2063 if (i != CS_SUCCESS) {
2064 cs_error(link->handle, RequestIO, i);
2068 /* Now allocate an interrupt line. Note that this does not actually
2069 * assign a handler to the interrupt. */
2071 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
2073 /* This actually configures the PCMCIA socket -- setting up the I/O
2074 * windows and the interrupt mapping. */
2076 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
2078 dev->irq = link->irq.AssignedIRQ;
2079 dev->base_addr = link->io.BasePort1;
2080 SET_NETDEV_DEV(dev, &handle_to_dev(handle));
2081 if (register_netdev(dev)) {
2082 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2086 SET_MODULE_OWNER(dev);
2090 * At this point, the dev_node_t structure(s) should be initialized and
2091 * arranged in a linked list at link->dev.
2093 link->dev = &this->node;
2094 link->state &= ~DEV_CONFIG_PENDING;
2096 this->base_addr = dev->base_addr;
2098 if (!wl3501_get_flash_mac_addr(this)) {
2099 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2103 strcpy(this->node.dev_name, dev->name);
2105 /* print probe information */
2106 printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2107 dev->name, this->base_addr, (int)dev->irq);
2108 for (i = 0; i < 6; i++) {
2109 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2110 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2114 * Initialize card parameters - added by jss
2116 this->net_type = IW_MODE_INFRA;
2118 this->join_sta_bss = 0;
2119 this->adhoc_times = 0;
2120 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2122 this->card_name[0] = '\0';
2123 this->firmware_date[0] = '\0';
2125 this->chan = iw_default_channel(this->reg_domain);
2126 strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2127 spin_lock_init(&this->lock);
2128 init_waitqueue_head(&this->wait);
2129 netif_start_queue(dev);
2132 cs_error(link->handle, last_fn, last_ret);
2134 wl3501_release(link);
2140 * wl3501_release - unregister the net, release PCMCIA configuration
2143 * After a card is removed, wl3501_release() will unregister the net device,
2144 * and release the PCMCIA configuration. If the device is still open, this
2145 * will be postponed until it is closed.
2147 static void wl3501_release(dev_link_t *link)
2149 struct net_device *dev = link->priv;
2151 /* Unlink the device chain */
2153 unregister_netdev(dev);
2157 /* Don't bother checking to see if these succeed or not */
2158 pcmcia_release_configuration(link->handle);
2159 pcmcia_release_io(link->handle, &link->io);
2160 pcmcia_release_irq(link->handle, &link->irq);
2161 link->state &= ~DEV_CONFIG;
2164 static int wl3501_suspend(struct pcmcia_device *p_dev)
2166 dev_link_t *link = dev_to_instance(p_dev);
2167 struct net_device *dev = link->priv;
2169 link->state |= DEV_SUSPEND;
2171 wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2172 if (link->state & DEV_CONFIG) {
2174 netif_device_detach(dev);
2175 pcmcia_release_configuration(link->handle);
2181 static int wl3501_resume(struct pcmcia_device *p_dev)
2183 dev_link_t *link = dev_to_instance(p_dev);
2184 struct net_device *dev = link->priv;
2186 wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2187 if (link->state & DEV_CONFIG) {
2188 pcmcia_request_configuration(link->handle, &link->conf);
2191 netif_device_attach(dev);
2199 static struct pcmcia_device_id wl3501_ids[] = {
2200 PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2203 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2205 static struct pcmcia_driver wl3501_driver = {
2206 .owner = THIS_MODULE,
2208 .name = "wl3501_cs",
2210 .probe = wl3501_attach,
2211 .remove = wl3501_detach,
2212 .id_table = wl3501_ids,
2213 .suspend = wl3501_suspend,
2214 .resume = wl3501_resume,
2217 static int __init wl3501_init_module(void)
2219 return pcmcia_register_driver(&wl3501_driver);
2222 static void __exit wl3501_exit_module(void)
2224 dprintk(0, ": unloading");
2225 pcmcia_unregister_driver(&wl3501_driver);
2226 BUG_ON(wl3501_dev_list != NULL);
2229 module_init(wl3501_init_module);
2230 module_exit(wl3501_exit_module);
2232 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2233 "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2234 "Gustavo Niemeyer <niemeyer@conectiva.com>");
2235 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2236 MODULE_LICENSE("GPL");