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