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