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