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