VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / wireless / prism54 / isl_ioctl.c
1 /*
2  *  
3  *  Copyright (C) 2002 Intersil Americas Inc.
4  *            (C) 2003,2004 Aurelien Alleaume <slts@free.fr>
5  *            (C) 2003 Herbert Valerio Riedel <hvr@gnu.org>
6  *            (C) 2003 Luis R. Rodriguez <mcgrof@ruslug.rutgers.edu>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include <linux/version.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/if_arp.h>
27 #include <linux/pci.h>
28
29 #include <asm/uaccess.h>
30
31 #include "prismcompat.h"
32 #include "isl_ioctl.h"
33 #include "islpci_mgt.h"
34 #include "isl_oid.h"            /* additional types and defs for isl38xx fw */
35 #include "oid_mgt.h"
36
37 #include <net/iw_handler.h>     /* New driver API */
38
39 static int init_mode = CARD_DEFAULT_IW_MODE;
40 static int init_channel = CARD_DEFAULT_CHANNEL;
41 static int init_wep = CARD_DEFAULT_WEP;
42 static int init_filter = CARD_DEFAULT_FILTER;
43 static int init_authen = CARD_DEFAULT_AUTHEN;
44 static int init_dot1x = CARD_DEFAULT_DOT1X;
45 static int init_conformance = CARD_DEFAULT_CONFORMANCE;
46 static int init_mlme = CARD_DEFAULT_MLME_MODE;
47
48 module_param(init_mode, int, 0);
49 MODULE_PARM_DESC(init_mode,
50                  "Set card mode:\n0: Auto\n1: Ad-Hoc\n2: Managed Client (Default)\n3: Master / Access Point\n4: Repeater (Not supported yet)\n5: Secondary (Not supported yet)\n6: Monitor");
51
52 module_param(init_channel, int, 0);
53 MODULE_PARM_DESC(init_channel,
54                  "Check `iwpriv ethx channel` for available channels");
55
56 module_param(init_wep, int, 0);
57 module_param(init_filter, int, 0);
58
59 module_param(init_authen, int, 0);
60 MODULE_PARM_DESC(init_authen,
61                  "Authentication method. Can be of seven types:\n0 0x0000: None\n1 0x0001: DOT11_AUTH_OS (Default)\n2 0x0002: DOT11_AUTH_SK\n3 0x0003: DOT11_AUTH_BOTH");
62
63 module_param(init_dot1x, int, 0);
64 MODULE_PARM_DESC(init_dot1x,
65                  "\n0: None/not set     (Default)\n1: DOT11_DOT1X_AUTHENABLED\n2: DOT11_DOT1X_KEYTXENABLED");
66
67 module_param(init_mlme, int, 0);
68 MODULE_PARM_DESC(init_mlme,
69                  "Sets the MAC layer management entity (MLME) mode of operation,\n0: DOT11_MLME_AUTO (Default)\n1: DOT11_MLME_INTERMEDIATE\n2: DOT11_MLME_EXTENDED");
70
71 /**
72  * prism54_mib_mode_helper - MIB change mode helper function
73  * @mib: the &struct islpci_mib object to modify
74  * @iw_mode: new mode (%IW_MODE_*)
75  * 
76  *  This is a helper function, hence it does not lock. Make sure
77  *  caller deals with locking *if* necessary. This function sets the 
78  *  mode-dependent mib values and does the mapping of the Linux 
79  *  Wireless API modes to Device firmware modes. It also checks for 
80  *  correct valid Linux wireless modes. 
81  */
82 int
83 prism54_mib_mode_helper(islpci_private *priv, u32 iw_mode)
84 {
85         u32 config = INL_CONFIG_MANUALRUN;
86         u32 mode, bsstype;
87
88         /* For now, just catch early the Repeater and Secondary modes here */
89         if (iw_mode == IW_MODE_REPEAT || iw_mode == IW_MODE_SECOND) {
90                 printk(KERN_DEBUG
91                        "%s(): Sorry, Repeater mode and Secondary mode "
92                        "are not yet supported by this driver.\n", __FUNCTION__);
93                 return -EINVAL;
94         }
95
96         priv->iw_mode = iw_mode;
97
98         switch (iw_mode) {
99         case IW_MODE_AUTO:
100                 mode = INL_MODE_CLIENT;
101                 bsstype = DOT11_BSSTYPE_ANY;
102                 break;
103         case IW_MODE_ADHOC:
104                 mode = INL_MODE_CLIENT;
105                 bsstype = DOT11_BSSTYPE_IBSS;
106                 break;
107         case IW_MODE_INFRA:
108                 mode = INL_MODE_CLIENT;
109                 bsstype = DOT11_BSSTYPE_INFRA;
110                 break;
111         case IW_MODE_MASTER:
112                 mode = INL_MODE_AP;
113                 bsstype = DOT11_BSSTYPE_INFRA;
114                 break;
115         case IW_MODE_MONITOR:
116                 mode = INL_MODE_PROMISCUOUS;
117                 bsstype = DOT11_BSSTYPE_ANY;
118                 config |= INL_CONFIG_RXANNEX;
119                 break;
120         default:
121                 return -EINVAL;
122         }
123
124         if (init_wds)
125                 config |= INL_CONFIG_WDS;
126         mgt_set(priv, DOT11_OID_BSSTYPE, &bsstype);
127         mgt_set(priv, OID_INL_CONFIG, &config);
128         mgt_set(priv, OID_INL_MODE, &mode);
129
130         return 0;
131 }
132
133 /**
134  * prism54_mib_init - fill MIB cache with defaults
135  *
136  *  this function initializes the struct given as @mib with defaults,
137  *  of which many are retrieved from the global module parameter
138  *  variables.  
139  */
140
141 void
142 prism54_mib_init(islpci_private *priv)
143 {
144         u32 t;
145         struct obj_buffer psm_buffer = {
146                 .size = PSM_BUFFER_SIZE,
147                 .addr = priv->device_psm_buffer
148         };
149
150         mgt_set(priv, DOT11_OID_CHANNEL, &init_channel);
151         mgt_set(priv, DOT11_OID_AUTHENABLE, &init_authen);
152         mgt_set(priv, DOT11_OID_PRIVACYINVOKED, &init_wep);
153
154         mgt_set(priv, DOT11_OID_PSMBUFFER, &psm_buffer);
155         mgt_set(priv, DOT11_OID_EXUNENCRYPTED, &init_filter);
156         mgt_set(priv, DOT11_OID_DOT1XENABLE, &init_dot1x);
157         mgt_set(priv, DOT11_OID_MLMEAUTOLEVEL, &init_mlme);
158         mgt_set(priv, OID_INL_DOT11D_CONFORMANCE, &init_conformance);
159
160         t = 127;
161         mgt_set(priv, OID_INL_OUTPUTPOWER, &t);
162
163         /* Important: we are setting a default wireless mode and we are 
164          * forcing a valid one, so prism54_mib_mode_helper should just set
165          * mib values depending on what the wireless mode given is. No need
166          * for it save old values */
167         if (init_mode > IW_MODE_MONITOR || init_mode < IW_MODE_AUTO) {
168                 printk(KERN_DEBUG "%s(): You passed a non-valid init_mode. "
169                        "Using default mode\n", __FUNCTION__);
170                 init_mode = CARD_DEFAULT_IW_MODE;
171         }
172         /* This sets all of the mode-dependent values */
173         prism54_mib_mode_helper(priv, init_mode);
174 }
175
176 /* this will be executed outside of atomic context thanks to
177  * schedule_work(), thus we can as well use sleeping semaphore
178  * locking */
179 void
180 prism54_update_stats(islpci_private *priv)
181 {
182         char *data;
183         int j;
184         struct obj_bss bss, *bss2;
185         union oid_res_t r;
186
187         if (down_interruptible(&priv->stats_sem))
188                 return;
189
190 /* Noise floor.
191  * I'm not sure if the unit is dBm.
192  * Note : If we are not connected, this value seems to be irrelevant. */
193
194         mgt_get_request(priv, DOT11_OID_NOISEFLOOR, 0, NULL, &r);
195         priv->local_iwstatistics.qual.noise = r.u;
196
197 /* Get the rssi of the link. To do this we need to retrieve a bss. */
198
199         /* First get the MAC address of the AP we are associated with. */
200         mgt_get_request(priv, DOT11_OID_BSSID, 0, NULL, &r);
201         data = r.ptr;
202
203         /* copy this MAC to the bss */
204         memcpy(bss.address, data, 6);
205         kfree(data);
206
207         /* now ask for the corresponding bss */
208         j = mgt_get_request(priv, DOT11_OID_BSSFIND, 0, (void *) &bss, &r);
209         bss2 = r.ptr;
210         /* report the rssi and use it to calculate
211          *  link quality through a signal-noise
212          *  ratio */
213         priv->local_iwstatistics.qual.level = bss2->rssi;
214         priv->local_iwstatistics.qual.qual =
215             bss2->rssi - priv->iwstatistics.qual.noise;
216
217         kfree(bss2);
218
219         /* report that the stats are new */
220         priv->local_iwstatistics.qual.updated = 0x7;
221
222 /* Rx : unable to decrypt the MPDU */
223         mgt_get_request(priv, DOT11_OID_PRIVRXFAILED, 0, NULL, &r);
224         priv->local_iwstatistics.discard.code = r.u;
225
226 /* Tx : Max MAC retries num reached */
227         mgt_get_request(priv, DOT11_OID_MPDUTXFAILED, 0, NULL, &r);
228         priv->local_iwstatistics.discard.retries = r.u;
229
230         up(&priv->stats_sem);
231
232         return;
233 }
234
235 struct iw_statistics *
236 prism54_get_wireless_stats(struct net_device *ndev)
237 {
238         islpci_private *priv = netdev_priv(ndev);
239
240         /* If the stats are being updated return old data */
241         if (down_trylock(&priv->stats_sem) == 0) {
242                 memcpy(&priv->iwstatistics, &priv->local_iwstatistics,
243                        sizeof (struct iw_statistics));
244                 /* They won't be marked updated for the next time */
245                 priv->local_iwstatistics.qual.updated = 0;
246                 up(&priv->stats_sem);
247         } else
248                 priv->iwstatistics.qual.updated = 0;
249
250         /* Update our wireless stats, but do not schedule to often 
251          * (max 1 HZ) */
252         if ((priv->stats_timestamp == 0) ||
253             time_after(jiffies, priv->stats_timestamp + 1 * HZ)) {
254                 schedule_work(&priv->stats_work);
255                 priv->stats_timestamp = jiffies;
256         }
257
258         return &priv->iwstatistics;
259 }
260
261 static int
262 prism54_commit(struct net_device *ndev, struct iw_request_info *info,
263                char *cwrq, char *extra)
264 {
265         islpci_private *priv = netdev_priv(ndev);
266
267         /* simply re-set the last set SSID, this should commit most stuff */
268
269         /* Commit in Monitor mode is not necessary, also setting essid
270          * in Monitor mode does not make sense and isn't allowed for this
271          * device's firmware */
272         if (priv->iw_mode != IW_MODE_MONITOR)
273                 return mgt_set_request(priv, DOT11_OID_SSID, 0, NULL);
274         return 0;
275 }
276
277 static int
278 prism54_get_name(struct net_device *ndev, struct iw_request_info *info,
279                  char *cwrq, char *extra)
280 {
281         islpci_private *priv = netdev_priv(ndev);
282         char *capabilities;
283         union oid_res_t r;
284         int rvalue;
285
286         if (islpci_get_state(priv) < PRV_STATE_INIT) {
287                 strncpy(cwrq, "NOT READY!", IFNAMSIZ);
288                 return 0;
289         }
290         rvalue = mgt_get_request(priv, OID_INL_PHYCAPABILITIES, 0, NULL, &r);
291
292         switch (r.u) {
293         case INL_PHYCAP_5000MHZ:
294                 capabilities = "IEEE 802.11a/b/g";
295                 break;
296         case INL_PHYCAP_FAA:
297                 capabilities = "IEEE 802.11b/g - FAA Support";
298                 break;
299         case INL_PHYCAP_2400MHZ:
300         default:
301                 capabilities = "IEEE 802.11b/g";        /* Default */
302                 break;
303         }
304         strncpy(cwrq, capabilities, IFNAMSIZ);
305         return rvalue;
306 }
307
308 static int
309 prism54_set_freq(struct net_device *ndev, struct iw_request_info *info,
310                  struct iw_freq *fwrq, char *extra)
311 {
312         islpci_private *priv = netdev_priv(ndev);
313         int rvalue;
314         u32 c;
315
316         if (fwrq->m < 1000)
317                 /* we have a channel number */
318                 c = fwrq->m;
319         else
320                 c = (fwrq->e == 1) ? channel_of_freq(fwrq->m / 100000) : 0;
321
322         rvalue = c ? mgt_set_request(priv, DOT11_OID_CHANNEL, 0, &c) : -EINVAL;
323
324         /* Call commit handler */
325         return (rvalue ? rvalue : -EINPROGRESS);
326 }
327
328 static int
329 prism54_get_freq(struct net_device *ndev, struct iw_request_info *info,
330                  struct iw_freq *fwrq, char *extra)
331 {
332         islpci_private *priv = netdev_priv(ndev);
333         union oid_res_t r;
334         int rvalue;
335
336         rvalue = mgt_get_request(priv, DOT11_OID_CHANNEL, 0, NULL, &r);
337
338         fwrq->m = r.u;
339         fwrq->e = 0;
340
341         return rvalue;
342 }
343
344 static int
345 prism54_set_mode(struct net_device *ndev, struct iw_request_info *info,
346                  __u32 * uwrq, char *extra)
347 {
348         islpci_private *priv = netdev_priv(ndev);
349         u32 mlmeautolevel = CARD_DEFAULT_MLME_MODE;
350
351         /* Let's see if the user passed a valid Linux Wireless mode */
352         if (*uwrq > IW_MODE_MONITOR || *uwrq < IW_MODE_AUTO) {
353                 printk(KERN_DEBUG
354                        "%s: %s() You passed a non-valid init_mode.\n",
355                        priv->ndev->name, __FUNCTION__);
356                 return -EINVAL;
357         }
358
359         down_write(&priv->mib_sem);
360
361         if (prism54_mib_mode_helper(priv, *uwrq)) {
362                 up_write(&priv->mib_sem);
363                 return -EOPNOTSUPP;
364         }
365
366         /* the ACL code needs an intermediate mlmeautolevel. The wpa stuff an
367          * extended one.
368          */
369         if ((*uwrq == IW_MODE_MASTER) && (priv->acl.policy != MAC_POLICY_OPEN))
370                 mlmeautolevel = DOT11_MLME_INTERMEDIATE;
371         if (priv->wpa)
372                 mlmeautolevel = DOT11_MLME_EXTENDED;
373
374         mgt_set(priv, DOT11_OID_MLMEAUTOLEVEL, &mlmeautolevel);
375
376         mgt_commit(priv);
377         priv->ndev->type = (priv->iw_mode == IW_MODE_MONITOR)
378             ? priv->monitor_type : ARPHRD_ETHER;
379         up_write(&priv->mib_sem);
380
381         return 0;
382 }
383
384 /* Use mib cache */
385 static int
386 prism54_get_mode(struct net_device *ndev, struct iw_request_info *info,
387                  __u32 * uwrq, char *extra)
388 {
389         islpci_private *priv = netdev_priv(ndev);
390
391         BUG_ON((priv->iw_mode < IW_MODE_AUTO) || (priv->iw_mode >
392                                                   IW_MODE_MONITOR));
393         *uwrq = priv->iw_mode;
394
395         return 0;
396 }
397
398 /* we use DOT11_OID_EDTHRESHOLD. From what I guess the card will not try to
399  * emit data if (sensitivity > rssi - noise) (in dBm).
400  * prism54_set_sens does not seem to work.
401  */
402
403 static int
404 prism54_set_sens(struct net_device *ndev, struct iw_request_info *info,
405                  struct iw_param *vwrq, char *extra)
406 {
407         islpci_private *priv = netdev_priv(ndev);
408         u32 sens;
409
410         /* by default  the card sets this to 20. */
411         sens = vwrq->disabled ? 20 : vwrq->value;
412
413         return mgt_set_request(priv, DOT11_OID_EDTHRESHOLD, 0, &sens);
414 }
415
416 static int
417 prism54_get_sens(struct net_device *ndev, struct iw_request_info *info,
418                  struct iw_param *vwrq, char *extra)
419 {
420         islpci_private *priv = netdev_priv(ndev);
421         union oid_res_t r;
422         int rvalue;
423
424         rvalue = mgt_get_request(priv, DOT11_OID_EDTHRESHOLD, 0, NULL, &r);
425
426         vwrq->value = r.u;
427         vwrq->disabled = (vwrq->value == 0);
428         vwrq->fixed = 1;
429
430         return rvalue;
431 }
432
433 static int
434 prism54_get_range(struct net_device *ndev, struct iw_request_info *info,
435                   struct iw_point *dwrq, char *extra)
436 {
437         struct iw_range *range = (struct iw_range *) extra;
438         islpci_private *priv = netdev_priv(ndev);
439         u8 *data;
440         int i, m, rvalue;
441         struct obj_frequencies *freq;
442         union oid_res_t r;
443
444         memset(range, 0, sizeof (struct iw_range));
445         dwrq->length = sizeof (struct iw_range);
446
447         /* set the wireless extension version number */
448         range->we_version_source = SUPPORTED_WIRELESS_EXT;
449         range->we_version_compiled = WIRELESS_EXT;
450
451         /* Now the encoding capabilities */
452         range->num_encoding_sizes = 3;
453         /* 64(40) bits WEP */
454         range->encoding_size[0] = 5;
455         /* 128(104) bits WEP */
456         range->encoding_size[1] = 13;
457         /* 256 bits for WPA-PSK */
458         range->encoding_size[2] = 32;
459         /* 4 keys are allowed */
460         range->max_encoding_tokens = 4;
461
462         /* we don't know the quality range... */
463         range->max_qual.level = 0;
464         range->max_qual.noise = 0;
465         range->max_qual.qual = 0;
466         /* these value describe an average quality. Needs more tweaking... */
467         range->avg_qual.level = -80;    /* -80 dBm */
468         range->avg_qual.noise = 0;      /* don't know what to put here */
469         range->avg_qual.qual = 0;
470
471         range->sensitivity = 200;
472
473         /* retry limit capabilities */
474         range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
475         range->retry_flags = IW_RETRY_LIMIT;
476         range->r_time_flags = IW_RETRY_LIFETIME;
477
478         /* I don't know the range. Put stupid things here */
479         range->min_retry = 1;
480         range->max_retry = 65535;
481         range->min_r_time = 1024;
482         range->max_r_time = 65535 * 1024;
483
484         /* txpower is supported in dBm's */
485         range->txpower_capa = IW_TXPOW_DBM;
486
487         if (islpci_get_state(priv) < PRV_STATE_INIT)
488                 return 0;
489
490         /* Request the device for the supported frequencies
491          * not really relevant since some devices will report the 5 GHz band
492          * frequencies even if they don't support them.
493          */
494         rvalue =
495             mgt_get_request(priv, DOT11_OID_SUPPORTEDFREQUENCIES, 0, NULL, &r);
496         freq = r.ptr;
497
498         range->num_channels = freq->nr;
499         range->num_frequency = freq->nr;
500
501         m = min(IW_MAX_FREQUENCIES, (int) freq->nr);
502         for (i = 0; i < m; i++) {
503                 range->freq[i].m = freq->mhz[i];
504                 range->freq[i].e = 6;
505                 range->freq[i].i = channel_of_freq(freq->mhz[i]);
506         }
507         kfree(freq);
508
509         rvalue |= mgt_get_request(priv, DOT11_OID_SUPPORTEDRATES, 0, NULL, &r);
510         data = r.ptr;
511
512         /* We got an array of char. It is NULL terminated. */
513         i = 0;
514         while ((i < IW_MAX_BITRATES) && (*data != 0)) {
515                 /*       the result must be in bps. The card gives us 500Kbps */
516                 range->bitrate[i] = *data * 500000;
517                 i++;
518                 data++;
519         }
520         range->num_bitrates = i;
521         kfree(r.ptr);
522
523         return rvalue;
524 }
525
526 /* Set AP address*/
527
528 static int
529 prism54_set_wap(struct net_device *ndev, struct iw_request_info *info,
530                 struct sockaddr *awrq, char *extra)
531 {
532         islpci_private *priv = netdev_priv(ndev);
533         char bssid[6];
534         int rvalue;
535
536         if (awrq->sa_family != ARPHRD_ETHER)
537                 return -EINVAL;
538
539         /* prepare the structure for the set object */
540         memcpy(&bssid[0], awrq->sa_data, 6);
541
542         /* set the bssid -- does this make sense when in AP mode? */
543         rvalue = mgt_set_request(priv, DOT11_OID_BSSID, 0, &bssid);
544
545         return (rvalue ? rvalue : -EINPROGRESS);        /* Call commit handler */
546 }
547
548 /* get AP address*/
549
550 static int
551 prism54_get_wap(struct net_device *ndev, struct iw_request_info *info,
552                 struct sockaddr *awrq, char *extra)
553 {
554         islpci_private *priv = netdev_priv(ndev);
555         union oid_res_t r;
556         int rvalue;
557
558         rvalue = mgt_get_request(priv, DOT11_OID_BSSID, 0, NULL, &r);
559         memcpy(awrq->sa_data, r.ptr, 6);
560         awrq->sa_family = ARPHRD_ETHER;
561         kfree(r.ptr);
562
563         return rvalue;
564 }
565
566 static int
567 prism54_set_scan(struct net_device *dev, struct iw_request_info *info,
568                  struct iw_param *vwrq, char *extra)
569 {
570         /* hehe the device does this automagicaly */
571         return 0;
572 }
573
574 /* a little helper that will translate our data into a card independent
575  * format that the Wireless Tools will understand. This was inspired by
576  * the "Aironet driver for 4500 and 4800 series cards" (GPL)
577  */
578
579 static char *
580 prism54_translate_bss(struct net_device *ndev, char *current_ev,
581                       char *end_buf, struct obj_bss *bss, char noise)
582 {
583         struct iw_event iwe;    /* Temporary buffer */
584         short cap;
585         islpci_private *priv = netdev_priv(ndev);
586
587         /* The first entry must be the MAC address */
588         memcpy(iwe.u.ap_addr.sa_data, bss->address, 6);
589         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
590         iwe.cmd = SIOCGIWAP;
591         current_ev =
592             iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
593
594         /* The following entries will be displayed in the same order we give them */
595
596         /* The ESSID. */
597         iwe.u.data.length = bss->ssid.length;
598         iwe.u.data.flags = 1;
599         iwe.cmd = SIOCGIWESSID;
600         current_ev = iwe_stream_add_point(current_ev, end_buf,
601                                           &iwe, bss->ssid.octets);
602
603         /* Capabilities */
604 #define CAP_ESS 0x01
605 #define CAP_IBSS 0x02
606 #define CAP_CRYPT 0x10
607
608         /* Mode */
609         cap = bss->capinfo;
610         iwe.u.mode = 0;
611         if (cap & CAP_ESS)
612                 iwe.u.mode = IW_MODE_MASTER;
613         else if (cap & CAP_IBSS)
614                 iwe.u.mode = IW_MODE_ADHOC;
615         iwe.cmd = SIOCGIWMODE;
616         if (iwe.u.mode)
617                 current_ev =
618                     iwe_stream_add_event(current_ev, end_buf, &iwe,
619                                          IW_EV_UINT_LEN);
620
621         /* Encryption capability */
622         if (cap & CAP_CRYPT)
623                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
624         else
625                 iwe.u.data.flags = IW_ENCODE_DISABLED;
626         iwe.u.data.length = 0;
627         iwe.cmd = SIOCGIWENCODE;
628         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, NULL);
629
630         /* Add frequency. (short) bss->channel is the frequency in MHz */
631         iwe.u.freq.m = channel_of_freq(bss->channel);
632         iwe.u.freq.e = 0;
633         iwe.cmd = SIOCGIWFREQ;
634         current_ev =
635             iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
636
637         /* Add quality statistics */
638         iwe.u.qual.level = bss->rssi;
639         iwe.u.qual.noise = noise;
640         /* do a simple SNR for quality */
641         iwe.u.qual.qual = bss->rssi - noise;
642         iwe.cmd = IWEVQUAL;
643         current_ev =
644             iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
645
646         if (priv->wpa) {
647                 u8 wpa_ie[MAX_WPA_IE_LEN];
648                 char *buf, *p;
649                 size_t wpa_ie_len;
650                 int i;
651
652                 wpa_ie_len = prism54_wpa_ie_get(priv, bss->address, wpa_ie);
653                 if (wpa_ie_len > 0 &&
654                     (buf = kmalloc(wpa_ie_len * 2 + 10, GFP_ATOMIC))) {
655                         p = buf;
656                         p += sprintf(p, "wpa_ie=");
657                         for (i = 0; i < wpa_ie_len; i++) {
658                                 p += sprintf(p, "%02x", wpa_ie[i]);
659                         }
660                         memset(&iwe, 0, sizeof (iwe));
661                         iwe.cmd = IWEVCUSTOM;
662                         iwe.u.data.length = strlen(buf);
663                         current_ev = iwe_stream_add_point(current_ev, end_buf,
664                                                           &iwe, buf);
665                         kfree(buf);
666                 }
667         }
668         return current_ev;
669 }
670
671 int
672 prism54_get_scan(struct net_device *ndev, struct iw_request_info *info,
673                  struct iw_point *dwrq, char *extra)
674 {
675         islpci_private *priv = netdev_priv(ndev);
676         int i, rvalue;
677         struct obj_bsslist *bsslist;
678         u32 noise = 0;
679         char *current_ev = extra;
680         union oid_res_t r;
681
682         if (islpci_get_state(priv) < PRV_STATE_INIT) {
683                 /* device is not ready, fail gently */
684                 dwrq->length = 0;
685                 return 0;
686         }
687
688         /* first get the noise value. We will use it to report the link quality */
689         rvalue = mgt_get_request(priv, DOT11_OID_NOISEFLOOR, 0, NULL, &r);
690         noise = r.u;
691
692         /* Ask the device for a list of known bss. We can report at most
693          * IW_MAX_AP=64 to the range struct. But the device won't repport anything
694          * if you change the value of IWMAX_BSS=24.
695          */
696         rvalue |= mgt_get_request(priv, DOT11_OID_BSSLIST, 0, NULL, &r);
697         bsslist = r.ptr;
698
699         /* ok now, scan the list and translate its info */
700         for (i = 0; i < min(IW_MAX_AP, (int) bsslist->nr); i++)
701                 current_ev = prism54_translate_bss(ndev, current_ev,
702                                                    extra + IW_SCAN_MAX_DATA,
703                                                    &(bsslist->bsslist[i]),
704                                                    noise);
705         kfree(bsslist);
706         dwrq->length = (current_ev - extra);
707         dwrq->flags = 0;        /* todo */
708
709         return rvalue;
710 }
711
712 static int
713 prism54_set_essid(struct net_device *ndev, struct iw_request_info *info,
714                   struct iw_point *dwrq, char *extra)
715 {
716         islpci_private *priv = netdev_priv(ndev);
717         struct obj_ssid essid;
718
719         memset(essid.octets, 0, 33);
720
721         /* Check if we were asked for `any' */
722         if (dwrq->flags && dwrq->length) {
723                 if (dwrq->length > min(33, IW_ESSID_MAX_SIZE + 1))
724                         return -E2BIG;
725                 essid.length = dwrq->length - 1;
726                 memcpy(essid.octets, extra, dwrq->length);
727         } else
728                 essid.length = 0;
729
730         if (priv->iw_mode != IW_MODE_MONITOR)
731                 return mgt_set_request(priv, DOT11_OID_SSID, 0, &essid);
732
733         /* If in monitor mode, just save to mib */
734         mgt_set(priv, DOT11_OID_SSID, &essid);
735         return 0;
736
737 }
738
739 static int
740 prism54_get_essid(struct net_device *ndev, struct iw_request_info *info,
741                   struct iw_point *dwrq, char *extra)
742 {
743         islpci_private *priv = netdev_priv(ndev);
744         struct obj_ssid *essid;
745         union oid_res_t r;
746         int rvalue;
747
748         rvalue = mgt_get_request(priv, DOT11_OID_SSID, 0, NULL, &r);
749         essid = r.ptr;
750
751         if (essid->length) {
752                 dwrq->flags = 1;        /* set ESSID to ON for Wireless Extensions */
753                 /* if it is to big, trunk it */
754                 dwrq->length = min(IW_ESSID_MAX_SIZE, essid->length + 1);
755         } else {
756                 dwrq->flags = 0;
757                 dwrq->length = 0;
758         }
759         essid->octets[essid->length] = '\0';
760         memcpy(extra, essid->octets, dwrq->length);
761         kfree(essid);
762
763         return rvalue;
764 }
765
766 /* Provides no functionality, just completes the ioctl. In essence this is a 
767  * just a cosmetic ioctl.
768  */
769 static int
770 prism54_set_nick(struct net_device *ndev, struct iw_request_info *info,
771                  struct iw_point *dwrq, char *extra)
772 {
773         islpci_private *priv = netdev_priv(ndev);
774
775         if (dwrq->length > IW_ESSID_MAX_SIZE)
776                 return -E2BIG;
777
778         down_write(&priv->mib_sem);
779         memset(priv->nickname, 0, sizeof (priv->nickname));
780         memcpy(priv->nickname, extra, dwrq->length);
781         up_write(&priv->mib_sem);
782
783         return 0;
784 }
785
786 static int
787 prism54_get_nick(struct net_device *ndev, struct iw_request_info *info,
788                  struct iw_point *dwrq, char *extra)
789 {
790         islpci_private *priv = netdev_priv(ndev);
791
792         dwrq->length = 0;
793
794         down_read(&priv->mib_sem);
795         dwrq->length = strlen(priv->nickname) + 1;
796         memcpy(extra, priv->nickname, dwrq->length);
797         up_read(&priv->mib_sem);
798
799         return 0;
800 }
801
802 /* Set the allowed Bitrates */
803
804 static int
805 prism54_set_rate(struct net_device *ndev,
806                  struct iw_request_info *info,
807                  struct iw_param *vwrq, char *extra)
808 {
809
810         islpci_private *priv = netdev_priv(ndev);
811         u32 rate, profile;
812         char *data;
813         int ret, i;
814         union oid_res_t r;
815
816         if (vwrq->value == -1) {
817                 /* auto mode. No limit. */
818                 profile = 1;
819                 return mgt_set_request(priv, DOT11_OID_PROFILES, 0, &profile);
820         }
821
822         ret = mgt_get_request(priv, DOT11_OID_SUPPORTEDRATES, 0, NULL, &r);
823         if (ret) {
824                 kfree(r.ptr);
825                 return ret;
826         }
827
828         rate = (u32) (vwrq->value / 500000);
829         data = r.ptr;
830         i = 0;
831
832         while (data[i]) {
833                 if (rate && (data[i] == rate)) {
834                         break;
835                 }
836                 if (vwrq->value == i) {
837                         break;
838                 }
839                 data[i] |= 0x80;
840                 i++;
841         }
842
843         if (!data[i]) {
844                 kfree(r.ptr);
845                 return -EINVAL;
846         }
847
848         data[i] |= 0x80;
849         data[i + 1] = 0;
850
851         /* Now, check if we want a fixed or auto value */
852         if (vwrq->fixed) {
853                 data[0] = data[i];
854                 data[1] = 0;
855         }
856
857 /*
858         i = 0;
859         printk("prism54 rate: ");
860         while(data[i]) {
861                 printk("%u ", data[i]);
862                 i++;
863         }
864         printk("0\n");
865 */
866         profile = -1;
867         ret = mgt_set_request(priv, DOT11_OID_PROFILES, 0, &profile);
868         ret |= mgt_set_request(priv, DOT11_OID_EXTENDEDRATES, 0, data);
869         ret |= mgt_set_request(priv, DOT11_OID_RATES, 0, data);
870
871         kfree(r.ptr);
872
873         return ret;
874 }
875
876 /* Get the current bit rate */
877 static int
878 prism54_get_rate(struct net_device *ndev,
879                  struct iw_request_info *info,
880                  struct iw_param *vwrq, char *extra)
881 {
882         islpci_private *priv = netdev_priv(ndev);
883         int rvalue;
884         char *data;
885         union oid_res_t r;
886
887         /* Get the current bit rate */
888         if ((rvalue = mgt_get_request(priv, GEN_OID_LINKSTATE, 0, NULL, &r)))
889                 return rvalue;
890         vwrq->value = r.u * 500000;
891
892         /* request the device for the enabled rates */
893         rvalue = mgt_get_request(priv, DOT11_OID_RATES, 0, NULL, &r);
894         if (rvalue) {
895                 kfree(r.ptr);
896                 return rvalue;
897         }
898         data = r.ptr;
899         vwrq->fixed = (data[0] != 0) && (data[1] == 0);
900         kfree(r.ptr);
901
902         return 0;
903 }
904
905 static int
906 prism54_set_rts(struct net_device *ndev, struct iw_request_info *info,
907                 struct iw_param *vwrq, char *extra)
908 {
909         islpci_private *priv = netdev_priv(ndev);
910
911         return mgt_set_request(priv, DOT11_OID_RTSTHRESH, 0, &vwrq->value);
912 }
913
914 static int
915 prism54_get_rts(struct net_device *ndev, struct iw_request_info *info,
916                 struct iw_param *vwrq, char *extra)
917 {
918         islpci_private *priv = netdev_priv(ndev);
919         union oid_res_t r;
920         int rvalue;
921
922         /* get the rts threshold */
923         rvalue = mgt_get_request(priv, DOT11_OID_RTSTHRESH, 0, NULL, &r);
924         vwrq->value = r.u;
925
926         return rvalue;
927 }
928
929 static int
930 prism54_set_frag(struct net_device *ndev, struct iw_request_info *info,
931                  struct iw_param *vwrq, char *extra)
932 {
933         islpci_private *priv = netdev_priv(ndev);
934
935         return mgt_set_request(priv, DOT11_OID_FRAGTHRESH, 0, &vwrq->value);
936 }
937
938 static int
939 prism54_get_frag(struct net_device *ndev, struct iw_request_info *info,
940                  struct iw_param *vwrq, char *extra)
941 {
942         islpci_private *priv = netdev_priv(ndev);
943         union oid_res_t r;
944         int rvalue;
945
946         rvalue = mgt_get_request(priv, DOT11_OID_FRAGTHRESH, 0, NULL, &r);
947         vwrq->value = r.u;
948
949         return rvalue;
950 }
951
952 /* Here we have (min,max) = max retries for (small frames, big frames). Where
953  * big frame <=>  bigger than the rts threshold
954  * small frame <=>  smaller than the rts threshold
955  * This is not really the behavior expected by the wireless tool but it seems
956  * to be a common behavior in other drivers.
957  */
958
959 static int
960 prism54_set_retry(struct net_device *ndev, struct iw_request_info *info,
961                   struct iw_param *vwrq, char *extra)
962 {
963         islpci_private *priv = netdev_priv(ndev);
964         u32 slimit = 0, llimit = 0;     /* short and long limit */
965         u32 lifetime = 0;
966         int rvalue = 0;
967
968         if (vwrq->disabled)
969                 /* we cannot disable this feature */
970                 return -EINVAL;
971
972         if (vwrq->flags & IW_RETRY_LIMIT) {
973                 if (vwrq->flags & IW_RETRY_MIN)
974                         slimit = vwrq->value;
975                 else if (vwrq->flags & IW_RETRY_MAX)
976                         llimit = vwrq->value;
977                 else {
978                         /* we are asked to set both */
979                         slimit = vwrq->value;
980                         llimit = vwrq->value;
981                 }
982         }
983         if (vwrq->flags & IW_RETRY_LIFETIME)
984                 /* Wireless tools use us unit while the device uses 1024 us unit */
985                 lifetime = vwrq->value / 1024;
986
987         /* now set what is requested */
988         if (slimit)
989                 rvalue =
990                     mgt_set_request(priv, DOT11_OID_SHORTRETRIES, 0, &slimit);
991         if (llimit)
992                 rvalue |=
993                     mgt_set_request(priv, DOT11_OID_LONGRETRIES, 0, &llimit);
994         if (lifetime)
995                 rvalue |=
996                     mgt_set_request(priv, DOT11_OID_MAXTXLIFETIME, 0,
997                                     &lifetime);
998         return rvalue;
999 }
1000
1001 static int
1002 prism54_get_retry(struct net_device *ndev, struct iw_request_info *info,
1003                   struct iw_param *vwrq, char *extra)
1004 {
1005         islpci_private *priv = netdev_priv(ndev);
1006         union oid_res_t r;
1007         int rvalue = 0;
1008         vwrq->disabled = 0;     /* It cannot be disabled */
1009
1010         if ((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
1011                 /* we are asked for the life time */
1012                 rvalue =
1013                     mgt_get_request(priv, DOT11_OID_MAXTXLIFETIME, 0, NULL, &r);
1014                 vwrq->value = r.u * 1024;
1015                 vwrq->flags = IW_RETRY_LIFETIME;
1016         } else if ((vwrq->flags & IW_RETRY_MAX)) {
1017                 /* we are asked for the long retry limit */
1018                 rvalue |=
1019                     mgt_get_request(priv, DOT11_OID_LONGRETRIES, 0, NULL, &r);
1020                 vwrq->value = r.u;
1021                 vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1022         } else {
1023                 /* default. get the  short retry limit */
1024                 rvalue |=
1025                     mgt_get_request(priv, DOT11_OID_SHORTRETRIES, 0, NULL, &r);
1026                 vwrq->value = r.u;
1027                 vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
1028         }
1029
1030         return rvalue;
1031 }
1032
1033 static int
1034 prism54_set_encode(struct net_device *ndev, struct iw_request_info *info,
1035                    struct iw_point *dwrq, char *extra)
1036 {
1037         islpci_private *priv = netdev_priv(ndev);
1038         int rvalue = 0, force = 0;
1039         int authen = DOT11_AUTH_OS, invoke = 0, exunencrypt = 0;
1040         union oid_res_t r;
1041
1042         /* with the new API, it's impossible to get a NULL pointer.
1043          * New version of iwconfig set the IW_ENCODE_NOKEY flag
1044          * when no key is given, but older versions don't. */
1045
1046         if (dwrq->length > 0) {
1047                 /* we have a key to set */
1048                 int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
1049                 int current_index;
1050                 struct obj_key key = { DOT11_PRIV_WEP, 0, "" };
1051
1052                 /* get the current key index */
1053                 rvalue = mgt_get_request(priv, DOT11_OID_DEFKEYID, 0, NULL, &r);
1054                 current_index = r.u;
1055                 /* Verify that the key is not marked as invalid */
1056                 if (!(dwrq->flags & IW_ENCODE_NOKEY)) {
1057                         key.length = dwrq->length > sizeof (key.key) ?
1058                             sizeof (key.key) : dwrq->length;
1059                         memcpy(key.key, extra, key.length);
1060                         if (key.length == 32)
1061                                 /* we want WPA-PSK */
1062                                 key.type = DOT11_PRIV_TKIP;
1063                         if ((index < 0) || (index > 3))
1064                                 /* no index provided use the current one */
1065                                 index = current_index;
1066
1067                         /* now send the key to the card  */
1068                         rvalue |=
1069                             mgt_set_request(priv, DOT11_OID_DEFKEYX, index,
1070                                             &key);
1071                 }
1072                 /*
1073                  * If a valid key is set, encryption should be enabled 
1074                  * (user may turn it off later).
1075                  * This is also how "iwconfig ethX key on" works
1076                  */
1077                 if ((index == current_index) && (key.length > 0))
1078                         force = 1;
1079         } else {
1080                 int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
1081                 if ((index >= 0) && (index <= 3)) {
1082                         /* we want to set the key index */
1083                         rvalue |=
1084                             mgt_set_request(priv, DOT11_OID_DEFKEYID, 0,
1085                                             &index);
1086                 } else {
1087                         if (!dwrq->flags & IW_ENCODE_MODE) {
1088                                 /* we cannot do anything. Complain. */
1089                                 return -EINVAL;
1090                         }
1091                 }
1092         }
1093         /* now read the flags */
1094         if (dwrq->flags & IW_ENCODE_DISABLED) {
1095                 /* Encoding disabled, 
1096                  * authen = DOT11_AUTH_OS;
1097                  * invoke = 0;
1098                  * exunencrypt = 0; */
1099         }
1100         if (dwrq->flags & IW_ENCODE_OPEN)
1101                 /* Encode but accept non-encoded packets. No auth */
1102                 invoke = 1;
1103         if ((dwrq->flags & IW_ENCODE_RESTRICTED) || force) {
1104                 /* Refuse non-encoded packets. Auth */
1105                 authen = DOT11_AUTH_BOTH;
1106                 invoke = 1;
1107                 exunencrypt = 1;
1108         }
1109         /* do the change if requested  */
1110         if ((dwrq->flags & IW_ENCODE_MODE) || force) {
1111                 rvalue |=
1112                     mgt_set_request(priv, DOT11_OID_AUTHENABLE, 0, &authen);
1113                 rvalue |=
1114                     mgt_set_request(priv, DOT11_OID_PRIVACYINVOKED, 0, &invoke);
1115                 rvalue |=
1116                     mgt_set_request(priv, DOT11_OID_EXUNENCRYPTED, 0,
1117                                     &exunencrypt);
1118         }
1119         return rvalue;
1120 }
1121
1122 static int
1123 prism54_get_encode(struct net_device *ndev, struct iw_request_info *info,
1124                    struct iw_point *dwrq, char *extra)
1125 {
1126         islpci_private *priv = netdev_priv(ndev);
1127         struct obj_key *key;
1128         u32 devindex, index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
1129         u32 authen = 0, invoke = 0, exunencrypt = 0;
1130         int rvalue;
1131         union oid_res_t r;
1132
1133         /* first get the flags */
1134         rvalue = mgt_get_request(priv, DOT11_OID_AUTHENABLE, 0, NULL, &r);
1135         authen = r.u;
1136         rvalue |= mgt_get_request(priv, DOT11_OID_PRIVACYINVOKED, 0, NULL, &r);
1137         invoke = r.u;
1138         rvalue |= mgt_get_request(priv, DOT11_OID_EXUNENCRYPTED, 0, NULL, &r);
1139         exunencrypt = r.u;
1140
1141         if (invoke && (authen == DOT11_AUTH_BOTH) && exunencrypt)
1142                 dwrq->flags = IW_ENCODE_RESTRICTED;
1143         else if ((authen == DOT11_AUTH_OS) && !exunencrypt) {
1144                 if (invoke)
1145                         dwrq->flags = IW_ENCODE_OPEN;
1146                 else
1147                         dwrq->flags = IW_ENCODE_DISABLED;
1148         } else
1149                 /* The card should not work in this state */
1150                 dwrq->flags = 0;
1151
1152         /* get the current device key index */
1153         rvalue |= mgt_get_request(priv, DOT11_OID_DEFKEYID, 0, NULL, &r);
1154         devindex = r.u;
1155         /* Now get the key, return it */
1156         if ((index < 0) || (index > 3))
1157                 /* no index provided, use the current one */
1158                 index = devindex;
1159         rvalue |= mgt_get_request(priv, DOT11_OID_DEFKEYX, index, NULL, &r);
1160         key = r.ptr;
1161         dwrq->length = key->length;
1162         memcpy(extra, key->key, dwrq->length);
1163         kfree(key);
1164         /* return the used key index */
1165         dwrq->flags |= devindex + 1;
1166
1167         return rvalue;
1168 }
1169
1170 static int
1171 prism54_get_txpower(struct net_device *ndev, struct iw_request_info *info,
1172                     struct iw_param *vwrq, char *extra)
1173 {
1174         islpci_private *priv = netdev_priv(ndev);
1175         union oid_res_t r;
1176         int rvalue;
1177
1178         rvalue = mgt_get_request(priv, OID_INL_OUTPUTPOWER, 0, NULL, &r);
1179         /* intersil firmware operates in 0.25 dBm (1/4 dBm) */
1180         vwrq->value = (s32) r.u / 4;
1181         vwrq->fixed = 1;
1182         /* radio is not turned of
1183          * btw: how is possible to turn off only the radio 
1184          */
1185         vwrq->disabled = 0;
1186
1187         return rvalue;
1188 }
1189
1190 static int
1191 prism54_set_txpower(struct net_device *ndev, struct iw_request_info *info,
1192                     struct iw_param *vwrq, char *extra)
1193 {
1194         islpci_private *priv = netdev_priv(ndev);
1195         s32 u = vwrq->value;
1196
1197         /* intersil firmware operates in 0.25 dBm (1/4) */
1198         u *= 4;
1199         if (vwrq->disabled) {
1200                 /* don't know how to disable radio */
1201                 printk(KERN_DEBUG
1202                        "%s: %s() disabling radio is not yet supported.\n",
1203                        priv->ndev->name, __FUNCTION__);
1204                 return -ENOTSUPP;
1205         } else if (vwrq->fixed)
1206                 /* currently only fixed value is supported */
1207                 return mgt_set_request(priv, OID_INL_OUTPUTPOWER, 0, &u);
1208         else {
1209                 printk(KERN_DEBUG
1210                        "%s: %s() auto power will be implemented later.\n",
1211                        priv->ndev->name, __FUNCTION__);
1212                 return -ENOTSUPP;
1213         }
1214 }
1215
1216 static int
1217 prism54_reset(struct net_device *ndev, struct iw_request_info *info,
1218               __u32 * uwrq, char *extra)
1219 {
1220         islpci_reset(netdev_priv(ndev), 0);
1221
1222         return 0;
1223 }
1224
1225 static int
1226 prism54_get_oid(struct net_device *ndev, struct iw_request_info *info,
1227                 struct iw_point *dwrq, char *extra)
1228 {
1229         union oid_res_t r;
1230         int rvalue;
1231         enum oid_num_t n = dwrq->flags;
1232
1233         rvalue = mgt_get_request((islpci_private *) ndev->priv, n, 0, NULL, &r);
1234         dwrq->length = mgt_response_to_str(n, &r, extra);
1235         if ((isl_oid[n].flags & OID_FLAG_TYPE) != OID_TYPE_U32)
1236                 kfree(r.ptr);
1237         return rvalue;
1238 }
1239
1240 static int
1241 prism54_set_u32(struct net_device *ndev, struct iw_request_info *info,
1242                 __u32 * uwrq, char *extra)
1243 {
1244         u32 oid = uwrq[0], u = uwrq[1];
1245
1246         return mgt_set_request((islpci_private *) ndev->priv, oid, 0, &u);
1247 }
1248
1249 static int
1250 prism54_set_raw(struct net_device *ndev, struct iw_request_info *info,
1251                 struct iw_point *dwrq, char *extra)
1252 {
1253         u32 oid = dwrq->flags;
1254
1255         return mgt_set_request((islpci_private *) ndev->priv, oid, 0, extra);
1256 }
1257
1258 void
1259 prism54_acl_init(struct islpci_acl *acl)
1260 {
1261         sema_init(&acl->sem, 1);
1262         INIT_LIST_HEAD(&acl->mac_list);
1263         acl->size = 0;
1264         acl->policy = MAC_POLICY_OPEN;
1265 }
1266
1267 static void
1268 prism54_clear_mac(struct islpci_acl *acl)
1269 {
1270         struct list_head *ptr, *next;
1271         struct mac_entry *entry;
1272
1273         if (down_interruptible(&acl->sem))
1274                 return;
1275
1276         if (acl->size == 0) {
1277                 up(&acl->sem);
1278                 return;
1279         }
1280
1281         for (ptr = acl->mac_list.next, next = ptr->next;
1282              ptr != &acl->mac_list; ptr = next, next = ptr->next) {
1283                 entry = list_entry(ptr, struct mac_entry, _list);
1284                 list_del(ptr);
1285                 kfree(entry);
1286         }
1287         acl->size = 0;
1288         up(&acl->sem);
1289 }
1290
1291 void
1292 prism54_acl_clean(struct islpci_acl *acl)
1293 {
1294         prism54_clear_mac(acl);
1295 }
1296
1297 static int
1298 prism54_add_mac(struct net_device *ndev, struct iw_request_info *info,
1299                 struct sockaddr *awrq, char *extra)
1300 {
1301         islpci_private *priv = netdev_priv(ndev);
1302         struct islpci_acl *acl = &priv->acl;
1303         struct mac_entry *entry;
1304         struct sockaddr *addr = (struct sockaddr *) extra;
1305
1306         if (addr->sa_family != ARPHRD_ETHER)
1307                 return -EOPNOTSUPP;
1308
1309         entry = kmalloc(sizeof (struct mac_entry), GFP_KERNEL);
1310         if (entry == NULL)
1311                 return -ENOMEM;
1312
1313         memcpy(entry->addr, addr->sa_data, ETH_ALEN);
1314
1315         if (down_interruptible(&acl->sem)) {
1316                 kfree(entry);
1317                 return -ERESTARTSYS;
1318         }
1319         list_add_tail(&entry->_list, &acl->mac_list);
1320         acl->size++;
1321         up(&acl->sem);
1322
1323         return 0;
1324 }
1325
1326 static int
1327 prism54_del_mac(struct net_device *ndev, struct iw_request_info *info,
1328                 struct sockaddr *awrq, char *extra)
1329 {
1330         islpci_private *priv = netdev_priv(ndev);
1331         struct islpci_acl *acl = &priv->acl;
1332         struct mac_entry *entry;
1333         struct list_head *ptr;
1334         struct sockaddr *addr = (struct sockaddr *) extra;
1335
1336         if (addr->sa_family != ARPHRD_ETHER)
1337                 return -EOPNOTSUPP;
1338
1339         if (down_interruptible(&acl->sem))
1340                 return -ERESTARTSYS;
1341         for (ptr = acl->mac_list.next; ptr != &acl->mac_list; ptr = ptr->next) {
1342                 entry = list_entry(ptr, struct mac_entry, _list);
1343
1344                 if (memcmp(entry->addr, addr->sa_data, ETH_ALEN) == 0) {
1345                         list_del(ptr);
1346                         acl->size--;
1347                         kfree(entry);
1348                         up(&acl->sem);
1349                         return 0;
1350                 }
1351         }
1352         up(&acl->sem);
1353         return -EINVAL;
1354 }
1355
1356 static int
1357 prism54_get_mac(struct net_device *ndev, struct iw_request_info *info,
1358                 struct iw_point *dwrq, char *extra)
1359 {
1360         islpci_private *priv = netdev_priv(ndev);
1361         struct islpci_acl *acl = &priv->acl;
1362         struct mac_entry *entry;
1363         struct list_head *ptr;
1364         struct sockaddr *dst = (struct sockaddr *) extra;
1365
1366         dwrq->length = 0;
1367
1368         if (down_interruptible(&acl->sem))
1369                 return -ERESTARTSYS;
1370
1371         for (ptr = acl->mac_list.next; ptr != &acl->mac_list; ptr = ptr->next) {
1372                 entry = list_entry(ptr, struct mac_entry, _list);
1373
1374                 memcpy(dst->sa_data, entry->addr, ETH_ALEN);
1375                 dst->sa_family = ARPHRD_ETHER;
1376                 dwrq->length++;
1377                 dst++;
1378         }
1379         up(&acl->sem);
1380         return 0;
1381 }
1382
1383 /* Setting policy also clears the MAC acl, even if we don't change the defaut
1384  * policy
1385  */
1386
1387 static int
1388 prism54_set_policy(struct net_device *ndev, struct iw_request_info *info,
1389                    __u32 * uwrq, char *extra)
1390 {
1391         islpci_private *priv = netdev_priv(ndev);
1392         struct islpci_acl *acl = &priv->acl;
1393         u32 mlmeautolevel;
1394
1395         prism54_clear_mac(acl);
1396
1397         if ((*uwrq < MAC_POLICY_OPEN) || (*uwrq > MAC_POLICY_REJECT))
1398                 return -EINVAL;
1399
1400         down_write(&priv->mib_sem);
1401
1402         acl->policy = *uwrq;
1403
1404         /* the ACL code needs an intermediate mlmeautolevel */
1405         if ((priv->iw_mode == IW_MODE_MASTER) &&
1406             (acl->policy != MAC_POLICY_OPEN))
1407                 mlmeautolevel = DOT11_MLME_INTERMEDIATE;
1408         else
1409                 mlmeautolevel = CARD_DEFAULT_MLME_MODE;
1410         if (priv->wpa)
1411                 mlmeautolevel = DOT11_MLME_EXTENDED;
1412         mgt_set(priv, DOT11_OID_MLMEAUTOLEVEL, &mlmeautolevel);
1413         /* restart the card with our new policy */
1414         mgt_commit(priv);
1415         up_write(&priv->mib_sem);
1416
1417         return 0;
1418 }
1419
1420 static int
1421 prism54_get_policy(struct net_device *ndev, struct iw_request_info *info,
1422                    __u32 * uwrq, char *extra)
1423 {
1424         islpci_private *priv = netdev_priv(ndev);
1425         struct islpci_acl *acl = &priv->acl;
1426
1427         *uwrq = acl->policy;
1428
1429         return 0;
1430 }
1431
1432 /* Return 1 only if client should be accepted. */
1433
1434 static int
1435 prism54_mac_accept(struct islpci_acl *acl, char *mac)
1436 {
1437         struct list_head *ptr;
1438         struct mac_entry *entry;
1439         int res = 0;
1440
1441         if (down_interruptible(&acl->sem))
1442                 return -ERESTARTSYS;
1443
1444         if (acl->policy == MAC_POLICY_OPEN) {
1445                 up(&acl->sem);
1446                 return 1;
1447         }
1448
1449         for (ptr = acl->mac_list.next; ptr != &acl->mac_list; ptr = ptr->next) {
1450                 entry = list_entry(ptr, struct mac_entry, _list);
1451                 if (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
1452                         res = 1;
1453                         break;
1454                 }
1455         }
1456         res = (acl->policy == MAC_POLICY_ACCEPT) ? !res : res;
1457         up(&acl->sem);
1458
1459         return res;
1460 }
1461
1462 static int
1463 prism54_kick_all(struct net_device *ndev, struct iw_request_info *info,
1464                  struct iw_point *dwrq, char *extra)
1465 {
1466         struct obj_mlme *mlme;
1467         int rvalue;
1468
1469         mlme = kmalloc(sizeof (struct obj_mlme), GFP_KERNEL);
1470         if (mlme == NULL)
1471                 return -ENOMEM;
1472
1473         /* Tell the card to kick every client */
1474         mlme->id = 0;
1475         rvalue =
1476             mgt_set_request(netdev_priv(ndev), DOT11_OID_DISASSOCIATE, 0, mlme);
1477         kfree(mlme);
1478
1479         return rvalue;
1480 }
1481
1482 static int
1483 prism54_kick_mac(struct net_device *ndev, struct iw_request_info *info,
1484                  struct sockaddr *awrq, char *extra)
1485 {
1486         struct obj_mlme *mlme;
1487         struct sockaddr *addr = (struct sockaddr *) extra;
1488         int rvalue;
1489
1490         if (addr->sa_family != ARPHRD_ETHER)
1491                 return -EOPNOTSUPP;
1492
1493         mlme = kmalloc(sizeof (struct obj_mlme), GFP_KERNEL);
1494         if (mlme == NULL)
1495                 return -ENOMEM;
1496
1497         /* Tell the card to only kick the corresponding bastard */
1498         memcpy(mlme->address, addr->sa_data, ETH_ALEN);
1499         mlme->id = -1;
1500         rvalue =
1501             mgt_set_request(netdev_priv(ndev), DOT11_OID_DISASSOCIATE, 0, mlme);
1502
1503         kfree(mlme);
1504
1505         return rvalue;
1506 }
1507
1508 /* Translate a TRAP oid into a wireless event. Called in islpci_mgt_receive. */
1509
1510 static void
1511 format_event(islpci_private *priv, char *dest, const char *str,
1512              const struct obj_mlme *mlme, u16 *length, int error)
1513 {
1514         const u8 *a = mlme->address;
1515         int n = snprintf(dest, IW_CUSTOM_MAX,
1516                          "%s %s %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X %s (%2.2X)",
1517                          str,
1518                          ((priv->iw_mode == IW_MODE_MASTER) ? "from" : "to"),
1519                          a[0], a[1], a[2], a[3], a[4], a[5],
1520                          (error ? (mlme->code ? " : REJECTED " : " : ACCEPTED ")
1521                           : ""), mlme->code);
1522         BUG_ON(n > IW_CUSTOM_MAX);
1523         *length = n;
1524 }
1525
1526 static void
1527 send_formatted_event(islpci_private *priv, const char *str,
1528                      const struct obj_mlme *mlme, int error)
1529 {
1530         union iwreq_data wrqu;
1531
1532         wrqu.data.pointer = kmalloc(IW_CUSTOM_MAX, GFP_KERNEL);
1533         if (!wrqu.data.pointer)
1534                 return;
1535         wrqu.data.length = 0;
1536         format_event(priv, wrqu.data.pointer, str, mlme, &wrqu.data.length,
1537                      error);
1538         wireless_send_event(priv->ndev, IWEVCUSTOM, &wrqu, wrqu.data.pointer);
1539         kfree(wrqu.data.pointer);
1540 }
1541
1542 static void
1543 send_simple_event(islpci_private *priv, const char *str)
1544 {
1545         union iwreq_data wrqu;
1546         int n = strlen(str);
1547
1548         wrqu.data.pointer = kmalloc(IW_CUSTOM_MAX, GFP_KERNEL);
1549         if (!wrqu.data.pointer)
1550                 return;
1551         BUG_ON(n > IW_CUSTOM_MAX);
1552         wrqu.data.length = n;
1553         strcpy(wrqu.data.pointer, str);
1554         wireless_send_event(priv->ndev, IWEVCUSTOM, &wrqu, wrqu.data.pointer);
1555         kfree(wrqu.data.pointer);
1556 }
1557
1558 static void
1559 link_changed(struct net_device *ndev, u32 bitrate)
1560 {
1561         islpci_private *priv = netdev_priv(ndev);
1562
1563         if (bitrate) {
1564                 if (priv->iw_mode == IW_MODE_INFRA) {
1565                         union iwreq_data uwrq;
1566                         prism54_get_wap(ndev, NULL, (struct sockaddr *) &uwrq,
1567                                         NULL);
1568                         wireless_send_event(ndev, SIOCGIWAP, &uwrq, NULL);
1569                 } else
1570                         send_simple_event(netdev_priv(ndev),
1571                                           "Link established");
1572         } else
1573                 send_simple_event(netdev_priv(ndev), "Link lost");
1574 }
1575
1576 /* Beacon/ProbeResp payload header */
1577 struct ieee80211_beacon_phdr {
1578         u8 timestamp[8];
1579         u16 beacon_int;
1580         u16 capab_info;
1581 } __attribute__ ((packed));
1582
1583 #define WLAN_EID_GENERIC 0xdd
1584 static u8 wpa_oid[4] = { 0x00, 0x50, 0xf2, 1 };
1585
1586 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
1587 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
1588
1589 void
1590 prism54_wpa_ie_add(islpci_private *priv, u8 *bssid,
1591                    u8 *wpa_ie, size_t wpa_ie_len)
1592 {
1593         struct list_head *ptr;
1594         struct islpci_bss_wpa_ie *bss = NULL;
1595
1596         if (wpa_ie_len > MAX_WPA_IE_LEN)
1597                 wpa_ie_len = MAX_WPA_IE_LEN;
1598
1599         if (down_interruptible(&priv->wpa_sem))
1600                 return;
1601
1602         /* try to use existing entry */
1603         list_for_each(ptr, &priv->bss_wpa_list) {
1604                 bss = list_entry(ptr, struct islpci_bss_wpa_ie, list);
1605                 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1606                         list_move(&bss->list, &priv->bss_wpa_list);
1607                         break;
1608                 }
1609                 bss = NULL;
1610         }
1611
1612         if (bss == NULL) {
1613                 /* add a new BSS entry; if max number of entries is already
1614                  * reached, replace the least recently updated */
1615                 if (priv->num_bss_wpa >= MAX_BSS_WPA_IE_COUNT) {
1616                         bss = list_entry(priv->bss_wpa_list.prev,
1617                                          struct islpci_bss_wpa_ie, list);
1618                         list_del(&bss->list);
1619                 } else {
1620                         bss = kmalloc(sizeof (*bss), GFP_ATOMIC);
1621                         if (bss != NULL) {
1622                                 priv->num_bss_wpa++;
1623                                 memset(bss, 0, sizeof (*bss));
1624                         }
1625                 }
1626                 if (bss != NULL) {
1627                         memcpy(bss->bssid, bssid, ETH_ALEN);
1628                         list_add(&bss->list, &priv->bss_wpa_list);
1629                 }
1630         }
1631
1632         if (bss != NULL) {
1633                 memcpy(bss->wpa_ie, wpa_ie, wpa_ie_len);
1634                 bss->wpa_ie_len = wpa_ie_len;
1635                 bss->last_update = jiffies;
1636         } else {
1637                 printk(KERN_DEBUG "Failed to add BSS WPA entry for " MACSTR
1638                        "\n", MAC2STR(bssid));
1639         }
1640
1641         /* expire old entries from WPA list */
1642         while (priv->num_bss_wpa > 0) {
1643                 bss = list_entry(priv->bss_wpa_list.prev,
1644                                  struct islpci_bss_wpa_ie, list);
1645                 if (!time_after(jiffies, bss->last_update + 60 * HZ))
1646                         break;
1647
1648                 list_del(&bss->list);
1649                 priv->num_bss_wpa--;
1650                 kfree(bss);
1651         }
1652
1653         up(&priv->wpa_sem);
1654 }
1655
1656 size_t
1657 prism54_wpa_ie_get(islpci_private *priv, u8 *bssid, u8 *wpa_ie)
1658 {
1659         struct list_head *ptr;
1660         struct islpci_bss_wpa_ie *bss = NULL;
1661         size_t len = 0;
1662
1663         if (down_interruptible(&priv->wpa_sem))
1664                 return 0;
1665
1666         list_for_each(ptr, &priv->bss_wpa_list) {
1667                 bss = list_entry(ptr, struct islpci_bss_wpa_ie, list);
1668                 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
1669                         break;
1670                 bss = NULL;
1671         }
1672         if (bss) {
1673                 len = bss->wpa_ie_len;
1674                 memcpy(wpa_ie, bss->wpa_ie, len);
1675         }
1676         up(&priv->wpa_sem);
1677
1678         return len;
1679 }
1680
1681 void
1682 prism54_wpa_ie_init(islpci_private *priv)
1683 {
1684         INIT_LIST_HEAD(&priv->bss_wpa_list);
1685         sema_init(&priv->wpa_sem, 1);
1686 }
1687
1688 void
1689 prism54_wpa_ie_clean(islpci_private *priv)
1690 {
1691         struct list_head *ptr, *n;
1692
1693         list_for_each_safe(ptr, n, &priv->bss_wpa_list) {
1694                 struct islpci_bss_wpa_ie *bss;
1695                 bss = list_entry(ptr, struct islpci_bss_wpa_ie, list);
1696                 kfree(bss);
1697         }
1698 }
1699
1700 static void
1701 prism54_process_bss_data(islpci_private *priv, u32 oid, u8 *addr,
1702                          u8 *payload, size_t len)
1703 {
1704         struct ieee80211_beacon_phdr *hdr;
1705         u8 *pos, *end;
1706
1707         if (!priv->wpa)
1708                 return;
1709
1710         hdr = (struct ieee80211_beacon_phdr *) payload;
1711         pos = (u8 *) (hdr + 1);
1712         end = payload + len;
1713         while (pos < end) {
1714                 if (pos + 2 + pos[1] > end) {
1715                         printk(KERN_DEBUG "Parsing Beacon/ProbeResp failed "
1716                                "for " MACSTR "\n", MAC2STR(addr));
1717                         return;
1718                 }
1719                 if (pos[0] == WLAN_EID_GENERIC && pos[1] >= 4 &&
1720                     memcmp(pos + 2, wpa_oid, 4) == 0) {
1721                         prism54_wpa_ie_add(priv, addr, pos, pos[1] + 2);
1722                         return;
1723                 }
1724                 pos += 2 + pos[1];
1725         }
1726 }
1727
1728 static void
1729 handle_request(islpci_private *priv, struct obj_mlme *mlme, enum oid_num_t oid)
1730 {
1731         if (((mlme->state == DOT11_STATE_AUTHING) ||
1732              (mlme->state == DOT11_STATE_ASSOCING))
1733             && mgt_mlme_answer(priv)) {
1734                 /* Someone is requesting auth and we must respond. Just send back
1735                  * the trap with error code set accordingly.
1736                  */
1737                 mlme->code = prism54_mac_accept(&priv->acl,
1738                                                 mlme->address) ? 0 : 1;
1739                 mgt_set_request(priv, oid, 0, mlme);
1740         }
1741 }
1742
1743 int
1744 prism54_process_trap_helper(islpci_private *priv, enum oid_num_t oid,
1745                             char *data)
1746 {
1747         struct obj_mlme *mlme = (struct obj_mlme *) data;
1748         size_t len;
1749         u8 *payload, *pos = (u8 *) (mlme + 1);
1750
1751         len = pos[0] | (pos[1] << 8);   /* little endian data length */
1752         payload = pos + 2;
1753
1754         /* I think all trapable objects are listed here.
1755          * Some oids have a EX version. The difference is that they are emitted
1756          * in DOT11_MLME_EXTENDED mode (set with DOT11_OID_MLMEAUTOLEVEL)
1757          * with more info.
1758          * The few events already defined by the wireless tools are not really
1759          * suited. We use the more flexible custom event facility.
1760          */
1761
1762         /* I fear prism54_process_bss_data won't work with big endian data */
1763         if ((oid == DOT11_OID_BEACON) || (oid == DOT11_OID_PROBE))
1764                 prism54_process_bss_data(priv, oid, mlme->address,
1765                                          payload, len);
1766
1767         mgt_le_to_cpu(isl_oid[oid].flags & OID_FLAG_TYPE, (void *) mlme);
1768
1769         switch (oid) {
1770
1771         case GEN_OID_LINKSTATE:
1772                 link_changed(priv->ndev, (u32) *data);
1773                 break;
1774
1775         case DOT11_OID_MICFAILURE:
1776                 send_simple_event(priv, "Mic failure");
1777                 break;
1778
1779         case DOT11_OID_DEAUTHENTICATE:
1780                 send_formatted_event(priv, "DeAuthenticate request", mlme, 0);
1781                 break;
1782
1783         case DOT11_OID_AUTHENTICATE:
1784                 handle_request(priv, mlme, oid);
1785                 send_formatted_event(priv, "Authenticate request", mlme, 1);
1786                 break;
1787
1788         case DOT11_OID_DISASSOCIATE:
1789                 send_formatted_event(priv, "Disassociate request", mlme, 0);
1790                 break;
1791
1792         case DOT11_OID_ASSOCIATE:
1793                 handle_request(priv, mlme, oid);
1794                 send_formatted_event(priv, "Associate request", mlme, 1);
1795                 break;
1796
1797         case DOT11_OID_REASSOCIATE:
1798                 handle_request(priv, mlme, oid);
1799                 send_formatted_event(priv, "ReAssociate request", mlme, 1);
1800                 break;
1801
1802         case DOT11_OID_BEACON:
1803                 send_formatted_event(priv,
1804                                      "Received a beacon from an unkown AP",
1805                                      mlme, 0);
1806                 break;
1807
1808         case DOT11_OID_PROBE:
1809                 /* we received a probe from a client. */
1810                 send_formatted_event(priv, "Received a probe from client", mlme,
1811                                      0);
1812                 break;
1813
1814                 /* Note : "mlme" is actually a "struct obj_mlmeex *" here, but this
1815                  * is backward compatible layout-wise with "struct obj_mlme".
1816                  */
1817
1818         case DOT11_OID_DEAUTHENTICATEEX:
1819                 send_formatted_event(priv, "DeAuthenticate request", mlme, 0);
1820                 break;
1821
1822         case DOT11_OID_AUTHENTICATEEX:
1823                 handle_request(priv, mlme, oid);
1824                 send_formatted_event(priv, "Authenticate request", mlme, 1);
1825                 break;
1826
1827         case DOT11_OID_DISASSOCIATEEX:
1828                 send_formatted_event(priv, "Disassociate request", mlme, 0);
1829                 break;
1830
1831         case DOT11_OID_ASSOCIATEEX:
1832                 handle_request(priv, mlme, oid);
1833                 send_formatted_event(priv, "Associate request", mlme, 1);
1834                 break;
1835
1836         case DOT11_OID_REASSOCIATEEX:
1837                 handle_request(priv, mlme, oid);
1838                 send_formatted_event(priv, "Reassociate request", mlme, 1);
1839                 break;
1840
1841         default:
1842                 return -EINVAL;
1843         }
1844
1845         return 0;
1846 }
1847
1848 /*
1849  * Process a device trap.  This is called via schedule_work(), outside of
1850  * interrupt context, no locks held.
1851  */
1852 void
1853 prism54_process_trap(void *data)
1854 {
1855         struct islpci_mgmtframe *frame = data;
1856         struct net_device *ndev = frame->ndev;
1857         enum oid_num_t n = mgt_oidtonum(frame->header->oid);
1858
1859         if (n != OID_NUM_LAST)
1860                 prism54_process_trap_helper(netdev_priv(ndev), n, frame->data);
1861         islpci_mgt_release(frame);
1862 }
1863
1864 int
1865 prism54_set_mac_address(struct net_device *ndev, void *addr)
1866 {
1867         islpci_private *priv = netdev_priv(ndev);
1868         int ret;
1869
1870         if (ndev->addr_len != 6)
1871                 return -EINVAL;
1872         ret = mgt_set_request(priv, GEN_OID_MACADDRESS, 0,
1873                               &((struct sockaddr *) addr)->sa_data);
1874         if (!ret)
1875                 memcpy(priv->ndev->dev_addr,
1876                        &((struct sockaddr *) addr)->sa_data, 6);
1877
1878         return ret;
1879 }
1880
1881 int
1882 prism54_set_wpa(struct net_device *ndev, struct iw_request_info *info,
1883                 __u32 * uwrq, char *extra)
1884 {
1885         islpci_private *priv = netdev_priv(ndev);
1886
1887         down_write(&priv->mib_sem);
1888
1889         priv->wpa = *uwrq;
1890         if (priv->wpa) {
1891                 u32 l = DOT11_MLME_EXTENDED;
1892                 mgt_set(priv, DOT11_OID_MLMEAUTOLEVEL, &l);
1893         }
1894         /* restart the card with new level. Needed ? */
1895         mgt_commit(priv);
1896         up_write(&priv->mib_sem);
1897
1898         return 0;
1899 }
1900
1901 int
1902 prism54_get_wpa(struct net_device *ndev, struct iw_request_info *info,
1903                 __u32 * uwrq, char *extra)
1904 {
1905         islpci_private *priv = netdev_priv(ndev);
1906         *uwrq = priv->wpa;
1907         return 0;
1908 }
1909
1910 int
1911 prism54_set_prismhdr(struct net_device *ndev, struct iw_request_info *info,
1912                      __u32 * uwrq, char *extra)
1913 {
1914         islpci_private *priv = netdev_priv(ndev);
1915         priv->monitor_type =
1916             (*uwrq ? ARPHRD_IEEE80211_PRISM : ARPHRD_IEEE80211);
1917         if (priv->iw_mode == IW_MODE_MONITOR)
1918                 priv->ndev->type = priv->monitor_type;
1919
1920         return 0;
1921 }
1922
1923 int
1924 prism54_get_prismhdr(struct net_device *ndev, struct iw_request_info *info,
1925                      __u32 * uwrq, char *extra)
1926 {
1927         islpci_private *priv = netdev_priv(ndev);
1928         *uwrq = (priv->monitor_type == ARPHRD_IEEE80211_PRISM);
1929         return 0;
1930 }
1931
1932 int
1933 prism54_debug_oid(struct net_device *ndev, struct iw_request_info *info,
1934                   __u32 * uwrq, char *extra)
1935 {
1936         islpci_private *priv = netdev_priv(ndev);
1937
1938         priv->priv_oid = *uwrq;
1939         printk("%s: oid 0x%08X\n", ndev->name, *uwrq);
1940
1941         return 0;
1942 }
1943
1944 int
1945 prism54_debug_get_oid(struct net_device *ndev, struct iw_request_info *info,
1946                       struct iw_point *data, char *extra)
1947 {
1948         islpci_private *priv = netdev_priv(ndev);
1949         struct islpci_mgmtframe *response = NULL;
1950         int ret = -EIO;
1951
1952         printk("%s: get_oid 0x%08X\n", ndev->name, priv->priv_oid);
1953         data->length = 0;
1954
1955         if (islpci_get_state(priv) >= PRV_STATE_INIT) {
1956                 ret =
1957                     islpci_mgt_transaction(priv->ndev, PIMFOR_OP_GET,
1958                                            priv->priv_oid, extra, 256,
1959                                            &response);
1960                 printk("%s: ret: %i\n", ndev->name, ret);
1961                 if (ret || !response
1962                     || response->header->operation == PIMFOR_OP_ERROR) {
1963                         if (response) {
1964                                 islpci_mgt_release(response);
1965                         }
1966                         printk("%s: EIO\n", ndev->name);
1967                         ret = -EIO;
1968                 }
1969                 if (!ret) {
1970                         data->length = response->header->length;
1971                         memcpy(extra, response->data, data->length);
1972                         islpci_mgt_release(response);
1973                         printk("%s: len: %i\n", ndev->name, data->length);
1974                 }
1975         }
1976
1977         return ret;
1978 }
1979
1980 int
1981 prism54_debug_set_oid(struct net_device *ndev, struct iw_request_info *info,
1982                       struct iw_point *data, char *extra)
1983 {
1984         islpci_private *priv = netdev_priv(ndev);
1985         struct islpci_mgmtframe *response = NULL;
1986         int ret = 0, response_op = PIMFOR_OP_ERROR;
1987
1988         printk("%s: set_oid 0x%08X\tlen: %d\n", ndev->name, priv->priv_oid,
1989                data->length);
1990
1991         if (islpci_get_state(priv) >= PRV_STATE_INIT) {
1992                 ret =
1993                     islpci_mgt_transaction(priv->ndev, PIMFOR_OP_SET,
1994                                            priv->priv_oid, extra, data->length,
1995                                            &response);
1996                 printk("%s: ret: %i\n", ndev->name, ret);
1997                 if (ret || !response
1998                     || response->header->operation == PIMFOR_OP_ERROR) {
1999                         if (response) {
2000                                 islpci_mgt_release(response);
2001                         }
2002                         printk("%s: EIO\n", ndev->name);
2003                         ret = -EIO;
2004                 }
2005                 if (!ret) {
2006                         response_op = response->header->operation;
2007                         printk("%s: response_op: %i\n", ndev->name,
2008                                response_op);
2009                         islpci_mgt_release(response);
2010                 }
2011         }
2012
2013         return (ret ? ret : -EINPROGRESS);
2014 }
2015
2016 static int
2017 prism54_set_spy(struct net_device *ndev,
2018                 struct iw_request_info *info,
2019                 union iwreq_data *uwrq, char *extra)
2020 {
2021         islpci_private *priv = netdev_priv(ndev);
2022         u32 u, oid = OID_INL_CONFIG;
2023
2024         down_write(&priv->mib_sem);
2025         mgt_get(priv, OID_INL_CONFIG, &u);
2026
2027         if ((uwrq->data.length == 0) && (priv->spy_data.spy_number > 0))
2028                 /* disable spy */
2029                 u &= ~INL_CONFIG_RXANNEX;
2030         else if ((uwrq->data.length > 0) && (priv->spy_data.spy_number == 0))
2031                 /* enable spy */
2032                 u |= INL_CONFIG_RXANNEX;
2033
2034         mgt_set(priv, OID_INL_CONFIG, &u);
2035         mgt_commit_list(priv, &oid, 1);
2036         up_write(&priv->mib_sem);
2037
2038         return iw_handler_set_spy(ndev, info, uwrq, extra);
2039 }
2040
2041 static const iw_handler prism54_handler[] = {
2042         (iw_handler) prism54_commit,    /* SIOCSIWCOMMIT */
2043         (iw_handler) prism54_get_name,  /* SIOCGIWNAME */
2044         (iw_handler) NULL,      /* SIOCSIWNWID */
2045         (iw_handler) NULL,      /* SIOCGIWNWID */
2046         (iw_handler) prism54_set_freq,  /* SIOCSIWFREQ */
2047         (iw_handler) prism54_get_freq,  /* SIOCGIWFREQ */
2048         (iw_handler) prism54_set_mode,  /* SIOCSIWMODE */
2049         (iw_handler) prism54_get_mode,  /* SIOCGIWMODE */
2050         (iw_handler) prism54_set_sens,  /* SIOCSIWSENS */
2051         (iw_handler) prism54_get_sens,  /* SIOCGIWSENS */
2052         (iw_handler) NULL,      /* SIOCSIWRANGE */
2053         (iw_handler) prism54_get_range, /* SIOCGIWRANGE */
2054         (iw_handler) NULL,      /* SIOCSIWPRIV */
2055         (iw_handler) NULL,      /* SIOCGIWPRIV */
2056         (iw_handler) NULL,      /* SIOCSIWSTATS */
2057         (iw_handler) NULL,      /* SIOCGIWSTATS */
2058         prism54_set_spy,        /* SIOCSIWSPY */
2059         iw_handler_get_spy,     /* SIOCGIWSPY */
2060         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
2061         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
2062         (iw_handler) prism54_set_wap,   /* SIOCSIWAP */
2063         (iw_handler) prism54_get_wap,   /* SIOCGIWAP */
2064         (iw_handler) NULL,      /* -- hole -- */
2065         (iw_handler) NULL,      /* SIOCGIWAPLIST depreciated */
2066         (iw_handler) prism54_set_scan,  /* SIOCSIWSCAN */
2067         (iw_handler) prism54_get_scan,  /* SIOCGIWSCAN */
2068         (iw_handler) prism54_set_essid, /* SIOCSIWESSID */
2069         (iw_handler) prism54_get_essid, /* SIOCGIWESSID */
2070         (iw_handler) prism54_set_nick,  /* SIOCSIWNICKN */
2071         (iw_handler) prism54_get_nick,  /* SIOCGIWNICKN */
2072         (iw_handler) NULL,      /* -- hole -- */
2073         (iw_handler) NULL,      /* -- hole -- */
2074         (iw_handler) prism54_set_rate,  /* SIOCSIWRATE */
2075         (iw_handler) prism54_get_rate,  /* SIOCGIWRATE */
2076         (iw_handler) prism54_set_rts,   /* SIOCSIWRTS */
2077         (iw_handler) prism54_get_rts,   /* SIOCGIWRTS */
2078         (iw_handler) prism54_set_frag,  /* SIOCSIWFRAG */
2079         (iw_handler) prism54_get_frag,  /* SIOCGIWFRAG */
2080         (iw_handler) prism54_set_txpower,       /* SIOCSIWTXPOW */
2081         (iw_handler) prism54_get_txpower,       /* SIOCGIWTXPOW */
2082         (iw_handler) prism54_set_retry, /* SIOCSIWRETRY */
2083         (iw_handler) prism54_get_retry, /* SIOCGIWRETRY */
2084         (iw_handler) prism54_set_encode,        /* SIOCSIWENCODE */
2085         (iw_handler) prism54_get_encode,        /* SIOCGIWENCODE */
2086         (iw_handler) NULL,      /* SIOCSIWPOWER */
2087         (iw_handler) NULL,      /* SIOCGIWPOWER */
2088 };
2089
2090 /* The low order bit identify a SET (0) or a GET (1) ioctl.  */
2091
2092 #define PRISM54_RESET           SIOCIWFIRSTPRIV
2093 #define PRISM54_GET_POLICY      SIOCIWFIRSTPRIV+1
2094 #define PRISM54_SET_POLICY      SIOCIWFIRSTPRIV+2
2095 #define PRISM54_GET_MAC         SIOCIWFIRSTPRIV+3
2096 #define PRISM54_ADD_MAC         SIOCIWFIRSTPRIV+4
2097
2098 #define PRISM54_DEL_MAC         SIOCIWFIRSTPRIV+6
2099
2100 #define PRISM54_KICK_MAC        SIOCIWFIRSTPRIV+8
2101
2102 #define PRISM54_KICK_ALL        SIOCIWFIRSTPRIV+10
2103
2104 #define PRISM54_GET_WPA         SIOCIWFIRSTPRIV+11
2105 #define PRISM54_SET_WPA         SIOCIWFIRSTPRIV+12
2106
2107 #define PRISM54_DBG_OID         SIOCIWFIRSTPRIV+14
2108 #define PRISM54_DBG_GET_OID     SIOCIWFIRSTPRIV+15
2109 #define PRISM54_DBG_SET_OID     SIOCIWFIRSTPRIV+16
2110
2111 #define PRISM54_GET_OID         SIOCIWFIRSTPRIV+17
2112 #define PRISM54_SET_OID_U32     SIOCIWFIRSTPRIV+18
2113 #define PRISM54_SET_OID_STR     SIOCIWFIRSTPRIV+20
2114 #define PRISM54_SET_OID_ADDR    SIOCIWFIRSTPRIV+22
2115
2116 #define PRISM54_GET_PRISMHDR    SIOCIWFIRSTPRIV+23
2117 #define PRISM54_SET_PRISMHDR    SIOCIWFIRSTPRIV+24
2118
2119 #define IWPRIV_SET_U32(n,x)     { n, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "s_"x }
2120 #define IWPRIV_SET_SSID(n,x)    { n, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | 1, 0, "s_"x }
2121 #define IWPRIV_SET_ADDR(n,x)    { n, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "s_"x }
2122 #define IWPRIV_GET(n,x) { n, 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | PRIV_STR_SIZE, "g_"x }
2123
2124 #define IWPRIV_U32(n,x)         IWPRIV_SET_U32(n,x), IWPRIV_GET(n,x)
2125 #define IWPRIV_SSID(n,x)        IWPRIV_SET_SSID(n,x), IWPRIV_GET(n,x)
2126 #define IWPRIV_ADDR(n,x)        IWPRIV_SET_ADDR(n,x), IWPRIV_GET(n,x)
2127
2128 /* Note : limited to 128 private ioctls (wireless tools 26) */
2129
2130 static const struct iw_priv_args prism54_private_args[] = {
2131 /*{ cmd, set_args, get_args, name } */
2132         {PRISM54_RESET, 0, 0, "reset"},
2133         {PRISM54_GET_PRISMHDR, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2134          "get_prismhdr"},
2135         {PRISM54_SET_PRISMHDR, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0,
2136          "set_prismhdr"},
2137         {PRISM54_GET_POLICY, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2138          "getPolicy"},
2139         {PRISM54_SET_POLICY, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0,
2140          "setPolicy"},
2141         {PRISM54_GET_MAC, 0, IW_PRIV_TYPE_ADDR | 64, "getMac"},
2142         {PRISM54_ADD_MAC, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0,
2143          "addMac"},
2144         {PRISM54_DEL_MAC, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0,
2145          "delMac"},
2146         {PRISM54_KICK_MAC, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0,
2147          "kickMac"},
2148         {PRISM54_KICK_ALL, 0, 0, "kickAll"},
2149         {PRISM54_GET_WPA, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2150          "get_wpa"},
2151         {PRISM54_SET_WPA, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0,
2152          "set_wpa"},
2153         {PRISM54_DBG_OID, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0,
2154          "dbg_oid"},
2155         {PRISM54_DBG_GET_OID, 0, IW_PRIV_TYPE_BYTE | 256, "dbg_get_oid"},
2156         {PRISM54_DBG_SET_OID, IW_PRIV_TYPE_BYTE | 256, 0, "dbg_set_oid"},
2157         /* --- sub-ioctls handlers --- */
2158         {PRISM54_GET_OID,
2159          0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | PRIV_STR_SIZE, ""},
2160         {PRISM54_SET_OID_U32,
2161          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, ""},
2162         {PRISM54_SET_OID_STR,
2163          IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | 1, 0, ""},
2164         {PRISM54_SET_OID_ADDR,
2165          IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, ""},
2166         /* --- sub-ioctls definitions --- */
2167         IWPRIV_ADDR(GEN_OID_MACADDRESS, "addr"),
2168         IWPRIV_GET(GEN_OID_LINKSTATE, "linkstate"),
2169         IWPRIV_U32(DOT11_OID_BSSTYPE, "bsstype"),
2170         IWPRIV_ADDR(DOT11_OID_BSSID, "bssid"),
2171         IWPRIV_U32(DOT11_OID_STATE, "state"),
2172         IWPRIV_U32(DOT11_OID_AID, "aid"),
2173
2174         IWPRIV_SSID(DOT11_OID_SSIDOVERRIDE, "ssidoverride"),
2175
2176         IWPRIV_U32(DOT11_OID_MEDIUMLIMIT, "medlimit"),
2177         IWPRIV_U32(DOT11_OID_BEACONPERIOD, "beacon"),
2178         IWPRIV_U32(DOT11_OID_DTIMPERIOD, "dtimperiod"),
2179
2180         IWPRIV_U32(DOT11_OID_AUTHENABLE, "authenable"),
2181         IWPRIV_U32(DOT11_OID_PRIVACYINVOKED, "privinvok"),
2182         IWPRIV_U32(DOT11_OID_EXUNENCRYPTED, "exunencrypt"),
2183
2184         IWPRIV_U32(DOT11_OID_REKEYTHRESHOLD, "rekeythresh"),
2185
2186         IWPRIV_U32(DOT11_OID_MAXTXLIFETIME, "maxtxlife"),
2187         IWPRIV_U32(DOT11_OID_MAXRXLIFETIME, "maxrxlife"),
2188         IWPRIV_U32(DOT11_OID_ALOFT_FIXEDRATE, "fixedrate"),
2189         IWPRIV_U32(DOT11_OID_MAXFRAMEBURST, "frameburst"),
2190         IWPRIV_U32(DOT11_OID_PSM, "psm"),
2191
2192         IWPRIV_U32(DOT11_OID_BRIDGELOCAL, "bridge"),
2193         IWPRIV_U32(DOT11_OID_CLIENTS, "clients"),
2194         IWPRIV_U32(DOT11_OID_CLIENTSASSOCIATED, "clientassoc"),
2195         IWPRIV_U32(DOT11_OID_DOT1XENABLE, "dot1xenable"),
2196         IWPRIV_U32(DOT11_OID_ANTENNARX, "rxant"),
2197         IWPRIV_U32(DOT11_OID_ANTENNATX, "txant"),
2198         IWPRIV_U32(DOT11_OID_ANTENNADIVERSITY, "antdivers"),
2199         IWPRIV_U32(DOT11_OID_EDTHRESHOLD, "edthresh"),
2200         IWPRIV_U32(DOT11_OID_PREAMBLESETTINGS, "preamble"),
2201         IWPRIV_GET(DOT11_OID_RATES, "rates"),
2202         IWPRIV_U32(DOT11_OID_OUTPUTPOWER, ".11outpower"),
2203         IWPRIV_GET(DOT11_OID_SUPPORTEDRATES, "supprates"),
2204         IWPRIV_GET(DOT11_OID_SUPPORTEDFREQUENCIES, "suppfreq"),
2205
2206         IWPRIV_U32(DOT11_OID_NOISEFLOOR, "noisefloor"),
2207         IWPRIV_GET(DOT11_OID_FREQUENCYACTIVITY, "freqactivity"),
2208         IWPRIV_U32(DOT11_OID_NONERPPROTECTION, "nonerpprotec"),
2209         IWPRIV_U32(DOT11_OID_PROFILES, "profile"),
2210         IWPRIV_GET(DOT11_OID_EXTENDEDRATES, "extrates"),
2211         IWPRIV_U32(DOT11_OID_MLMEAUTOLEVEL, "mlmelevel"),
2212
2213         IWPRIV_GET(DOT11_OID_BSSS, "bsss"),
2214         IWPRIV_GET(DOT11_OID_BSSLIST, "bsslist"),
2215         IWPRIV_U32(OID_INL_MODE, "mode"),
2216         IWPRIV_U32(OID_INL_CONFIG, "config"),
2217         IWPRIV_U32(OID_INL_DOT11D_CONFORMANCE, ".11dconform"),
2218         IWPRIV_GET(OID_INL_PHYCAPABILITIES, "phycapa"),
2219         IWPRIV_U32(OID_INL_OUTPUTPOWER, "outpower"),
2220 };
2221
2222 static const iw_handler prism54_private_handler[] = {
2223         (iw_handler) prism54_reset,
2224         (iw_handler) prism54_get_policy,
2225         (iw_handler) prism54_set_policy,
2226         (iw_handler) prism54_get_mac,
2227         (iw_handler) prism54_add_mac,
2228         (iw_handler) NULL,
2229         (iw_handler) prism54_del_mac,
2230         (iw_handler) NULL,
2231         (iw_handler) prism54_kick_mac,
2232         (iw_handler) NULL,
2233         (iw_handler) prism54_kick_all,
2234         (iw_handler) prism54_get_wpa,
2235         (iw_handler) prism54_set_wpa,
2236         (iw_handler) NULL,
2237         (iw_handler) prism54_debug_oid,
2238         (iw_handler) prism54_debug_get_oid,
2239         (iw_handler) prism54_debug_set_oid,
2240         (iw_handler) prism54_get_oid,
2241         (iw_handler) prism54_set_u32,
2242         (iw_handler) NULL,
2243         (iw_handler) prism54_set_raw,
2244         (iw_handler) NULL,
2245         (iw_handler) prism54_set_raw,
2246         (iw_handler) prism54_get_prismhdr,
2247         (iw_handler) prism54_set_prismhdr,
2248 };
2249
2250 const struct iw_handler_def prism54_handler_def = {
2251         .num_standard = sizeof (prism54_handler) / sizeof (iw_handler),
2252         .num_private = sizeof (prism54_private_handler) / sizeof (iw_handler),
2253         .num_private_args =
2254             sizeof (prism54_private_args) / sizeof (struct iw_priv_args),
2255         .standard = (iw_handler *) prism54_handler,
2256         .private = (iw_handler *) prism54_private_handler,
2257         .private_args = (struct iw_priv_args *) prism54_private_args,
2258         .spy_offset = offsetof(islpci_private, spy_data),
2259 };
2260
2261 /* For ioctls that don't work with the new API */
2262
2263 int
2264 prism54_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2265 {
2266
2267         return -EOPNOTSUPP;
2268 }