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 int wl3501_config(struct pcmcia_device *link);
107 static void wl3501_release(struct pcmcia_device *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);
229 static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
231 wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
235 * Get Ethernet MAC addresss.
237 * WARNING: We switch to FPAGE0 and switc back again.
238 * Making sure there is no other WL function beening called by ISR.
240 static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
242 int base_addr = this->base_addr;
245 wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
246 wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
247 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
249 /* wait for reading EEPROM */
251 this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
253 this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
255 this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
257 this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
259 this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
261 this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
263 this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
265 wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
266 wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
267 wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
269 this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
271 this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
272 /* switch to SRAM Page 0 (for safety) */
273 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
275 /* The MAC addr should be 00:60:... */
276 return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
280 * wl3501_set_to_wla - Move 'size' bytes from PC to card
281 * @dest: Card addressing space
282 * @src: PC addressing space
283 * @size: Bytes to move
285 * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
287 static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
290 /* switch to SRAM Page 0 */
291 wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
293 /* set LMAL and LMAH */
294 wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
295 wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
297 /* rep out to Port A */
298 wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
302 * wl3501_get_from_wla - Move 'size' bytes from card to PC
303 * @src: Card addressing space
304 * @dest: PC addressing space
305 * @size: Bytes to move
307 * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
309 static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
312 /* switch to SRAM Page 0 */
313 wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
315 /* set LMAL and LMAH */
316 wl3501_outb(src & 0xff, this->base_addr + WL3501_NIC_LMAL);
317 wl3501_outb((src >> 8) & 0x7f, this->base_addr + WL3501_NIC_LMAH);
319 /* rep get from Port A */
320 insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
324 * Get/Allocate a free Tx Data Buffer
326 * *--------------*-----------------*----------------------------------*
327 * | PLCP | MAC Header | DST SRC Data ... |
328 * | (24 bytes) | (30 bytes) | (6) (6) (Ethernet Row Data) |
329 * *--------------*-----------------*----------------------------------*
330 * \ \- IEEE 802.11 -/ \-------------- len --------------/
331 * \-struct wl3501_80211_tx_hdr--/ \-------- Ethernet Frame -------/
333 * Return = Postion in Card
335 static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
337 u16 next, blk_cnt = 0, zero = 0;
338 u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
341 if (full_len > this->tx_buffer_cnt * 254)
343 ret = this->tx_buffer_head;
349 wl3501_get_from_wla(this, this->tx_buffer_head, &next,
352 wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
354 this->tx_buffer_head = next;
356 /* if buffer is not enough */
357 if (!next && full_len) {
358 this->tx_buffer_head = ret;
363 this->tx_buffer_cnt -= blk_cnt;
369 * Free an allocated Tx Buffer. ptr must be correct position.
371 static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
373 /* check if all space is not free */
374 if (!this->tx_buffer_head)
375 this->tx_buffer_head = ptr;
377 wl3501_set_to_wla(this, this->tx_buffer_tail,
382 this->tx_buffer_cnt++;
383 wl3501_get_from_wla(this, ptr, &next, sizeof(next));
384 this->tx_buffer_tail = ptr;
389 static int wl3501_esbq_req_test(struct wl3501_card *this)
393 wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
397 static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
401 wl3501_set_to_wla(this, this->esbq_req_head, ptr, 2);
402 wl3501_set_to_wla(this, this->esbq_req_head + 2, &tmp, sizeof(tmp));
403 this->esbq_req_head += 4;
404 if (this->esbq_req_head >= this->esbq_req_end)
405 this->esbq_req_head = this->esbq_req_start;
408 static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
412 if (wl3501_esbq_req_test(this)) {
413 u16 ptr = wl3501_get_tx_buffer(this, sig_size);
415 wl3501_set_to_wla(this, ptr, sig, sig_size);
416 wl3501_esbq_req(this, &ptr);
423 static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
426 struct wl3501_get_req sig = {
427 .sig_id = WL3501_SIG_GET_REQ,
433 spin_lock_irqsave(&this->lock, flags);
434 if (wl3501_esbq_req_test(this)) {
435 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
437 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
438 wl3501_esbq_req(this, &ptr);
439 this->sig_get_confirm.mib_status = 255;
440 spin_unlock_irqrestore(&this->lock, flags);
441 rc = wait_event_interruptible(this->wait,
442 this->sig_get_confirm.mib_status != 255);
444 memcpy(bf, this->sig_get_confirm.mib_value,
449 spin_unlock_irqrestore(&this->lock, flags);
454 static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
456 struct wl3501_pwr_mgmt_req sig = {
457 .sig_id = WL3501_SIG_PWR_MGMT_REQ,
465 spin_lock_irqsave(&this->lock, flags);
466 if (wl3501_esbq_req_test(this)) {
467 u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
469 wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
470 wl3501_esbq_req(this, &ptr);
471 this->sig_pwr_mgmt_confirm.status = 255;
472 spin_unlock_irqrestore(&this->lock, flags);
473 rc = wait_event_interruptible(this->wait,
474 this->sig_pwr_mgmt_confirm.status != 255);
475 printk(KERN_INFO "%s: %s status=%d\n", __FUNCTION__,
476 suspend ? "suspend" : "resume",
477 this->sig_pwr_mgmt_confirm.status);
481 spin_unlock_irqrestore(&this->lock, flags);
487 * wl3501_send_pkt - Send a packet.
492 * data = Ethernet raw frame. (e.g. data[0] - data[5] is Dest MAC Addr,
493 * data[6] - data[11] is Src MAC Addr)
496 static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
498 u16 bf, sig_bf, next, tmplen, pktlen;
499 struct wl3501_md_req sig = {
500 .sig_id = WL3501_SIG_MD_REQ,
502 u8 *pdata = (char *)data;
505 if (wl3501_esbq_req_test(this)) {
506 sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
508 if (!sig_bf) /* No free buffer available */
510 bf = wl3501_get_tx_buffer(this, len + 26 + 24);
512 /* No free buffer available */
513 wl3501_free_tx_buffer(this, sig_bf);
517 memcpy(&sig.daddr[0], pdata, 12);
521 if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
522 u8 addr4[ETH_ALEN] = {
523 [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
526 wl3501_set_to_wla(this, bf + 2 +
527 offsetof(struct wl3501_tx_hdr, addr4),
528 addr4, sizeof(addr4));
529 sig.size = pktlen + 24 + 4 + 6;
530 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
531 tmplen = 254 - sizeof(struct wl3501_tx_hdr);
537 wl3501_set_to_wla(this,
538 bf + 2 + sizeof(struct wl3501_tx_hdr),
541 wl3501_get_from_wla(this, bf, &next, sizeof(next));
544 sig.size = pktlen + 24 + 4 - 2;
547 if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
548 tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
554 wl3501_set_to_wla(this, bf + 2 +
555 offsetof(struct wl3501_tx_hdr, addr4),
558 wl3501_get_from_wla(this, bf, &next, sizeof(next));
569 wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
571 wl3501_get_from_wla(this, bf, &next, sizeof(next));
574 wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
575 wl3501_esbq_req(this, &sig_bf);
581 static int wl3501_mgmt_resync(struct wl3501_card *this)
583 struct wl3501_resync_req sig = {
584 .sig_id = WL3501_SIG_RESYNC_REQ,
587 return wl3501_esbq_exec(this, &sig, sizeof(sig));
590 static inline int wl3501_fw_bss_type(struct wl3501_card *this)
592 return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
593 WL3501_NET_TYPE_ADHOC;
596 static inline int wl3501_fw_cap_info(struct wl3501_card *this)
598 return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
599 WL3501_MGMT_CAPABILITY_IBSS;
602 static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
604 struct wl3501_scan_req sig = {
605 .sig_id = WL3501_SIG_SCAN_REQ,
606 .scan_type = WL3501_SCAN_TYPE_ACTIVE,
608 .min_chan_time = chan_time,
609 .max_chan_time = chan_time,
610 .bss_type = wl3501_fw_bss_type(this),
613 this->bss_cnt = this->join_sta_bss = 0;
614 return wl3501_esbq_exec(this, &sig, sizeof(sig));
617 static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
619 struct wl3501_join_req sig = {
620 .sig_id = WL3501_SIG_JOIN_REQ,
624 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
631 memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
632 return wl3501_esbq_exec(this, &sig, sizeof(sig));
635 static int wl3501_mgmt_start(struct wl3501_card *this)
637 struct wl3501_start_req sig = {
638 .sig_id = WL3501_SIG_START_REQ,
639 .beacon_period = 400,
643 .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
650 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
653 .data_rate_labels = {
654 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
655 IW_MGMT_RATE_LABEL_1MBIT,
656 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
657 IW_MGMT_RATE_LABEL_2MBIT,
660 .operational_rset = {
662 .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
665 .data_rate_labels = {
666 [0] = IW_MGMT_RATE_LABEL_MANDATORY |
667 IW_MGMT_RATE_LABEL_1MBIT,
668 [1] = IW_MGMT_RATE_LABEL_MANDATORY |
669 IW_MGMT_RATE_LABEL_2MBIT,
674 .id = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
679 .bss_type = wl3501_fw_bss_type(this),
680 .cap_info = wl3501_fw_cap_info(this),
683 iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
684 iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
685 return wl3501_esbq_exec(this, &sig, sizeof(sig));
688 static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
692 struct wl3501_scan_confirm sig;
695 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
696 if (sig.status == WL3501_STATUS_SUCCESS) {
697 dprintk(3, "success");
698 if ((this->net_type == IW_MODE_INFRA &&
699 (sig.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
700 (this->net_type == IW_MODE_ADHOC &&
701 (sig.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
702 this->net_type == IW_MODE_AUTO) {
703 if (!this->essid.el.len)
705 else if (this->essid.el.len == 3 &&
706 !memcmp(this->essid.essid, "ANY", 3))
708 else if (this->essid.el.len != sig.ssid.el.len)
710 else if (memcmp(this->essid.essid, sig.ssid.essid,
716 for (i = 0; i < this->bss_cnt; i++) {
717 if (!memcmp(this->bss_set[i].bssid,
718 sig.bssid, ETH_ALEN)) {
724 if (matchflag && (i < 20)) {
725 memcpy(&this->bss_set[i].beacon_period,
726 &sig.beacon_period, 73);
728 this->rssi = sig.rssi;
731 } else if (sig.status == WL3501_STATUS_TIMEOUT) {
732 dprintk(3, "timeout");
733 this->join_sta_bss = 0;
734 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
735 if (!wl3501_mgmt_join(this, i))
737 this->join_sta_bss = i;
738 if (this->join_sta_bss == this->bss_cnt) {
739 if (this->net_type == IW_MODE_INFRA)
740 wl3501_mgmt_scan(this, 100);
743 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
744 wl3501_mgmt_start(this);
746 wl3501_mgmt_scan(this, 100);
753 * wl3501_block_interrupt - Mask interrupt from SUTRO
756 * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
757 * Return: 1 if interrupt is originally enabled
759 static int wl3501_block_interrupt(struct wl3501_card *this)
761 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
762 u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
763 WL3501_GCR_ENECINT));
765 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
766 return old & WL3501_GCR_ENECINT;
770 * wl3501_unblock_interrupt - Enable interrupt from SUTRO
773 * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
774 * Return: 1 if interrupt is originally enabled
776 static int wl3501_unblock_interrupt(struct wl3501_card *this)
778 u8 old = inb(this->base_addr + WL3501_NIC_GCR);
779 u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
782 wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
783 return old & WL3501_GCR_ENECINT;
787 * wl3501_receive - Receive data from Receive Queue.
789 * Receive data from Receive Queue.
792 * @bf: address of host
793 * @size: size of buffer.
795 static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
797 u16 next_addr, next_addr1;
801 wl3501_get_from_wla(this, this->start_seg + 2,
802 &next_addr, sizeof(next_addr));
803 if (size > WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr)) {
804 wl3501_get_from_wla(this,
806 sizeof(struct wl3501_rx_hdr), data,
808 sizeof(struct wl3501_rx_hdr));
809 size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
810 data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
812 wl3501_get_from_wla(this,
814 sizeof(struct wl3501_rx_hdr),
819 if (size > WL3501_BLKSZ - 5) {
820 wl3501_get_from_wla(this, next_addr + 5, data,
822 size -= WL3501_BLKSZ - 5;
823 data += WL3501_BLKSZ - 5;
824 wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
826 next_addr = next_addr1;
828 wl3501_get_from_wla(this, next_addr + 5, data, size);
835 static void wl3501_esbq_req_free(struct wl3501_card *this)
840 if (this->esbq_req_head == this->esbq_req_tail)
842 wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
845 wl3501_get_from_wla(this, this->esbq_req_tail, &addr, sizeof(addr));
846 wl3501_free_tx_buffer(this, addr);
847 this->esbq_req_tail += 4;
848 if (this->esbq_req_tail >= this->esbq_req_end)
849 this->esbq_req_tail = this->esbq_req_start;
854 static int wl3501_esbq_confirm(struct wl3501_card *this)
858 wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
862 static void wl3501_online(struct net_device *dev)
864 struct wl3501_card *this = dev->priv;
866 printk(KERN_INFO "%s: Wireless LAN online. BSSID: "
867 "%02X %02X %02X %02X %02X %02X\n", dev->name,
868 this->bssid[0], this->bssid[1], this->bssid[2],
869 this->bssid[3], this->bssid[4], this->bssid[5]);
870 netif_wake_queue(dev);
873 static void wl3501_esbq_confirm_done(struct wl3501_card *this)
877 wl3501_set_to_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
878 this->esbq_confirm += 4;
879 if (this->esbq_confirm >= this->esbq_confirm_end)
880 this->esbq_confirm = this->esbq_confirm_start;
883 static int wl3501_mgmt_auth(struct wl3501_card *this)
885 struct wl3501_auth_req sig = {
886 .sig_id = WL3501_SIG_AUTH_REQ,
887 .type = WL3501_SYS_TYPE_OPEN,
892 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
893 return wl3501_esbq_exec(this, &sig, sizeof(sig));
896 static int wl3501_mgmt_association(struct wl3501_card *this)
898 struct wl3501_assoc_req sig = {
899 .sig_id = WL3501_SIG_ASSOC_REQ,
901 .listen_interval = 5,
902 .cap_info = this->cap_info,
906 memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
907 return wl3501_esbq_exec(this, &sig, sizeof(sig));
910 static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
912 struct wl3501_card *this = dev->priv;
913 struct wl3501_join_confirm sig;
916 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
917 if (sig.status == WL3501_STATUS_SUCCESS) {
918 if (this->net_type == IW_MODE_INFRA) {
919 if (this->join_sta_bss < this->bss_cnt) {
920 const int i = this->join_sta_bss;
922 this->bss_set[i].bssid, ETH_ALEN);
923 this->chan = this->bss_set[i].ds_pset.chan;
924 iw_copy_mgmt_info_element(&this->keep_essid.el,
925 &this->bss_set[i].ssid.el);
926 wl3501_mgmt_auth(this);
929 const int i = this->join_sta_bss;
931 memcpy(&this->bssid, &this->bss_set[i].bssid, ETH_ALEN);
932 this->chan = this->bss_set[i].ds_pset.chan;
933 iw_copy_mgmt_info_element(&this->keep_essid.el,
934 &this->bss_set[i].ssid.el);
939 this->join_sta_bss++;
940 for (i = this->join_sta_bss; i < this->bss_cnt; i++)
941 if (!wl3501_mgmt_join(this, i))
943 this->join_sta_bss = i;
944 if (this->join_sta_bss == this->bss_cnt) {
945 if (this->net_type == IW_MODE_INFRA)
946 wl3501_mgmt_scan(this, 100);
949 if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
950 wl3501_mgmt_start(this);
952 wl3501_mgmt_scan(this, 100);
958 static inline void wl3501_alarm_interrupt(struct net_device *dev,
959 struct wl3501_card *this)
961 if (this->net_type == IW_MODE_INFRA) {
962 printk(KERN_INFO "Wireless LAN offline\n");
963 netif_stop_queue(dev);
964 wl3501_mgmt_resync(this);
968 static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
969 struct wl3501_card *this,
972 struct wl3501_md_confirm sig;
975 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
976 wl3501_free_tx_buffer(this, sig.data);
977 if (netif_queue_stopped(dev))
978 netif_wake_queue(dev);
981 static inline void wl3501_md_ind_interrupt(struct net_device *dev,
982 struct wl3501_card *this, u16 addr)
984 struct wl3501_md_ind sig;
986 u8 rssi, addr4[ETH_ALEN];
989 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
990 this->start_seg = sig.data;
991 wl3501_get_from_wla(this,
992 sig.data + offsetof(struct wl3501_rx_hdr, rssi),
993 &rssi, sizeof(rssi));
994 this->rssi = rssi <= 63 ? (rssi * 100) / 64 : 255;
996 wl3501_get_from_wla(this,
998 offsetof(struct wl3501_rx_hdr, addr4),
999 &addr4, sizeof(addr4));
1000 if (!(addr4[0] == 0xAA && addr4[1] == 0xAA &&
1001 addr4[2] == 0x03 && addr4[4] == 0x00)) {
1002 printk(KERN_INFO "Insupported packet type!\n");
1005 pkt_len = sig.size + 12 - 24 - 4 - 6;
1007 skb = dev_alloc_skb(pkt_len + 5);
1010 printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
1011 dev->name, pkt_len);
1012 this->stats.rx_dropped++;
1015 skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
1016 eth_copy_and_sum(skb, (unsigned char *)&sig.daddr, 12, 0);
1017 wl3501_receive(this, skb->data, pkt_len);
1018 skb_put(skb, pkt_len);
1019 skb->protocol = eth_type_trans(skb, dev);
1020 dev->last_rx = jiffies;
1021 this->stats.rx_packets++;
1022 this->stats.rx_bytes += skb->len;
1027 static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
1028 u16 addr, void *sig, int size)
1030 dprintk(3, "entry");
1031 wl3501_get_from_wla(this, addr, &this->sig_get_confirm,
1032 sizeof(this->sig_get_confirm));
1033 wake_up(&this->wait);
1036 static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
1037 struct wl3501_card *this,
1040 struct wl3501_start_confirm sig;
1042 dprintk(3, "entry");
1043 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1044 if (sig.status == WL3501_STATUS_SUCCESS)
1045 netif_wake_queue(dev);
1048 static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
1051 struct wl3501_card *this = dev->priv;
1052 struct wl3501_assoc_confirm sig;
1054 dprintk(3, "entry");
1055 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1057 if (sig.status == WL3501_STATUS_SUCCESS)
1061 static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
1064 struct wl3501_auth_confirm sig;
1066 dprintk(3, "entry");
1067 wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
1069 if (sig.status == WL3501_STATUS_SUCCESS)
1070 wl3501_mgmt_association(this);
1072 wl3501_mgmt_resync(this);
1075 static inline void wl3501_rx_interrupt(struct net_device *dev)
1080 struct wl3501_card *this = dev->priv;
1082 dprintk(3, "entry");
1085 if (!wl3501_esbq_confirm(this))
1087 wl3501_get_from_wla(this, this->esbq_confirm, &addr, sizeof(addr));
1088 wl3501_get_from_wla(this, addr + 2, &sig_id, sizeof(sig_id));
1091 case WL3501_SIG_DEAUTH_IND:
1092 case WL3501_SIG_DISASSOC_IND:
1093 case WL3501_SIG_ALARM:
1094 wl3501_alarm_interrupt(dev, this);
1096 case WL3501_SIG_MD_CONFIRM:
1097 wl3501_md_confirm_interrupt(dev, this, addr);
1099 case WL3501_SIG_MD_IND:
1100 wl3501_md_ind_interrupt(dev, this, addr);
1102 case WL3501_SIG_GET_CONFIRM:
1103 wl3501_get_confirm_interrupt(this, addr,
1104 &this->sig_get_confirm,
1105 sizeof(this->sig_get_confirm));
1107 case WL3501_SIG_PWR_MGMT_CONFIRM:
1108 wl3501_get_confirm_interrupt(this, addr,
1109 &this->sig_pwr_mgmt_confirm,
1110 sizeof(this->sig_pwr_mgmt_confirm));
1112 case WL3501_SIG_START_CONFIRM:
1113 wl3501_start_confirm_interrupt(dev, this, addr);
1115 case WL3501_SIG_SCAN_CONFIRM:
1116 wl3501_mgmt_scan_confirm(this, addr);
1118 case WL3501_SIG_JOIN_CONFIRM:
1119 wl3501_mgmt_join_confirm(dev, addr);
1121 case WL3501_SIG_ASSOC_CONFIRM:
1122 wl3501_assoc_confirm_interrupt(dev, addr);
1124 case WL3501_SIG_AUTH_CONFIRM:
1125 wl3501_auth_confirm_interrupt(this, addr);
1127 case WL3501_SIG_RESYNC_CONFIRM:
1128 wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
1131 wl3501_esbq_confirm_done(this);
1133 /* free request if necessary */
1135 wl3501_esbq_req_free(this);
1140 static inline void wl3501_ack_interrupt(struct wl3501_card *this)
1142 wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
1146 * wl3501_interrupt - Hardware interrupt from card.
1147 * @irq - Interrupt number
1148 * @dev_id - net_device
1151 * We must acknowledge the interrupt as soon as possible, and block the
1152 * interrupt from the same card immediately to prevent re-entry.
1154 * Before accessing the Control_Status_Block, we must lock SUTRO first.
1155 * On the other hand, to prevent SUTRO from malfunctioning, we must
1156 * unlock the SUTRO as soon as possible.
1158 static irqreturn_t wl3501_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1160 struct net_device *dev = (struct net_device *)dev_id;
1161 struct wl3501_card *this;
1167 spin_lock(&this->lock);
1168 wl3501_ack_interrupt(this);
1169 wl3501_block_interrupt(this);
1170 wl3501_rx_interrupt(dev);
1171 wl3501_unblock_interrupt(this);
1172 spin_unlock(&this->lock);
1174 return IRQ_RETVAL(handled);
1177 printk(KERN_ERR "%s: irq %d for unknown device.\n", __FUNCTION__, irq);
1181 static int wl3501_reset_board(struct wl3501_card *this)
1187 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1188 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1189 wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
1191 /* Reset SRAM 0x480 to zero */
1192 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1195 wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
1197 WL3501_NOPLOOP(1024 * 50);
1199 wl3501_unblock_interrupt(this); /* acme: was commented */
1201 /* Polling Self_Test_Status */
1202 for (i = 0; i < 10000; i++) {
1203 wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
1206 /* firmware complete all test successfully */
1208 wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
1213 printk(KERN_WARNING "%s: failed to reset the board!\n", __FUNCTION__);
1219 static int wl3501_init_firmware(struct wl3501_card *this)
1222 int rc = wl3501_reset_board(this);
1226 this->card_name[0] = '\0';
1227 wl3501_get_from_wla(this, 0x1a00,
1228 this->card_name, sizeof(this->card_name));
1229 this->card_name[sizeof(this->card_name) - 1] = '\0';
1230 this->firmware_date[0] = '\0';
1231 wl3501_get_from_wla(this, 0x1a40,
1232 this->firmware_date, sizeof(this->firmware_date));
1233 this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
1234 /* Switch to SRAM Page 0 */
1235 wl3501_switch_page(this, WL3501_BSS_SPAGE0);
1236 /* Read parameter from card */
1237 wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
1238 wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
1239 wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
1240 wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
1241 wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
1242 wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
1243 this->esbq_req_tail = this->esbq_req_head = this->esbq_req_start;
1244 this->esbq_req_end += this->esbq_req_start;
1245 this->esbq_confirm = this->esbq_confirm_start;
1246 this->esbq_confirm_end += this->esbq_confirm_start;
1247 /* Initial Tx Buffer */
1248 this->tx_buffer_cnt = 1;
1249 ptr = this->tx_buffer_head;
1250 next = ptr + WL3501_BLKSZ;
1251 while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
1252 this->tx_buffer_cnt++;
1253 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1255 next = ptr + WL3501_BLKSZ;
1259 wl3501_set_to_wla(this, ptr, &next, sizeof(next));
1260 this->tx_buffer_tail = ptr;
1264 printk(KERN_WARNING "%s: failed!\n", __FUNCTION__);
1268 static int wl3501_close(struct net_device *dev)
1270 struct wl3501_card *this = dev->priv;
1272 unsigned long flags;
1273 struct pcmcia_device *link;
1276 spin_lock_irqsave(&this->lock, flags);
1279 /* Stop wl3501_hard_start_xmit() from now on */
1280 netif_stop_queue(dev);
1281 wl3501_ack_interrupt(this);
1283 /* Mask interrupts from the SUTRO */
1284 wl3501_block_interrupt(this);
1287 printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1288 spin_unlock_irqrestore(&this->lock, flags);
1293 * wl3501_reset - Reset the SUTRO.
1294 * @dev - network device
1296 * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1297 * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1298 * is running. It seems to be dangerous.
1300 static int wl3501_reset(struct net_device *dev)
1302 struct wl3501_card *this = dev->priv;
1305 wl3501_block_interrupt(this);
1307 if (wl3501_init_firmware(this)) {
1308 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1310 /* Free IRQ, and mark IRQ as unused */
1311 free_irq(dev->irq, dev);
1316 * Queue has to be started only when the Card is Started
1318 netif_stop_queue(dev);
1319 this->adhoc_times = 0;
1320 wl3501_ack_interrupt(this);
1321 wl3501_unblock_interrupt(this);
1322 wl3501_mgmt_scan(this, 100);
1323 dprintk(1, "%s: device reset", dev->name);
1329 static void wl3501_tx_timeout(struct net_device *dev)
1331 struct wl3501_card *this = dev->priv;
1332 struct net_device_stats *stats = &this->stats;
1333 unsigned long flags;
1337 spin_lock_irqsave(&this->lock, flags);
1338 rc = wl3501_reset(dev);
1339 spin_unlock_irqrestore(&this->lock, flags);
1341 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1344 dev->trans_start = jiffies;
1345 netif_wake_queue(dev);
1351 * 1 - Could not transmit (dev_queue_xmit will queue it)
1352 * and try to sent it later
1354 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1357 struct wl3501_card *this = dev->priv;
1358 unsigned long flags;
1360 spin_lock_irqsave(&this->lock, flags);
1361 enabled = wl3501_block_interrupt(this);
1362 dev->trans_start = jiffies;
1363 rc = wl3501_send_pkt(this, skb->data, skb->len);
1365 wl3501_unblock_interrupt(this);
1367 ++this->stats.tx_dropped;
1368 netif_stop_queue(dev);
1370 ++this->stats.tx_packets;
1371 this->stats.tx_bytes += skb->len;
1374 if (this->tx_buffer_cnt < 2)
1375 netif_stop_queue(dev);
1377 spin_unlock_irqrestore(&this->lock, flags);
1381 static int wl3501_open(struct net_device *dev)
1384 struct wl3501_card *this = dev->priv;
1385 unsigned long flags;
1386 struct pcmcia_device *link;
1389 spin_lock_irqsave(&this->lock, flags);
1390 if (!pcmcia_dev_present(link))
1392 netif_device_attach(dev);
1395 /* Initial WL3501 firmware */
1396 dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1397 if (wl3501_init_firmware(this))
1399 /* Initial device variables */
1400 this->adhoc_times = 0;
1401 /* Acknowledge Interrupt, for cleaning last state */
1402 wl3501_ack_interrupt(this);
1404 /* Enable interrupt from card after all */
1405 wl3501_unblock_interrupt(this);
1406 wl3501_mgmt_scan(this, 100);
1408 dprintk(1, "%s: WL3501 opened", dev->name);
1409 printk(KERN_INFO "%s: Card Name: %s\n"
1410 "%s: Firmware Date: %s\n",
1411 dev->name, this->card_name,
1412 dev->name, this->firmware_date);
1414 spin_unlock_irqrestore(&this->lock, flags);
1417 printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1421 static struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1423 struct wl3501_card *this = dev->priv;
1425 return &this->stats;
1428 static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1430 struct wl3501_card *this = dev->priv;
1431 struct iw_statistics *wstats = &this->wstats;
1432 u32 value; /* size checked: it is u32 */
1434 memset(wstats, 0, sizeof(*wstats));
1435 wstats->status = netif_running(dev);
1436 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1437 &value, sizeof(value)))
1438 wstats->discard.code += value;
1439 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1440 &value, sizeof(value)))
1441 wstats->discard.code += value;
1442 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1443 &value, sizeof(value)))
1444 wstats->discard.code += value;
1445 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1446 &value, sizeof(value)))
1447 wstats->discard.retries = value;
1448 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1449 &value, sizeof(value)))
1450 wstats->discard.misc += value;
1451 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1452 &value, sizeof(value)))
1453 wstats->discard.misc += value;
1454 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1455 &value, sizeof(value)))
1456 wstats->discard.misc += value;
1457 if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1458 &value, sizeof(value)))
1459 wstats->discard.misc += value;
1463 static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1465 strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
1468 static struct ethtool_ops ops = {
1469 .get_drvinfo = wl3501_get_drvinfo
1473 * wl3501_detach - deletes a driver "instance"
1476 * This deletes a driver "instance". The device is de-registered with Card
1477 * Services. If it has been released, all local data structures are freed.
1478 * Otherwise, the structures will be freed when the device is released.
1480 static void wl3501_detach(struct pcmcia_device *link)
1482 struct net_device *dev = link->priv;
1484 /* If the device is currently configured and active, we won't actually
1485 * delete it yet. Instead, it is marked so that when the release()
1486 * function is called, that will trigger a proper detach(). */
1488 while (link->open > 0)
1491 netif_device_detach(dev);
1492 wl3501_release(link);
1495 free_netdev(link->priv);
1500 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1501 union iwreq_data *wrqu, char *extra)
1503 strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1507 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1508 union iwreq_data *wrqu, char *extra)
1510 struct wl3501_card *this = dev->priv;
1511 int channel = wrqu->freq.m;
1514 if (iw_valid_channel(this->reg_domain, channel)) {
1515 this->chan = channel;
1516 rc = wl3501_reset(dev);
1521 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1522 union iwreq_data *wrqu, char *extra)
1524 struct wl3501_card *this = dev->priv;
1526 wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1531 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1532 union iwreq_data *wrqu, char *extra)
1536 if (wrqu->mode == IW_MODE_INFRA ||
1537 wrqu->mode == IW_MODE_ADHOC ||
1538 wrqu->mode == IW_MODE_AUTO) {
1539 struct wl3501_card *this = dev->priv;
1541 this->net_type = wrqu->mode;
1542 rc = wl3501_reset(dev);
1547 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1548 union iwreq_data *wrqu, char *extra)
1550 struct wl3501_card *this = dev->priv;
1552 wrqu->mode = this->net_type;
1556 static int wl3501_get_sens(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->sens.value = this->rssi;
1562 wrqu->sens.disabled = !wrqu->sens.value;
1563 wrqu->sens.fixed = 1;
1567 static int wl3501_get_range(struct net_device *dev,
1568 struct iw_request_info *info,
1569 union iwreq_data *wrqu, char *extra)
1571 struct iw_range *range = (struct iw_range *)extra;
1573 /* Set the length (very important for backward compatibility) */
1574 wrqu->data.length = sizeof(*range);
1576 /* Set all the info we don't care or don't know about to zero */
1577 memset(range, 0, sizeof(*range));
1579 /* Set the Wireless Extension versions */
1580 range->we_version_compiled = WIRELESS_EXT;
1581 range->we_version_source = 1;
1582 range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
1583 /* FIXME: study the code to fill in more fields... */
1587 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1588 union iwreq_data *wrqu, char *extra)
1590 struct wl3501_card *this = dev->priv;
1591 static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1594 /* FIXME: we support other ARPHRDs...*/
1595 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1597 if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1598 /* FIXME: rescan? */
1600 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1601 /* FIXME: rescan? deassoc & scan? */
1607 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1608 union iwreq_data *wrqu, char *extra)
1610 struct wl3501_card *this = dev->priv;
1612 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1613 memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1617 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1618 union iwreq_data *wrqu, char *extra)
1621 * FIXME: trigger scanning with a reset, yes, I'm lazy
1623 return wl3501_reset(dev);
1626 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1627 union iwreq_data *wrqu, char *extra)
1629 struct wl3501_card *this = dev->priv;
1631 char *current_ev = extra;
1632 struct iw_event iwe;
1634 for (i = 0; i < this->bss_cnt; ++i) {
1635 iwe.cmd = SIOCGIWAP;
1636 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1637 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1638 current_ev = iwe_stream_add_event(current_ev,
1639 extra + IW_SCAN_MAX_DATA,
1640 &iwe, IW_EV_ADDR_LEN);
1641 iwe.cmd = SIOCGIWESSID;
1642 iwe.u.data.flags = 1;
1643 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1644 current_ev = iwe_stream_add_point(current_ev,
1645 extra + IW_SCAN_MAX_DATA,
1647 this->bss_set[i].ssid.essid);
1648 iwe.cmd = SIOCGIWMODE;
1649 iwe.u.mode = this->bss_set[i].bss_type;
1650 current_ev = iwe_stream_add_event(current_ev,
1651 extra + IW_SCAN_MAX_DATA,
1652 &iwe, IW_EV_UINT_LEN);
1653 iwe.cmd = SIOCGIWFREQ;
1654 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1656 current_ev = iwe_stream_add_event(current_ev,
1657 extra + IW_SCAN_MAX_DATA,
1658 &iwe, IW_EV_FREQ_LEN);
1659 iwe.cmd = SIOCGIWENCODE;
1660 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1661 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1663 iwe.u.data.flags = IW_ENCODE_DISABLED;
1664 iwe.u.data.length = 0;
1665 current_ev = iwe_stream_add_point(current_ev,
1666 extra + IW_SCAN_MAX_DATA,
1669 /* Length of data */
1670 wrqu->data.length = (current_ev - extra);
1671 wrqu->data.flags = 0; /* FIXME: set properly these flags */
1675 static int wl3501_set_essid(struct net_device *dev,
1676 struct iw_request_info *info,
1677 union iwreq_data *wrqu, char *extra)
1679 struct wl3501_card *this = dev->priv;
1681 if (wrqu->data.flags) {
1682 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1684 extra, wrqu->data.length);
1685 } else { /* We accept any ESSID */
1686 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1687 &this->essid.el, "ANY", 3);
1689 return wl3501_reset(dev);
1692 static int wl3501_get_essid(struct net_device *dev,
1693 struct iw_request_info *info,
1694 union iwreq_data *wrqu, char *extra)
1696 struct wl3501_card *this = dev->priv;
1697 unsigned long flags;
1699 spin_lock_irqsave(&this->lock, flags);
1700 wrqu->essid.flags = 1;
1701 wrqu->essid.length = this->essid.el.len;
1702 memcpy(extra, this->essid.essid, this->essid.el.len);
1703 spin_unlock_irqrestore(&this->lock, flags);
1707 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1708 union iwreq_data *wrqu, char *extra)
1710 struct wl3501_card *this = dev->priv;
1712 if (wrqu->data.length > sizeof(this->nick))
1714 strlcpy(this->nick, extra, wrqu->data.length);
1718 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1719 union iwreq_data *wrqu, char *extra)
1721 struct wl3501_card *this = dev->priv;
1723 strlcpy(extra, this->nick, 32);
1724 wrqu->data.length = strlen(extra);
1728 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1729 union iwreq_data *wrqu, char *extra)
1732 * FIXME: have to see from where to get this info, perhaps this card
1733 * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1734 * common with the Planet Access Points. -acme
1736 wrqu->bitrate.value = 2000000;
1737 wrqu->bitrate.fixed = 1;
1741 static int wl3501_get_rts_threshold(struct net_device *dev,
1742 struct iw_request_info *info,
1743 union iwreq_data *wrqu, char *extra)
1745 u16 threshold; /* size checked: it is u16 */
1746 struct wl3501_card *this = dev->priv;
1747 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1748 &threshold, sizeof(threshold));
1750 wrqu->rts.value = threshold;
1751 wrqu->rts.disabled = threshold >= 2347;
1752 wrqu->rts.fixed = 1;
1757 static int wl3501_get_frag_threshold(struct net_device *dev,
1758 struct iw_request_info *info,
1759 union iwreq_data *wrqu, char *extra)
1761 u16 threshold; /* size checked: it is u16 */
1762 struct wl3501_card *this = dev->priv;
1763 int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1764 &threshold, sizeof(threshold));
1766 wrqu->frag.value = threshold;
1767 wrqu->frag.disabled = threshold >= 2346;
1768 wrqu->frag.fixed = 1;
1773 static int wl3501_get_txpow(struct net_device *dev,
1774 struct iw_request_info *info,
1775 union iwreq_data *wrqu, char *extra)
1778 struct wl3501_card *this = dev->priv;
1779 int rc = wl3501_get_mib_value(this,
1780 WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1781 &txpow, sizeof(txpow));
1783 wrqu->txpower.value = txpow;
1784 wrqu->txpower.disabled = 0;
1786 * From the MIB values I think this can be configurable,
1787 * as it lists several tx power levels -acme
1789 wrqu->txpower.fixed = 0;
1790 wrqu->txpower.flags = IW_TXPOW_MWATT;
1795 static int wl3501_get_retry(struct net_device *dev,
1796 struct iw_request_info *info,
1797 union iwreq_data *wrqu, char *extra)
1799 u8 retry; /* size checked: it is u8 */
1800 struct wl3501_card *this = dev->priv;
1801 int rc = wl3501_get_mib_value(this,
1802 WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1803 &retry, sizeof(retry));
1806 if (wrqu->retry.flags & IW_RETRY_MAX) {
1807 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1810 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1811 &retry, sizeof(retry));
1814 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1816 wrqu->retry.value = retry;
1817 wrqu->retry.disabled = 0;
1822 static int wl3501_get_encode(struct net_device *dev,
1823 struct iw_request_info *info,
1824 union iwreq_data *wrqu, char *extra)
1826 u8 implemented, restricted, keys[100], len_keys, tocopy;
1827 struct wl3501_card *this = dev->priv;
1828 int rc = wl3501_get_mib_value(this,
1829 WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1830 &implemented, sizeof(implemented));
1834 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1837 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1838 &restricted, sizeof(restricted));
1841 wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1843 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1844 &len_keys, sizeof(len_keys));
1847 rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1851 tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1852 tocopy = min_t(u8, tocopy, 100);
1853 wrqu->encoding.length = tocopy;
1854 memset(extra, 0, tocopy);
1855 memcpy(extra, keys, tocopy);
1860 static int wl3501_get_power(struct net_device *dev,
1861 struct iw_request_info *info,
1862 union iwreq_data *wrqu, char *extra)
1865 struct wl3501_card *this = dev->priv;
1866 int rc = wl3501_get_mib_value(this,
1867 WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1868 &pwr_state, sizeof(pwr_state));
1871 wrqu->power.disabled = !pwr_state;
1872 wrqu->power.flags = IW_POWER_ON;
1877 static const iw_handler wl3501_handler[] = {
1878 [SIOCGIWNAME - SIOCIWFIRST] = wl3501_get_name,
1879 [SIOCSIWFREQ - SIOCIWFIRST] = wl3501_set_freq,
1880 [SIOCGIWFREQ - SIOCIWFIRST] = wl3501_get_freq,
1881 [SIOCSIWMODE - SIOCIWFIRST] = wl3501_set_mode,
1882 [SIOCGIWMODE - SIOCIWFIRST] = wl3501_get_mode,
1883 [SIOCGIWSENS - SIOCIWFIRST] = wl3501_get_sens,
1884 [SIOCGIWRANGE - SIOCIWFIRST] = wl3501_get_range,
1885 [SIOCSIWSPY - SIOCIWFIRST] = iw_handler_set_spy,
1886 [SIOCGIWSPY - SIOCIWFIRST] = iw_handler_get_spy,
1887 [SIOCSIWTHRSPY - SIOCIWFIRST] = iw_handler_set_thrspy,
1888 [SIOCGIWTHRSPY - SIOCIWFIRST] = iw_handler_get_thrspy,
1889 [SIOCSIWAP - SIOCIWFIRST] = wl3501_set_wap,
1890 [SIOCGIWAP - SIOCIWFIRST] = wl3501_get_wap,
1891 [SIOCSIWSCAN - SIOCIWFIRST] = wl3501_set_scan,
1892 [SIOCGIWSCAN - SIOCIWFIRST] = wl3501_get_scan,
1893 [SIOCSIWESSID - SIOCIWFIRST] = wl3501_set_essid,
1894 [SIOCGIWESSID - SIOCIWFIRST] = wl3501_get_essid,
1895 [SIOCSIWNICKN - SIOCIWFIRST] = wl3501_set_nick,
1896 [SIOCGIWNICKN - SIOCIWFIRST] = wl3501_get_nick,
1897 [SIOCGIWRATE - SIOCIWFIRST] = wl3501_get_rate,
1898 [SIOCGIWRTS - SIOCIWFIRST] = wl3501_get_rts_threshold,
1899 [SIOCGIWFRAG - SIOCIWFIRST] = wl3501_get_frag_threshold,
1900 [SIOCGIWTXPOW - SIOCIWFIRST] = wl3501_get_txpow,
1901 [SIOCGIWRETRY - SIOCIWFIRST] = wl3501_get_retry,
1902 [SIOCGIWENCODE - SIOCIWFIRST] = wl3501_get_encode,
1903 [SIOCGIWPOWER - SIOCIWFIRST] = wl3501_get_power,
1906 static const struct iw_handler_def wl3501_handler_def = {
1907 .num_standard = sizeof(wl3501_handler) / sizeof(iw_handler),
1908 .standard = (iw_handler *)wl3501_handler,
1909 .get_wireless_stats = wl3501_get_wireless_stats,
1913 * wl3501_attach - creates an "instance" of the driver
1915 * Creates an "instance" of the driver, allocating local data structures for
1916 * one device. The device is registered with Card Services.
1918 * The dev_link structure is initialized, but we don't actually configure the
1919 * card at this point -- we wait until we receive a card insertion event.
1921 static int wl3501_probe(struct pcmcia_device *p_dev)
1923 struct net_device *dev;
1924 struct wl3501_card *this;
1926 /* The io structure describes IO port mapping */
1927 p_dev->io.NumPorts1 = 16;
1928 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1929 p_dev->io.IOAddrLines = 5;
1931 /* Interrupt setup */
1932 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1933 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
1934 p_dev->irq.Handler = wl3501_interrupt;
1936 /* General socket configuration */
1937 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
1938 p_dev->conf.IntType = INT_MEMORY_AND_IO;
1939 p_dev->conf.ConfigIndex = 1;
1940 p_dev->conf.Present = PRESENT_OPTION;
1942 dev = alloc_etherdev(sizeof(struct wl3501_card));
1945 dev->open = wl3501_open;
1946 dev->stop = wl3501_close;
1947 dev->hard_start_xmit = wl3501_hard_start_xmit;
1948 dev->tx_timeout = wl3501_tx_timeout;
1949 dev->watchdog_timeo = 5 * HZ;
1950 dev->get_stats = wl3501_get_stats;
1952 this->wireless_data.spy_data = &this->spy_data;
1953 this->p_dev = p_dev;
1954 dev->wireless_data = &this->wireless_data;
1955 dev->wireless_handlers = (struct iw_handler_def *)&wl3501_handler_def;
1956 SET_ETHTOOL_OPS(dev, &ops);
1957 netif_stop_queue(dev);
1958 p_dev->priv = p_dev->irq.Instance = dev;
1960 return wl3501_config(p_dev);
1965 #define CS_CHECK(fn, ret) \
1966 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
1969 * wl3501_config - configure the PCMCIA socket and make eth device available
1972 * wl3501_config() is scheduled to run after a CARD_INSERTION event is
1973 * received, to configure the PCMCIA socket, and to make the ethernet device
1974 * available to the system.
1976 static int wl3501_config(struct pcmcia_device *link)
1980 struct net_device *dev = link->priv;
1981 int i = 0, j, last_fn, last_ret;
1982 unsigned char bf[64];
1983 struct wl3501_card *this;
1985 /* This reads the card's CONFIG tuple to find its config registers. */
1986 tuple.Attributes = 0;
1987 tuple.DesiredTuple = CISTPL_CONFIG;
1988 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
1989 tuple.TupleData = bf;
1990 tuple.TupleDataMax = sizeof(bf);
1991 tuple.TupleOffset = 0;
1992 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
1993 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
1994 link->conf.ConfigBase = parse.config.base;
1995 link->conf.Present = parse.config.rmask[0];
1997 /* Try allocating IO ports. This tries a few fixed addresses. If you
1998 * want, you can also read the card's config table to pick addresses --
1999 * see the serial driver for an example. */
2001 for (j = 0x280; j < 0x400; j += 0x20) {
2002 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
2003 * 0x200-0x2ff, and so on, because this seems safer */
2004 link->io.BasePort1 = j;
2005 link->io.BasePort2 = link->io.BasePort1 + 0x10;
2006 i = pcmcia_request_io(link, &link->io);
2007 if (i == CS_SUCCESS)
2010 if (i != CS_SUCCESS) {
2011 cs_error(link, RequestIO, i);
2015 /* Now allocate an interrupt line. Note that this does not actually
2016 * assign a handler to the interrupt. */
2018 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
2020 /* This actually configures the PCMCIA socket -- setting up the I/O
2021 * windows and the interrupt mapping. */
2023 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
2025 dev->irq = link->irq.AssignedIRQ;
2026 dev->base_addr = link->io.BasePort1;
2027 SET_NETDEV_DEV(dev, &handle_to_dev(link));
2028 if (register_netdev(dev)) {
2029 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2033 SET_MODULE_OWNER(dev);
2037 * At this point, the dev_node_t structure(s) should be initialized and
2038 * arranged in a linked list at link->dev_node.
2040 link->dev_node = &this->node;
2042 this->base_addr = dev->base_addr;
2044 if (!wl3501_get_flash_mac_addr(this)) {
2045 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2049 strcpy(this->node.dev_name, dev->name);
2051 /* print probe information */
2052 printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2053 dev->name, this->base_addr, (int)dev->irq);
2054 for (i = 0; i < 6; i++) {
2055 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2056 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2060 * Initialize card parameters - added by jss
2062 this->net_type = IW_MODE_INFRA;
2064 this->join_sta_bss = 0;
2065 this->adhoc_times = 0;
2066 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2068 this->card_name[0] = '\0';
2069 this->firmware_date[0] = '\0';
2071 this->chan = iw_default_channel(this->reg_domain);
2072 strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2073 spin_lock_init(&this->lock);
2074 init_waitqueue_head(&this->wait);
2075 netif_start_queue(dev);
2079 cs_error(link, last_fn, last_ret);
2081 wl3501_release(link);
2086 * wl3501_release - unregister the net, release PCMCIA configuration
2089 * After a card is removed, wl3501_release() will unregister the net device,
2090 * and release the PCMCIA configuration. If the device is still open, this
2091 * will be postponed until it is closed.
2093 static void wl3501_release(struct pcmcia_device *link)
2095 struct net_device *dev = link->priv;
2097 /* Unlink the device chain */
2099 unregister_netdev(dev);
2101 pcmcia_disable_device(link);
2104 static int wl3501_suspend(struct pcmcia_device *link)
2106 struct net_device *dev = link->priv;
2108 wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2110 netif_device_detach(dev);
2115 static int wl3501_resume(struct pcmcia_device *link)
2117 struct net_device *dev = link->priv;
2119 wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2122 netif_device_attach(dev);
2129 static struct pcmcia_device_id wl3501_ids[] = {
2130 PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
2133 MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
2135 static struct pcmcia_driver wl3501_driver = {
2136 .owner = THIS_MODULE,
2138 .name = "wl3501_cs",
2140 .probe = wl3501_probe,
2141 .remove = wl3501_detach,
2142 .id_table = wl3501_ids,
2143 .suspend = wl3501_suspend,
2144 .resume = wl3501_resume,
2147 static int __init wl3501_init_module(void)
2149 return pcmcia_register_driver(&wl3501_driver);
2152 static void __exit wl3501_exit_module(void)
2154 pcmcia_unregister_driver(&wl3501_driver);
2157 module_init(wl3501_init_module);
2158 module_exit(wl3501_exit_module);
2160 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2161 "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2162 "Gustavo Niemeyer <niemeyer@conectiva.com>");
2163 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2164 MODULE_LICENSE("GPL");