Pull ec into release branch
[linux-2.6] / drivers / net / wireless / hostap / hostap_ioctl.c
1 /* ioctl() (mostly Linux Wireless Extensions) routines for Host AP driver */
2
3 #include <linux/types.h>
4 #include <linux/ethtool.h>
5 #include <net/ieee80211_crypt.h>
6
7 #include "hostap_wlan.h"
8 #include "hostap.h"
9 #include "hostap_ap.h"
10
11 static struct iw_statistics *hostap_get_wireless_stats(struct net_device *dev)
12 {
13         struct hostap_interface *iface;
14         local_info_t *local;
15         struct iw_statistics *wstats;
16
17         iface = netdev_priv(dev);
18         local = iface->local;
19
20         /* Why are we doing that ? Jean II */
21         if (iface->type != HOSTAP_INTERFACE_MAIN)
22                 return NULL;
23
24         wstats = &local->wstats;
25
26         wstats->status = 0;
27         wstats->discard.code =
28                 local->comm_tallies.rx_discards_wep_undecryptable;
29         wstats->discard.misc =
30                 local->comm_tallies.rx_fcs_errors +
31                 local->comm_tallies.rx_discards_no_buffer +
32                 local->comm_tallies.tx_discards_wrong_sa;
33
34         wstats->discard.retries =
35                 local->comm_tallies.tx_retry_limit_exceeded;
36         wstats->discard.fragment =
37                 local->comm_tallies.rx_message_in_bad_msg_fragments;
38
39         if (local->iw_mode != IW_MODE_MASTER &&
40             local->iw_mode != IW_MODE_REPEAT) {
41                 int update = 1;
42 #ifdef in_atomic
43                 /* RID reading might sleep and it must not be called in
44                  * interrupt context or while atomic. However, this
45                  * function seems to be called while atomic (at least in Linux
46                  * 2.5.59). Update signal quality values only if in suitable
47                  * context. Otherwise, previous values read from tick timer
48                  * will be used. */
49                 if (in_atomic())
50                         update = 0;
51 #endif /* in_atomic */
52
53                 if (update && prism2_update_comms_qual(dev) == 0)
54                         wstats->qual.updated = IW_QUAL_ALL_UPDATED |
55                                 IW_QUAL_DBM;
56
57                 wstats->qual.qual = local->comms_qual;
58                 wstats->qual.level = local->avg_signal;
59                 wstats->qual.noise = local->avg_noise;
60         } else {
61                 wstats->qual.qual = 0;
62                 wstats->qual.level = 0;
63                 wstats->qual.noise = 0;
64                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
65         }
66
67         return wstats;
68 }
69
70
71 static int prism2_get_datarates(struct net_device *dev, u8 *rates)
72 {
73         struct hostap_interface *iface;
74         local_info_t *local;
75         u8 buf[12];
76         int len;
77         u16 val;
78
79         iface = netdev_priv(dev);
80         local = iface->local;
81
82         len = local->func->get_rid(dev, HFA384X_RID_SUPPORTEDDATARATES, buf,
83                                    sizeof(buf), 0);
84         if (len < 2)
85                 return 0;
86
87         val = le16_to_cpu(*(u16 *) buf); /* string length */
88
89         if (len - 2 < val || val > 10)
90                 return 0;
91
92         memcpy(rates, buf + 2, val);
93         return val;
94 }
95
96
97 static int prism2_get_name(struct net_device *dev,
98                            struct iw_request_info *info,
99                            char *name, char *extra)
100 {
101         u8 rates[10];
102         int len, i, over2 = 0;
103
104         len = prism2_get_datarates(dev, rates);
105
106         for (i = 0; i < len; i++) {
107                 if (rates[i] == 0x0b || rates[i] == 0x16) {
108                         over2 = 1;
109                         break;
110                 }
111         }
112
113         strcpy(name, over2 ? "IEEE 802.11b" : "IEEE 802.11-DS");
114
115         return 0;
116 }
117
118
119 static void prism2_crypt_delayed_deinit(local_info_t *local,
120                                         struct ieee80211_crypt_data **crypt)
121 {
122         struct ieee80211_crypt_data *tmp;
123         unsigned long flags;
124
125         tmp = *crypt;
126         *crypt = NULL;
127
128         if (tmp == NULL)
129                 return;
130
131         /* must not run ops->deinit() while there may be pending encrypt or
132          * decrypt operations. Use a list of delayed deinits to avoid needing
133          * locking. */
134
135         spin_lock_irqsave(&local->lock, flags);
136         list_add(&tmp->list, &local->crypt_deinit_list);
137         if (!timer_pending(&local->crypt_deinit_timer)) {
138                 local->crypt_deinit_timer.expires = jiffies + HZ;
139                 add_timer(&local->crypt_deinit_timer);
140         }
141         spin_unlock_irqrestore(&local->lock, flags);
142 }
143
144
145 static int prism2_ioctl_siwencode(struct net_device *dev,
146                                   struct iw_request_info *info,
147                                   struct iw_point *erq, char *keybuf)
148 {
149         struct hostap_interface *iface;
150         local_info_t *local;
151         int i;
152         struct ieee80211_crypt_data **crypt;
153
154         iface = netdev_priv(dev);
155         local = iface->local;
156
157         i = erq->flags & IW_ENCODE_INDEX;
158         if (i < 1 || i > 4)
159                 i = local->tx_keyidx;
160         else
161                 i--;
162         if (i < 0 || i >= WEP_KEYS)
163                 return -EINVAL;
164
165         crypt = &local->crypt[i];
166
167         if (erq->flags & IW_ENCODE_DISABLED) {
168                 if (*crypt)
169                         prism2_crypt_delayed_deinit(local, crypt);
170                 goto done;
171         }
172
173         if (*crypt != NULL && (*crypt)->ops != NULL &&
174             strcmp((*crypt)->ops->name, "WEP") != 0) {
175                 /* changing to use WEP; deinit previously used algorithm */
176                 prism2_crypt_delayed_deinit(local, crypt);
177         }
178
179         if (*crypt == NULL) {
180                 struct ieee80211_crypt_data *new_crypt;
181
182                 /* take WEP into use */
183                 new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
184                                 GFP_KERNEL);
185                 if (new_crypt == NULL)
186                         return -ENOMEM;
187                 new_crypt->ops = ieee80211_get_crypto_ops("WEP");
188                 if (!new_crypt->ops) {
189                         request_module("ieee80211_crypt_wep");
190                         new_crypt->ops = ieee80211_get_crypto_ops("WEP");
191                 }
192                 if (new_crypt->ops)
193                         new_crypt->priv = new_crypt->ops->init(i);
194                 if (!new_crypt->ops || !new_crypt->priv) {
195                         kfree(new_crypt);
196                         new_crypt = NULL;
197
198                         printk(KERN_WARNING "%s: could not initialize WEP: "
199                                "load module hostap_crypt_wep.o\n",
200                                dev->name);
201                         return -EOPNOTSUPP;
202                 }
203                 *crypt = new_crypt;
204         }
205
206         if (erq->length > 0) {
207                 int len = erq->length <= 5 ? 5 : 13;
208                 int first = 1, j;
209                 if (len > erq->length)
210                         memset(keybuf + erq->length, 0, len - erq->length);
211                 (*crypt)->ops->set_key(keybuf, len, NULL, (*crypt)->priv);
212                 for (j = 0; j < WEP_KEYS; j++) {
213                         if (j != i && local->crypt[j]) {
214                                 first = 0;
215                                 break;
216                         }
217                 }
218                 if (first)
219                         local->tx_keyidx = i;
220         } else {
221                 /* No key data - just set the default TX key index */
222                 local->tx_keyidx = i;
223         }
224
225  done:
226         local->open_wep = erq->flags & IW_ENCODE_OPEN;
227
228         if (hostap_set_encryption(local)) {
229                 printk(KERN_DEBUG "%s: set_encryption failed\n", dev->name);
230                 return -EINVAL;
231         }
232
233         /* Do not reset port0 if card is in Managed mode since resetting will
234          * generate new IEEE 802.11 authentication which may end up in looping
235          * with IEEE 802.1X. Prism2 documentation seem to require port reset
236          * after WEP configuration. However, keys are apparently changed at
237          * least in Managed mode. */
238         if (local->iw_mode != IW_MODE_INFRA && local->func->reset_port(dev)) {
239                 printk(KERN_DEBUG "%s: reset_port failed\n", dev->name);
240                 return -EINVAL;
241         }
242
243         return 0;
244 }
245
246
247 static int prism2_ioctl_giwencode(struct net_device *dev,
248                                   struct iw_request_info *info,
249                                   struct iw_point *erq, char *key)
250 {
251         struct hostap_interface *iface;
252         local_info_t *local;
253         int i, len;
254         u16 val;
255         struct ieee80211_crypt_data *crypt;
256
257         iface = netdev_priv(dev);
258         local = iface->local;
259
260         i = erq->flags & IW_ENCODE_INDEX;
261         if (i < 1 || i > 4)
262                 i = local->tx_keyidx;
263         else
264                 i--;
265         if (i < 0 || i >= WEP_KEYS)
266                 return -EINVAL;
267
268         crypt = local->crypt[i];
269         erq->flags = i + 1;
270
271         if (crypt == NULL || crypt->ops == NULL) {
272                 erq->length = 0;
273                 erq->flags |= IW_ENCODE_DISABLED;
274                 return 0;
275         }
276
277         if (strcmp(crypt->ops->name, "WEP") != 0) {
278                 /* only WEP is supported with wireless extensions, so just
279                  * report that encryption is used */
280                 erq->length = 0;
281                 erq->flags |= IW_ENCODE_ENABLED;
282                 return 0;
283         }
284
285         /* Reads from HFA384X_RID_CNFDEFAULTKEY* return bogus values, so show
286          * the keys from driver buffer */
287         len = crypt->ops->get_key(key, WEP_KEY_LEN, NULL, crypt->priv);
288         erq->length = (len >= 0 ? len : 0);
289
290         if (local->func->get_rid(dev, HFA384X_RID_CNFWEPFLAGS, &val, 2, 1) < 0)
291         {
292                 printk("CNFWEPFLAGS reading failed\n");
293                 return -EOPNOTSUPP;
294         }
295         le16_to_cpus(&val);
296         if (val & HFA384X_WEPFLAGS_PRIVACYINVOKED)
297                 erq->flags |= IW_ENCODE_ENABLED;
298         else
299                 erq->flags |= IW_ENCODE_DISABLED;
300         if (val & HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED)
301                 erq->flags |= IW_ENCODE_RESTRICTED;
302         else
303                 erq->flags |= IW_ENCODE_OPEN;
304
305         return 0;
306 }
307
308
309 static int hostap_set_rate(struct net_device *dev)
310 {
311         struct hostap_interface *iface;
312         local_info_t *local;
313         int ret, basic_rates;
314
315         iface = netdev_priv(dev);
316         local = iface->local;
317
318         basic_rates = local->basic_rates & local->tx_rate_control;
319         if (!basic_rates || basic_rates != local->basic_rates) {
320                 printk(KERN_INFO "%s: updating basic rate set automatically "
321                        "to match with the new supported rate set\n",
322                        dev->name);
323                 if (!basic_rates)
324                         basic_rates = local->tx_rate_control;
325
326                 local->basic_rates = basic_rates;
327                 if (hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
328                                     basic_rates))
329                         printk(KERN_WARNING "%s: failed to set "
330                                "cnfBasicRates\n", dev->name);
331         }
332
333         ret = (hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
334                                local->tx_rate_control) ||
335                hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
336                                local->tx_rate_control) ||
337                local->func->reset_port(dev));
338
339         if (ret) {
340                 printk(KERN_WARNING "%s: TXRateControl/cnfSupportedRates "
341                        "setting to 0x%x failed\n",
342                        dev->name, local->tx_rate_control);
343         }
344
345         /* Update TX rate configuration for all STAs based on new operational
346          * rate set. */
347         hostap_update_rates(local);
348
349         return ret;
350 }
351
352
353 static int prism2_ioctl_siwrate(struct net_device *dev,
354                                 struct iw_request_info *info,
355                                 struct iw_param *rrq, char *extra)
356 {
357         struct hostap_interface *iface;
358         local_info_t *local;
359
360         iface = netdev_priv(dev);
361         local = iface->local;
362
363         if (rrq->fixed) {
364                 switch (rrq->value) {
365                 case 11000000:
366                         local->tx_rate_control = HFA384X_RATES_11MBPS;
367                         break;
368                 case 5500000:
369                         local->tx_rate_control = HFA384X_RATES_5MBPS;
370                         break;
371                 case 2000000:
372                         local->tx_rate_control = HFA384X_RATES_2MBPS;
373                         break;
374                 case 1000000:
375                         local->tx_rate_control = HFA384X_RATES_1MBPS;
376                         break;
377                 default:
378                         local->tx_rate_control = HFA384X_RATES_1MBPS |
379                                 HFA384X_RATES_2MBPS | HFA384X_RATES_5MBPS |
380                                 HFA384X_RATES_11MBPS;
381                         break;
382                 }
383         } else {
384                 switch (rrq->value) {
385                 case 11000000:
386                         local->tx_rate_control = HFA384X_RATES_1MBPS |
387                                 HFA384X_RATES_2MBPS | HFA384X_RATES_5MBPS |
388                                 HFA384X_RATES_11MBPS;
389                         break;
390                 case 5500000:
391                         local->tx_rate_control = HFA384X_RATES_1MBPS |
392                                 HFA384X_RATES_2MBPS | HFA384X_RATES_5MBPS;
393                         break;
394                 case 2000000:
395                         local->tx_rate_control = HFA384X_RATES_1MBPS |
396                                 HFA384X_RATES_2MBPS;
397                         break;
398                 case 1000000:
399                         local->tx_rate_control = HFA384X_RATES_1MBPS;
400                         break;
401                 default:
402                         local->tx_rate_control = HFA384X_RATES_1MBPS |
403                                 HFA384X_RATES_2MBPS | HFA384X_RATES_5MBPS |
404                                 HFA384X_RATES_11MBPS;
405                         break;
406                 }
407         }
408
409         return hostap_set_rate(dev);
410 }
411
412
413 static int prism2_ioctl_giwrate(struct net_device *dev,
414                                 struct iw_request_info *info,
415                                 struct iw_param *rrq, char *extra)
416 {
417         u16 val;
418         struct hostap_interface *iface;
419         local_info_t *local;
420         int ret = 0;
421
422         iface = netdev_priv(dev);
423         local = iface->local;
424
425         if (local->func->get_rid(dev, HFA384X_RID_TXRATECONTROL, &val, 2, 1) <
426             0)
427                 return -EINVAL;
428
429         if ((val & 0x1) && (val > 1))
430                 rrq->fixed = 0;
431         else
432                 rrq->fixed = 1;
433
434         if (local->iw_mode == IW_MODE_MASTER && local->ap != NULL &&
435             !local->fw_tx_rate_control) {
436                 /* HFA384X_RID_CURRENTTXRATE seems to always be 2 Mbps in
437                  * Host AP mode, so use the recorded TX rate of the last sent
438                  * frame */
439                 rrq->value = local->ap->last_tx_rate > 0 ?
440                         local->ap->last_tx_rate * 100000 : 11000000;
441                 return 0;
442         }
443
444         if (local->func->get_rid(dev, HFA384X_RID_CURRENTTXRATE, &val, 2, 1) <
445             0)
446                 return -EINVAL;
447
448         switch (val) {
449         case HFA384X_RATES_1MBPS:
450                 rrq->value = 1000000;
451                 break;
452         case HFA384X_RATES_2MBPS:
453                 rrq->value = 2000000;
454                 break;
455         case HFA384X_RATES_5MBPS:
456                 rrq->value = 5500000;
457                 break;
458         case HFA384X_RATES_11MBPS:
459                 rrq->value = 11000000;
460                 break;
461         default:
462                 /* should not happen */
463                 rrq->value = 11000000;
464                 ret = -EINVAL;
465                 break;
466         }
467
468         return ret;
469 }
470
471
472 static int prism2_ioctl_siwsens(struct net_device *dev,
473                                 struct iw_request_info *info,
474                                 struct iw_param *sens, char *extra)
475 {
476         struct hostap_interface *iface;
477         local_info_t *local;
478
479         iface = netdev_priv(dev);
480         local = iface->local;
481
482         /* Set the desired AP density */
483         if (sens->value < 1 || sens->value > 3)
484                 return -EINVAL;
485
486         if (hostap_set_word(dev, HFA384X_RID_CNFSYSTEMSCALE, sens->value) ||
487             local->func->reset_port(dev))
488                 return -EINVAL;
489
490         return 0;
491 }
492
493 static int prism2_ioctl_giwsens(struct net_device *dev,
494                                 struct iw_request_info *info,
495                                 struct iw_param *sens, char *extra)
496 {
497         struct hostap_interface *iface;
498         local_info_t *local;
499         u16 val;
500
501         iface = netdev_priv(dev);
502         local = iface->local;
503
504         /* Get the current AP density */
505         if (local->func->get_rid(dev, HFA384X_RID_CNFSYSTEMSCALE, &val, 2, 1) <
506             0)
507                 return -EINVAL;
508
509         sens->value = __le16_to_cpu(val);
510         sens->fixed = 1;
511
512         return 0;
513 }
514
515
516 /* Deprecated in new wireless extension API */
517 static int prism2_ioctl_giwaplist(struct net_device *dev,
518                                   struct iw_request_info *info,
519                                   struct iw_point *data, char *extra)
520 {
521         struct hostap_interface *iface;
522         local_info_t *local;
523         struct sockaddr *addr;
524         struct iw_quality *qual;
525
526         iface = netdev_priv(dev);
527         local = iface->local;
528
529         if (local->iw_mode != IW_MODE_MASTER) {
530                 printk(KERN_DEBUG "SIOCGIWAPLIST is currently only supported "
531                        "in Host AP mode\n");
532                 data->length = 0;
533                 return -EOPNOTSUPP;
534         }
535
536         addr = kmalloc(sizeof(struct sockaddr) * IW_MAX_AP, GFP_KERNEL);
537         qual = kmalloc(sizeof(struct iw_quality) * IW_MAX_AP, GFP_KERNEL);
538         if (addr == NULL || qual == NULL) {
539                 kfree(addr);
540                 kfree(qual);
541                 data->length = 0;
542                 return -ENOMEM;
543         }
544
545         data->length = prism2_ap_get_sta_qual(local, addr, qual, IW_MAX_AP, 1);
546
547         memcpy(extra, &addr, sizeof(struct sockaddr) * data->length);
548         data->flags = 1; /* has quality information */
549         memcpy(extra + sizeof(struct sockaddr) * data->length, &qual,
550                sizeof(struct iw_quality) * data->length);
551
552         kfree(addr);
553         kfree(qual);
554         return 0;
555 }
556
557
558 static int prism2_ioctl_siwrts(struct net_device *dev,
559                                struct iw_request_info *info,
560                                struct iw_param *rts, char *extra)
561 {
562         struct hostap_interface *iface;
563         local_info_t *local;
564         u16 val;
565
566         iface = netdev_priv(dev);
567         local = iface->local;
568
569         if (rts->disabled)
570                 val = __constant_cpu_to_le16(2347);
571         else if (rts->value < 0 || rts->value > 2347)
572                 return -EINVAL;
573         else
574                 val = __cpu_to_le16(rts->value);
575
576         if (local->func->set_rid(dev, HFA384X_RID_RTSTHRESHOLD, &val, 2) ||
577             local->func->reset_port(dev))
578                 return -EINVAL;
579
580         local->rts_threshold = rts->value;
581
582         return 0;
583 }
584
585 static int prism2_ioctl_giwrts(struct net_device *dev,
586                                struct iw_request_info *info,
587                                struct iw_param *rts, char *extra)
588 {
589         struct hostap_interface *iface;
590         local_info_t *local;
591         u16 val;
592
593         iface = netdev_priv(dev);
594         local = iface->local;
595
596         if (local->func->get_rid(dev, HFA384X_RID_RTSTHRESHOLD, &val, 2, 1) <
597             0)
598                 return -EINVAL;
599
600         rts->value = __le16_to_cpu(val);
601         rts->disabled = (rts->value == 2347);
602         rts->fixed = 1;
603
604         return 0;
605 }
606
607
608 static int prism2_ioctl_siwfrag(struct net_device *dev,
609                                 struct iw_request_info *info,
610                                 struct iw_param *rts, char *extra)
611 {
612         struct hostap_interface *iface;
613         local_info_t *local;
614         u16 val;
615
616         iface = netdev_priv(dev);
617         local = iface->local;
618
619         if (rts->disabled)
620                 val = __constant_cpu_to_le16(2346);
621         else if (rts->value < 256 || rts->value > 2346)
622                 return -EINVAL;
623         else
624                 val = __cpu_to_le16(rts->value & ~0x1); /* even numbers only */
625
626         local->fragm_threshold = rts->value & ~0x1;
627         if (local->func->set_rid(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD, &val,
628                                  2)
629             || local->func->reset_port(dev))
630                 return -EINVAL;
631
632         return 0;
633 }
634
635 static int prism2_ioctl_giwfrag(struct net_device *dev,
636                                 struct iw_request_info *info,
637                                 struct iw_param *rts, char *extra)
638 {
639         struct hostap_interface *iface;
640         local_info_t *local;
641         u16 val;
642
643         iface = netdev_priv(dev);
644         local = iface->local;
645
646         if (local->func->get_rid(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
647                                  &val, 2, 1) < 0)
648                 return -EINVAL;
649
650         rts->value = __le16_to_cpu(val);
651         rts->disabled = (rts->value == 2346);
652         rts->fixed = 1;
653
654         return 0;
655 }
656
657
658 #ifndef PRISM2_NO_STATION_MODES
659 static int hostap_join_ap(struct net_device *dev)
660 {
661         struct hostap_interface *iface;
662         local_info_t *local;
663         struct hfa384x_join_request req;
664         unsigned long flags;
665         int i;
666         struct hfa384x_hostscan_result *entry;
667
668         iface = netdev_priv(dev);
669         local = iface->local;
670
671         memcpy(req.bssid, local->preferred_ap, ETH_ALEN);
672         req.channel = 0;
673
674         spin_lock_irqsave(&local->lock, flags);
675         for (i = 0; i < local->last_scan_results_count; i++) {
676                 if (!local->last_scan_results)
677                         break;
678                 entry = &local->last_scan_results[i];
679                 if (memcmp(local->preferred_ap, entry->bssid, ETH_ALEN) == 0) {
680                         req.channel = entry->chid;
681                         break;
682                 }
683         }
684         spin_unlock_irqrestore(&local->lock, flags);
685
686         if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
687                                  sizeof(req))) {
688                 printk(KERN_DEBUG "%s: JoinRequest " MACSTR
689                        " failed\n",
690                        dev->name, MAC2STR(local->preferred_ap));
691                 return -1;
692         }
693
694         printk(KERN_DEBUG "%s: Trying to join BSSID " MACSTR "\n",
695                dev->name, MAC2STR(local->preferred_ap));
696
697         return 0;
698 }
699 #endif /* PRISM2_NO_STATION_MODES */
700
701
702 static int prism2_ioctl_siwap(struct net_device *dev,
703                               struct iw_request_info *info,
704                               struct sockaddr *ap_addr, char *extra)
705 {
706 #ifdef PRISM2_NO_STATION_MODES
707         return -EOPNOTSUPP;
708 #else /* PRISM2_NO_STATION_MODES */
709         struct hostap_interface *iface;
710         local_info_t *local;
711
712         iface = netdev_priv(dev);
713         local = iface->local;
714
715         memcpy(local->preferred_ap, &ap_addr->sa_data, ETH_ALEN);
716
717         if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA) {
718                 struct hfa384x_scan_request scan_req;
719                 memset(&scan_req, 0, sizeof(scan_req));
720                 scan_req.channel_list = __constant_cpu_to_le16(0x3fff);
721                 scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
722                 if (local->func->set_rid(dev, HFA384X_RID_SCANREQUEST,
723                                          &scan_req, sizeof(scan_req))) {
724                         printk(KERN_DEBUG "%s: ScanResults request failed - "
725                                "preferred AP delayed to next unsolicited "
726                                "scan\n", dev->name);
727                 }
728         } else if (local->host_roaming == 2 &&
729                    local->iw_mode == IW_MODE_INFRA) {
730                 if (hostap_join_ap(dev))
731                         return -EINVAL;
732         } else {
733                 printk(KERN_DEBUG "%s: Preferred AP (SIOCSIWAP) is used only "
734                        "in Managed mode when host_roaming is enabled\n",
735                        dev->name);
736         }
737
738         return 0;
739 #endif /* PRISM2_NO_STATION_MODES */
740 }
741
742 static int prism2_ioctl_giwap(struct net_device *dev,
743                               struct iw_request_info *info,
744                               struct sockaddr *ap_addr, char *extra)
745 {
746         struct hostap_interface *iface;
747         local_info_t *local;
748
749         iface = netdev_priv(dev);
750         local = iface->local;
751
752         ap_addr->sa_family = ARPHRD_ETHER;
753         switch (iface->type) {
754         case HOSTAP_INTERFACE_AP:
755                 memcpy(&ap_addr->sa_data, dev->dev_addr, ETH_ALEN);
756                 break;
757         case HOSTAP_INTERFACE_STA:
758                 memcpy(&ap_addr->sa_data, local->assoc_ap_addr, ETH_ALEN);
759                 break;
760         case HOSTAP_INTERFACE_WDS:
761                 memcpy(&ap_addr->sa_data, iface->u.wds.remote_addr, ETH_ALEN);
762                 break;
763         default:
764                 if (local->func->get_rid(dev, HFA384X_RID_CURRENTBSSID,
765                                          &ap_addr->sa_data, ETH_ALEN, 1) < 0)
766                         return -EOPNOTSUPP;
767
768                 /* local->bssid is also updated in LinkStatus handler when in
769                  * station mode */
770                 memcpy(local->bssid, &ap_addr->sa_data, ETH_ALEN);
771                 break;
772         }
773
774         return 0;
775 }
776
777
778 static int prism2_ioctl_siwnickn(struct net_device *dev,
779                                  struct iw_request_info *info,
780                                  struct iw_point *data, char *nickname)
781 {
782         struct hostap_interface *iface;
783         local_info_t *local;
784
785         iface = netdev_priv(dev);
786         local = iface->local;
787
788         memset(local->name, 0, sizeof(local->name));
789         memcpy(local->name, nickname, data->length);
790         local->name_set = 1;
791
792         if (hostap_set_string(dev, HFA384X_RID_CNFOWNNAME, local->name) ||
793             local->func->reset_port(dev))
794                 return -EINVAL;
795
796         return 0;
797 }
798
799 static int prism2_ioctl_giwnickn(struct net_device *dev,
800                                  struct iw_request_info *info,
801                                  struct iw_point *data, char *nickname)
802 {
803         struct hostap_interface *iface;
804         local_info_t *local;
805         int len;
806         char name[MAX_NAME_LEN + 3];
807         u16 val;
808
809         iface = netdev_priv(dev);
810         local = iface->local;
811
812         len = local->func->get_rid(dev, HFA384X_RID_CNFOWNNAME,
813                                    &name, MAX_NAME_LEN + 2, 0);
814         val = __le16_to_cpu(*(u16 *) name);
815         if (len > MAX_NAME_LEN + 2 || len < 0 || val > MAX_NAME_LEN)
816                 return -EOPNOTSUPP;
817
818         name[val + 2] = '\0';
819         data->length = val + 1;
820         memcpy(nickname, name + 2, val + 1);
821
822         return 0;
823 }
824
825
826 static int prism2_ioctl_siwfreq(struct net_device *dev,
827                                 struct iw_request_info *info,
828                                 struct iw_freq *freq, char *extra)
829 {
830         struct hostap_interface *iface;
831         local_info_t *local;
832
833         iface = netdev_priv(dev);
834         local = iface->local;
835
836         /* freq => chan. */
837         if (freq->e == 1 &&
838             freq->m / 100000 >= freq_list[0] &&
839             freq->m / 100000 <= freq_list[FREQ_COUNT - 1]) {
840                 int ch;
841                 int fr = freq->m / 100000;
842                 for (ch = 0; ch < FREQ_COUNT; ch++) {
843                         if (fr == freq_list[ch]) {
844                                 freq->e = 0;
845                                 freq->m = ch + 1;
846                                 break;
847                         }
848                 }
849         }
850
851         if (freq->e != 0 || freq->m < 1 || freq->m > FREQ_COUNT ||
852             !(local->channel_mask & (1 << (freq->m - 1))))
853                 return -EINVAL;
854
855         local->channel = freq->m; /* channel is used in prism2_setup_rids() */
856         if (hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel) ||
857             local->func->reset_port(dev))
858                 return -EINVAL;
859
860         return 0;
861 }
862
863 static int prism2_ioctl_giwfreq(struct net_device *dev,
864                                 struct iw_request_info *info,
865                                 struct iw_freq *freq, char *extra)
866 {
867         struct hostap_interface *iface;
868         local_info_t *local;
869         u16 val;
870
871         iface = netdev_priv(dev);
872         local = iface->local;
873
874         if (local->func->get_rid(dev, HFA384X_RID_CURRENTCHANNEL, &val, 2, 1) <
875             0)
876                 return -EINVAL;
877
878         le16_to_cpus(&val);
879         if (val < 1 || val > FREQ_COUNT)
880                 return -EINVAL;
881
882         freq->m = freq_list[val - 1] * 100000;
883         freq->e = 1;
884
885         return 0;
886 }
887
888
889 static void hostap_monitor_set_type(local_info_t *local)
890 {
891         struct net_device *dev = local->ddev;
892
893         if (dev == NULL)
894                 return;
895
896         if (local->monitor_type == PRISM2_MONITOR_PRISM ||
897             local->monitor_type == PRISM2_MONITOR_CAPHDR) {
898                 dev->type = ARPHRD_IEEE80211_PRISM;
899                 dev->hard_header_parse =
900                         hostap_80211_prism_header_parse;
901         } else {
902                 dev->type = ARPHRD_IEEE80211;
903                 dev->hard_header_parse = hostap_80211_header_parse;
904         }
905 }
906
907
908 static int prism2_ioctl_siwessid(struct net_device *dev,
909                                  struct iw_request_info *info,
910                                  struct iw_point *data, char *ssid)
911 {
912         struct hostap_interface *iface;
913         local_info_t *local;
914
915         iface = netdev_priv(dev);
916         local = iface->local;
917
918         if (iface->type == HOSTAP_INTERFACE_WDS)
919                 return -EOPNOTSUPP;
920
921         if (data->flags == 0)
922                 ssid[0] = '\0'; /* ANY */
923
924         if (local->iw_mode == IW_MODE_MASTER && ssid[0] == '\0') {
925                 /* Setting SSID to empty string seems to kill the card in
926                  * Host AP mode */
927                 printk(KERN_DEBUG "%s: Host AP mode does not support "
928                        "'Any' essid\n", dev->name);
929                 return -EINVAL;
930         }
931
932         memcpy(local->essid, ssid, data->length);
933         local->essid[data->length] = '\0';
934
935         if ((!local->fw_ap &&
936              hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID, local->essid))
937             || hostap_set_string(dev, HFA384X_RID_CNFOWNSSID, local->essid) ||
938             local->func->reset_port(dev))
939                 return -EINVAL;
940
941         return 0;
942 }
943
944 static int prism2_ioctl_giwessid(struct net_device *dev,
945                                  struct iw_request_info *info,
946                                  struct iw_point *data, char *essid)
947 {
948         struct hostap_interface *iface;
949         local_info_t *local;
950         u16 val;
951
952         iface = netdev_priv(dev);
953         local = iface->local;
954
955         if (iface->type == HOSTAP_INTERFACE_WDS)
956                 return -EOPNOTSUPP;
957
958         data->flags = 1; /* active */
959         if (local->iw_mode == IW_MODE_MASTER) {
960                 data->length = strlen(local->essid);
961                 memcpy(essid, local->essid, IW_ESSID_MAX_SIZE);
962         } else {
963                 int len;
964                 char ssid[MAX_SSID_LEN + 2];
965                 memset(ssid, 0, sizeof(ssid));
966                 len = local->func->get_rid(dev, HFA384X_RID_CURRENTSSID,
967                                            &ssid, MAX_SSID_LEN + 2, 0);
968                 val = __le16_to_cpu(*(u16 *) ssid);
969                 if (len > MAX_SSID_LEN + 2 || len < 0 || val > MAX_SSID_LEN) {
970                         return -EOPNOTSUPP;
971                 }
972                 data->length = val;
973                 memcpy(essid, ssid + 2, IW_ESSID_MAX_SIZE);
974         }
975
976         return 0;
977 }
978
979
980 static int prism2_ioctl_giwrange(struct net_device *dev,
981                                  struct iw_request_info *info,
982                                  struct iw_point *data, char *extra)
983 {
984         struct hostap_interface *iface;
985         local_info_t *local;
986         struct iw_range *range = (struct iw_range *) extra;
987         u8 rates[10];
988         u16 val;
989         int i, len, over2;
990
991         iface = netdev_priv(dev);
992         local = iface->local;
993
994         data->length = sizeof(struct iw_range);
995         memset(range, 0, sizeof(struct iw_range));
996
997         /* TODO: could fill num_txpower and txpower array with
998          * something; however, there are 128 different values.. */
999
1000         range->txpower_capa = IW_TXPOW_DBM;
1001
1002         if (local->iw_mode == IW_MODE_INFRA || local->iw_mode == IW_MODE_ADHOC)
1003         {
1004                 range->min_pmp = 1 * 1024;
1005                 range->max_pmp = 65535 * 1024;
1006                 range->min_pmt = 1 * 1024;
1007                 range->max_pmt = 1000 * 1024;
1008                 range->pmp_flags = IW_POWER_PERIOD;
1009                 range->pmt_flags = IW_POWER_TIMEOUT;
1010                 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT |
1011                         IW_POWER_UNICAST_R | IW_POWER_ALL_R;
1012         }
1013
1014         range->we_version_compiled = WIRELESS_EXT;
1015         range->we_version_source = 18;
1016
1017         range->retry_capa = IW_RETRY_LIMIT;
1018         range->retry_flags = IW_RETRY_LIMIT;
1019         range->min_retry = 0;
1020         range->max_retry = 255;
1021
1022         range->num_channels = FREQ_COUNT;
1023
1024         val = 0;
1025         for (i = 0; i < FREQ_COUNT; i++) {
1026                 if (local->channel_mask & (1 << i)) {
1027                         range->freq[val].i = i + 1;
1028                         range->freq[val].m = freq_list[i] * 100000;
1029                         range->freq[val].e = 1;
1030                         val++;
1031                 }
1032                 if (val == IW_MAX_FREQUENCIES)
1033                         break;
1034         }
1035         range->num_frequency = val;
1036
1037         if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1038                 range->max_qual.qual = 70; /* what is correct max? This was not
1039                                             * documented exactly. At least
1040                                             * 69 has been observed. */
1041                 range->max_qual.level = 0; /* dB */
1042                 range->max_qual.noise = 0; /* dB */
1043
1044                 /* What would be suitable values for "average/typical" qual? */
1045                 range->avg_qual.qual = 20;
1046                 range->avg_qual.level = -60;
1047                 range->avg_qual.noise = -95;
1048         } else {
1049                 range->max_qual.qual = 92; /* 0 .. 92 */
1050                 range->max_qual.level = 154; /* 27 .. 154 */
1051                 range->max_qual.noise = 154; /* 27 .. 154 */
1052         }
1053         range->sensitivity = 3;
1054
1055         range->max_encoding_tokens = WEP_KEYS;
1056         range->num_encoding_sizes = 2;
1057         range->encoding_size[0] = 5;
1058         range->encoding_size[1] = 13;
1059
1060         over2 = 0;
1061         len = prism2_get_datarates(dev, rates);
1062         range->num_bitrates = 0;
1063         for (i = 0; i < len; i++) {
1064                 if (range->num_bitrates < IW_MAX_BITRATES) {
1065                         range->bitrate[range->num_bitrates] =
1066                                 rates[i] * 500000;
1067                         range->num_bitrates++;
1068                 }
1069                 if (rates[i] == 0x0b || rates[i] == 0x16)
1070                         over2 = 1;
1071         }
1072         /* estimated maximum TCP throughput values (bps) */
1073         range->throughput = over2 ? 5500000 : 1500000;
1074
1075         range->min_rts = 0;
1076         range->max_rts = 2347;
1077         range->min_frag = 256;
1078         range->max_frag = 2346;
1079
1080         /* Event capability (kernel + driver) */
1081         range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
1082                                 IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
1083                                 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
1084                                 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
1085         range->event_capa[1] = IW_EVENT_CAPA_K_1;
1086         range->event_capa[4] = (IW_EVENT_CAPA_MASK(IWEVTXDROP) |
1087                                 IW_EVENT_CAPA_MASK(IWEVCUSTOM) |
1088                                 IW_EVENT_CAPA_MASK(IWEVREGISTERED) |
1089                                 IW_EVENT_CAPA_MASK(IWEVEXPIRED));
1090
1091         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
1092                 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
1093
1094         return 0;
1095 }
1096
1097
1098 static int hostap_monitor_mode_enable(local_info_t *local)
1099 {
1100         struct net_device *dev = local->dev;
1101
1102         printk(KERN_DEBUG "Enabling monitor mode\n");
1103         hostap_monitor_set_type(local);
1104
1105         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
1106                             HFA384X_PORTTYPE_PSEUDO_IBSS)) {
1107                 printk(KERN_DEBUG "Port type setting for monitor mode "
1108                        "failed\n");
1109                 return -EOPNOTSUPP;
1110         }
1111
1112         /* Host decrypt is needed to get the IV and ICV fields;
1113          * however, monitor mode seems to remove WEP flag from frame
1114          * control field */
1115         if (hostap_set_word(dev, HFA384X_RID_CNFWEPFLAGS,
1116                             HFA384X_WEPFLAGS_HOSTENCRYPT |
1117                             HFA384X_WEPFLAGS_HOSTDECRYPT)) {
1118                 printk(KERN_DEBUG "WEP flags setting failed\n");
1119                 return -EOPNOTSUPP;
1120         }
1121
1122         if (local->func->reset_port(dev) ||
1123             local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1124                              (HFA384X_TEST_MONITOR << 8),
1125                              0, NULL, NULL)) {
1126                 printk(KERN_DEBUG "Setting monitor mode failed\n");
1127                 return -EOPNOTSUPP;
1128         }
1129
1130         return 0;
1131 }
1132
1133
1134 static int hostap_monitor_mode_disable(local_info_t *local)
1135 {
1136         struct net_device *dev = local->ddev;
1137
1138         if (dev == NULL)
1139                 return -1;
1140
1141         printk(KERN_DEBUG "%s: Disabling monitor mode\n", dev->name);
1142         dev->type = ARPHRD_ETHER;
1143         dev->hard_header_parse = local->saved_eth_header_parse;
1144         if (local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1145                              (HFA384X_TEST_STOP << 8),
1146                              0, NULL, NULL))
1147                 return -1;
1148         return hostap_set_encryption(local);
1149 }
1150
1151
1152 static int prism2_ioctl_siwmode(struct net_device *dev,
1153                                 struct iw_request_info *info,
1154                                 __u32 *mode, char *extra)
1155 {
1156         struct hostap_interface *iface;
1157         local_info_t *local;
1158         int double_reset = 0;
1159
1160         iface = netdev_priv(dev);
1161         local = iface->local;
1162
1163         if (*mode != IW_MODE_ADHOC && *mode != IW_MODE_INFRA &&
1164             *mode != IW_MODE_MASTER && *mode != IW_MODE_REPEAT &&
1165             *mode != IW_MODE_MONITOR)
1166                 return -EOPNOTSUPP;
1167
1168 #ifdef PRISM2_NO_STATION_MODES
1169         if (*mode == IW_MODE_ADHOC || *mode == IW_MODE_INFRA)
1170                 return -EOPNOTSUPP;
1171 #endif /* PRISM2_NO_STATION_MODES */
1172
1173         if (*mode == local->iw_mode)
1174                 return 0;
1175
1176         if (*mode == IW_MODE_MASTER && local->essid[0] == '\0') {
1177                 printk(KERN_WARNING "%s: empty SSID not allowed in Master "
1178                        "mode\n", dev->name);
1179                 return -EINVAL;
1180         }
1181
1182         if (local->iw_mode == IW_MODE_MONITOR)
1183                 hostap_monitor_mode_disable(local);
1184
1185         if ((local->iw_mode == IW_MODE_ADHOC ||
1186              local->iw_mode == IW_MODE_MONITOR) && *mode == IW_MODE_MASTER) {
1187                 /* There seems to be a firmware bug in at least STA f/w v1.5.6
1188                  * that leaves beacon frames to use IBSS type when moving from
1189                  * IBSS to Host AP mode. Doing double Port0 reset seems to be
1190                  * enough to workaround this. */
1191                 double_reset = 1;
1192         }
1193
1194         printk(KERN_DEBUG "prism2: %s: operating mode changed "
1195                "%d -> %d\n", dev->name, local->iw_mode, *mode);
1196         local->iw_mode = *mode;
1197
1198         if (local->iw_mode == IW_MODE_MONITOR)
1199                 hostap_monitor_mode_enable(local);
1200         else if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
1201                  !local->fw_encrypt_ok) {
1202                 printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
1203                        "a workaround for firmware bug in Host AP mode WEP\n",
1204                        dev->name);
1205                 local->host_encrypt = 1;
1206         }
1207
1208         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
1209                             hostap_get_porttype(local)))
1210                 return -EOPNOTSUPP;
1211
1212         if (local->func->reset_port(dev))
1213                 return -EINVAL;
1214         if (double_reset && local->func->reset_port(dev))
1215                 return -EINVAL;
1216
1217         if (local->iw_mode != IW_MODE_INFRA && local->iw_mode != IW_MODE_ADHOC)
1218         {
1219                 /* netif_carrier is used only in client modes for now, so make
1220                  * sure carrier is on when moving to non-client modes. */
1221                 netif_carrier_on(local->dev);
1222                 netif_carrier_on(local->ddev);
1223         }
1224         return 0;
1225 }
1226
1227
1228 static int prism2_ioctl_giwmode(struct net_device *dev,
1229                                 struct iw_request_info *info,
1230                                 __u32 *mode, char *extra)
1231 {
1232         struct hostap_interface *iface;
1233         local_info_t *local;
1234
1235         iface = netdev_priv(dev);
1236         local = iface->local;
1237
1238         switch (iface->type) {
1239         case HOSTAP_INTERFACE_STA:
1240                 *mode = IW_MODE_INFRA;
1241                 break;
1242         case HOSTAP_INTERFACE_WDS:
1243                 *mode = IW_MODE_REPEAT;
1244                 break;
1245         default:
1246                 *mode = local->iw_mode;
1247                 break;
1248         }
1249         return 0;
1250 }
1251
1252
1253 static int prism2_ioctl_siwpower(struct net_device *dev,
1254                                  struct iw_request_info *info,
1255                                  struct iw_param *wrq, char *extra)
1256 {
1257 #ifdef PRISM2_NO_STATION_MODES
1258         return -EOPNOTSUPP;
1259 #else /* PRISM2_NO_STATION_MODES */
1260         int ret = 0;
1261
1262         if (wrq->disabled)
1263                 return hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 0);
1264
1265         switch (wrq->flags & IW_POWER_MODE) {
1266         case IW_POWER_UNICAST_R:
1267                 ret = hostap_set_word(dev, HFA384X_RID_CNFMULTICASTRECEIVE, 0);
1268                 if (ret)
1269                         return ret;
1270                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1271                 if (ret)
1272                         return ret;
1273                 break;
1274         case IW_POWER_ALL_R:
1275                 ret = hostap_set_word(dev, HFA384X_RID_CNFMULTICASTRECEIVE, 1);
1276                 if (ret)
1277                         return ret;
1278                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1279                 if (ret)
1280                         return ret;
1281                 break;
1282         case IW_POWER_ON:
1283                 break;
1284         default:
1285                 return -EINVAL;
1286         }
1287
1288         if (wrq->flags & IW_POWER_TIMEOUT) {
1289                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1290                 if (ret)
1291                         return ret;
1292                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMHOLDOVERDURATION,
1293                                       wrq->value / 1024);
1294                 if (ret)
1295                         return ret;
1296         }
1297         if (wrq->flags & IW_POWER_PERIOD) {
1298                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1299                 if (ret)
1300                         return ret;
1301                 ret = hostap_set_word(dev, HFA384X_RID_CNFMAXSLEEPDURATION,
1302                                       wrq->value / 1024);
1303                 if (ret)
1304                         return ret;
1305         }
1306
1307         return ret;
1308 #endif /* PRISM2_NO_STATION_MODES */
1309 }
1310
1311
1312 static int prism2_ioctl_giwpower(struct net_device *dev,
1313                                  struct iw_request_info *info,
1314                                  struct iw_param *rrq, char *extra)
1315 {
1316 #ifdef PRISM2_NO_STATION_MODES
1317         return -EOPNOTSUPP;
1318 #else /* PRISM2_NO_STATION_MODES */
1319         struct hostap_interface *iface;
1320         local_info_t *local;
1321         u16 enable, mcast;
1322
1323         iface = netdev_priv(dev);
1324         local = iface->local;
1325
1326         if (local->func->get_rid(dev, HFA384X_RID_CNFPMENABLED, &enable, 2, 1)
1327             < 0)
1328                 return -EINVAL;
1329
1330         if (!__le16_to_cpu(enable)) {
1331                 rrq->disabled = 1;
1332                 return 0;
1333         }
1334
1335         rrq->disabled = 0;
1336
1337         if ((rrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
1338                 u16 timeout;
1339                 if (local->func->get_rid(dev,
1340                                          HFA384X_RID_CNFPMHOLDOVERDURATION,
1341                                          &timeout, 2, 1) < 0)
1342                         return -EINVAL;
1343
1344                 rrq->flags = IW_POWER_TIMEOUT;
1345                 rrq->value = __le16_to_cpu(timeout) * 1024;
1346         } else {
1347                 u16 period;
1348                 if (local->func->get_rid(dev, HFA384X_RID_CNFMAXSLEEPDURATION,
1349                                          &period, 2, 1) < 0)
1350                         return -EINVAL;
1351
1352                 rrq->flags = IW_POWER_PERIOD;
1353                 rrq->value = __le16_to_cpu(period) * 1024;
1354         }
1355
1356         if (local->func->get_rid(dev, HFA384X_RID_CNFMULTICASTRECEIVE, &mcast,
1357                                  2, 1) < 0)
1358                 return -EINVAL;
1359
1360         if (__le16_to_cpu(mcast))
1361                 rrq->flags |= IW_POWER_ALL_R;
1362         else
1363                 rrq->flags |= IW_POWER_UNICAST_R;
1364
1365         return 0;
1366 #endif /* PRISM2_NO_STATION_MODES */
1367 }
1368
1369
1370 static int prism2_ioctl_siwretry(struct net_device *dev,
1371                                  struct iw_request_info *info,
1372                                  struct iw_param *rrq, char *extra)
1373 {
1374         struct hostap_interface *iface;
1375         local_info_t *local;
1376
1377         iface = netdev_priv(dev);
1378         local = iface->local;
1379
1380         if (rrq->disabled)
1381                 return -EINVAL;
1382
1383         /* setting retry limits is not supported with the current station
1384          * firmware code; simulate this with alternative retry count for now */
1385         if (rrq->flags == IW_RETRY_LIMIT) {
1386                 if (rrq->value < 0) {
1387                         /* disable manual retry count setting and use firmware
1388                          * defaults */
1389                         local->manual_retry_count = -1;
1390                         local->tx_control &= ~HFA384X_TX_CTRL_ALT_RTRY;
1391                 } else {
1392                         if (hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1393                                             rrq->value)) {
1394                                 printk(KERN_DEBUG "%s: Alternate retry count "
1395                                        "setting to %d failed\n",
1396                                        dev->name, rrq->value);
1397                                 return -EOPNOTSUPP;
1398                         }
1399
1400                         local->manual_retry_count = rrq->value;
1401                         local->tx_control |= HFA384X_TX_CTRL_ALT_RTRY;
1402                 }
1403                 return 0;
1404         }
1405
1406         return -EOPNOTSUPP;
1407
1408 #if 0
1409         /* what could be done, if firmware would support this.. */
1410
1411         if (rrq->flags & IW_RETRY_LIMIT) {
1412                 if (rrq->flags & IW_RETRY_LONG)
1413                         HFA384X_RID_LONGRETRYLIMIT = rrq->value;
1414                 else if (rrq->flags & IW_RETRY_SHORT)
1415                         HFA384X_RID_SHORTRETRYLIMIT = rrq->value;
1416                 else {
1417                         HFA384X_RID_LONGRETRYLIMIT = rrq->value;
1418                         HFA384X_RID_SHORTRETRYLIMIT = rrq->value;
1419                 }
1420
1421         }
1422
1423         if (rrq->flags & IW_RETRY_LIFETIME) {
1424                 HFA384X_RID_MAXTRANSMITLIFETIME = rrq->value / 1024;
1425         }
1426
1427         return 0;
1428 #endif /* 0 */
1429 }
1430
1431 static int prism2_ioctl_giwretry(struct net_device *dev,
1432                                  struct iw_request_info *info,
1433                                  struct iw_param *rrq, char *extra)
1434 {
1435         struct hostap_interface *iface;
1436         local_info_t *local;
1437         u16 shortretry, longretry, lifetime, altretry;
1438
1439         iface = netdev_priv(dev);
1440         local = iface->local;
1441
1442         if (local->func->get_rid(dev, HFA384X_RID_SHORTRETRYLIMIT, &shortretry,
1443                                  2, 1) < 0 ||
1444             local->func->get_rid(dev, HFA384X_RID_LONGRETRYLIMIT, &longretry,
1445                                  2, 1) < 0 ||
1446             local->func->get_rid(dev, HFA384X_RID_MAXTRANSMITLIFETIME,
1447                                  &lifetime, 2, 1) < 0)
1448                 return -EINVAL;
1449
1450         le16_to_cpus(&shortretry);
1451         le16_to_cpus(&longretry);
1452         le16_to_cpus(&lifetime);
1453
1454         rrq->disabled = 0;
1455
1456         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
1457                 rrq->flags = IW_RETRY_LIFETIME;
1458                 rrq->value = lifetime * 1024;
1459         } else {
1460                 if (local->manual_retry_count >= 0) {
1461                         rrq->flags = IW_RETRY_LIMIT;
1462                         if (local->func->get_rid(dev,
1463                                                  HFA384X_RID_CNFALTRETRYCOUNT,
1464                                                  &altretry, 2, 1) >= 0)
1465                                 rrq->value = le16_to_cpu(altretry);
1466                         else
1467                                 rrq->value = local->manual_retry_count;
1468                 } else if ((rrq->flags & IW_RETRY_LONG)) {
1469                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1470                         rrq->value = longretry;
1471                 } else {
1472                         rrq->flags = IW_RETRY_LIMIT;
1473                         rrq->value = shortretry;
1474                         if (shortretry != longretry)
1475                                 rrq->flags |= IW_RETRY_SHORT;
1476                 }
1477         }
1478         return 0;
1479 }
1480
1481
1482 /* Note! This TX power controlling is experimental and should not be used in
1483  * production use. It just sets raw power register and does not use any kind of
1484  * feedback information from the measured TX power (CR58). This is now
1485  * commented out to make sure that it is not used by accident. TX power
1486  * configuration will be enabled again after proper algorithm using feedback
1487  * has been implemented. */
1488
1489 #ifdef RAW_TXPOWER_SETTING
1490 /* Map HFA386x's CR31 to and from dBm with some sort of ad hoc mapping..
1491  * This version assumes following mapping:
1492  * CR31 is 7-bit value with -64 to +63 range.
1493  * -64 is mapped into +20dBm and +63 into -43dBm.
1494  * This is certainly not an exact mapping for every card, but at least
1495  * increasing dBm value should correspond to increasing TX power.
1496  */
1497
1498 static int prism2_txpower_hfa386x_to_dBm(u16 val)
1499 {
1500         signed char tmp;
1501
1502         if (val > 255)
1503                 val = 255;
1504
1505         tmp = val;
1506         tmp >>= 2;
1507
1508         return -12 - tmp;
1509 }
1510
1511 static u16 prism2_txpower_dBm_to_hfa386x(int val)
1512 {
1513         signed char tmp;
1514
1515         if (val > 20)
1516                 return 128;
1517         else if (val < -43)
1518                 return 127;
1519
1520         tmp = val;
1521         tmp = -12 - tmp;
1522         tmp <<= 2;
1523
1524         return (unsigned char) tmp;
1525 }
1526 #endif /* RAW_TXPOWER_SETTING */
1527
1528
1529 static int prism2_ioctl_siwtxpow(struct net_device *dev,
1530                                  struct iw_request_info *info,
1531                                  struct iw_param *rrq, char *extra)
1532 {
1533         struct hostap_interface *iface;
1534         local_info_t *local;
1535 #ifdef RAW_TXPOWER_SETTING
1536         char *tmp;
1537 #endif
1538         u16 val;
1539         int ret = 0;
1540
1541         iface = netdev_priv(dev);
1542         local = iface->local;
1543
1544         if (rrq->disabled) {
1545                 if (local->txpower_type != PRISM2_TXPOWER_OFF) {
1546                         val = 0xff; /* use all standby and sleep modes */
1547                         ret = local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF,
1548                                                HFA386X_CR_A_D_TEST_MODES2,
1549                                                &val, NULL);
1550                         printk(KERN_DEBUG "%s: Turning radio off: %s\n",
1551                                dev->name, ret ? "failed" : "OK");
1552                         local->txpower_type = PRISM2_TXPOWER_OFF;
1553                 }
1554                 return (ret ? -EOPNOTSUPP : 0);
1555         }
1556
1557         if (local->txpower_type == PRISM2_TXPOWER_OFF) {
1558                 val = 0; /* disable all standby and sleep modes */
1559                 ret = local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF,
1560                                        HFA386X_CR_A_D_TEST_MODES2, &val, NULL);
1561                 printk(KERN_DEBUG "%s: Turning radio on: %s\n",
1562                        dev->name, ret ? "failed" : "OK");
1563                 local->txpower_type = PRISM2_TXPOWER_UNKNOWN;
1564         }
1565
1566 #ifdef RAW_TXPOWER_SETTING
1567         if (!rrq->fixed && local->txpower_type != PRISM2_TXPOWER_AUTO) {
1568                 printk(KERN_DEBUG "Setting ALC on\n");
1569                 val = HFA384X_TEST_CFG_BIT_ALC;
1570                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1571                                  (HFA384X_TEST_CFG_BITS << 8), 1, &val, NULL);
1572                 local->txpower_type = PRISM2_TXPOWER_AUTO;
1573                 return 0;
1574         }
1575
1576         if (local->txpower_type != PRISM2_TXPOWER_FIXED) {
1577                 printk(KERN_DEBUG "Setting ALC off\n");
1578                 val = HFA384X_TEST_CFG_BIT_ALC;
1579                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1580                                  (HFA384X_TEST_CFG_BITS << 8), 0, &val, NULL);
1581                         local->txpower_type = PRISM2_TXPOWER_FIXED;
1582         }
1583
1584         if (rrq->flags == IW_TXPOW_DBM)
1585                 tmp = "dBm";
1586         else if (rrq->flags == IW_TXPOW_MWATT)
1587                 tmp = "mW";
1588         else
1589                 tmp = "UNKNOWN";
1590         printk(KERN_DEBUG "Setting TX power to %d %s\n", rrq->value, tmp);
1591
1592         if (rrq->flags != IW_TXPOW_DBM) {
1593                 printk("SIOCSIWTXPOW with mW is not supported; use dBm\n");
1594                 return -EOPNOTSUPP;
1595         }
1596
1597         local->txpower = rrq->value;
1598         val = prism2_txpower_dBm_to_hfa386x(local->txpower);
1599         if (local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF,
1600                              HFA386X_CR_MANUAL_TX_POWER, &val, NULL))
1601                 ret = -EOPNOTSUPP;
1602 #else /* RAW_TXPOWER_SETTING */
1603         if (rrq->fixed)
1604                 ret = -EOPNOTSUPP;
1605 #endif /* RAW_TXPOWER_SETTING */
1606
1607         return ret;
1608 }
1609
1610 static int prism2_ioctl_giwtxpow(struct net_device *dev,
1611                                  struct iw_request_info *info,
1612                                  struct iw_param *rrq, char *extra)
1613 {
1614 #ifdef RAW_TXPOWER_SETTING
1615         struct hostap_interface *iface;
1616         local_info_t *local;
1617         u16 resp0;
1618
1619         iface = netdev_priv(dev);
1620         local = iface->local;
1621
1622         rrq->flags = IW_TXPOW_DBM;
1623         rrq->disabled = 0;
1624         rrq->fixed = 0;
1625
1626         if (local->txpower_type == PRISM2_TXPOWER_AUTO) {
1627                 if (local->func->cmd(dev, HFA384X_CMDCODE_READMIF,
1628                                      HFA386X_CR_MANUAL_TX_POWER,
1629                                      NULL, &resp0) == 0) {
1630                         rrq->value = prism2_txpower_hfa386x_to_dBm(resp0);
1631                 } else {
1632                         /* Could not get real txpower; guess 15 dBm */
1633                         rrq->value = 15;
1634                 }
1635         } else if (local->txpower_type == PRISM2_TXPOWER_OFF) {
1636                 rrq->value = 0;
1637                 rrq->disabled = 1;
1638         } else if (local->txpower_type == PRISM2_TXPOWER_FIXED) {
1639                 rrq->value = local->txpower;
1640                 rrq->fixed = 1;
1641         } else {
1642                 printk("SIOCGIWTXPOW - unknown txpower_type=%d\n",
1643                        local->txpower_type);
1644         }
1645         return 0;
1646 #else /* RAW_TXPOWER_SETTING */
1647         return -EOPNOTSUPP;
1648 #endif /* RAW_TXPOWER_SETTING */
1649 }
1650
1651
1652 #ifndef PRISM2_NO_STATION_MODES
1653
1654 /* HostScan request works with and without host_roaming mode. In addition, it
1655  * does not break current association. However, it requires newer station
1656  * firmware version (>= 1.3.1) than scan request. */
1657 static int prism2_request_hostscan(struct net_device *dev,
1658                                    u8 *ssid, u8 ssid_len)
1659 {
1660         struct hostap_interface *iface;
1661         local_info_t *local;
1662         struct hfa384x_hostscan_request scan_req;
1663
1664         iface = netdev_priv(dev);
1665         local = iface->local;
1666
1667         memset(&scan_req, 0, sizeof(scan_req));
1668         scan_req.channel_list = cpu_to_le16(local->channel_mask &
1669                                             local->scan_channel_mask);
1670         scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
1671         if (ssid) {
1672                 if (ssid_len > 32)
1673                         return -EINVAL;
1674                 scan_req.target_ssid_len = cpu_to_le16(ssid_len);
1675                 memcpy(scan_req.target_ssid, ssid, ssid_len);
1676         }
1677
1678         if (local->func->set_rid(dev, HFA384X_RID_HOSTSCAN, &scan_req,
1679                                  sizeof(scan_req))) {
1680                 printk(KERN_DEBUG "%s: HOSTSCAN failed\n", dev->name);
1681                 return -EINVAL;
1682         }
1683         return 0;
1684 }
1685
1686
1687 static int prism2_request_scan(struct net_device *dev)
1688 {
1689         struct hostap_interface *iface;
1690         local_info_t *local;
1691         struct hfa384x_scan_request scan_req;
1692         int ret = 0;
1693
1694         iface = netdev_priv(dev);
1695         local = iface->local;
1696
1697         memset(&scan_req, 0, sizeof(scan_req));
1698         scan_req.channel_list = cpu_to_le16(local->channel_mask &
1699                                             local->scan_channel_mask);
1700         scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
1701
1702         /* FIX:
1703          * It seems to be enough to set roaming mode for a short moment to
1704          * host-based and then setup scanrequest data and return the mode to
1705          * firmware-based.
1706          *
1707          * Master mode would need to drop to Managed mode for a short while
1708          * to make scanning work.. Or sweep through the different channels and
1709          * use passive scan based on beacons. */
1710
1711         if (!local->host_roaming)
1712                 hostap_set_word(dev, HFA384X_RID_CNFROAMINGMODE,
1713                                 HFA384X_ROAMING_HOST);
1714
1715         if (local->func->set_rid(dev, HFA384X_RID_SCANREQUEST, &scan_req,
1716                                  sizeof(scan_req))) {
1717                 printk(KERN_DEBUG "SCANREQUEST failed\n");
1718                 ret = -EINVAL;
1719         }
1720
1721         if (!local->host_roaming)
1722                 hostap_set_word(dev, HFA384X_RID_CNFROAMINGMODE,
1723                                 HFA384X_ROAMING_FIRMWARE);
1724
1725         return 0;
1726 }
1727
1728 #else /* !PRISM2_NO_STATION_MODES */
1729
1730 static inline int prism2_request_hostscan(struct net_device *dev,
1731                                           u8 *ssid, u8 ssid_len)
1732 {
1733         return -EOPNOTSUPP;
1734 }
1735
1736
1737 static inline int prism2_request_scan(struct net_device *dev)
1738 {
1739         return -EOPNOTSUPP;
1740 }
1741
1742 #endif /* !PRISM2_NO_STATION_MODES */
1743
1744
1745 static int prism2_ioctl_siwscan(struct net_device *dev,
1746                                 struct iw_request_info *info,
1747                                 struct iw_point *data, char *extra)
1748 {
1749         struct hostap_interface *iface;
1750         local_info_t *local;
1751         int ret;
1752         u8 *ssid = NULL, ssid_len = 0;
1753         struct iw_scan_req *req = (struct iw_scan_req *) extra;
1754
1755         iface = netdev_priv(dev);
1756         local = iface->local;
1757
1758         if (data->length < sizeof(struct iw_scan_req))
1759                 req = NULL;
1760
1761         if (local->iw_mode == IW_MODE_MASTER) {
1762                 /* In master mode, we just return the results of our local
1763                  * tables, so we don't need to start anything...
1764                  * Jean II */
1765                 data->length = 0;
1766                 return 0;
1767         }
1768
1769         if (!local->dev_enabled)
1770                 return -ENETDOWN;
1771
1772         if (req && data->flags & IW_SCAN_THIS_ESSID) {
1773                 ssid = req->essid;
1774                 ssid_len = req->essid_len;
1775
1776                 if (ssid_len &&
1777                     ((local->iw_mode != IW_MODE_INFRA &&
1778                       local->iw_mode != IW_MODE_ADHOC) ||
1779                      (local->sta_fw_ver < PRISM2_FW_VER(1,3,1))))
1780                         return -EOPNOTSUPP;
1781         }
1782
1783         if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1))
1784                 ret = prism2_request_hostscan(dev, ssid, ssid_len);
1785         else
1786                 ret = prism2_request_scan(dev);
1787
1788         if (ret == 0)
1789                 local->scan_timestamp = jiffies;
1790
1791         /* Could inquire F101, F103 or wait for SIOCGIWSCAN and read RID */
1792
1793         return ret;
1794 }
1795
1796
1797 #ifndef PRISM2_NO_STATION_MODES
1798 static char * __prism2_translate_scan(local_info_t *local,
1799                                       struct hfa384x_hostscan_result *scan,
1800                                       struct hostap_bss_info *bss,
1801                                       char *current_ev, char *end_buf)
1802 {
1803         int i, chan;
1804         struct iw_event iwe;
1805         char *current_val;
1806         u16 capabilities;
1807         u8 *pos;
1808         u8 *ssid, *bssid;
1809         size_t ssid_len;
1810         char *buf;
1811
1812         if (bss) {
1813                 ssid = bss->ssid;
1814                 ssid_len = bss->ssid_len;
1815                 bssid = bss->bssid;
1816         } else {
1817                 ssid = scan->ssid;
1818                 ssid_len = le16_to_cpu(scan->ssid_len);
1819                 bssid = scan->bssid;
1820         }
1821         if (ssid_len > 32)
1822                 ssid_len = 32;
1823
1824         /* First entry *MUST* be the AP MAC address */
1825         memset(&iwe, 0, sizeof(iwe));
1826         iwe.cmd = SIOCGIWAP;
1827         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1828         memcpy(iwe.u.ap_addr.sa_data, bssid, ETH_ALEN);
1829         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1830                                           IW_EV_ADDR_LEN);
1831
1832         /* Other entries will be displayed in the order we give them */
1833
1834         memset(&iwe, 0, sizeof(iwe));
1835         iwe.cmd = SIOCGIWESSID;
1836         iwe.u.data.length = ssid_len;
1837         iwe.u.data.flags = 1;
1838         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, ssid);
1839
1840         memset(&iwe, 0, sizeof(iwe));
1841         iwe.cmd = SIOCGIWMODE;
1842         if (bss) {
1843                 capabilities = bss->capab_info;
1844         } else {
1845                 capabilities = le16_to_cpu(scan->capability);
1846         }
1847         if (capabilities & (WLAN_CAPABILITY_ESS |
1848                             WLAN_CAPABILITY_IBSS)) {
1849                 if (capabilities & WLAN_CAPABILITY_ESS)
1850                         iwe.u.mode = IW_MODE_MASTER;
1851                 else
1852                         iwe.u.mode = IW_MODE_ADHOC;
1853                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1854                                                   IW_EV_UINT_LEN);
1855         }
1856
1857         memset(&iwe, 0, sizeof(iwe));
1858         iwe.cmd = SIOCGIWFREQ;
1859         if (scan) {
1860                 chan = le16_to_cpu(scan->chid);
1861         } else if (bss) {
1862                 chan = bss->chan;
1863         } else {
1864                 chan = 0;
1865         }
1866
1867         if (chan > 0) {
1868                 iwe.u.freq.m = freq_list[chan - 1] * 100000;
1869                 iwe.u.freq.e = 1;
1870                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1871                                                   IW_EV_FREQ_LEN);
1872         }
1873
1874         if (scan) {
1875                 memset(&iwe, 0, sizeof(iwe));
1876                 iwe.cmd = IWEVQUAL;
1877                 if (local->last_scan_type == PRISM2_HOSTSCAN) {
1878                         iwe.u.qual.level = le16_to_cpu(scan->sl);
1879                         iwe.u.qual.noise = le16_to_cpu(scan->anl);
1880                 } else {
1881                         iwe.u.qual.level =
1882                                 HFA384X_LEVEL_TO_dBm(le16_to_cpu(scan->sl));
1883                         iwe.u.qual.noise =
1884                                 HFA384X_LEVEL_TO_dBm(le16_to_cpu(scan->anl));
1885                 }
1886                 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED
1887                         | IW_QUAL_NOISE_UPDATED
1888                         | IW_QUAL_QUAL_INVALID
1889                         | IW_QUAL_DBM;
1890                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1891                                                   IW_EV_QUAL_LEN);
1892         }
1893
1894         memset(&iwe, 0, sizeof(iwe));
1895         iwe.cmd = SIOCGIWENCODE;
1896         if (capabilities & WLAN_CAPABILITY_PRIVACY)
1897                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1898         else
1899                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1900         iwe.u.data.length = 0;
1901         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
1902
1903         /* TODO: add SuppRates into BSS table */
1904         if (scan) {
1905                 memset(&iwe, 0, sizeof(iwe));
1906                 iwe.cmd = SIOCGIWRATE;
1907                 current_val = current_ev + IW_EV_LCP_LEN;
1908                 pos = scan->sup_rates;
1909                 for (i = 0; i < sizeof(scan->sup_rates); i++) {
1910                         if (pos[i] == 0)
1911                                 break;
1912                         /* Bit rate given in 500 kb/s units (+ 0x80) */
1913                         iwe.u.bitrate.value = ((pos[i] & 0x7f) * 500000);
1914                         current_val = iwe_stream_add_value(
1915                                 current_ev, current_val, end_buf, &iwe,
1916                                 IW_EV_PARAM_LEN);
1917                 }
1918                 /* Check if we added any event */
1919                 if ((current_val - current_ev) > IW_EV_LCP_LEN)
1920                         current_ev = current_val;
1921         }
1922
1923         /* TODO: add BeaconInt,resp_rate,atim into BSS table */
1924         buf = kmalloc(MAX_WPA_IE_LEN * 2 + 30, GFP_ATOMIC);
1925         if (buf && scan) {
1926                 memset(&iwe, 0, sizeof(iwe));
1927                 iwe.cmd = IWEVCUSTOM;
1928                 sprintf(buf, "bcn_int=%d", le16_to_cpu(scan->beacon_interval));
1929                 iwe.u.data.length = strlen(buf);
1930                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1931                                                   buf);
1932
1933                 memset(&iwe, 0, sizeof(iwe));
1934                 iwe.cmd = IWEVCUSTOM;
1935                 sprintf(buf, "resp_rate=%d", le16_to_cpu(scan->rate));
1936                 iwe.u.data.length = strlen(buf);
1937                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1938                                                   buf);
1939
1940                 if (local->last_scan_type == PRISM2_HOSTSCAN &&
1941                     (capabilities & WLAN_CAPABILITY_IBSS)) {
1942                         memset(&iwe, 0, sizeof(iwe));
1943                         iwe.cmd = IWEVCUSTOM;
1944                         sprintf(buf, "atim=%d", le16_to_cpu(scan->atim));
1945                         iwe.u.data.length = strlen(buf);
1946                         current_ev = iwe_stream_add_point(current_ev, end_buf,
1947                                                           &iwe, buf);
1948                 }
1949         }
1950         kfree(buf);
1951
1952         if (bss && bss->wpa_ie_len > 0 && bss->wpa_ie_len <= MAX_WPA_IE_LEN) {
1953                 memset(&iwe, 0, sizeof(iwe));
1954                 iwe.cmd = IWEVGENIE;
1955                 iwe.u.data.length = bss->wpa_ie_len;
1956                 current_ev = iwe_stream_add_point(
1957                         current_ev, end_buf, &iwe, bss->wpa_ie);
1958         }
1959
1960         if (bss && bss->rsn_ie_len > 0 && bss->rsn_ie_len <= MAX_WPA_IE_LEN) {
1961                 memset(&iwe, 0, sizeof(iwe));
1962                 iwe.cmd = IWEVGENIE;
1963                 iwe.u.data.length = bss->rsn_ie_len;
1964                 current_ev = iwe_stream_add_point(
1965                         current_ev, end_buf, &iwe, bss->rsn_ie);
1966         }
1967
1968         return current_ev;
1969 }
1970
1971
1972 /* Translate scan data returned from the card to a card independant
1973  * format that the Wireless Tools will understand - Jean II */
1974 static inline int prism2_translate_scan(local_info_t *local,
1975                                         char *buffer, int buflen)
1976 {
1977         struct hfa384x_hostscan_result *scan;
1978         int entry, hostscan;
1979         char *current_ev = buffer;
1980         char *end_buf = buffer + buflen;
1981         struct list_head *ptr;
1982
1983         spin_lock_bh(&local->lock);
1984
1985         list_for_each(ptr, &local->bss_list) {
1986                 struct hostap_bss_info *bss;
1987                 bss = list_entry(ptr, struct hostap_bss_info, list);
1988                 bss->included = 0;
1989         }
1990
1991         hostscan = local->last_scan_type == PRISM2_HOSTSCAN;
1992         for (entry = 0; entry < local->last_scan_results_count; entry++) {
1993                 int found = 0;
1994                 scan = &local->last_scan_results[entry];
1995
1996                 /* Report every SSID if the AP is using multiple SSIDs. If no
1997                  * BSS record is found (e.g., when WPA mode is disabled),
1998                  * report the AP once. */
1999                 list_for_each(ptr, &local->bss_list) {
2000                         struct hostap_bss_info *bss;
2001                         bss = list_entry(ptr, struct hostap_bss_info, list);
2002                         if (memcmp(bss->bssid, scan->bssid, ETH_ALEN) == 0) {
2003                                 bss->included = 1;
2004                                 current_ev = __prism2_translate_scan(
2005                                         local, scan, bss, current_ev, end_buf);
2006                                 found++;
2007                         }
2008                 }
2009                 if (!found) {
2010                         current_ev = __prism2_translate_scan(
2011                                 local, scan, NULL, current_ev, end_buf);
2012                 }
2013                 /* Check if there is space for one more entry */
2014                 if ((end_buf - current_ev) <= IW_EV_ADDR_LEN) {
2015                         /* Ask user space to try again with a bigger buffer */
2016                         spin_unlock_bh(&local->lock);
2017                         return -E2BIG;
2018                 }
2019         }
2020
2021         /* Prism2 firmware has limits (32 at least in some versions) for number
2022          * of BSSes in scan results. Extend this limit by using local BSS list.
2023          */
2024         list_for_each(ptr, &local->bss_list) {
2025                 struct hostap_bss_info *bss;
2026                 bss = list_entry(ptr, struct hostap_bss_info, list);
2027                 if (bss->included)
2028                         continue;
2029                 current_ev = __prism2_translate_scan(local, NULL, bss,
2030                                                      current_ev, end_buf);
2031                 /* Check if there is space for one more entry */
2032                 if ((end_buf - current_ev) <= IW_EV_ADDR_LEN) {
2033                         /* Ask user space to try again with a bigger buffer */
2034                         spin_unlock_bh(&local->lock);
2035                         return -E2BIG;
2036                 }
2037         }
2038
2039         spin_unlock_bh(&local->lock);
2040
2041         return current_ev - buffer;
2042 }
2043 #endif /* PRISM2_NO_STATION_MODES */
2044
2045
2046 static inline int prism2_ioctl_giwscan_sta(struct net_device *dev,
2047                                            struct iw_request_info *info,
2048                                            struct iw_point *data, char *extra)
2049 {
2050 #ifdef PRISM2_NO_STATION_MODES
2051         return -EOPNOTSUPP;
2052 #else /* PRISM2_NO_STATION_MODES */
2053         struct hostap_interface *iface;
2054         local_info_t *local;
2055         int res;
2056
2057         iface = netdev_priv(dev);
2058         local = iface->local;
2059
2060         /* Wait until the scan is finished. We can probably do better
2061          * than that - Jean II */
2062         if (local->scan_timestamp &&
2063             time_before(jiffies, local->scan_timestamp + 3 * HZ)) {
2064                 /* Important note : we don't want to block the caller
2065                  * until results are ready for various reasons.
2066                  * First, managing wait queues is complex and racy
2067                  * (there may be multiple simultaneous callers).
2068                  * Second, we grab some rtnetlink lock before comming
2069                  * here (in dev_ioctl()).
2070                  * Third, the caller can wait on the Wireless Event
2071                  * - Jean II */
2072                 return -EAGAIN;
2073         }
2074         local->scan_timestamp = 0;
2075
2076         res = prism2_translate_scan(local, extra, data->length);
2077
2078         if (res >= 0) {
2079                 data->length = res;
2080                 return 0;
2081         } else {
2082                 data->length = 0;
2083                 return res;
2084         }
2085 #endif /* PRISM2_NO_STATION_MODES */
2086 }
2087
2088
2089 static int prism2_ioctl_giwscan(struct net_device *dev,
2090                                 struct iw_request_info *info,
2091                                 struct iw_point *data, char *extra)
2092 {
2093         struct hostap_interface *iface;
2094         local_info_t *local;
2095         int res;
2096
2097         iface = netdev_priv(dev);
2098         local = iface->local;
2099
2100         if (local->iw_mode == IW_MODE_MASTER) {
2101                 /* In MASTER mode, it doesn't make sense to go around
2102                  * scanning the frequencies and make the stations we serve
2103                  * wait when what the user is really interested about is the
2104                  * list of stations and access points we are talking to.
2105                  * So, just extract results from our cache...
2106                  * Jean II */
2107
2108                 /* Translate to WE format */
2109                 res = prism2_ap_translate_scan(dev, extra);
2110                 if (res >= 0) {
2111                         printk(KERN_DEBUG "Scan result translation succeeded "
2112                                "(length=%d)\n", res);
2113                         data->length = res;
2114                         return 0;
2115                 } else {
2116                         printk(KERN_DEBUG
2117                                "Scan result translation failed (res=%d)\n",
2118                                res);
2119                         data->length = 0;
2120                         return res;
2121                 }
2122         } else {
2123                 /* Station mode */
2124                 return prism2_ioctl_giwscan_sta(dev, info, data, extra);
2125         }
2126 }
2127
2128
2129 static const struct iw_priv_args prism2_priv[] = {
2130         { PRISM2_IOCTL_MONITOR,
2131           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "monitor" },
2132         { PRISM2_IOCTL_READMIF,
2133           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
2134           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, "readmif" },
2135         { PRISM2_IOCTL_WRITEMIF,
2136           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 2, 0, "writemif" },
2137         { PRISM2_IOCTL_RESET,
2138           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "reset" },
2139         { PRISM2_IOCTL_INQUIRE,
2140           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "inquire" },
2141         { PRISM2_IOCTL_SET_RID_WORD,
2142           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "set_rid_word" },
2143         { PRISM2_IOCTL_MACCMD,
2144           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "maccmd" },
2145         { PRISM2_IOCTL_WDS_ADD,
2146           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "wds_add" },
2147         { PRISM2_IOCTL_WDS_DEL,
2148           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "wds_del" },
2149         { PRISM2_IOCTL_ADDMAC,
2150           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "addmac" },
2151         { PRISM2_IOCTL_DELMAC,
2152           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "delmac" },
2153         { PRISM2_IOCTL_KICKMAC,
2154           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "kickmac" },
2155         /* --- raw access to sub-ioctls --- */
2156         { PRISM2_IOCTL_PRISM2_PARAM,
2157           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "prism2_param" },
2158         { PRISM2_IOCTL_GET_PRISM2_PARAM,
2159           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2160           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getprism2_param" },
2161         /* --- sub-ioctls handlers --- */
2162         { PRISM2_IOCTL_PRISM2_PARAM,
2163           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "" },
2164         { PRISM2_IOCTL_GET_PRISM2_PARAM,
2165           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "" },
2166         /* --- sub-ioctls definitions --- */
2167         { PRISM2_PARAM_TXRATECTRL,
2168           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "txratectrl" },
2169         { PRISM2_PARAM_TXRATECTRL,
2170           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gettxratectrl" },
2171         { PRISM2_PARAM_BEACON_INT,
2172           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "beacon_int" },
2173         { PRISM2_PARAM_BEACON_INT,
2174           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbeacon_int" },
2175 #ifndef PRISM2_NO_STATION_MODES
2176         { PRISM2_PARAM_PSEUDO_IBSS,
2177           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "pseudo_ibss" },
2178         { PRISM2_PARAM_PSEUDO_IBSS,
2179           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getpseudo_ibss" },
2180 #endif /* PRISM2_NO_STATION_MODES */
2181         { PRISM2_PARAM_ALC,
2182           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "alc" },
2183         { PRISM2_PARAM_ALC,
2184           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getalc" },
2185         { PRISM2_PARAM_DUMP,
2186           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "dump" },
2187         { PRISM2_PARAM_DUMP,
2188           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdump" },
2189         { PRISM2_PARAM_OTHER_AP_POLICY,
2190           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "other_ap_policy" },
2191         { PRISM2_PARAM_OTHER_AP_POLICY,
2192           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getother_ap_pol" },
2193         { PRISM2_PARAM_AP_MAX_INACTIVITY,
2194           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "max_inactivity" },
2195         { PRISM2_PARAM_AP_MAX_INACTIVITY,
2196           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmax_inactivi" },
2197         { PRISM2_PARAM_AP_BRIDGE_PACKETS,
2198           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "bridge_packets" },
2199         { PRISM2_PARAM_AP_BRIDGE_PACKETS,
2200           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbridge_packe" },
2201         { PRISM2_PARAM_DTIM_PERIOD,
2202           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "dtim_period" },
2203         { PRISM2_PARAM_DTIM_PERIOD,
2204           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdtim_period" },
2205         { PRISM2_PARAM_AP_NULLFUNC_ACK,
2206           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "nullfunc_ack" },
2207         { PRISM2_PARAM_AP_NULLFUNC_ACK,
2208           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getnullfunc_ack" },
2209         { PRISM2_PARAM_MAX_WDS,
2210           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "max_wds" },
2211         { PRISM2_PARAM_MAX_WDS,
2212           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmax_wds" },
2213         { PRISM2_PARAM_AP_AUTOM_AP_WDS,
2214           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "autom_ap_wds" },
2215         { PRISM2_PARAM_AP_AUTOM_AP_WDS,
2216           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getautom_ap_wds" },
2217         { PRISM2_PARAM_AP_AUTH_ALGS,
2218           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ap_auth_algs" },
2219         { PRISM2_PARAM_AP_AUTH_ALGS,
2220           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getap_auth_algs" },
2221         { PRISM2_PARAM_MONITOR_ALLOW_FCSERR,
2222           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "allow_fcserr" },
2223         { PRISM2_PARAM_MONITOR_ALLOW_FCSERR,
2224           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getallow_fcserr" },
2225         { PRISM2_PARAM_HOST_ENCRYPT,
2226           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_encrypt" },
2227         { PRISM2_PARAM_HOST_ENCRYPT,
2228           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_encrypt" },
2229         { PRISM2_PARAM_HOST_DECRYPT,
2230           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_decrypt" },
2231         { PRISM2_PARAM_HOST_DECRYPT,
2232           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_decrypt" },
2233 #ifndef PRISM2_NO_STATION_MODES
2234         { PRISM2_PARAM_HOST_ROAMING,
2235           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_roaming" },
2236         { PRISM2_PARAM_HOST_ROAMING,
2237           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_roaming" },
2238 #endif /* PRISM2_NO_STATION_MODES */
2239         { PRISM2_PARAM_BCRX_STA_KEY,
2240           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "bcrx_sta_key" },
2241         { PRISM2_PARAM_BCRX_STA_KEY,
2242           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbcrx_sta_key" },
2243         { PRISM2_PARAM_IEEE_802_1X,
2244           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ieee_802_1x" },
2245         { PRISM2_PARAM_IEEE_802_1X,
2246           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getieee_802_1x" },
2247         { PRISM2_PARAM_ANTSEL_TX,
2248           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "antsel_tx" },
2249         { PRISM2_PARAM_ANTSEL_TX,
2250           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getantsel_tx" },
2251         { PRISM2_PARAM_ANTSEL_RX,
2252           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "antsel_rx" },
2253         { PRISM2_PARAM_ANTSEL_RX,
2254           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getantsel_rx" },
2255         { PRISM2_PARAM_MONITOR_TYPE,
2256           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "monitor_type" },
2257         { PRISM2_PARAM_MONITOR_TYPE,
2258           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmonitor_type" },
2259         { PRISM2_PARAM_WDS_TYPE,
2260           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wds_type" },
2261         { PRISM2_PARAM_WDS_TYPE,
2262           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getwds_type" },
2263         { PRISM2_PARAM_HOSTSCAN,
2264           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostscan" },
2265         { PRISM2_PARAM_HOSTSCAN,
2266           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostscan" },
2267         { PRISM2_PARAM_AP_SCAN,
2268           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ap_scan" },
2269         { PRISM2_PARAM_AP_SCAN,
2270           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getap_scan" },
2271         { PRISM2_PARAM_ENH_SEC,
2272           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "enh_sec" },
2273         { PRISM2_PARAM_ENH_SEC,
2274           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getenh_sec" },
2275 #ifdef PRISM2_IO_DEBUG
2276         { PRISM2_PARAM_IO_DEBUG,
2277           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "io_debug" },
2278         { PRISM2_PARAM_IO_DEBUG,
2279           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getio_debug" },
2280 #endif /* PRISM2_IO_DEBUG */
2281         { PRISM2_PARAM_BASIC_RATES,
2282           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "basic_rates" },
2283         { PRISM2_PARAM_BASIC_RATES,
2284           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbasic_rates" },
2285         { PRISM2_PARAM_OPER_RATES,
2286           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "oper_rates" },
2287         { PRISM2_PARAM_OPER_RATES,
2288           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getoper_rates" },
2289         { PRISM2_PARAM_HOSTAPD,
2290           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostapd" },
2291         { PRISM2_PARAM_HOSTAPD,
2292           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostapd" },
2293         { PRISM2_PARAM_HOSTAPD_STA,
2294           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostapd_sta" },
2295         { PRISM2_PARAM_HOSTAPD_STA,
2296           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostapd_sta" },
2297         { PRISM2_PARAM_WPA,
2298           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wpa" },
2299         { PRISM2_PARAM_WPA,
2300           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getwpa" },
2301         { PRISM2_PARAM_PRIVACY_INVOKED,
2302           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "privacy_invoked" },
2303         { PRISM2_PARAM_PRIVACY_INVOKED,
2304           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getprivacy_invo" },
2305         { PRISM2_PARAM_TKIP_COUNTERMEASURES,
2306           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "tkip_countermea" },
2307         { PRISM2_PARAM_TKIP_COUNTERMEASURES,
2308           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gettkip_counter" },
2309         { PRISM2_PARAM_DROP_UNENCRYPTED,
2310           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "drop_unencrypte" },
2311         { PRISM2_PARAM_DROP_UNENCRYPTED,
2312           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdrop_unencry" },
2313         { PRISM2_PARAM_SCAN_CHANNEL_MASK,
2314           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "scan_channels" },
2315         { PRISM2_PARAM_SCAN_CHANNEL_MASK,
2316           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getscan_channel" },
2317 };
2318
2319
2320 static int prism2_ioctl_priv_inquire(struct net_device *dev, int *i)
2321 {
2322         struct hostap_interface *iface;
2323         local_info_t *local;
2324
2325         iface = netdev_priv(dev);
2326         local = iface->local;
2327
2328         if (local->func->cmd(dev, HFA384X_CMDCODE_INQUIRE, *i, NULL, NULL))
2329                 return -EOPNOTSUPP;
2330
2331         return 0;
2332 }
2333
2334
2335 static int prism2_ioctl_priv_prism2_param(struct net_device *dev,
2336                                           struct iw_request_info *info,
2337                                           void *wrqu, char *extra)
2338 {
2339         struct hostap_interface *iface;
2340         local_info_t *local;
2341         int *i = (int *) extra;
2342         int param = *i;
2343         int value = *(i + 1);
2344         int ret = 0;
2345         u16 val;
2346
2347         iface = netdev_priv(dev);
2348         local = iface->local;
2349
2350         switch (param) {
2351         case PRISM2_PARAM_TXRATECTRL:
2352                 local->fw_tx_rate_control = value;
2353                 break;
2354
2355         case PRISM2_PARAM_BEACON_INT:
2356                 if (hostap_set_word(dev, HFA384X_RID_CNFBEACONINT, value) ||
2357                     local->func->reset_port(dev))
2358                         ret = -EINVAL;
2359                 else
2360                         local->beacon_int = value;
2361                 break;
2362
2363 #ifndef PRISM2_NO_STATION_MODES
2364         case PRISM2_PARAM_PSEUDO_IBSS:
2365                 if (value == local->pseudo_adhoc)
2366                         break;
2367
2368                 if (value != 0 && value != 1) {
2369                         ret = -EINVAL;
2370                         break;
2371                 }
2372
2373                 printk(KERN_DEBUG "prism2: %s: pseudo IBSS change %d -> %d\n",
2374                        dev->name, local->pseudo_adhoc, value);
2375                 local->pseudo_adhoc = value;
2376                 if (local->iw_mode != IW_MODE_ADHOC)
2377                         break;
2378
2379                 if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2380                                     hostap_get_porttype(local))) {
2381                         ret = -EOPNOTSUPP;
2382                         break;
2383                 }
2384
2385                 if (local->func->reset_port(dev))
2386                         ret = -EINVAL;
2387                 break;
2388 #endif /* PRISM2_NO_STATION_MODES */
2389
2390         case PRISM2_PARAM_ALC:
2391                 printk(KERN_DEBUG "%s: %s ALC\n", dev->name,
2392                        value == 0 ? "Disabling" : "Enabling");
2393                 val = HFA384X_TEST_CFG_BIT_ALC;
2394                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
2395                                  (HFA384X_TEST_CFG_BITS << 8),
2396                                  value == 0 ? 0 : 1, &val, NULL);
2397                 break;
2398
2399         case PRISM2_PARAM_DUMP:
2400                 local->frame_dump = value;
2401                 break;
2402
2403         case PRISM2_PARAM_OTHER_AP_POLICY:
2404                 if (value < 0 || value > 3) {
2405                         ret = -EINVAL;
2406                         break;
2407                 }
2408                 if (local->ap != NULL)
2409                         local->ap->ap_policy = value;
2410                 break;
2411
2412         case PRISM2_PARAM_AP_MAX_INACTIVITY:
2413                 if (value < 0 || value > 7 * 24 * 60 * 60) {
2414                         ret = -EINVAL;
2415                         break;
2416                 }
2417                 if (local->ap != NULL)
2418                         local->ap->max_inactivity = value * HZ;
2419                 break;
2420
2421         case PRISM2_PARAM_AP_BRIDGE_PACKETS:
2422                 if (local->ap != NULL)
2423                         local->ap->bridge_packets = value;
2424                 break;
2425
2426         case PRISM2_PARAM_DTIM_PERIOD:
2427                 if (value < 0 || value > 65535) {
2428                         ret = -EINVAL;
2429                         break;
2430                 }
2431                 if (hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD, value)
2432                     || local->func->reset_port(dev))
2433                         ret = -EINVAL;
2434                 else
2435                         local->dtim_period = value;
2436                 break;
2437
2438         case PRISM2_PARAM_AP_NULLFUNC_ACK:
2439                 if (local->ap != NULL)
2440                         local->ap->nullfunc_ack = value;
2441                 break;
2442
2443         case PRISM2_PARAM_MAX_WDS:
2444                 local->wds_max_connections = value;
2445                 break;
2446
2447         case PRISM2_PARAM_AP_AUTOM_AP_WDS:
2448                 if (local->ap != NULL) {
2449                         if (!local->ap->autom_ap_wds && value) {
2450                                 /* add WDS link to all APs in STA table */
2451                                 hostap_add_wds_links(local);
2452                         }
2453                         local->ap->autom_ap_wds = value;
2454                 }
2455                 break;
2456
2457         case PRISM2_PARAM_AP_AUTH_ALGS:
2458                 local->auth_algs = value;
2459                 if (hostap_set_auth_algs(local))
2460                         ret = -EINVAL;
2461                 break;
2462
2463         case PRISM2_PARAM_MONITOR_ALLOW_FCSERR:
2464                 local->monitor_allow_fcserr = value;
2465                 break;
2466
2467         case PRISM2_PARAM_HOST_ENCRYPT:
2468                 local->host_encrypt = value;
2469                 if (hostap_set_encryption(local) ||
2470                     local->func->reset_port(dev))
2471                         ret = -EINVAL;
2472                 break;
2473
2474         case PRISM2_PARAM_HOST_DECRYPT:
2475                 local->host_decrypt = value;
2476                 if (hostap_set_encryption(local) ||
2477                     local->func->reset_port(dev))
2478                         ret = -EINVAL;
2479                 break;
2480
2481 #ifndef PRISM2_NO_STATION_MODES
2482         case PRISM2_PARAM_HOST_ROAMING:
2483                 if (value < 0 || value > 2) {
2484                         ret = -EINVAL;
2485                         break;
2486                 }
2487                 local->host_roaming = value;
2488                 if (hostap_set_roaming(local) || local->func->reset_port(dev))
2489                         ret = -EINVAL;
2490                 break;
2491 #endif /* PRISM2_NO_STATION_MODES */
2492
2493         case PRISM2_PARAM_BCRX_STA_KEY:
2494                 local->bcrx_sta_key = value;
2495                 break;
2496
2497         case PRISM2_PARAM_IEEE_802_1X:
2498                 local->ieee_802_1x = value;
2499                 break;
2500
2501         case PRISM2_PARAM_ANTSEL_TX:
2502                 if (value < 0 || value > HOSTAP_ANTSEL_HIGH) {
2503                         ret = -EINVAL;
2504                         break;
2505                 }
2506                 local->antsel_tx = value;
2507                 hostap_set_antsel(local);
2508                 break;
2509
2510         case PRISM2_PARAM_ANTSEL_RX:
2511                 if (value < 0 || value > HOSTAP_ANTSEL_HIGH) {
2512                         ret = -EINVAL;
2513                         break;
2514                 }
2515                 local->antsel_rx = value;
2516                 hostap_set_antsel(local);
2517                 break;
2518
2519         case PRISM2_PARAM_MONITOR_TYPE:
2520                 if (value != PRISM2_MONITOR_80211 &&
2521                     value != PRISM2_MONITOR_CAPHDR &&
2522                     value != PRISM2_MONITOR_PRISM) {
2523                         ret = -EINVAL;
2524                         break;
2525                 }
2526                 local->monitor_type = value;
2527                 if (local->iw_mode == IW_MODE_MONITOR)
2528                         hostap_monitor_set_type(local);
2529                 break;
2530
2531         case PRISM2_PARAM_WDS_TYPE:
2532                 local->wds_type = value;
2533                 break;
2534
2535         case PRISM2_PARAM_HOSTSCAN:
2536         {
2537                 struct hfa384x_hostscan_request scan_req;
2538                 u16 rate;
2539
2540                 memset(&scan_req, 0, sizeof(scan_req));
2541                 scan_req.channel_list = __constant_cpu_to_le16(0x3fff);
2542                 switch (value) {
2543                 case 1: rate = HFA384X_RATES_1MBPS; break;
2544                 case 2: rate = HFA384X_RATES_2MBPS; break;
2545                 case 3: rate = HFA384X_RATES_5MBPS; break;
2546                 case 4: rate = HFA384X_RATES_11MBPS; break;
2547                 default: rate = HFA384X_RATES_1MBPS; break;
2548                 }
2549                 scan_req.txrate = cpu_to_le16(rate);
2550                 /* leave SSID empty to accept all SSIDs */
2551
2552                 if (local->iw_mode == IW_MODE_MASTER) {
2553                         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2554                                             HFA384X_PORTTYPE_BSS) ||
2555                             local->func->reset_port(dev))
2556                                 printk(KERN_DEBUG "Leaving Host AP mode "
2557                                        "for HostScan failed\n");
2558                 }
2559
2560                 if (local->func->set_rid(dev, HFA384X_RID_HOSTSCAN, &scan_req,
2561                                          sizeof(scan_req))) {
2562                         printk(KERN_DEBUG "HOSTSCAN failed\n");
2563                         ret = -EINVAL;
2564                 }
2565                 if (local->iw_mode == IW_MODE_MASTER) {
2566                         wait_queue_t __wait;
2567                         init_waitqueue_entry(&__wait, current);
2568                         add_wait_queue(&local->hostscan_wq, &__wait);
2569                         set_current_state(TASK_INTERRUPTIBLE);
2570                         schedule_timeout(HZ);
2571                         if (signal_pending(current))
2572                                 ret = -EINTR;
2573                         set_current_state(TASK_RUNNING);
2574                         remove_wait_queue(&local->hostscan_wq, &__wait);
2575
2576                         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2577                                             HFA384X_PORTTYPE_HOSTAP) ||
2578                             local->func->reset_port(dev))
2579                                 printk(KERN_DEBUG "Returning to Host AP mode "
2580                                        "after HostScan failed\n");
2581                 }
2582                 break;
2583         }
2584
2585         case PRISM2_PARAM_AP_SCAN:
2586                 local->passive_scan_interval = value;
2587                 if (timer_pending(&local->passive_scan_timer))
2588                         del_timer(&local->passive_scan_timer);
2589                 if (value > 0) {
2590                         local->passive_scan_timer.expires = jiffies +
2591                                 local->passive_scan_interval * HZ;
2592                         add_timer(&local->passive_scan_timer);
2593                 }
2594                 break;
2595
2596         case PRISM2_PARAM_ENH_SEC:
2597                 if (value < 0 || value > 3) {
2598                         ret = -EINVAL;
2599                         break;
2600                 }
2601                 local->enh_sec = value;
2602                 if (hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY,
2603                                     local->enh_sec) ||
2604                     local->func->reset_port(dev)) {
2605                         printk(KERN_INFO "%s: cnfEnhSecurity requires STA f/w "
2606                                "1.6.3 or newer\n", dev->name);
2607                         ret = -EOPNOTSUPP;
2608                 }
2609                 break;
2610
2611 #ifdef PRISM2_IO_DEBUG
2612         case PRISM2_PARAM_IO_DEBUG:
2613                 local->io_debug_enabled = value;
2614                 break;
2615 #endif /* PRISM2_IO_DEBUG */
2616
2617         case PRISM2_PARAM_BASIC_RATES:
2618                 if ((value & local->tx_rate_control) != value || value == 0) {
2619                         printk(KERN_INFO "%s: invalid basic rate set - basic "
2620                                "rates must be in supported rate set\n",
2621                                dev->name);
2622                         ret = -EINVAL;
2623                         break;
2624                 }
2625                 local->basic_rates = value;
2626                 if (hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
2627                                     local->basic_rates) ||
2628                     local->func->reset_port(dev))
2629                         ret = -EINVAL;
2630                 break;
2631
2632         case PRISM2_PARAM_OPER_RATES:
2633                 local->tx_rate_control = value;
2634                 if (hostap_set_rate(dev))
2635                         ret = -EINVAL;
2636                 break;
2637
2638         case PRISM2_PARAM_HOSTAPD:
2639                 ret = hostap_set_hostapd(local, value, 1);
2640                 break;
2641
2642         case PRISM2_PARAM_HOSTAPD_STA:
2643                 ret = hostap_set_hostapd_sta(local, value, 1);
2644                 break;
2645
2646         case PRISM2_PARAM_WPA:
2647                 local->wpa = value;
2648                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
2649                         ret = -EOPNOTSUPP;
2650                 else if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE,
2651                                          value ? 1 : 0))
2652                         ret = -EINVAL;
2653                 break;
2654
2655         case PRISM2_PARAM_PRIVACY_INVOKED:
2656                 local->privacy_invoked = value;
2657                 if (hostap_set_encryption(local) ||
2658                     local->func->reset_port(dev))
2659                         ret = -EINVAL;
2660                 break;
2661
2662         case PRISM2_PARAM_TKIP_COUNTERMEASURES:
2663                 local->tkip_countermeasures = value;
2664                 break;
2665
2666         case PRISM2_PARAM_DROP_UNENCRYPTED:
2667                 local->drop_unencrypted = value;
2668                 break;
2669
2670         case PRISM2_PARAM_SCAN_CHANNEL_MASK:
2671                 local->scan_channel_mask = value;
2672                 break;
2673
2674         default:
2675                 printk(KERN_DEBUG "%s: prism2_param: unknown param %d\n",
2676                        dev->name, param);
2677                 ret = -EOPNOTSUPP;
2678                 break;
2679         }
2680
2681         return ret;
2682 }
2683
2684
2685 static int prism2_ioctl_priv_get_prism2_param(struct net_device *dev,
2686                                               struct iw_request_info *info,
2687                                               void *wrqu, char *extra)
2688 {
2689         struct hostap_interface *iface;
2690         local_info_t *local;
2691         int *param = (int *) extra;
2692         int ret = 0;
2693
2694         iface = netdev_priv(dev);
2695         local = iface->local;
2696
2697         switch (*param) {
2698         case PRISM2_PARAM_TXRATECTRL:
2699                 *param = local->fw_tx_rate_control;
2700                 break;
2701
2702         case PRISM2_PARAM_BEACON_INT:
2703                 *param = local->beacon_int;
2704                 break;
2705
2706         case PRISM2_PARAM_PSEUDO_IBSS:
2707                 *param = local->pseudo_adhoc;
2708                 break;
2709
2710         case PRISM2_PARAM_ALC:
2711                 ret = -EOPNOTSUPP; /* FIX */
2712                 break;
2713
2714         case PRISM2_PARAM_DUMP:
2715                 *param = local->frame_dump;
2716                 break;
2717
2718         case PRISM2_PARAM_OTHER_AP_POLICY:
2719                 if (local->ap != NULL)
2720                         *param = local->ap->ap_policy;
2721                 else
2722                         ret = -EOPNOTSUPP;
2723                 break;
2724
2725         case PRISM2_PARAM_AP_MAX_INACTIVITY:
2726                 if (local->ap != NULL)
2727                         *param = local->ap->max_inactivity / HZ;
2728                 else
2729                         ret = -EOPNOTSUPP;
2730                 break;
2731
2732         case PRISM2_PARAM_AP_BRIDGE_PACKETS:
2733                 if (local->ap != NULL)
2734                         *param = local->ap->bridge_packets;
2735                 else
2736                         ret = -EOPNOTSUPP;
2737                 break;
2738
2739         case PRISM2_PARAM_DTIM_PERIOD:
2740                 *param = local->dtim_period;
2741                 break;
2742
2743         case PRISM2_PARAM_AP_NULLFUNC_ACK:
2744                 if (local->ap != NULL)
2745                         *param = local->ap->nullfunc_ack;
2746                 else
2747                         ret = -EOPNOTSUPP;
2748                 break;
2749
2750         case PRISM2_PARAM_MAX_WDS:
2751                 *param = local->wds_max_connections;
2752                 break;
2753
2754         case PRISM2_PARAM_AP_AUTOM_AP_WDS:
2755                 if (local->ap != NULL)
2756                         *param = local->ap->autom_ap_wds;
2757                 else
2758                         ret = -EOPNOTSUPP;
2759                 break;
2760
2761         case PRISM2_PARAM_AP_AUTH_ALGS:
2762                 *param = local->auth_algs;
2763                 break;
2764
2765         case PRISM2_PARAM_MONITOR_ALLOW_FCSERR:
2766                 *param = local->monitor_allow_fcserr;
2767                 break;
2768
2769         case PRISM2_PARAM_HOST_ENCRYPT:
2770                 *param = local->host_encrypt;
2771                 break;
2772
2773         case PRISM2_PARAM_HOST_DECRYPT:
2774                 *param = local->host_decrypt;
2775                 break;
2776
2777         case PRISM2_PARAM_HOST_ROAMING:
2778                 *param = local->host_roaming;
2779                 break;
2780
2781         case PRISM2_PARAM_BCRX_STA_KEY:
2782                 *param = local->bcrx_sta_key;
2783                 break;
2784
2785         case PRISM2_PARAM_IEEE_802_1X:
2786                 *param = local->ieee_802_1x;
2787                 break;
2788
2789         case PRISM2_PARAM_ANTSEL_TX:
2790                 *param = local->antsel_tx;
2791                 break;
2792
2793         case PRISM2_PARAM_ANTSEL_RX:
2794                 *param = local->antsel_rx;
2795                 break;
2796
2797         case PRISM2_PARAM_MONITOR_TYPE:
2798                 *param = local->monitor_type;
2799                 break;
2800
2801         case PRISM2_PARAM_WDS_TYPE:
2802                 *param = local->wds_type;
2803                 break;
2804
2805         case PRISM2_PARAM_HOSTSCAN:
2806                 ret = -EOPNOTSUPP;
2807                 break;
2808
2809         case PRISM2_PARAM_AP_SCAN:
2810                 *param = local->passive_scan_interval;
2811                 break;
2812
2813         case PRISM2_PARAM_ENH_SEC:
2814                 *param = local->enh_sec;
2815                 break;
2816
2817 #ifdef PRISM2_IO_DEBUG
2818         case PRISM2_PARAM_IO_DEBUG:
2819                 *param = local->io_debug_enabled;
2820                 break;
2821 #endif /* PRISM2_IO_DEBUG */
2822
2823         case PRISM2_PARAM_BASIC_RATES:
2824                 *param = local->basic_rates;
2825                 break;
2826
2827         case PRISM2_PARAM_OPER_RATES:
2828                 *param = local->tx_rate_control;
2829                 break;
2830
2831         case PRISM2_PARAM_HOSTAPD:
2832                 *param = local->hostapd;
2833                 break;
2834
2835         case PRISM2_PARAM_HOSTAPD_STA:
2836                 *param = local->hostapd_sta;
2837                 break;
2838
2839         case PRISM2_PARAM_WPA:
2840                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
2841                         ret = -EOPNOTSUPP;
2842                 *param = local->wpa;
2843                 break;
2844
2845         case PRISM2_PARAM_PRIVACY_INVOKED:
2846                 *param = local->privacy_invoked;
2847                 break;
2848
2849         case PRISM2_PARAM_TKIP_COUNTERMEASURES:
2850                 *param = local->tkip_countermeasures;
2851                 break;
2852
2853         case PRISM2_PARAM_DROP_UNENCRYPTED:
2854                 *param = local->drop_unencrypted;
2855                 break;
2856
2857         case PRISM2_PARAM_SCAN_CHANNEL_MASK:
2858                 *param = local->scan_channel_mask;
2859                 break;
2860
2861         default:
2862                 printk(KERN_DEBUG "%s: get_prism2_param: unknown param %d\n",
2863                        dev->name, *param);
2864                 ret = -EOPNOTSUPP;
2865                 break;
2866         }
2867
2868         return ret;
2869 }
2870
2871
2872 static int prism2_ioctl_priv_readmif(struct net_device *dev,
2873                                      struct iw_request_info *info,
2874                                      void *wrqu, char *extra)
2875 {
2876         struct hostap_interface *iface;
2877         local_info_t *local;
2878         u16 resp0;
2879
2880         iface = netdev_priv(dev);
2881         local = iface->local;
2882
2883         if (local->func->cmd(dev, HFA384X_CMDCODE_READMIF, *extra, NULL,
2884                              &resp0))
2885                 return -EOPNOTSUPP;
2886         else
2887                 *extra = resp0;
2888
2889         return 0;
2890 }
2891
2892
2893 static int prism2_ioctl_priv_writemif(struct net_device *dev,
2894                                       struct iw_request_info *info,
2895                                       void *wrqu, char *extra)
2896 {
2897         struct hostap_interface *iface;
2898         local_info_t *local;
2899         u16 cr, val;
2900
2901         iface = netdev_priv(dev);
2902         local = iface->local;
2903
2904         cr = *extra;
2905         val = *(extra + 1);
2906         if (local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF, cr, &val, NULL))
2907                 return -EOPNOTSUPP;
2908
2909         return 0;
2910 }
2911
2912
2913 static int prism2_ioctl_priv_monitor(struct net_device *dev, int *i)
2914 {
2915         struct hostap_interface *iface;
2916         local_info_t *local;
2917         int ret = 0;
2918         u32 mode;
2919
2920         iface = netdev_priv(dev);
2921         local = iface->local;
2922
2923         printk(KERN_DEBUG "%s: process %d (%s) used deprecated iwpriv monitor "
2924                "- update software to use iwconfig mode monitor\n",
2925                dev->name, current->pid, current->comm);
2926
2927         /* Backward compatibility code - this can be removed at some point */
2928
2929         if (*i == 0) {
2930                 /* Disable monitor mode - old mode was not saved, so go to
2931                  * Master mode */
2932                 mode = IW_MODE_MASTER;
2933                 ret = prism2_ioctl_siwmode(dev, NULL, &mode, NULL);
2934         } else if (*i == 1) {
2935                 /* netlink socket mode is not supported anymore since it did
2936                  * not separate different devices from each other and was not
2937                  * best method for delivering large amount of packets to
2938                  * user space */
2939                 ret = -EOPNOTSUPP;
2940         } else if (*i == 2 || *i == 3) {
2941                 switch (*i) {
2942                 case 2:
2943                         local->monitor_type = PRISM2_MONITOR_80211;
2944                         break;
2945                 case 3:
2946                         local->monitor_type = PRISM2_MONITOR_PRISM;
2947                         break;
2948                 }
2949                 mode = IW_MODE_MONITOR;
2950                 ret = prism2_ioctl_siwmode(dev, NULL, &mode, NULL);
2951                 hostap_monitor_mode_enable(local);
2952         } else
2953                 ret = -EINVAL;
2954
2955         return ret;
2956 }
2957
2958
2959 static int prism2_ioctl_priv_reset(struct net_device *dev, int *i)
2960 {
2961         struct hostap_interface *iface;
2962         local_info_t *local;
2963
2964         iface = netdev_priv(dev);
2965         local = iface->local;
2966
2967         printk(KERN_DEBUG "%s: manual reset request(%d)\n", dev->name, *i);
2968         switch (*i) {
2969         case 0:
2970                 /* Disable and enable card */
2971                 local->func->hw_shutdown(dev, 1);
2972                 local->func->hw_config(dev, 0);
2973                 break;
2974
2975         case 1:
2976                 /* COR sreset */
2977                 local->func->hw_reset(dev);
2978                 break;
2979
2980         case 2:
2981                 /* Disable and enable port 0 */
2982                 local->func->reset_port(dev);
2983                 break;
2984
2985         case 3:
2986                 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
2987                 if (local->func->cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL,
2988                                      NULL))
2989                         return -EINVAL;
2990                 break;
2991
2992         case 4:
2993                 if (local->func->cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL,
2994                                      NULL))
2995                         return -EINVAL;
2996                 break;
2997
2998         default:
2999                 printk(KERN_DEBUG "Unknown reset request %d\n", *i);
3000                 return -EOPNOTSUPP;
3001         }
3002
3003         return 0;
3004 }
3005
3006
3007 static int prism2_ioctl_priv_set_rid_word(struct net_device *dev, int *i)
3008 {
3009         int rid = *i;
3010         int value = *(i + 1);
3011
3012         printk(KERN_DEBUG "%s: Set RID[0x%X] = %d\n", dev->name, rid, value);
3013
3014         if (hostap_set_word(dev, rid, value))
3015                 return -EINVAL;
3016
3017         return 0;
3018 }
3019
3020
3021 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3022 static int ap_mac_cmd_ioctl(local_info_t *local, int *cmd)
3023 {
3024         int ret = 0;
3025
3026         switch (*cmd) {
3027         case AP_MAC_CMD_POLICY_OPEN:
3028                 local->ap->mac_restrictions.policy = MAC_POLICY_OPEN;
3029                 break;
3030         case AP_MAC_CMD_POLICY_ALLOW:
3031                 local->ap->mac_restrictions.policy = MAC_POLICY_ALLOW;
3032                 break;
3033         case AP_MAC_CMD_POLICY_DENY:
3034                 local->ap->mac_restrictions.policy = MAC_POLICY_DENY;
3035                 break;
3036         case AP_MAC_CMD_FLUSH:
3037                 ap_control_flush_macs(&local->ap->mac_restrictions);
3038                 break;
3039         case AP_MAC_CMD_KICKALL:
3040                 ap_control_kickall(local->ap);
3041                 hostap_deauth_all_stas(local->dev, local->ap, 0);
3042                 break;
3043         default:
3044                 ret = -EOPNOTSUPP;
3045                 break;
3046         }
3047
3048         return ret;
3049 }
3050 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3051
3052
3053 #ifdef PRISM2_DOWNLOAD_SUPPORT
3054 static int prism2_ioctl_priv_download(local_info_t *local, struct iw_point *p)
3055 {
3056         struct prism2_download_param *param;
3057         int ret = 0;
3058
3059         if (p->length < sizeof(struct prism2_download_param) ||
3060             p->length > 1024 || !p->pointer)
3061                 return -EINVAL;
3062
3063         param = (struct prism2_download_param *)
3064                 kmalloc(p->length, GFP_KERNEL);
3065         if (param == NULL)
3066                 return -ENOMEM;
3067
3068         if (copy_from_user(param, p->pointer, p->length)) {
3069                 ret = -EFAULT;
3070                 goto out;
3071         }
3072
3073         if (p->length < sizeof(struct prism2_download_param) +
3074             param->num_areas * sizeof(struct prism2_download_area)) {
3075                 ret = -EINVAL;
3076                 goto out;
3077         }
3078
3079         ret = local->func->download(local, param);
3080
3081  out:
3082         kfree(param);
3083         return ret;
3084 }
3085 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3086
3087
3088 static int prism2_set_genericelement(struct net_device *dev, u8 *elem,
3089                                      size_t len)
3090 {
3091         struct hostap_interface *iface = dev->priv;
3092         local_info_t *local = iface->local;
3093         u8 *buf;
3094
3095         /*
3096          * Add 16-bit length in the beginning of the buffer because Prism2 RID
3097          * includes it.
3098          */
3099         buf = kmalloc(len + 2, GFP_KERNEL);
3100         if (buf == NULL)
3101                 return -ENOMEM;
3102
3103         *((u16 *) buf) = cpu_to_le16(len);
3104         memcpy(buf + 2, elem, len);
3105
3106         kfree(local->generic_elem);
3107         local->generic_elem = buf;
3108         local->generic_elem_len = len + 2;
3109
3110         return local->func->set_rid(local->dev, HFA384X_RID_GENERICELEMENT,
3111                                     buf, len + 2);
3112 }
3113
3114
3115 static int prism2_ioctl_siwauth(struct net_device *dev,
3116                                 struct iw_request_info *info,
3117                                 struct iw_param *data, char *extra)
3118 {
3119         struct hostap_interface *iface = dev->priv;
3120         local_info_t *local = iface->local;
3121
3122         switch (data->flags & IW_AUTH_INDEX) {
3123         case IW_AUTH_WPA_VERSION:
3124         case IW_AUTH_CIPHER_PAIRWISE:
3125         case IW_AUTH_CIPHER_GROUP:
3126         case IW_AUTH_KEY_MGMT:
3127                 /*
3128                  * Host AP driver does not use these parameters and allows
3129                  * wpa_supplicant to control them internally.
3130                  */
3131                 break;
3132         case IW_AUTH_TKIP_COUNTERMEASURES:
3133                 local->tkip_countermeasures = data->value;
3134                 break;
3135         case IW_AUTH_DROP_UNENCRYPTED:
3136                 local->drop_unencrypted = data->value;
3137                 break;
3138         case IW_AUTH_80211_AUTH_ALG:
3139                 local->auth_algs = data->value;
3140                 break;
3141         case IW_AUTH_WPA_ENABLED:
3142                 if (data->value == 0) {
3143                         local->wpa = 0;
3144                         if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
3145                                 break;
3146                         prism2_set_genericelement(dev, "", 0);
3147                         local->host_roaming = 0;
3148                         local->privacy_invoked = 0;
3149                         if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE,
3150                                             0) ||
3151                             hostap_set_roaming(local) ||
3152                             hostap_set_encryption(local) ||
3153                             local->func->reset_port(dev))
3154                                 return -EINVAL;
3155                         break;
3156                 }
3157                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
3158                         return -EOPNOTSUPP;
3159                 local->host_roaming = 2;
3160                 local->privacy_invoked = 1;
3161                 local->wpa = 1;
3162                 if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1) ||
3163                     hostap_set_roaming(local) ||
3164                     hostap_set_encryption(local) ||
3165                     local->func->reset_port(dev))
3166                         return -EINVAL;
3167                 break;
3168         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3169                 local->ieee_802_1x = data->value;
3170                 break;
3171         case IW_AUTH_PRIVACY_INVOKED:
3172                 local->privacy_invoked = data->value;
3173                 break;
3174         default:
3175                 return -EOPNOTSUPP;
3176         }
3177         return 0;
3178 }
3179
3180
3181 static int prism2_ioctl_giwauth(struct net_device *dev,
3182                                 struct iw_request_info *info,
3183                                 struct iw_param *data, char *extra)
3184 {
3185         struct hostap_interface *iface = dev->priv;
3186         local_info_t *local = iface->local;
3187
3188         switch (data->flags & IW_AUTH_INDEX) {
3189         case IW_AUTH_WPA_VERSION:
3190         case IW_AUTH_CIPHER_PAIRWISE:
3191         case IW_AUTH_CIPHER_GROUP:
3192         case IW_AUTH_KEY_MGMT:
3193                 /*
3194                  * Host AP driver does not use these parameters and allows
3195                  * wpa_supplicant to control them internally.
3196                  */
3197                 return -EOPNOTSUPP;
3198         case IW_AUTH_TKIP_COUNTERMEASURES:
3199                 data->value = local->tkip_countermeasures;
3200                 break;
3201         case IW_AUTH_DROP_UNENCRYPTED:
3202                 data->value = local->drop_unencrypted;
3203                 break;
3204         case IW_AUTH_80211_AUTH_ALG:
3205                 data->value = local->auth_algs;
3206                 break;
3207         case IW_AUTH_WPA_ENABLED:
3208                 data->value = local->wpa;
3209                 break;
3210         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3211                 data->value = local->ieee_802_1x;
3212                 break;
3213         default:
3214                 return -EOPNOTSUPP;
3215         }
3216         return 0;
3217 }
3218
3219
3220 static int prism2_ioctl_siwencodeext(struct net_device *dev,
3221                                      struct iw_request_info *info,
3222                                      struct iw_point *erq, char *extra)
3223 {
3224         struct hostap_interface *iface = dev->priv;
3225         local_info_t *local = iface->local;
3226         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
3227         int i, ret = 0;
3228         struct ieee80211_crypto_ops *ops;
3229         struct ieee80211_crypt_data **crypt;
3230         void *sta_ptr;
3231         u8 *addr;
3232         const char *alg, *module;
3233
3234         i = erq->flags & IW_ENCODE_INDEX;
3235         if (i > WEP_KEYS)
3236                 return -EINVAL;
3237         if (i < 1 || i > WEP_KEYS)
3238                 i = local->tx_keyidx;
3239         else
3240                 i--;
3241         if (i < 0 || i >= WEP_KEYS)
3242                 return -EINVAL;
3243
3244         addr = ext->addr.sa_data;
3245         if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff &&
3246             addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) {
3247                 sta_ptr = NULL;
3248                 crypt = &local->crypt[i];
3249         } else {
3250                 if (i != 0)
3251                         return -EINVAL;
3252                 sta_ptr = ap_crypt_get_ptrs(local->ap, addr, 0, &crypt);
3253                 if (sta_ptr == NULL) {
3254                         if (local->iw_mode == IW_MODE_INFRA) {
3255                                 /*
3256                                  * TODO: add STA entry for the current AP so
3257                                  * that unicast key can be used. For now, this
3258                                  * is emulated by using default key idx 0.
3259                                  */
3260                                 i = 0;
3261                                 crypt = &local->crypt[i];
3262                         } else
3263                                 return -EINVAL;
3264                 }
3265         }
3266
3267         if ((erq->flags & IW_ENCODE_DISABLED) ||
3268             ext->alg == IW_ENCODE_ALG_NONE) {
3269                 if (*crypt)
3270                         prism2_crypt_delayed_deinit(local, crypt);
3271                 goto done;
3272         }
3273
3274         switch (ext->alg) {
3275         case IW_ENCODE_ALG_WEP:
3276                 alg = "WEP";
3277                 module = "ieee80211_crypt_wep";
3278                 break;
3279         case IW_ENCODE_ALG_TKIP:
3280                 alg = "TKIP";
3281                 module = "ieee80211_crypt_tkip";
3282                 break;
3283         case IW_ENCODE_ALG_CCMP:
3284                 alg = "CCMP";
3285                 module = "ieee80211_crypt_ccmp";
3286                 break;
3287         default:
3288                 printk(KERN_DEBUG "%s: unsupported algorithm %d\n",
3289                        local->dev->name, ext->alg);
3290                 ret = -EOPNOTSUPP;
3291                 goto done;
3292         }
3293
3294         ops = ieee80211_get_crypto_ops(alg);
3295         if (ops == NULL) {
3296                 request_module(module);
3297                 ops = ieee80211_get_crypto_ops(alg);
3298         }
3299         if (ops == NULL) {
3300                 printk(KERN_DEBUG "%s: unknown crypto alg '%s'\n",
3301                        local->dev->name, alg);
3302                 ret = -EOPNOTSUPP;
3303                 goto done;
3304         }
3305
3306         if (sta_ptr || ext->alg != IW_ENCODE_ALG_WEP) {
3307                 /*
3308                  * Per station encryption and other than WEP algorithms
3309                  * require host-based encryption, so force them on
3310                  * automatically.
3311                  */
3312                 local->host_decrypt = local->host_encrypt = 1;
3313         }
3314
3315         if (*crypt == NULL || (*crypt)->ops != ops) {
3316                 struct ieee80211_crypt_data *new_crypt;
3317
3318                 prism2_crypt_delayed_deinit(local, crypt);
3319
3320                 new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
3321                                 GFP_KERNEL);
3322                 if (new_crypt == NULL) {
3323                         ret = -ENOMEM;
3324                         goto done;
3325                 }
3326                 new_crypt->ops = ops;
3327                 new_crypt->priv = new_crypt->ops->init(i);
3328                 if (new_crypt->priv == NULL) {
3329                         kfree(new_crypt);
3330                         ret = -EINVAL;
3331                         goto done;
3332                 }
3333
3334                 *crypt = new_crypt;
3335         }
3336
3337         /*
3338          * TODO: if ext_flags does not have IW_ENCODE_EXT_RX_SEQ_VALID, the
3339          * existing seq# should not be changed.
3340          * TODO: if ext_flags has IW_ENCODE_EXT_TX_SEQ_VALID, next TX seq#
3341          * should be changed to something else than zero.
3342          */
3343         if ((!(ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) || ext->key_len > 0)
3344             && (*crypt)->ops->set_key &&
3345             (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
3346                                    (*crypt)->priv) < 0) {
3347                 printk(KERN_DEBUG "%s: key setting failed\n",
3348                        local->dev->name);
3349                 ret = -EINVAL;
3350                 goto done;
3351         }
3352
3353         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
3354                 if (!sta_ptr)
3355                         local->tx_keyidx = i;
3356         }
3357
3358
3359         if (sta_ptr == NULL && ext->key_len > 0) {
3360                 int first = 1, j;
3361                 for (j = 0; j < WEP_KEYS; j++) {
3362                         if (j != i && local->crypt[j]) {
3363                                 first = 0;
3364                                 break;
3365                         }
3366                 }
3367                 if (first)
3368                         local->tx_keyidx = i;
3369         }
3370
3371  done:
3372         if (sta_ptr)
3373                 hostap_handle_sta_release(sta_ptr);
3374
3375         local->open_wep = erq->flags & IW_ENCODE_OPEN;
3376
3377         /*
3378          * Do not reset port0 if card is in Managed mode since resetting will
3379          * generate new IEEE 802.11 authentication which may end up in looping
3380          * with IEEE 802.1X. Prism2 documentation seem to require port reset
3381          * after WEP configuration. However, keys are apparently changed at
3382          * least in Managed mode.
3383          */
3384         if (ret == 0 &&
3385             (hostap_set_encryption(local) ||
3386              (local->iw_mode != IW_MODE_INFRA &&
3387               local->func->reset_port(local->dev))))
3388                 ret = -EINVAL;
3389
3390         return ret;
3391 }
3392
3393
3394 static int prism2_ioctl_giwencodeext(struct net_device *dev,
3395                                      struct iw_request_info *info,
3396                                      struct iw_point *erq, char *extra)
3397 {
3398         struct hostap_interface *iface = dev->priv;
3399         local_info_t *local = iface->local;
3400         struct ieee80211_crypt_data **crypt;
3401         void *sta_ptr;
3402         int max_key_len, i;
3403         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
3404         u8 *addr;
3405
3406         max_key_len = erq->length - sizeof(*ext);
3407         if (max_key_len < 0)
3408                 return -EINVAL;
3409
3410         i = erq->flags & IW_ENCODE_INDEX;
3411         if (i < 1 || i > WEP_KEYS)
3412                 i = local->tx_keyidx;
3413         else
3414                 i--;
3415
3416         addr = ext->addr.sa_data;
3417         if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff &&
3418             addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) {
3419                 sta_ptr = NULL;
3420                 crypt = &local->crypt[i];
3421         } else {
3422                 i = 0;
3423                 sta_ptr = ap_crypt_get_ptrs(local->ap, addr, 0, &crypt);
3424                 if (sta_ptr == NULL)
3425                         return -EINVAL;
3426         }
3427         erq->flags = i + 1;
3428         memset(ext, 0, sizeof(*ext));
3429
3430         if (*crypt == NULL || (*crypt)->ops == NULL) {
3431                 ext->alg = IW_ENCODE_ALG_NONE;
3432                 ext->key_len = 0;
3433                 erq->flags |= IW_ENCODE_DISABLED;
3434         } else {
3435                 if (strcmp((*crypt)->ops->name, "WEP") == 0)
3436                         ext->alg = IW_ENCODE_ALG_WEP;
3437                 else if (strcmp((*crypt)->ops->name, "TKIP") == 0)
3438                         ext->alg = IW_ENCODE_ALG_TKIP;
3439                 else if (strcmp((*crypt)->ops->name, "CCMP") == 0)
3440                         ext->alg = IW_ENCODE_ALG_CCMP;
3441                 else
3442                         return -EINVAL;
3443
3444                 if ((*crypt)->ops->get_key) {
3445                         ext->key_len =
3446                                 (*crypt)->ops->get_key(ext->key,
3447                                                        max_key_len,
3448                                                        ext->tx_seq,
3449                                                        (*crypt)->priv);
3450                         if (ext->key_len &&
3451                             (ext->alg == IW_ENCODE_ALG_TKIP ||
3452                              ext->alg == IW_ENCODE_ALG_CCMP))
3453                                 ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
3454                 }
3455         }
3456
3457         if (sta_ptr)
3458                 hostap_handle_sta_release(sta_ptr);
3459
3460         return 0;
3461 }
3462
3463
3464 static int prism2_ioctl_set_encryption(local_info_t *local,
3465                                        struct prism2_hostapd_param *param,
3466                                        int param_len)
3467 {
3468         int ret = 0;
3469         struct ieee80211_crypto_ops *ops;
3470         struct ieee80211_crypt_data **crypt;
3471         void *sta_ptr;
3472
3473         param->u.crypt.err = 0;
3474         param->u.crypt.alg[HOSTAP_CRYPT_ALG_NAME_LEN - 1] = '\0';
3475
3476         if (param_len !=
3477             (int) ((char *) param->u.crypt.key - (char *) param) +
3478             param->u.crypt.key_len)
3479                 return -EINVAL;
3480
3481         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3482             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3483             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3484                 if (param->u.crypt.idx >= WEP_KEYS)
3485                         return -EINVAL;
3486                 sta_ptr = NULL;
3487                 crypt = &local->crypt[param->u.crypt.idx];
3488         } else {
3489                 if (param->u.crypt.idx)
3490                         return -EINVAL;
3491                 sta_ptr = ap_crypt_get_ptrs(
3492                         local->ap, param->sta_addr,
3493                         (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_PERMANENT),
3494                         &crypt);
3495
3496                 if (sta_ptr == NULL) {
3497                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
3498                         return -EINVAL;
3499                 }
3500         }
3501
3502         if (strcmp(param->u.crypt.alg, "none") == 0) {
3503                 if (crypt)
3504                         prism2_crypt_delayed_deinit(local, crypt);
3505                 goto done;
3506         }
3507
3508         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3509         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
3510                 request_module("ieee80211_crypt_wep");
3511                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3512         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
3513                 request_module("ieee80211_crypt_tkip");
3514                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3515         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
3516                 request_module("ieee80211_crypt_ccmp");
3517                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3518         }
3519         if (ops == NULL) {
3520                 printk(KERN_DEBUG "%s: unknown crypto alg '%s'\n",
3521                        local->dev->name, param->u.crypt.alg);
3522                 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ALG;
3523                 ret = -EINVAL;
3524                 goto done;
3525         }
3526
3527         /* station based encryption and other than WEP algorithms require
3528          * host-based encryption, so force them on automatically */
3529         local->host_decrypt = local->host_encrypt = 1;
3530
3531         if (*crypt == NULL || (*crypt)->ops != ops) {
3532                 struct ieee80211_crypt_data *new_crypt;
3533
3534                 prism2_crypt_delayed_deinit(local, crypt);
3535
3536                 new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
3537                                 GFP_KERNEL);
3538                 if (new_crypt == NULL) {
3539                         ret = -ENOMEM;
3540                         goto done;
3541                 }
3542                 new_crypt->ops = ops;
3543                 new_crypt->priv = new_crypt->ops->init(param->u.crypt.idx);
3544                 if (new_crypt->priv == NULL) {
3545                         kfree(new_crypt);
3546                         param->u.crypt.err =
3547                                 HOSTAP_CRYPT_ERR_CRYPT_INIT_FAILED;
3548                         ret = -EINVAL;
3549                         goto done;
3550                 }
3551
3552                 *crypt = new_crypt;
3553         }
3554
3555         if ((!(param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) ||
3556              param->u.crypt.key_len > 0) && (*crypt)->ops->set_key &&
3557             (*crypt)->ops->set_key(param->u.crypt.key,
3558                                    param->u.crypt.key_len, param->u.crypt.seq,
3559                                    (*crypt)->priv) < 0) {
3560                 printk(KERN_DEBUG "%s: key setting failed\n",
3561                        local->dev->name);
3562                 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
3563                 ret = -EINVAL;
3564                 goto done;
3565         }
3566
3567         if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
3568                 if (!sta_ptr)
3569                         local->tx_keyidx = param->u.crypt.idx;
3570                 else if (param->u.crypt.idx) {
3571                         printk(KERN_DEBUG "%s: TX key idx setting failed\n",
3572                                local->dev->name);
3573                         param->u.crypt.err =
3574                                 HOSTAP_CRYPT_ERR_TX_KEY_SET_FAILED;
3575                         ret = -EINVAL;
3576                         goto done;
3577                 }
3578         }
3579
3580  done:
3581         if (sta_ptr)
3582                 hostap_handle_sta_release(sta_ptr);
3583
3584         /* Do not reset port0 if card is in Managed mode since resetting will
3585          * generate new IEEE 802.11 authentication which may end up in looping
3586          * with IEEE 802.1X. Prism2 documentation seem to require port reset
3587          * after WEP configuration. However, keys are apparently changed at
3588          * least in Managed mode. */
3589         if (ret == 0 &&
3590             (hostap_set_encryption(local) ||
3591              (local->iw_mode != IW_MODE_INFRA &&
3592               local->func->reset_port(local->dev)))) {
3593                 param->u.crypt.err = HOSTAP_CRYPT_ERR_CARD_CONF_FAILED;
3594                 return -EINVAL;
3595         }
3596
3597         return ret;
3598 }
3599
3600
3601 static int prism2_ioctl_get_encryption(local_info_t *local,
3602                                        struct prism2_hostapd_param *param,
3603                                        int param_len)
3604 {
3605         struct ieee80211_crypt_data **crypt;
3606         void *sta_ptr;
3607         int max_key_len;
3608
3609         param->u.crypt.err = 0;
3610
3611         max_key_len = param_len -
3612                 (int) ((char *) param->u.crypt.key - (char *) param);
3613         if (max_key_len < 0)
3614                 return -EINVAL;
3615
3616         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3617             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3618             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3619                 sta_ptr = NULL;
3620                 if (param->u.crypt.idx >= WEP_KEYS)
3621                         param->u.crypt.idx = local->tx_keyidx;
3622                 crypt = &local->crypt[param->u.crypt.idx];
3623         } else {
3624                 param->u.crypt.idx = 0;
3625                 sta_ptr = ap_crypt_get_ptrs(local->ap, param->sta_addr, 0,
3626                                             &crypt);
3627
3628                 if (sta_ptr == NULL) {
3629                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
3630                         return -EINVAL;
3631                 }
3632         }
3633
3634         if (*crypt == NULL || (*crypt)->ops == NULL) {
3635                 memcpy(param->u.crypt.alg, "none", 5);
3636                 param->u.crypt.key_len = 0;
3637                 param->u.crypt.idx = 0xff;
3638         } else {
3639                 strncpy(param->u.crypt.alg, (*crypt)->ops->name,
3640                         HOSTAP_CRYPT_ALG_NAME_LEN);
3641                 param->u.crypt.key_len = 0;
3642
3643                 memset(param->u.crypt.seq, 0, 8);
3644                 if ((*crypt)->ops->get_key) {
3645                         param->u.crypt.key_len =
3646                                 (*crypt)->ops->get_key(param->u.crypt.key,
3647                                                        max_key_len,
3648                                                        param->u.crypt.seq,
3649                                                        (*crypt)->priv);
3650                 }
3651         }
3652
3653         if (sta_ptr)
3654                 hostap_handle_sta_release(sta_ptr);
3655
3656         return 0;
3657 }
3658
3659
3660 static int prism2_ioctl_get_rid(local_info_t *local,
3661                                 struct prism2_hostapd_param *param,
3662                                 int param_len)
3663 {
3664         int max_len, res;
3665
3666         max_len = param_len - PRISM2_HOSTAPD_RID_HDR_LEN;
3667         if (max_len < 0)
3668                 return -EINVAL;
3669
3670         res = local->func->get_rid(local->dev, param->u.rid.rid,
3671                                    param->u.rid.data, param->u.rid.len, 0);
3672         if (res >= 0) {
3673                 param->u.rid.len = res;
3674                 return 0;
3675         }
3676
3677         return res;
3678 }
3679
3680
3681 static int prism2_ioctl_set_rid(local_info_t *local,
3682                                 struct prism2_hostapd_param *param,
3683                                 int param_len)
3684 {
3685         int max_len;
3686
3687         max_len = param_len - PRISM2_HOSTAPD_RID_HDR_LEN;
3688         if (max_len < 0 || max_len < param->u.rid.len)
3689                 return -EINVAL;
3690
3691         return local->func->set_rid(local->dev, param->u.rid.rid,
3692                                     param->u.rid.data, param->u.rid.len);
3693 }
3694
3695
3696 static int prism2_ioctl_set_assoc_ap_addr(local_info_t *local,
3697                                           struct prism2_hostapd_param *param,
3698                                           int param_len)
3699 {
3700         printk(KERN_DEBUG "%ssta: associated as client with AP " MACSTR "\n",
3701                local->dev->name, MAC2STR(param->sta_addr));
3702         memcpy(local->assoc_ap_addr, param->sta_addr, ETH_ALEN);
3703         return 0;
3704 }
3705
3706
3707 static int prism2_ioctl_siwgenie(struct net_device *dev,
3708                                  struct iw_request_info *info,
3709                                  struct iw_point *data, char *extra)
3710 {
3711         return prism2_set_genericelement(dev, extra, data->length);
3712 }
3713
3714
3715 static int prism2_ioctl_giwgenie(struct net_device *dev,
3716                                  struct iw_request_info *info,
3717                                  struct iw_point *data, char *extra)
3718 {
3719         struct hostap_interface *iface = dev->priv;
3720         local_info_t *local = iface->local;
3721         int len = local->generic_elem_len - 2;
3722
3723         if (len <= 0 || local->generic_elem == NULL) {
3724                 data->length = 0;
3725                 return 0;
3726         }
3727
3728         if (data->length < len)
3729                 return -E2BIG;
3730
3731         data->length = len;
3732         memcpy(extra, local->generic_elem + 2, len);
3733
3734         return 0;
3735 }
3736
3737
3738 static int prism2_ioctl_set_generic_element(local_info_t *local,
3739                                             struct prism2_hostapd_param *param,
3740                                             int param_len)
3741 {
3742         int max_len, len;
3743
3744         len = param->u.generic_elem.len;
3745         max_len = param_len - PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN;
3746         if (max_len < 0 || max_len < len)
3747                 return -EINVAL;
3748
3749         return prism2_set_genericelement(local->dev,
3750                                          param->u.generic_elem.data, len);
3751 }
3752
3753
3754 static int prism2_ioctl_siwmlme(struct net_device *dev,
3755                                 struct iw_request_info *info,
3756                                 struct iw_point *data, char *extra)
3757 {
3758         struct hostap_interface *iface = dev->priv;
3759         local_info_t *local = iface->local;
3760         struct iw_mlme *mlme = (struct iw_mlme *) extra;
3761         u16 reason;
3762
3763         reason = cpu_to_le16(mlme->reason_code);
3764
3765         switch (mlme->cmd) {
3766         case IW_MLME_DEAUTH:
3767                 return prism2_sta_send_mgmt(local, mlme->addr.sa_data,
3768                                             IEEE80211_STYPE_DEAUTH,
3769                                             (u8 *) &reason, 2);
3770         case IW_MLME_DISASSOC:
3771                 return prism2_sta_send_mgmt(local, mlme->addr.sa_data,
3772                                             IEEE80211_STYPE_DISASSOC,
3773                                             (u8 *) &reason, 2);
3774         default:
3775                 return -EOPNOTSUPP;
3776         }
3777 }
3778
3779
3780 static int prism2_ioctl_mlme(local_info_t *local,
3781                              struct prism2_hostapd_param *param)
3782 {
3783         u16 reason;
3784
3785         reason = cpu_to_le16(param->u.mlme.reason_code);
3786         switch (param->u.mlme.cmd) {
3787         case MLME_STA_DEAUTH:
3788                 return prism2_sta_send_mgmt(local, param->sta_addr,
3789                                             IEEE80211_STYPE_DEAUTH,
3790                                             (u8 *) &reason, 2);
3791         case MLME_STA_DISASSOC:
3792                 return prism2_sta_send_mgmt(local, param->sta_addr,
3793                                             IEEE80211_STYPE_DISASSOC,
3794                                             (u8 *) &reason, 2);
3795         default:
3796                 return -EOPNOTSUPP;
3797         }
3798 }
3799
3800
3801 static int prism2_ioctl_scan_req(local_info_t *local,
3802                                  struct prism2_hostapd_param *param)
3803 {
3804 #ifndef PRISM2_NO_STATION_MODES
3805         if ((local->iw_mode != IW_MODE_INFRA &&
3806              local->iw_mode != IW_MODE_ADHOC) ||
3807             (local->sta_fw_ver < PRISM2_FW_VER(1,3,1)))
3808                 return -EOPNOTSUPP;
3809
3810         if (!local->dev_enabled)
3811                 return -ENETDOWN;
3812
3813         return prism2_request_hostscan(local->dev, param->u.scan_req.ssid,
3814                                        param->u.scan_req.ssid_len);
3815 #else /* PRISM2_NO_STATION_MODES */
3816         return -EOPNOTSUPP;
3817 #endif /* PRISM2_NO_STATION_MODES */
3818 }
3819
3820
3821 static int prism2_ioctl_priv_hostapd(local_info_t *local, struct iw_point *p)
3822 {
3823         struct prism2_hostapd_param *param;
3824         int ret = 0;
3825         int ap_ioctl = 0;
3826
3827         if (p->length < sizeof(struct prism2_hostapd_param) ||
3828             p->length > PRISM2_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
3829                 return -EINVAL;
3830
3831         param = kmalloc(p->length, GFP_KERNEL);
3832         if (param == NULL)
3833                 return -ENOMEM;
3834
3835         if (copy_from_user(param, p->pointer, p->length)) {
3836                 ret = -EFAULT;
3837                 goto out;
3838         }
3839
3840         switch (param->cmd) {
3841         case PRISM2_SET_ENCRYPTION:
3842                 ret = prism2_ioctl_set_encryption(local, param, p->length);
3843                 break;
3844         case PRISM2_GET_ENCRYPTION:
3845                 ret = prism2_ioctl_get_encryption(local, param, p->length);
3846                 break;
3847         case PRISM2_HOSTAPD_GET_RID:
3848                 ret = prism2_ioctl_get_rid(local, param, p->length);
3849                 break;
3850         case PRISM2_HOSTAPD_SET_RID:
3851                 ret = prism2_ioctl_set_rid(local, param, p->length);
3852                 break;
3853         case PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR:
3854                 ret = prism2_ioctl_set_assoc_ap_addr(local, param, p->length);
3855                 break;
3856         case PRISM2_HOSTAPD_SET_GENERIC_ELEMENT:
3857                 ret = prism2_ioctl_set_generic_element(local, param,
3858                                                        p->length);
3859                 break;
3860         case PRISM2_HOSTAPD_MLME:
3861                 ret = prism2_ioctl_mlme(local, param);
3862                 break;
3863         case PRISM2_HOSTAPD_SCAN_REQ:
3864                 ret = prism2_ioctl_scan_req(local, param);
3865                 break;
3866         default:
3867                 ret = prism2_hostapd(local->ap, param);
3868                 ap_ioctl = 1;
3869                 break;
3870         }
3871
3872         if (ret == 1 || !ap_ioctl) {
3873                 if (copy_to_user(p->pointer, param, p->length)) {
3874                         ret = -EFAULT;
3875                         goto out;
3876                 } else if (ap_ioctl)
3877                         ret = 0;
3878         }
3879
3880  out:
3881         kfree(param);
3882         return ret;
3883 }
3884
3885
3886 static void prism2_get_drvinfo(struct net_device *dev,
3887                                struct ethtool_drvinfo *info)
3888 {
3889         struct hostap_interface *iface;
3890         local_info_t *local;
3891
3892         iface = netdev_priv(dev);
3893         local = iface->local;
3894
3895         strncpy(info->driver, "hostap", sizeof(info->driver) - 1);
3896         snprintf(info->fw_version, sizeof(info->fw_version) - 1,
3897                  "%d.%d.%d", (local->sta_fw_ver >> 16) & 0xff,
3898                  (local->sta_fw_ver >> 8) & 0xff,
3899                  local->sta_fw_ver & 0xff);
3900 }
3901
3902 const struct ethtool_ops prism2_ethtool_ops = {
3903         .get_drvinfo = prism2_get_drvinfo
3904 };
3905
3906
3907 /* Structures to export the Wireless Handlers */
3908
3909 static const iw_handler prism2_handler[] =
3910 {
3911         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
3912         (iw_handler) prism2_get_name,                   /* SIOCGIWNAME */
3913         (iw_handler) NULL,                              /* SIOCSIWNWID */
3914         (iw_handler) NULL,                              /* SIOCGIWNWID */
3915         (iw_handler) prism2_ioctl_siwfreq,              /* SIOCSIWFREQ */
3916         (iw_handler) prism2_ioctl_giwfreq,              /* SIOCGIWFREQ */
3917         (iw_handler) prism2_ioctl_siwmode,              /* SIOCSIWMODE */
3918         (iw_handler) prism2_ioctl_giwmode,              /* SIOCGIWMODE */
3919         (iw_handler) prism2_ioctl_siwsens,              /* SIOCSIWSENS */
3920         (iw_handler) prism2_ioctl_giwsens,              /* SIOCGIWSENS */
3921         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
3922         (iw_handler) prism2_ioctl_giwrange,             /* SIOCGIWRANGE */
3923         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
3924         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
3925         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
3926         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
3927         iw_handler_set_spy,                             /* SIOCSIWSPY */
3928         iw_handler_get_spy,                             /* SIOCGIWSPY */
3929         iw_handler_set_thrspy,                          /* SIOCSIWTHRSPY */
3930         iw_handler_get_thrspy,                          /* SIOCGIWTHRSPY */
3931         (iw_handler) prism2_ioctl_siwap,                /* SIOCSIWAP */
3932         (iw_handler) prism2_ioctl_giwap,                /* SIOCGIWAP */
3933         (iw_handler) prism2_ioctl_siwmlme,              /* SIOCSIWMLME */
3934         (iw_handler) prism2_ioctl_giwaplist,            /* SIOCGIWAPLIST */
3935         (iw_handler) prism2_ioctl_siwscan,              /* SIOCSIWSCAN */
3936         (iw_handler) prism2_ioctl_giwscan,              /* SIOCGIWSCAN */
3937         (iw_handler) prism2_ioctl_siwessid,             /* SIOCSIWESSID */
3938         (iw_handler) prism2_ioctl_giwessid,             /* SIOCGIWESSID */
3939         (iw_handler) prism2_ioctl_siwnickn,             /* SIOCSIWNICKN */
3940         (iw_handler) prism2_ioctl_giwnickn,             /* SIOCGIWNICKN */
3941         (iw_handler) NULL,                              /* -- hole -- */
3942         (iw_handler) NULL,                              /* -- hole -- */
3943         (iw_handler) prism2_ioctl_siwrate,              /* SIOCSIWRATE */
3944         (iw_handler) prism2_ioctl_giwrate,              /* SIOCGIWRATE */
3945         (iw_handler) prism2_ioctl_siwrts,               /* SIOCSIWRTS */
3946         (iw_handler) prism2_ioctl_giwrts,               /* SIOCGIWRTS */
3947         (iw_handler) prism2_ioctl_siwfrag,              /* SIOCSIWFRAG */
3948         (iw_handler) prism2_ioctl_giwfrag,              /* SIOCGIWFRAG */
3949         (iw_handler) prism2_ioctl_siwtxpow,             /* SIOCSIWTXPOW */
3950         (iw_handler) prism2_ioctl_giwtxpow,             /* SIOCGIWTXPOW */
3951         (iw_handler) prism2_ioctl_siwretry,             /* SIOCSIWRETRY */
3952         (iw_handler) prism2_ioctl_giwretry,             /* SIOCGIWRETRY */
3953         (iw_handler) prism2_ioctl_siwencode,            /* SIOCSIWENCODE */
3954         (iw_handler) prism2_ioctl_giwencode,            /* SIOCGIWENCODE */
3955         (iw_handler) prism2_ioctl_siwpower,             /* SIOCSIWPOWER */
3956         (iw_handler) prism2_ioctl_giwpower,             /* SIOCGIWPOWER */
3957         (iw_handler) NULL,                              /* -- hole -- */
3958         (iw_handler) NULL,                              /* -- hole -- */
3959         (iw_handler) prism2_ioctl_siwgenie,             /* SIOCSIWGENIE */
3960         (iw_handler) prism2_ioctl_giwgenie,             /* SIOCGIWGENIE */
3961         (iw_handler) prism2_ioctl_siwauth,              /* SIOCSIWAUTH */
3962         (iw_handler) prism2_ioctl_giwauth,              /* SIOCGIWAUTH */
3963         (iw_handler) prism2_ioctl_siwencodeext,         /* SIOCSIWENCODEEXT */
3964         (iw_handler) prism2_ioctl_giwencodeext,         /* SIOCGIWENCODEEXT */
3965         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
3966         (iw_handler) NULL,                              /* -- hole -- */
3967 };
3968
3969 static const iw_handler prism2_private_handler[] =
3970 {                                                       /* SIOCIWFIRSTPRIV + */
3971         (iw_handler) prism2_ioctl_priv_prism2_param,    /* 0 */
3972         (iw_handler) prism2_ioctl_priv_get_prism2_param, /* 1 */
3973         (iw_handler) prism2_ioctl_priv_writemif,        /* 2 */
3974         (iw_handler) prism2_ioctl_priv_readmif,         /* 3 */
3975 };
3976
3977 const struct iw_handler_def hostap_iw_handler_def =
3978 {
3979         .num_standard   = sizeof(prism2_handler) / sizeof(iw_handler),
3980         .num_private    = sizeof(prism2_private_handler) / sizeof(iw_handler),
3981         .num_private_args = sizeof(prism2_priv) / sizeof(struct iw_priv_args),
3982         .standard       = (iw_handler *) prism2_handler,
3983         .private        = (iw_handler *) prism2_private_handler,
3984         .private_args   = (struct iw_priv_args *) prism2_priv,
3985         .get_wireless_stats = hostap_get_wireless_stats,
3986 };
3987
3988
3989 int hostap_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3990 {
3991         struct iwreq *wrq = (struct iwreq *) ifr;
3992         struct hostap_interface *iface;
3993         local_info_t *local;
3994         int ret = 0;
3995
3996         iface = netdev_priv(dev);
3997         local = iface->local;
3998
3999         switch (cmd) {
4000                 /* Private ioctls (iwpriv) that have not yet been converted
4001                  * into new wireless extensions API */
4002
4003         case PRISM2_IOCTL_INQUIRE:
4004                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4005                 else ret = prism2_ioctl_priv_inquire(dev, (int *) wrq->u.name);
4006                 break;
4007
4008         case PRISM2_IOCTL_MONITOR:
4009                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4010                 else ret = prism2_ioctl_priv_monitor(dev, (int *) wrq->u.name);
4011                 break;
4012
4013         case PRISM2_IOCTL_RESET:
4014                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4015                 else ret = prism2_ioctl_priv_reset(dev, (int *) wrq->u.name);
4016                 break;
4017
4018         case PRISM2_IOCTL_WDS_ADD:
4019                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4020                 else ret = prism2_wds_add(local, wrq->u.ap_addr.sa_data, 1);
4021                 break;
4022
4023         case PRISM2_IOCTL_WDS_DEL:
4024                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4025                 else ret = prism2_wds_del(local, wrq->u.ap_addr.sa_data, 1, 0);
4026                 break;
4027
4028         case PRISM2_IOCTL_SET_RID_WORD:
4029                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4030                 else ret = prism2_ioctl_priv_set_rid_word(dev,
4031                                                           (int *) wrq->u.name);
4032                 break;
4033
4034 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
4035         case PRISM2_IOCTL_MACCMD:
4036                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4037                 else ret = ap_mac_cmd_ioctl(local, (int *) wrq->u.name);
4038                 break;
4039
4040         case PRISM2_IOCTL_ADDMAC:
4041                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4042                 else ret = ap_control_add_mac(&local->ap->mac_restrictions,
4043                                               wrq->u.ap_addr.sa_data);
4044                 break;
4045         case PRISM2_IOCTL_DELMAC:
4046                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4047                 else ret = ap_control_del_mac(&local->ap->mac_restrictions,
4048                                               wrq->u.ap_addr.sa_data);
4049                 break;
4050         case PRISM2_IOCTL_KICKMAC:
4051                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4052                 else ret = ap_control_kick_mac(local->ap, local->dev,
4053                                                wrq->u.ap_addr.sa_data);
4054                 break;
4055 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
4056
4057
4058                 /* Private ioctls that are not used with iwpriv;
4059                  * in SIOCDEVPRIVATE range */
4060
4061 #ifdef PRISM2_DOWNLOAD_SUPPORT
4062         case PRISM2_IOCTL_DOWNLOAD:
4063                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4064                 else ret = prism2_ioctl_priv_download(local, &wrq->u.data);
4065                 break;
4066 #endif /* PRISM2_DOWNLOAD_SUPPORT */
4067
4068         case PRISM2_IOCTL_HOSTAPD:
4069                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4070                 else ret = prism2_ioctl_priv_hostapd(local, &wrq->u.data);
4071                 break;
4072
4073         default:
4074                 ret = -EOPNOTSUPP;
4075                 break;
4076         }
4077
4078         return ret;
4079 }