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