+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.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         if (link->state & DEV_STALE_CONFIG) {
1310                 link->state |= DEV_RELEASE_PENDING;
1311                 wl3501_release(link);
1312         }
1313         rc = 0;
1314         printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
1315 out:
1316         spin_unlock_irqrestore(&this->lock, flags);
1317         return rc;
1318 }
1319
1320 /**
1321  * wl3501_reset - Reset the SUTRO.
1322  * @dev - network device
1323  *
1324  * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
1325  * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
1326  * is running. It seems to be dangerous.
1327  */
1328 static int wl3501_reset(struct net_device *dev)
1329 {
1330         struct wl3501_card *this = dev->priv;
1331         int rc = -ENODEV;
1332
1333         wl3501_block_interrupt(this);
1334
1335         if (wl3501_init_firmware(this)) {
1336                 printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
1337                        dev->name);
1338                 /* Free IRQ, and mark IRQ as unused */
1339                 free_irq(dev->irq, dev);
1340                 goto out;
1341         }
1342
1343         /*
1344          * Queue has to be started only when the Card is Started
1345          */
1346         netif_stop_queue(dev);
1347         this->adhoc_times = 0;
1348         wl3501_ack_interrupt(this);
1349         wl3501_unblock_interrupt(this);
1350         wl3501_mgmt_scan(this, 100);
1351         dprintk(1, "%s: device reset", dev->name);
1352         rc = 0;
1353 out:
1354         return rc;
1355 }
1356
1357 static void wl3501_tx_timeout(struct net_device *dev)
1358 {
1359         struct wl3501_card *this = dev->priv;
1360         struct net_device_stats *stats = &this->stats;
1361         unsigned long flags;
1362         int rc;
1363
1364         stats->tx_errors++;
1365         spin_lock_irqsave(&this->lock, flags);
1366         rc = wl3501_reset(dev);
1367         spin_unlock_irqrestore(&this->lock, flags);
1368         if (rc)
1369                 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1370                        dev->name, rc);
1371         else {
1372                 dev->trans_start = jiffies;
1373                 netif_wake_queue(dev);
1374         }
1375 }
1376
1377 /*
1378  * Return : 0 - OK
1379  *          1 - Could not transmit (dev_queue_xmit will queue it)
1380  *              and try to sent it later
1381  */
1382 static int wl3501_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1383 {
1384         int enabled, rc;
1385         struct wl3501_card *this = dev->priv;
1386         unsigned long flags;
1387
1388         spin_lock_irqsave(&this->lock, flags);
1389         enabled = wl3501_block_interrupt(this);
1390         dev->trans_start = jiffies;
1391         rc = wl3501_send_pkt(this, skb->data, skb->len);
1392         if (enabled)
1393                 wl3501_unblock_interrupt(this);
1394         if (rc) {
1395                 ++this->stats.tx_dropped;
1396                 netif_stop_queue(dev);
1397         } else {
1398                 ++this->stats.tx_packets;
1399                 this->stats.tx_bytes += skb->len;
1400                 kfree_skb(skb);
1401
1402                 if (this->tx_buffer_cnt < 2)
1403                         netif_stop_queue(dev);
1404         }
1405         spin_unlock_irqrestore(&this->lock, flags);
1406         return rc;
1407 }
1408
1409 static int wl3501_open(struct net_device *dev)
1410 {
1411         int rc = -ENODEV;
1412         struct wl3501_card *this = dev->priv;
1413         unsigned long flags;
1414         dev_link_t *link;
1415
1416         spin_lock_irqsave(&this->lock, flags);
1417         /* Check if the device is in wl3501_dev_list */
1418         for (link = wl3501_dev_list; link; link = link->next)
1419                 if (link->priv == dev)
1420                         break;
1421         if (!DEV_OK(link))
1422                 goto out;
1423         netif_device_attach(dev);
1424         link->open++;
1425
1426         /* Initial WL3501 firmware */
1427         dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
1428         if (wl3501_init_firmware(this))
1429                 goto fail;
1430         /* Initial device variables */
1431         this->adhoc_times = 0;
1432         /* Acknowledge Interrupt, for cleaning last state */
1433         wl3501_ack_interrupt(this);
1434
1435         /* Enable interrupt from card after all */
1436         wl3501_unblock_interrupt(this);
1437         wl3501_mgmt_scan(this, 100);
1438         rc = 0;
1439         dprintk(1, "%s: WL3501 opened", dev->name);
1440         printk(KERN_INFO "%s: Card Name: %s\n"
1441                          "%s: Firmware Date: %s\n",
1442                          dev->name, this->card_name,
1443                          dev->name, this->firmware_date);
1444 out:
1445         spin_unlock_irqrestore(&this->lock, flags);
1446         return rc;
1447 fail:
1448         printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
1449         goto out;
1450 }
1451
1452 struct net_device_stats *wl3501_get_stats(struct net_device *dev)
1453 {
1454         struct wl3501_card *this = dev->priv;
1455
1456         return &this->stats;
1457 }
1458
1459 struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
1460 {
1461         struct wl3501_card *this = dev->priv;
1462         struct iw_statistics *wstats = &this->wstats;
1463         u32 value; /* size checked: it is u32 */
1464
1465         memset(wstats, 0, sizeof(*wstats));
1466         wstats->status = netif_running(dev);
1467         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
1468                                   &value, sizeof(value)))
1469                 wstats->discard.code += value;
1470         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
1471                                   &value, sizeof(value)))
1472                 wstats->discard.code += value;
1473         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
1474                                   &value, sizeof(value)))
1475                 wstats->discard.code += value;
1476         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
1477                                   &value, sizeof(value)))
1478                 wstats->discard.retries = value;
1479         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
1480                                   &value, sizeof(value)))
1481                 wstats->discard.misc += value;
1482         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
1483                                   &value, sizeof(value)))
1484                 wstats->discard.misc += value;
1485         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
1486                                   &value, sizeof(value)))
1487                 wstats->discard.misc += value;
1488         if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
1489                                   &value, sizeof(value)))
1490                 wstats->discard.misc += value;
1491         return wstats;
1492 }
1493
1494 static inline int wl3501_ethtool_ioctl(struct net_device *dev, void *uaddr)
1495 {
1496         u32 ethcmd;
1497         int rc = -EFAULT;
1498
1499         if (copy_from_user(&ethcmd, uaddr, sizeof(ethcmd)))
1500                 goto out;
1501
1502         switch (ethcmd) {
1503         case ETHTOOL_GDRVINFO: {
1504                 struct ethtool_drvinfo info = { .cmd = ETHTOOL_GDRVINFO, };
1505
1506                 strlcpy(info.driver, wl3501_dev_info, sizeof(info.driver));
1507                 rc = copy_to_user(uaddr, &info, sizeof(info)) ? -EFAULT : 1;
1508         }
1509         default:
1510                 rc = -EOPNOTSUPP;
1511                 break;
1512         }
1513 out:
1514         return rc;
1515 }
1516
1517 /**
1518  * wl3501_ioctl - Perform IOCTL call functions
1519  * @dev - network device
1520  * @ifreq - request
1521  * @cmd - command
1522  *
1523  * Perform IOCTL call functions here. Some are privileged operations and the
1524  * effective uid is checked in those cases.
1525  *
1526  * This part is optional. Needed only if you want to run wlu (unix version).
1527  *
1528  * CAUTION: To prevent interrupted by wl3501_interrupt() and timer-based
1529  * wl3501_hard_start_xmit() from other interrupts, this should be run
1530  * single-threaded.
1531  */
1532 static int wl3501_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1533 {
1534         int rc = -ENODEV;
1535
1536         if (netif_device_present(dev)) {
1537                 rc = -EOPNOTSUPP;
1538                 if (cmd == SIOCETHTOOL)
1539                         rc = wl3501_ethtool_ioctl(dev, (void *)rq->ifr_data);
1540         }
1541         return rc;
1542 }
1543
1544 /**
1545  * wl3501_detach - deletes a driver "instance"
1546  * @link - FILL_IN
1547  *
1548  * This deletes a driver "instance". The device is de-registered with Card
1549  * Services. If it has been released, all local data structures are freed.
1550  * Otherwise, the structures will be freed when the device is released.
1551  */
1552 static void wl3501_detach(dev_link_t *link)
1553 {
1554         dev_link_t **linkp;
1555
1556         /* Locate device structure */
1557         for (linkp = &wl3501_dev_list; *linkp; linkp = &(*linkp)->next)
1558                 if (*linkp == link)
1559                         break;
1560         if (!*linkp)
1561                 goto out;
1562
1563         /* If the device is currently configured and active, we won't actually
1564          * delete it yet.  Instead, it is marked so that when the release()
1565          * function is called, that will trigger a proper detach(). */
1566
1567         if (link->state & DEV_CONFIG) {
1568 #ifdef PCMCIA_DEBUG
1569                 printk(KERN_DEBUG "wl3501_cs: detach postponed, '%s' "
1570                        "still locked\n", link->dev->dev_name);
1571 #endif
1572                 goto out;
1573         }
1574
1575         /* Break the link with Card Services */
1576         if (link->handle)
1577                 pcmcia_deregister_client(link->handle);
1578
1579         /* Unlink device structure, free pieces */
1580         *linkp = link->next;
1581
1582         if (link->priv)
1583                 free_netdev(link->priv);
1584         kfree(link);
1585 out:
1586         return;
1587 }
1588
1589 static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
1590                            union iwreq_data *wrqu, char *extra)
1591 {
1592         strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
1593         return 0;
1594 }
1595
1596 static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
1597                            union iwreq_data *wrqu, char *extra)
1598 {
1599         struct wl3501_card *this = dev->priv;
1600         int channel = wrqu->freq.m;
1601         int rc = -EINVAL;
1602
1603         if (iw_valid_channel(this->reg_domain, channel)) {
1604                 this->chan = channel;
1605                 rc = wl3501_reset(dev);
1606         }
1607         return rc;
1608 }
1609
1610 static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
1611                            union iwreq_data *wrqu, char *extra)
1612 {
1613         struct wl3501_card *this = dev->priv;
1614
1615         wrqu->freq.m = wl3501_chan2freq[this->chan - 1] * 100000;
1616         wrqu->freq.e = 1;
1617         return 0;
1618 }
1619
1620 static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
1621                            union iwreq_data *wrqu, char *extra)
1622 {
1623         int rc = -EINVAL;
1624
1625         if (wrqu->mode == IW_MODE_INFRA ||
1626             wrqu->mode == IW_MODE_ADHOC ||
1627             wrqu->mode == IW_MODE_AUTO) {
1628                 struct wl3501_card *this = dev->priv;
1629
1630                 this->net_type = wrqu->mode;
1631                 rc = wl3501_reset(dev);
1632         }
1633         return rc;
1634 }
1635
1636 static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
1637                            union iwreq_data *wrqu, char *extra)
1638 {
1639         struct wl3501_card *this = dev->priv;
1640
1641         wrqu->mode = this->net_type;
1642         return 0;
1643 }
1644
1645 static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
1646                            union iwreq_data *wrqu, char *extra)
1647 {
1648         struct wl3501_card *this = dev->priv;
1649
1650         wrqu->sens.value = this->rssi;
1651         wrqu->sens.disabled = !wrqu->sens.value;
1652         wrqu->sens.fixed = 1;
1653         return 0;
1654 }
1655
1656 static int wl3501_get_range(struct net_device *dev,
1657                             struct iw_request_info *info,
1658                             union iwreq_data *wrqu, char *extra)
1659 {
1660         struct iw_range *range = (struct iw_range *)extra;
1661
1662         /* Set the length (very important for backward compatibility) */
1663         wrqu->data.length = sizeof(*range);
1664
1665         /* Set all the info we don't care or don't know about to zero */
1666         memset(range, 0, sizeof(*range));
1667
1668         /* Set the Wireless Extension versions */
1669         range->we_version_compiled      = WIRELESS_EXT;
1670         range->we_version_source        = 1;
1671         range->throughput               = 2 * 1000 * 1000;     /* ~2 Mb/s */
1672         /* FIXME: study the code to fill in more fields... */
1673         return 0;
1674 }
1675
1676 static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
1677                           union iwreq_data *wrqu, char *extra)
1678 {
1679         struct wl3501_card *this = dev->priv;
1680         static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
1681         int rc = -EINVAL;
1682
1683         /* FIXME: we support other ARPHRDs...*/
1684         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
1685                 goto out;
1686         if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
1687                 /* FIXME: rescan? */
1688         } else
1689                 memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
1690                 /* FIXME: rescan? deassoc & scan? */
1691         rc = 0;
1692 out:
1693         return rc;
1694 }
1695
1696 static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
1697                           union iwreq_data *wrqu, char *extra)
1698 {
1699         struct wl3501_card *this = dev->priv;
1700
1701         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1702         memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
1703         return 0;
1704 }
1705
1706 static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
1707                            union iwreq_data *wrqu, char *extra)
1708 {
1709         /*
1710          * FIXME: trigger scanning with a reset, yes, I'm lazy
1711          */
1712         return wl3501_reset(dev);
1713 }
1714
1715 static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
1716                            union iwreq_data *wrqu, char *extra)
1717 {
1718         struct wl3501_card *this = dev->priv;
1719         int i;
1720         char *current_ev = extra;
1721         struct iw_event iwe;
1722
1723         for (i = 0; i < this->bss_cnt; ++i) {
1724                 iwe.cmd                 = SIOCGIWAP;
1725                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1726                 memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
1727                 current_ev = iwe_stream_add_event(current_ev,
1728                                                   extra + IW_SCAN_MAX_DATA,
1729                                                   &iwe, IW_EV_ADDR_LEN);
1730                 iwe.cmd           = SIOCGIWESSID;
1731                 iwe.u.data.flags  = 1;
1732                 iwe.u.data.length = this->bss_set[i].ssid.el.len;
1733                 current_ev = iwe_stream_add_point(current_ev,
1734                                                   extra + IW_SCAN_MAX_DATA,
1735                                                   &iwe,
1736                                                   this->bss_set[i].ssid.essid);
1737                 iwe.cmd    = SIOCGIWMODE;
1738                 iwe.u.mode = this->bss_set[i].bss_type;
1739                 current_ev = iwe_stream_add_event(current_ev,
1740                                                   extra + IW_SCAN_MAX_DATA,
1741                                                   &iwe, IW_EV_UINT_LEN);
1742                 iwe.cmd = SIOCGIWFREQ;
1743                 iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
1744                 iwe.u.freq.e = 0;
1745                 current_ev = iwe_stream_add_event(current_ev,
1746                                                   extra + IW_SCAN_MAX_DATA,
1747                                                   &iwe, IW_EV_FREQ_LEN);
1748                 iwe.cmd = SIOCGIWENCODE;
1749                 if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
1750                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1751                 else
1752                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1753                 iwe.u.data.length = 0;
1754                 current_ev = iwe_stream_add_point(current_ev,
1755                                                   extra + IW_SCAN_MAX_DATA,
1756                                                   &iwe, NULL);
1757         }
1758         /* Length of data */
1759         wrqu->data.length = (current_ev - extra);
1760         wrqu->data.flags = 0; /* FIXME: set properly these flags */
1761         return 0;
1762 }
1763
1764 static int wl3501_set_essid(struct net_device *dev,
1765                             struct iw_request_info *info,
1766                             union iwreq_data *wrqu, char *extra)
1767 {
1768         struct wl3501_card *this = dev->priv;
1769
1770         if (wrqu->data.flags) {
1771                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1772                                          &this->essid.el,
1773                                          extra, wrqu->data.length);
1774         } else { /* We accept any ESSID */
1775                 iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
1776                                          &this->essid.el, "ANY", 3);
1777         }
1778         return wl3501_reset(dev);
1779 }
1780
1781 static int wl3501_get_essid(struct net_device *dev,
1782                             struct iw_request_info *info,
1783                             union iwreq_data *wrqu, char *extra)
1784 {
1785         struct wl3501_card *this = dev->priv;
1786         unsigned long flags;
1787
1788         spin_lock_irqsave(&this->lock, flags);
1789         wrqu->essid.flags  = 1;
1790         wrqu->essid.length = this->essid.el.len;
1791         memcpy(extra, this->essid.essid, this->essid.el.len);
1792         spin_unlock_irqrestore(&this->lock, flags);
1793         return 0;
1794 }
1795
1796 static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
1797                            union iwreq_data *wrqu, char *extra)
1798 {
1799         struct wl3501_card *this = dev->priv;
1800
1801         if (wrqu->data.length > sizeof(this->nick))
1802                 return -E2BIG;
1803         strlcpy(this->nick, extra, wrqu->data.length);
1804         return 0;
1805 }
1806
1807 static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
1808                            union iwreq_data *wrqu, char *extra)
1809 {
1810         struct wl3501_card *this = dev->priv;
1811
1812         strlcpy(extra, this->nick, 32);
1813         wrqu->data.length = strlen(extra);
1814         return 0;
1815 }
1816
1817 static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
1818                            union iwreq_data *wrqu, char *extra)
1819 {
1820         /*
1821          * FIXME: have to see from where to get this info, perhaps this card
1822          * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
1823          * common with the Planet Access Points. -acme
1824          */
1825         wrqu->bitrate.value = 2000000;
1826         wrqu->bitrate.fixed = 1;
1827         return 0;
1828 }
1829
1830 static int wl3501_get_rts_threshold(struct net_device *dev,
1831                                     struct iw_request_info *info,
1832                                     union iwreq_data *wrqu, char *extra)
1833 {
1834         u16 threshold; /* size checked: it is u16 */
1835         struct wl3501_card *this = dev->priv;
1836         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
1837                                       &threshold, sizeof(threshold));
1838         if (!rc) {
1839                 wrqu->rts.value = threshold;
1840                 wrqu->rts.disabled = threshold >= 2347;
1841                 wrqu->rts.fixed = 1;
1842         }
1843         return rc;
1844 }
1845
1846 static int wl3501_get_frag_threshold(struct net_device *dev,
1847                                      struct iw_request_info *info,
1848                                      union iwreq_data *wrqu, char *extra)
1849 {
1850         u16 threshold; /* size checked: it is u16 */
1851         struct wl3501_card *this = dev->priv;
1852         int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
1853                                       &threshold, sizeof(threshold));
1854         if (!rc) {
1855                 wrqu->frag.value = threshold;
1856                 wrqu->frag.disabled = threshold >= 2346;
1857                 wrqu->frag.fixed = 1;
1858         }
1859         return rc;
1860 }
1861
1862 static int wl3501_get_txpow(struct net_device *dev,
1863                             struct iw_request_info *info,
1864                             union iwreq_data *wrqu, char *extra)
1865 {
1866         u16 txpow;
1867         struct wl3501_card *this = dev->priv;
1868         int rc = wl3501_get_mib_value(this,
1869                                       WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
1870                                       &txpow, sizeof(txpow));
1871         if (!rc) {
1872                 wrqu->txpower.value = txpow;
1873                 wrqu->txpower.disabled = 0;
1874                 /*
1875                  * From the MIB values I think this can be configurable,
1876                  * as it lists several tx power levels -acme
1877                  */
1878                 wrqu->txpower.fixed = 0;
1879                 wrqu->txpower.flags = IW_TXPOW_MWATT;
1880         }
1881         return rc;
1882 }
1883
1884 static int wl3501_get_retry(struct net_device *dev,
1885                             struct iw_request_info *info,
1886                             union iwreq_data *wrqu, char *extra)
1887 {
1888         u8 retry; /* size checked: it is u8 */
1889         struct wl3501_card *this = dev->priv;
1890         int rc = wl3501_get_mib_value(this,
1891                                       WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
1892                                       &retry, sizeof(retry));
1893         if (rc)
1894                 goto out;
1895         if (wrqu->retry.flags & IW_RETRY_MAX) {
1896                 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1897                 goto set_value;
1898         }
1899         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
1900                                   &retry, sizeof(retry));
1901         if (rc)
1902                 goto out;
1903         wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1904 set_value:
1905         wrqu->retry.value = retry;
1906         wrqu->retry.disabled = 0;
1907 out:
1908         return rc;
1909 }
1910
1911 static int wl3501_get_encode(struct net_device *dev,
1912                              struct iw_request_info *info,
1913                              union iwreq_data *wrqu, char *extra)
1914 {
1915         u8 implemented, restricted, keys[100], len_keys, tocopy;
1916         struct wl3501_card *this = dev->priv;
1917         int rc = wl3501_get_mib_value(this,
1918                                       WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
1919                                       &implemented, sizeof(implemented));
1920         if (rc)
1921                 goto out;
1922         if (!implemented) {
1923                 wrqu->encoding.flags = IW_ENCODE_DISABLED;
1924                 goto out;
1925         }
1926         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
1927                                   &restricted, sizeof(restricted));
1928         if (rc)
1929                 goto out;
1930         wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
1931                                             IW_ENCODE_OPEN;
1932         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
1933                                   &len_keys, sizeof(len_keys));
1934         if (rc)
1935                 goto out;
1936         rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
1937                                   keys, len_keys);
1938         if (rc)
1939                 goto out;
1940         tocopy = min_t(u8, len_keys, wrqu->encoding.length);
1941         tocopy = min_t(u8, tocopy, 100);
1942         wrqu->encoding.length = tocopy;
1943         memset(extra, 0, tocopy);
1944         memcpy(extra, keys, tocopy);
1945 out:
1946         return rc;
1947 }
1948
1949 static int wl3501_get_power(struct net_device *dev,
1950                             struct iw_request_info *info,
1951                             union iwreq_data *wrqu, char *extra)
1952 {
1953         u8 pwr_state;
1954         struct wl3501_card *this = dev->priv;
1955         int rc = wl3501_get_mib_value(this,
1956                                       WL3501_MIB_ATTR_CURRENT_PWR_STATE,
1957                                       &pwr_state, sizeof(pwr_state));
1958         if (rc)
1959                 goto out;
1960         wrqu->power.disabled = !pwr_state;
1961         wrqu->power.flags = IW_POWER_ON;
1962 out:
1963         return rc;
1964 }
1965
1966 static const iw_handler wl3501_handler[] = {
1967         [SIOCGIWNAME    - SIOCIWFIRST] = wl3501_get_name,
1968         [SIOCSIWFREQ    - SIOCIWFIRST] = wl3501_set_freq,
1969         [SIOCGIWFREQ    - SIOCIWFIRST] = wl3501_get_freq,
1970         [SIOCSIWMODE    - SIOCIWFIRST] = wl3501_set_mode,
1971         [SIOCGIWMODE    - SIOCIWFIRST] = wl3501_get_mode,
1972         [SIOCGIWSENS    - SIOCIWFIRST] = wl3501_get_sens,
1973         [SIOCGIWRANGE   - SIOCIWFIRST] = wl3501_get_range,
1974         [SIOCSIWSPY     - SIOCIWFIRST] = iw_handler_set_spy,
1975         [SIOCGIWSPY     - SIOCIWFIRST] = iw_handler_get_spy,
1976         [SIOCSIWTHRSPY  - SIOCIWFIRST] = iw_handler_set_thrspy,
1977         [SIOCGIWTHRSPY  - SIOCIWFIRST] = iw_handler_get_thrspy,
1978         [SIOCSIWAP      - SIOCIWFIRST] = wl3501_set_wap,
1979         [SIOCGIWAP      - SIOCIWFIRST] = wl3501_get_wap,
1980         [SIOCSIWSCAN    - SIOCIWFIRST] = wl3501_set_scan,
1981         [SIOCGIWSCAN    - SIOCIWFIRST] = wl3501_get_scan,
1982         [SIOCSIWESSID   - SIOCIWFIRST] = wl3501_set_essid,
1983         [SIOCGIWESSID   - SIOCIWFIRST] = wl3501_get_essid,
1984         [SIOCSIWNICKN   - SIOCIWFIRST] = wl3501_set_nick,
1985         [SIOCGIWNICKN   - SIOCIWFIRST] = wl3501_get_nick,
1986         [SIOCGIWRATE    - SIOCIWFIRST] = wl3501_get_rate,
1987         [SIOCGIWRTS     - SIOCIWFIRST] = wl3501_get_rts_threshold,
1988         [SIOCGIWFRAG    - SIOCIWFIRST] = wl3501_get_frag_threshold,
1989         [SIOCGIWTXPOW   - SIOCIWFIRST] = wl3501_get_txpow,
1990         [SIOCGIWRETRY   - SIOCIWFIRST] = wl3501_get_retry,
1991         [SIOCGIWENCODE  - SIOCIWFIRST] = wl3501_get_encode,
1992         [SIOCGIWPOWER   - SIOCIWFIRST] = wl3501_get_power,
1993 };
1994
1995 static const struct iw_handler_def wl3501_handler_def = {
1996         .num_standard   = sizeof(wl3501_handler) / sizeof(iw_handler),
1997         .standard       = (iw_handler *)wl3501_handler,
1998         .spy_offset     = offsetof(struct wl3501_card, spy_data),
1999 };
2000
2001 /**
2002  * wl3501_attach - creates an "instance" of the driver
2003  *
2004  * Creates an "instance" of the driver, allocating local data structures for
2005  * one device.  The device is registered with Card Services.
2006  *
2007  * The dev_link structure is initialized, but we don't actually configure the
2008  * card at this point -- we wait until we receive a card insertion event.
2009  */
2010 static dev_link_t *wl3501_attach(void)
2011 {
2012         client_reg_t client_reg;
2013         dev_link_t *link;
2014         struct net_device *dev;
2015         int ret, i;
2016
2017         /* Initialize the dev_link_t structure */
2018         link = kmalloc(sizeof(*link), GFP_KERNEL);
2019         if (!link)
2020                 goto out;
2021         memset(link, 0, sizeof(struct dev_link_t));
2022
2023         /* The io structure describes IO port mapping */
2024         link->io.NumPorts1      = 16;
2025         link->io.Attributes1    = IO_DATA_PATH_WIDTH_8;
2026         link->io.IOAddrLines    = 5;
2027
2028         /* Interrupt setup */
2029         link->irq.Attributes    = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
2030         link->irq.IRQInfo1      = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
2031         link->irq.IRQInfo2      = wl3501_irq_mask;
2032         if (wl3501_irq_list[0] != -1)
2033                 for (i = 0; i < 4; i++)
2034                         link->irq.IRQInfo2 |= 1 << wl3501_irq_list[i];
2035         link->irq.Handler = wl3501_interrupt;
2036
2037         /* General socket configuration */
2038         link->conf.Attributes   = CONF_ENABLE_IRQ;
2039         link->conf.Vcc          = 50;
2040         link->conf.IntType      = INT_MEMORY_AND_IO;
2041         link->conf.ConfigIndex  = 1;
2042         link->conf.Present      = PRESENT_OPTION;
2043
2044         dev = alloc_etherdev(sizeof(struct wl3501_card));
2045         if (!dev)
2046                 goto out_link;
2047         dev->open               = wl3501_open;
2048         dev->stop               = wl3501_close;
2049         dev->hard_start_xmit    = wl3501_hard_start_xmit;
2050         dev->tx_timeout         = wl3501_tx_timeout;
2051         dev->watchdog_timeo     = 5 * HZ;
2052         dev->get_stats          = wl3501_get_stats;
2053         dev->get_wireless_stats = wl3501_get_wireless_stats;
2054         dev->do_ioctl           = wl3501_ioctl;
2055         dev->wireless_handlers  = (struct iw_handler_def *)&wl3501_handler_def;
2056         netif_stop_queue(dev);
2057         link->priv = link->irq.Instance = dev;
2058
2059         /* Register with Card Services */
2060         link->next               = wl3501_dev_list;
2061         wl3501_dev_list          = link;
2062         client_reg.dev_info      = &wl3501_dev_info;
2063         client_reg.Attributes    = INFO_IO_CLIENT | INFO_CARD_SHARE;
2064         client_reg.EventMask     = CS_EVENT_CARD_INSERTION |
2065                                    CS_EVENT_RESET_PHYSICAL |
2066                                    CS_EVENT_CARD_RESET |
2067                                    CS_EVENT_CARD_REMOVAL |
2068                                    CS_EVENT_PM_SUSPEND |
2069                                    CS_EVENT_PM_RESUME;
2070         client_reg.event_handler = wl3501_event;
2071         client_reg.Version       = 0x0210;
2072         client_reg.event_callback_args.client_data = link;
2073         ret = pcmcia_register_client(&link->handle, &client_reg);
2074         if (ret) {
2075                 cs_error(link->handle, RegisterClient, ret);
2076                 wl3501_detach(link);
2077                 link = NULL;
2078         }
2079 out:
2080         return link;
2081 out_link:
2082         kfree(link);
2083         link = NULL;
2084         goto out;
2085 }
2086
2087 #define CS_CHECK(fn, ret) \
2088 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
2089
2090 /**
2091  * wl3501_config - configure the PCMCIA socket and make eth device available
2092  * @link - FILL_IN
2093  *
2094  * wl3501_config() is scheduled to run after a CARD_INSERTION event is
2095  * received, to configure the PCMCIA socket, and to make the ethernet device
2096  * available to the system.
2097  */
2098 static void wl3501_config(dev_link_t *link)
2099 {
2100         tuple_t tuple;
2101         cisparse_t parse;
2102         client_handle_t handle = link->handle;
2103         struct net_device *dev = link->priv;
2104         int i = 0, j, last_fn, last_ret;
2105         unsigned char bf[64];
2106         struct wl3501_card *this;
2107
2108         /* This reads the card's CONFIG tuple to find its config registers. */
2109         tuple.Attributes        = 0;
2110         tuple.DesiredTuple      = CISTPL_CONFIG;
2111         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
2112         tuple.TupleData         = bf;
2113         tuple.TupleDataMax      = sizeof(bf);
2114         tuple.TupleOffset       = 0;
2115         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
2116         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
2117         link->conf.ConfigBase   = parse.config.base;
2118         link->conf.Present      = parse.config.rmask[0];
2119
2120         /* Configure card */
2121         link->state |= DEV_CONFIG;
2122
2123         /* Try allocating IO ports.  This tries a few fixed addresses.  If you
2124          * want, you can also read the card's config table to pick addresses --
2125          * see the serial driver for an example. */
2126
2127         for (j = 0x280; j < 0x400; j += 0x20) {
2128                 /* The '^0x300' is so that we probe 0x300-0x3ff first, then
2129                  * 0x200-0x2ff, and so on, because this seems safer */
2130                 link->io.BasePort1 = j;
2131                 link->io.BasePort2 = link->io.BasePort1 + 0x10;
2132                 i = pcmcia_request_io(link->handle, &link->io);
2133                 if (i == CS_SUCCESS)
2134                         break;
2135         }
2136         if (i != CS_SUCCESS) {
2137                 cs_error(link->handle, RequestIO, i);
2138                 goto failed;
2139         }
2140
2141         /* Now allocate an interrupt line. Note that this does not actually
2142          * assign a handler to the interrupt. */
2143
2144         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
2145
2146         /* This actually configures the PCMCIA socket -- setting up the I/O
2147          * windows and the interrupt mapping.  */
2148
2149         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
2150
2151         dev->irq = link->irq.AssignedIRQ;
2152         dev->base_addr = link->io.BasePort1;
2153         if (register_netdev(dev)) {
2154                 printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
2155                 goto failed;
2156         }
2157
2158         SET_MODULE_OWNER(dev);
2159
2160         this = dev->priv;
2161         /*
2162          * At this point, the dev_node_t structure(s) should be initialized and
2163          * arranged in a linked list at link->dev.
2164          */
2165         link->dev = &this->node;
2166         link->state &= ~DEV_CONFIG_PENDING;
2167
2168         this->base_addr = dev->base_addr;
2169
2170         if (!wl3501_get_flash_mac_addr(this)) {
2171                 printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
2172                        dev->name);
2173                 goto failed;
2174         }
2175         strcpy(this->node.dev_name, dev->name);
2176
2177         /* print probe information */
2178         printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, MAC addr in flash ROM:",
2179                dev->name, this->base_addr, (int)dev->irq);
2180         for (i = 0; i < 6; i++) {
2181                 dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
2182                 printk("%c%02x", i ? ':' : ' ', dev->dev_addr[i]);
2183         }
2184         printk("\n");
2185         /*
2186          * Initialize card parameters - added by jss
2187          */
2188         this->net_type          = IW_MODE_INFRA;
2189         this->bss_cnt           = 0;
2190         this->join_sta_bss      = 0;
2191         this->adhoc_times       = 0;
2192         iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
2193                                  "ANY", 3);
2194         this->card_name[0]      = '\0';
2195         this->firmware_date[0]  = '\0';
2196         this->rssi              = 255;
2197         this->chan              = iw_default_channel(this->reg_domain);
2198         strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
2199         spin_lock_init(&this->lock);
2200         init_waitqueue_head(&this->wait);
2201         netif_start_queue(dev);
2202         goto out;
2203 cs_failed:
2204         cs_error(link->handle, last_fn, last_ret);
2205 failed:
2206         wl3501_release(link);
2207 out:
2208         return;
2209 }
2210
2211 /**
2212  * wl3501_release - unregister the net, release PCMCIA configuration
2213  * @arg - link
2214  *
2215  * After a card is removed, wl3501_release() will unregister the net device,
2216  * and release the PCMCIA configuration.  If the device is still open, this
2217  * will be postponed until it is closed.
2218  */
2219 static void wl3501_release(dev_link_t *link)
2220 {
2221         struct net_device *dev = link->priv;
2222
2223         /* If the device is currently in use, we won't release until it is
2224          * actually closed. */
2225         if (link->open) {
2226                 dprintk(1, "release postponed, '%s' still open",
2227                         link->dev->dev_name);
2228                 link->state |= DEV_STALE_CONFIG;
2229                 goto out;
2230         }
2231
2232         /* Unlink the device chain */
2233         if (link->dev) {
2234                 unregister_netdev(dev);
2235                 link->dev = NULL;
2236         }
2237
2238         /* Don't bother checking to see if these succeed or not */
2239         pcmcia_release_configuration(link->handle);
2240         pcmcia_release_io(link->handle, &link->io);
2241         pcmcia_release_irq(link->handle, &link->irq);
2242         link->state &= ~DEV_CONFIG;
2243
2244         if (link->state & DEV_STALE_CONFIG)
2245                 wl3501_detach(link);
2246 out:
2247         return;
2248 }
2249
2250 /**
2251  * wl3501_event - The card status event handler
2252  * @event - event
2253  * @pri - priority
2254  * @args - arguments for this event
2255  *
2256  * The card status event handler. Mostly, this schedules other stuff to run
2257  * after an event is received. A CARD_REMOVAL event also sets some flags to
2258  * discourage the net drivers from trying to talk to the card any more.
2259  *
2260  * When a CARD_REMOVAL event is received, we immediately set a flag to block
2261  * future accesses to this device. All the functions that actually access the
2262  * device should check this flag to make sure the card is still present.
2263  */
2264 static int wl3501_event(event_t event, int pri, event_callback_args_t *args)
2265 {
2266         dev_link_t *link = args->client_data;
2267         struct net_device *dev = link->priv;
2268
2269         switch (event) {
2270         case CS_EVENT_CARD_REMOVAL:
2271                 link->state &= ~DEV_PRESENT;
2272                 if (link->state & DEV_CONFIG) {
2273                         while (link->open > 0)
2274                                 wl3501_close(dev);
2275                         netif_device_detach(dev);
2276                         wl3501_release(link);
2277                 }
2278                 break;
2279         case CS_EVENT_CARD_INSERTION:
2280                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
2281                 wl3501_config(link);
2282                 break;
2283         case CS_EVENT_PM_SUSPEND:
2284                 link->state |= DEV_SUSPEND;
2285                 wl3501_pwr_mgmt(dev->priv, WL3501_SUSPEND);
2286                 /* Fall through... */
2287         case CS_EVENT_RESET_PHYSICAL:
2288                 if (link->state & DEV_CONFIG) {
2289                         if (link->open)
2290                                 netif_device_detach(dev);
2291                         pcmcia_release_configuration(link->handle);
2292                 }
2293                 break;
2294         case CS_EVENT_PM_RESUME:
2295                 link->state &= ~DEV_SUSPEND;
2296                 wl3501_pwr_mgmt(dev->priv, WL3501_RESUME);
2297                 /* Fall through... */
2298         case CS_EVENT_CARD_RESET:
2299                 if (link->state & DEV_CONFIG) {
2300                         pcmcia_request_configuration(link->handle, &link->conf);
2301                         if (link->open) {
2302                                 wl3501_reset(dev);
2303                                 netif_device_attach(dev);
2304                         }
2305                 }
2306                 break;
2307         }
2308         return 0;
2309 }
2310
2311 static struct pcmcia_driver wl3501_driver = {
2312         .owner          = THIS_MODULE,
2313         .drv            = {
2314                 .name   = "wl3501_cs",
2315         },
2316         .attach         = wl3501_attach,
2317         .detach         = wl3501_detach,
2318 };
2319
2320 static int __init wl3501_init_module(void)
2321 {
2322         return pcmcia_register_driver(&wl3501_driver);
2323 }
2324
2325 static void __exit wl3501_exit_module(void)
2326 {
2327         dprintk(0, ": unloading");
2328         pcmcia_unregister_driver(&wl3501_driver);
2329         while (wl3501_dev_list) {
2330                 /* Mark the device as non-existing to minimize calls to card */
2331                 wl3501_dev_list->state &= ~DEV_PRESENT;
2332                 if (wl3501_dev_list->state & DEV_CONFIG)
2333                         wl3501_release(wl3501_dev_list);
2334                 wl3501_detach(wl3501_dev_list);
2335         }
2336 }
2337
2338 module_init(wl3501_init_module);
2339 module_exit(wl3501_exit_module);
2340
2341 MODULE_PARM(wl3501_irq_mask, "i");
2342 MODULE_PARM(wl3501_irq_list, "1-4i");
2343 MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
2344               "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
2345               "Gustavo Niemeyer <niemeyer@conectiva.com>");
2346 MODULE_DESCRIPTION("Planet wl3501 wireless driver");
2347 MODULE_LICENSE("GPL");