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