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