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