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