Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-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(*(__le16 *) buf); /* string length */
88
89         if (len - 2 < val || val > 10)
90                 return 0;
91
92         memcpy(rates, buf + 2, val);
93         return val;
94 }
95
96
97 static int prism2_get_name(struct net_device *dev,
98                            struct iw_request_info *info,
99                            char *name, char *extra)
100 {
101         u8 rates[10];
102         int len, i, over2 = 0;
103
104         len = prism2_get_datarates(dev, rates);
105
106         for (i = 0; i < len; i++) {
107                 if (rates[i] == 0x0b || rates[i] == 0x16) {
108                         over2 = 1;
109                         break;
110                 }
111         }
112
113         strcpy(name, over2 ? "IEEE 802.11b" : "IEEE 802.11-DS");
114
115         return 0;
116 }
117
118
119 static 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         __le16 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         __le16 val;
565
566         iface = netdev_priv(dev);
567         local = iface->local;
568
569         if (rts->disabled)
570                 val = 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         __le16 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         __le16 val;
615
616         iface = netdev_priv(dev);
617         local = iface->local;
618
619         if (rts->disabled)
620                 val = 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         __le16 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 = cpu_to_le16(0x3fff);
722                 scan_req.txrate = 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(*(__le16 *) 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(*(__le16 *) 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         if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1))
1093                 range->scan_capa = IW_SCAN_CAPA_ESSID;
1094
1095         return 0;
1096 }
1097
1098
1099 static int hostap_monitor_mode_enable(local_info_t *local)
1100 {
1101         struct net_device *dev = local->dev;
1102
1103         printk(KERN_DEBUG "Enabling monitor mode\n");
1104         hostap_monitor_set_type(local);
1105
1106         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
1107                             HFA384X_PORTTYPE_PSEUDO_IBSS)) {
1108                 printk(KERN_DEBUG "Port type setting for monitor mode "
1109                        "failed\n");
1110                 return -EOPNOTSUPP;
1111         }
1112
1113         /* Host decrypt is needed to get the IV and ICV fields;
1114          * however, monitor mode seems to remove WEP flag from frame
1115          * control field */
1116         if (hostap_set_word(dev, HFA384X_RID_CNFWEPFLAGS,
1117                             HFA384X_WEPFLAGS_HOSTENCRYPT |
1118                             HFA384X_WEPFLAGS_HOSTDECRYPT)) {
1119                 printk(KERN_DEBUG "WEP flags setting failed\n");
1120                 return -EOPNOTSUPP;
1121         }
1122
1123         if (local->func->reset_port(dev) ||
1124             local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1125                              (HFA384X_TEST_MONITOR << 8),
1126                              0, NULL, NULL)) {
1127                 printk(KERN_DEBUG "Setting monitor mode failed\n");
1128                 return -EOPNOTSUPP;
1129         }
1130
1131         return 0;
1132 }
1133
1134
1135 static int hostap_monitor_mode_disable(local_info_t *local)
1136 {
1137         struct net_device *dev = local->ddev;
1138
1139         if (dev == NULL)
1140                 return -1;
1141
1142         printk(KERN_DEBUG "%s: Disabling monitor mode\n", dev->name);
1143         dev->type = ARPHRD_ETHER;
1144
1145         if (local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1146                              (HFA384X_TEST_STOP << 8),
1147                              0, NULL, NULL))
1148                 return -1;
1149         return hostap_set_encryption(local);
1150 }
1151
1152
1153 static int prism2_ioctl_siwmode(struct net_device *dev,
1154                                 struct iw_request_info *info,
1155                                 __u32 *mode, char *extra)
1156 {
1157         struct hostap_interface *iface;
1158         local_info_t *local;
1159         int double_reset = 0;
1160
1161         iface = netdev_priv(dev);
1162         local = iface->local;
1163
1164         if (*mode != IW_MODE_ADHOC && *mode != IW_MODE_INFRA &&
1165             *mode != IW_MODE_MASTER && *mode != IW_MODE_REPEAT &&
1166             *mode != IW_MODE_MONITOR)
1167                 return -EOPNOTSUPP;
1168
1169 #ifdef PRISM2_NO_STATION_MODES
1170         if (*mode == IW_MODE_ADHOC || *mode == IW_MODE_INFRA)
1171                 return -EOPNOTSUPP;
1172 #endif /* PRISM2_NO_STATION_MODES */
1173
1174         if (*mode == local->iw_mode)
1175                 return 0;
1176
1177         if (*mode == IW_MODE_MASTER && local->essid[0] == '\0') {
1178                 printk(KERN_WARNING "%s: empty SSID not allowed in Master "
1179                        "mode\n", dev->name);
1180                 return -EINVAL;
1181         }
1182
1183         if (local->iw_mode == IW_MODE_MONITOR)
1184                 hostap_monitor_mode_disable(local);
1185
1186         if ((local->iw_mode == IW_MODE_ADHOC ||
1187              local->iw_mode == IW_MODE_MONITOR) && *mode == IW_MODE_MASTER) {
1188                 /* There seems to be a firmware bug in at least STA f/w v1.5.6
1189                  * that leaves beacon frames to use IBSS type when moving from
1190                  * IBSS to Host AP mode. Doing double Port0 reset seems to be
1191                  * enough to workaround this. */
1192                 double_reset = 1;
1193         }
1194
1195         printk(KERN_DEBUG "prism2: %s: operating mode changed "
1196                "%d -> %d\n", dev->name, local->iw_mode, *mode);
1197         local->iw_mode = *mode;
1198
1199         if (local->iw_mode == IW_MODE_MONITOR)
1200                 hostap_monitor_mode_enable(local);
1201         else if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
1202                  !local->fw_encrypt_ok) {
1203                 printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
1204                        "a workaround for firmware bug in Host AP mode WEP\n",
1205                        dev->name);
1206                 local->host_encrypt = 1;
1207         }
1208
1209         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
1210                             hostap_get_porttype(local)))
1211                 return -EOPNOTSUPP;
1212
1213         if (local->func->reset_port(dev))
1214                 return -EINVAL;
1215         if (double_reset && local->func->reset_port(dev))
1216                 return -EINVAL;
1217
1218         if (local->iw_mode != IW_MODE_INFRA && local->iw_mode != IW_MODE_ADHOC)
1219         {
1220                 /* netif_carrier is used only in client modes for now, so make
1221                  * sure carrier is on when moving to non-client modes. */
1222                 netif_carrier_on(local->dev);
1223                 netif_carrier_on(local->ddev);
1224         }
1225         return 0;
1226 }
1227
1228
1229 static int prism2_ioctl_giwmode(struct net_device *dev,
1230                                 struct iw_request_info *info,
1231                                 __u32 *mode, char *extra)
1232 {
1233         struct hostap_interface *iface;
1234         local_info_t *local;
1235
1236         iface = netdev_priv(dev);
1237         local = iface->local;
1238
1239         switch (iface->type) {
1240         case HOSTAP_INTERFACE_STA:
1241                 *mode = IW_MODE_INFRA;
1242                 break;
1243         case HOSTAP_INTERFACE_WDS:
1244                 *mode = IW_MODE_REPEAT;
1245                 break;
1246         default:
1247                 *mode = local->iw_mode;
1248                 break;
1249         }
1250         return 0;
1251 }
1252
1253
1254 static int prism2_ioctl_siwpower(struct net_device *dev,
1255                                  struct iw_request_info *info,
1256                                  struct iw_param *wrq, char *extra)
1257 {
1258 #ifdef PRISM2_NO_STATION_MODES
1259         return -EOPNOTSUPP;
1260 #else /* PRISM2_NO_STATION_MODES */
1261         int ret = 0;
1262
1263         if (wrq->disabled)
1264                 return hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 0);
1265
1266         switch (wrq->flags & IW_POWER_MODE) {
1267         case IW_POWER_UNICAST_R:
1268                 ret = hostap_set_word(dev, HFA384X_RID_CNFMULTICASTRECEIVE, 0);
1269                 if (ret)
1270                         return ret;
1271                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1272                 if (ret)
1273                         return ret;
1274                 break;
1275         case IW_POWER_ALL_R:
1276                 ret = hostap_set_word(dev, HFA384X_RID_CNFMULTICASTRECEIVE, 1);
1277                 if (ret)
1278                         return ret;
1279                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1280                 if (ret)
1281                         return ret;
1282                 break;
1283         case IW_POWER_ON:
1284                 break;
1285         default:
1286                 return -EINVAL;
1287         }
1288
1289         if (wrq->flags & IW_POWER_TIMEOUT) {
1290                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1291                 if (ret)
1292                         return ret;
1293                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMHOLDOVERDURATION,
1294                                       wrq->value / 1024);
1295                 if (ret)
1296                         return ret;
1297         }
1298         if (wrq->flags & IW_POWER_PERIOD) {
1299                 ret = hostap_set_word(dev, HFA384X_RID_CNFPMENABLED, 1);
1300                 if (ret)
1301                         return ret;
1302                 ret = hostap_set_word(dev, HFA384X_RID_CNFMAXSLEEPDURATION,
1303                                       wrq->value / 1024);
1304                 if (ret)
1305                         return ret;
1306         }
1307
1308         return ret;
1309 #endif /* PRISM2_NO_STATION_MODES */
1310 }
1311
1312
1313 static int prism2_ioctl_giwpower(struct net_device *dev,
1314                                  struct iw_request_info *info,
1315                                  struct iw_param *rrq, char *extra)
1316 {
1317 #ifdef PRISM2_NO_STATION_MODES
1318         return -EOPNOTSUPP;
1319 #else /* PRISM2_NO_STATION_MODES */
1320         struct hostap_interface *iface;
1321         local_info_t *local;
1322         __le16 enable, mcast;
1323
1324         iface = netdev_priv(dev);
1325         local = iface->local;
1326
1327         if (local->func->get_rid(dev, HFA384X_RID_CNFPMENABLED, &enable, 2, 1)
1328             < 0)
1329                 return -EINVAL;
1330
1331         if (!le16_to_cpu(enable)) {
1332                 rrq->disabled = 1;
1333                 return 0;
1334         }
1335
1336         rrq->disabled = 0;
1337
1338         if ((rrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
1339                 __le16 timeout;
1340                 if (local->func->get_rid(dev,
1341                                          HFA384X_RID_CNFPMHOLDOVERDURATION,
1342                                          &timeout, 2, 1) < 0)
1343                         return -EINVAL;
1344
1345                 rrq->flags = IW_POWER_TIMEOUT;
1346                 rrq->value = le16_to_cpu(timeout) * 1024;
1347         } else {
1348                 __le16 period;
1349                 if (local->func->get_rid(dev, HFA384X_RID_CNFMAXSLEEPDURATION,
1350                                          &period, 2, 1) < 0)
1351                         return -EINVAL;
1352
1353                 rrq->flags = IW_POWER_PERIOD;
1354                 rrq->value = le16_to_cpu(period) * 1024;
1355         }
1356
1357         if (local->func->get_rid(dev, HFA384X_RID_CNFMULTICASTRECEIVE, &mcast,
1358                                  2, 1) < 0)
1359                 return -EINVAL;
1360
1361         if (le16_to_cpu(mcast))
1362                 rrq->flags |= IW_POWER_ALL_R;
1363         else
1364                 rrq->flags |= IW_POWER_UNICAST_R;
1365
1366         return 0;
1367 #endif /* PRISM2_NO_STATION_MODES */
1368 }
1369
1370
1371 static int prism2_ioctl_siwretry(struct net_device *dev,
1372                                  struct iw_request_info *info,
1373                                  struct iw_param *rrq, char *extra)
1374 {
1375         struct hostap_interface *iface;
1376         local_info_t *local;
1377
1378         iface = netdev_priv(dev);
1379         local = iface->local;
1380
1381         if (rrq->disabled)
1382                 return -EINVAL;
1383
1384         /* setting retry limits is not supported with the current station
1385          * firmware code; simulate this with alternative retry count for now */
1386         if (rrq->flags == IW_RETRY_LIMIT) {
1387                 if (rrq->value < 0) {
1388                         /* disable manual retry count setting and use firmware
1389                          * defaults */
1390                         local->manual_retry_count = -1;
1391                         local->tx_control &= ~HFA384X_TX_CTRL_ALT_RTRY;
1392                 } else {
1393                         if (hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1394                                             rrq->value)) {
1395                                 printk(KERN_DEBUG "%s: Alternate retry count "
1396                                        "setting to %d failed\n",
1397                                        dev->name, rrq->value);
1398                                 return -EOPNOTSUPP;
1399                         }
1400
1401                         local->manual_retry_count = rrq->value;
1402                         local->tx_control |= HFA384X_TX_CTRL_ALT_RTRY;
1403                 }
1404                 return 0;
1405         }
1406
1407         return -EOPNOTSUPP;
1408
1409 #if 0
1410         /* what could be done, if firmware would support this.. */
1411
1412         if (rrq->flags & IW_RETRY_LIMIT) {
1413                 if (rrq->flags & IW_RETRY_LONG)
1414                         HFA384X_RID_LONGRETRYLIMIT = rrq->value;
1415                 else if (rrq->flags & IW_RETRY_SHORT)
1416                         HFA384X_RID_SHORTRETRYLIMIT = rrq->value;
1417                 else {
1418                         HFA384X_RID_LONGRETRYLIMIT = rrq->value;
1419                         HFA384X_RID_SHORTRETRYLIMIT = rrq->value;
1420                 }
1421
1422         }
1423
1424         if (rrq->flags & IW_RETRY_LIFETIME) {
1425                 HFA384X_RID_MAXTRANSMITLIFETIME = rrq->value / 1024;
1426         }
1427
1428         return 0;
1429 #endif /* 0 */
1430 }
1431
1432 static int prism2_ioctl_giwretry(struct net_device *dev,
1433                                  struct iw_request_info *info,
1434                                  struct iw_param *rrq, char *extra)
1435 {
1436         struct hostap_interface *iface;
1437         local_info_t *local;
1438         __le16 shortretry, longretry, lifetime, altretry;
1439
1440         iface = netdev_priv(dev);
1441         local = iface->local;
1442
1443         if (local->func->get_rid(dev, HFA384X_RID_SHORTRETRYLIMIT, &shortretry,
1444                                  2, 1) < 0 ||
1445             local->func->get_rid(dev, HFA384X_RID_LONGRETRYLIMIT, &longretry,
1446                                  2, 1) < 0 ||
1447             local->func->get_rid(dev, HFA384X_RID_MAXTRANSMITLIFETIME,
1448                                  &lifetime, 2, 1) < 0)
1449                 return -EINVAL;
1450
1451         rrq->disabled = 0;
1452
1453         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
1454                 rrq->flags = IW_RETRY_LIFETIME;
1455                 rrq->value = le16_to_cpu(lifetime) * 1024;
1456         } else {
1457                 if (local->manual_retry_count >= 0) {
1458                         rrq->flags = IW_RETRY_LIMIT;
1459                         if (local->func->get_rid(dev,
1460                                                  HFA384X_RID_CNFALTRETRYCOUNT,
1461                                                  &altretry, 2, 1) >= 0)
1462                                 rrq->value = le16_to_cpu(altretry);
1463                         else
1464                                 rrq->value = local->manual_retry_count;
1465                 } else if ((rrq->flags & IW_RETRY_LONG)) {
1466                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1467                         rrq->value = le16_to_cpu(longretry);
1468                 } else {
1469                         rrq->flags = IW_RETRY_LIMIT;
1470                         rrq->value = le16_to_cpu(shortretry);
1471                         if (shortretry != longretry)
1472                                 rrq->flags |= IW_RETRY_SHORT;
1473                 }
1474         }
1475         return 0;
1476 }
1477
1478
1479 /* Note! This TX power controlling is experimental and should not be used in
1480  * production use. It just sets raw power register and does not use any kind of
1481  * feedback information from the measured TX power (CR58). This is now
1482  * commented out to make sure that it is not used by accident. TX power
1483  * configuration will be enabled again after proper algorithm using feedback
1484  * has been implemented. */
1485
1486 #ifdef RAW_TXPOWER_SETTING
1487 /* Map HFA386x's CR31 to and from dBm with some sort of ad hoc mapping..
1488  * This version assumes following mapping:
1489  * CR31 is 7-bit value with -64 to +63 range.
1490  * -64 is mapped into +20dBm and +63 into -43dBm.
1491  * This is certainly not an exact mapping for every card, but at least
1492  * increasing dBm value should correspond to increasing TX power.
1493  */
1494
1495 static int prism2_txpower_hfa386x_to_dBm(u16 val)
1496 {
1497         signed char tmp;
1498
1499         if (val > 255)
1500                 val = 255;
1501
1502         tmp = val;
1503         tmp >>= 2;
1504
1505         return -12 - tmp;
1506 }
1507
1508 static u16 prism2_txpower_dBm_to_hfa386x(int val)
1509 {
1510         signed char tmp;
1511
1512         if (val > 20)
1513                 return 128;
1514         else if (val < -43)
1515                 return 127;
1516
1517         tmp = val;
1518         tmp = -12 - tmp;
1519         tmp <<= 2;
1520
1521         return (unsigned char) tmp;
1522 }
1523 #endif /* RAW_TXPOWER_SETTING */
1524
1525
1526 static int prism2_ioctl_siwtxpow(struct net_device *dev,
1527                                  struct iw_request_info *info,
1528                                  struct iw_param *rrq, char *extra)
1529 {
1530         struct hostap_interface *iface;
1531         local_info_t *local;
1532 #ifdef RAW_TXPOWER_SETTING
1533         char *tmp;
1534 #endif
1535         u16 val;
1536         int ret = 0;
1537
1538         iface = netdev_priv(dev);
1539         local = iface->local;
1540
1541         if (rrq->disabled) {
1542                 if (local->txpower_type != PRISM2_TXPOWER_OFF) {
1543                         val = 0xff; /* use all standby and sleep modes */
1544                         ret = local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF,
1545                                                HFA386X_CR_A_D_TEST_MODES2,
1546                                                &val, NULL);
1547                         printk(KERN_DEBUG "%s: Turning radio off: %s\n",
1548                                dev->name, ret ? "failed" : "OK");
1549                         local->txpower_type = PRISM2_TXPOWER_OFF;
1550                 }
1551                 return (ret ? -EOPNOTSUPP : 0);
1552         }
1553
1554         if (local->txpower_type == PRISM2_TXPOWER_OFF) {
1555                 val = 0; /* disable all standby and sleep modes */
1556                 ret = local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF,
1557                                        HFA386X_CR_A_D_TEST_MODES2, &val, NULL);
1558                 printk(KERN_DEBUG "%s: Turning radio on: %s\n",
1559                        dev->name, ret ? "failed" : "OK");
1560                 local->txpower_type = PRISM2_TXPOWER_UNKNOWN;
1561         }
1562
1563 #ifdef RAW_TXPOWER_SETTING
1564         if (!rrq->fixed && local->txpower_type != PRISM2_TXPOWER_AUTO) {
1565                 printk(KERN_DEBUG "Setting ALC on\n");
1566                 val = HFA384X_TEST_CFG_BIT_ALC;
1567                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1568                                  (HFA384X_TEST_CFG_BITS << 8), 1, &val, NULL);
1569                 local->txpower_type = PRISM2_TXPOWER_AUTO;
1570                 return 0;
1571         }
1572
1573         if (local->txpower_type != PRISM2_TXPOWER_FIXED) {
1574                 printk(KERN_DEBUG "Setting ALC off\n");
1575                 val = HFA384X_TEST_CFG_BIT_ALC;
1576                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
1577                                  (HFA384X_TEST_CFG_BITS << 8), 0, &val, NULL);
1578                         local->txpower_type = PRISM2_TXPOWER_FIXED;
1579         }
1580
1581         if (rrq->flags == IW_TXPOW_DBM)
1582                 tmp = "dBm";
1583         else if (rrq->flags == IW_TXPOW_MWATT)
1584                 tmp = "mW";
1585         else
1586                 tmp = "UNKNOWN";
1587         printk(KERN_DEBUG "Setting TX power to %d %s\n", rrq->value, tmp);
1588
1589         if (rrq->flags != IW_TXPOW_DBM) {
1590                 printk("SIOCSIWTXPOW with mW is not supported; use dBm\n");
1591                 return -EOPNOTSUPP;
1592         }
1593
1594         local->txpower = rrq->value;
1595         val = prism2_txpower_dBm_to_hfa386x(local->txpower);
1596         if (local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF,
1597                              HFA386X_CR_MANUAL_TX_POWER, &val, NULL))
1598                 ret = -EOPNOTSUPP;
1599 #else /* RAW_TXPOWER_SETTING */
1600         if (rrq->fixed)
1601                 ret = -EOPNOTSUPP;
1602 #endif /* RAW_TXPOWER_SETTING */
1603
1604         return ret;
1605 }
1606
1607 static int prism2_ioctl_giwtxpow(struct net_device *dev,
1608                                  struct iw_request_info *info,
1609                                  struct iw_param *rrq, char *extra)
1610 {
1611 #ifdef RAW_TXPOWER_SETTING
1612         struct hostap_interface *iface;
1613         local_info_t *local;
1614         u16 resp0;
1615
1616         iface = netdev_priv(dev);
1617         local = iface->local;
1618
1619         rrq->flags = IW_TXPOW_DBM;
1620         rrq->disabled = 0;
1621         rrq->fixed = 0;
1622
1623         if (local->txpower_type == PRISM2_TXPOWER_AUTO) {
1624                 if (local->func->cmd(dev, HFA384X_CMDCODE_READMIF,
1625                                      HFA386X_CR_MANUAL_TX_POWER,
1626                                      NULL, &resp0) == 0) {
1627                         rrq->value = prism2_txpower_hfa386x_to_dBm(resp0);
1628                 } else {
1629                         /* Could not get real txpower; guess 15 dBm */
1630                         rrq->value = 15;
1631                 }
1632         } else if (local->txpower_type == PRISM2_TXPOWER_OFF) {
1633                 rrq->value = 0;
1634                 rrq->disabled = 1;
1635         } else if (local->txpower_type == PRISM2_TXPOWER_FIXED) {
1636                 rrq->value = local->txpower;
1637                 rrq->fixed = 1;
1638         } else {
1639                 printk("SIOCGIWTXPOW - unknown txpower_type=%d\n",
1640                        local->txpower_type);
1641         }
1642         return 0;
1643 #else /* RAW_TXPOWER_SETTING */
1644         return -EOPNOTSUPP;
1645 #endif /* RAW_TXPOWER_SETTING */
1646 }
1647
1648
1649 #ifndef PRISM2_NO_STATION_MODES
1650
1651 /* HostScan request works with and without host_roaming mode. In addition, it
1652  * does not break current association. However, it requires newer station
1653  * firmware version (>= 1.3.1) than scan request. */
1654 static int prism2_request_hostscan(struct net_device *dev,
1655                                    u8 *ssid, u8 ssid_len)
1656 {
1657         struct hostap_interface *iface;
1658         local_info_t *local;
1659         struct hfa384x_hostscan_request scan_req;
1660
1661         iface = netdev_priv(dev);
1662         local = iface->local;
1663
1664         memset(&scan_req, 0, sizeof(scan_req));
1665         scan_req.channel_list = cpu_to_le16(local->channel_mask &
1666                                             local->scan_channel_mask);
1667         scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
1668         if (ssid) {
1669                 if (ssid_len > 32)
1670                         return -EINVAL;
1671                 scan_req.target_ssid_len = cpu_to_le16(ssid_len);
1672                 memcpy(scan_req.target_ssid, ssid, ssid_len);
1673         }
1674
1675         if (local->func->set_rid(dev, HFA384X_RID_HOSTSCAN, &scan_req,
1676                                  sizeof(scan_req))) {
1677                 printk(KERN_DEBUG "%s: HOSTSCAN failed\n", dev->name);
1678                 return -EINVAL;
1679         }
1680         return 0;
1681 }
1682
1683
1684 static int prism2_request_scan(struct net_device *dev)
1685 {
1686         struct hostap_interface *iface;
1687         local_info_t *local;
1688         struct hfa384x_scan_request scan_req;
1689         int ret = 0;
1690
1691         iface = netdev_priv(dev);
1692         local = iface->local;
1693
1694         memset(&scan_req, 0, sizeof(scan_req));
1695         scan_req.channel_list = cpu_to_le16(local->channel_mask &
1696                                             local->scan_channel_mask);
1697         scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
1698
1699         /* FIX:
1700          * It seems to be enough to set roaming mode for a short moment to
1701          * host-based and then setup scanrequest data and return the mode to
1702          * firmware-based.
1703          *
1704          * Master mode would need to drop to Managed mode for a short while
1705          * to make scanning work.. Or sweep through the different channels and
1706          * use passive scan based on beacons. */
1707
1708         if (!local->host_roaming)
1709                 hostap_set_word(dev, HFA384X_RID_CNFROAMINGMODE,
1710                                 HFA384X_ROAMING_HOST);
1711
1712         if (local->func->set_rid(dev, HFA384X_RID_SCANREQUEST, &scan_req,
1713                                  sizeof(scan_req))) {
1714                 printk(KERN_DEBUG "SCANREQUEST failed\n");
1715                 ret = -EINVAL;
1716         }
1717
1718         if (!local->host_roaming)
1719                 hostap_set_word(dev, HFA384X_RID_CNFROAMINGMODE,
1720                                 HFA384X_ROAMING_FIRMWARE);
1721
1722         return 0;
1723 }
1724
1725 #else /* !PRISM2_NO_STATION_MODES */
1726
1727 static inline int prism2_request_hostscan(struct net_device *dev,
1728                                           u8 *ssid, u8 ssid_len)
1729 {
1730         return -EOPNOTSUPP;
1731 }
1732
1733
1734 static inline int prism2_request_scan(struct net_device *dev)
1735 {
1736         return -EOPNOTSUPP;
1737 }
1738
1739 #endif /* !PRISM2_NO_STATION_MODES */
1740
1741
1742 static int prism2_ioctl_siwscan(struct net_device *dev,
1743                                 struct iw_request_info *info,
1744                                 struct iw_point *data, char *extra)
1745 {
1746         struct hostap_interface *iface;
1747         local_info_t *local;
1748         int ret;
1749         u8 *ssid = NULL, ssid_len = 0;
1750         struct iw_scan_req *req = (struct iw_scan_req *) extra;
1751
1752         iface = netdev_priv(dev);
1753         local = iface->local;
1754
1755         if (data->length < sizeof(struct iw_scan_req))
1756                 req = NULL;
1757
1758         if (local->iw_mode == IW_MODE_MASTER) {
1759                 /* In master mode, we just return the results of our local
1760                  * tables, so we don't need to start anything...
1761                  * Jean II */
1762                 data->length = 0;
1763                 return 0;
1764         }
1765
1766         if (!local->dev_enabled)
1767                 return -ENETDOWN;
1768
1769         if (req && data->flags & IW_SCAN_THIS_ESSID) {
1770                 ssid = req->essid;
1771                 ssid_len = req->essid_len;
1772
1773                 if (ssid_len &&
1774                     ((local->iw_mode != IW_MODE_INFRA &&
1775                       local->iw_mode != IW_MODE_ADHOC) ||
1776                      (local->sta_fw_ver < PRISM2_FW_VER(1,3,1))))
1777                         return -EOPNOTSUPP;
1778         }
1779
1780         if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1))
1781                 ret = prism2_request_hostscan(dev, ssid, ssid_len);
1782         else
1783                 ret = prism2_request_scan(dev);
1784
1785         if (ret == 0)
1786                 local->scan_timestamp = jiffies;
1787
1788         /* Could inquire F101, F103 or wait for SIOCGIWSCAN and read RID */
1789
1790         return ret;
1791 }
1792
1793
1794 #ifndef PRISM2_NO_STATION_MODES
1795 static char * __prism2_translate_scan(local_info_t *local,
1796                                       struct hfa384x_hostscan_result *scan,
1797                                       struct hostap_bss_info *bss,
1798                                       char *current_ev, char *end_buf)
1799 {
1800         int i, chan;
1801         struct iw_event iwe;
1802         char *current_val;
1803         u16 capabilities;
1804         u8 *pos;
1805         u8 *ssid, *bssid;
1806         size_t ssid_len;
1807         char *buf;
1808
1809         if (bss) {
1810                 ssid = bss->ssid;
1811                 ssid_len = bss->ssid_len;
1812                 bssid = bss->bssid;
1813         } else {
1814                 ssid = scan->ssid;
1815                 ssid_len = le16_to_cpu(scan->ssid_len);
1816                 bssid = scan->bssid;
1817         }
1818         if (ssid_len > 32)
1819                 ssid_len = 32;
1820
1821         /* First entry *MUST* be the AP MAC address */
1822         memset(&iwe, 0, sizeof(iwe));
1823         iwe.cmd = SIOCGIWAP;
1824         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1825         memcpy(iwe.u.ap_addr.sa_data, bssid, ETH_ALEN);
1826         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1827                                           IW_EV_ADDR_LEN);
1828
1829         /* Other entries will be displayed in the order we give them */
1830
1831         memset(&iwe, 0, sizeof(iwe));
1832         iwe.cmd = SIOCGIWESSID;
1833         iwe.u.data.length = ssid_len;
1834         iwe.u.data.flags = 1;
1835         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, ssid);
1836
1837         memset(&iwe, 0, sizeof(iwe));
1838         iwe.cmd = SIOCGIWMODE;
1839         if (bss) {
1840                 capabilities = bss->capab_info;
1841         } else {
1842                 capabilities = le16_to_cpu(scan->capability);
1843         }
1844         if (capabilities & (WLAN_CAPABILITY_ESS |
1845                             WLAN_CAPABILITY_IBSS)) {
1846                 if (capabilities & WLAN_CAPABILITY_ESS)
1847                         iwe.u.mode = IW_MODE_MASTER;
1848                 else
1849                         iwe.u.mode = IW_MODE_ADHOC;
1850                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1851                                                   IW_EV_UINT_LEN);
1852         }
1853
1854         memset(&iwe, 0, sizeof(iwe));
1855         iwe.cmd = SIOCGIWFREQ;
1856         if (scan) {
1857                 chan = le16_to_cpu(scan->chid);
1858         } else if (bss) {
1859                 chan = bss->chan;
1860         } else {
1861                 chan = 0;
1862         }
1863
1864         if (chan > 0) {
1865                 iwe.u.freq.m = freq_list[chan - 1] * 100000;
1866                 iwe.u.freq.e = 1;
1867                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1868                                                   IW_EV_FREQ_LEN);
1869         }
1870
1871         if (scan) {
1872                 memset(&iwe, 0, sizeof(iwe));
1873                 iwe.cmd = IWEVQUAL;
1874                 if (local->last_scan_type == PRISM2_HOSTSCAN) {
1875                         iwe.u.qual.level = le16_to_cpu(scan->sl);
1876                         iwe.u.qual.noise = le16_to_cpu(scan->anl);
1877                 } else {
1878                         iwe.u.qual.level =
1879                                 HFA384X_LEVEL_TO_dBm(le16_to_cpu(scan->sl));
1880                         iwe.u.qual.noise =
1881                                 HFA384X_LEVEL_TO_dBm(le16_to_cpu(scan->anl));
1882                 }
1883                 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED
1884                         | IW_QUAL_NOISE_UPDATED
1885                         | IW_QUAL_QUAL_INVALID
1886                         | IW_QUAL_DBM;
1887                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1888                                                   IW_EV_QUAL_LEN);
1889         }
1890
1891         memset(&iwe, 0, sizeof(iwe));
1892         iwe.cmd = SIOCGIWENCODE;
1893         if (capabilities & WLAN_CAPABILITY_PRIVACY)
1894                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1895         else
1896                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1897         iwe.u.data.length = 0;
1898         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
1899
1900         /* TODO: add SuppRates into BSS table */
1901         if (scan) {
1902                 memset(&iwe, 0, sizeof(iwe));
1903                 iwe.cmd = SIOCGIWRATE;
1904                 current_val = current_ev + IW_EV_LCP_LEN;
1905                 pos = scan->sup_rates;
1906                 for (i = 0; i < sizeof(scan->sup_rates); i++) {
1907                         if (pos[i] == 0)
1908                                 break;
1909                         /* Bit rate given in 500 kb/s units (+ 0x80) */
1910                         iwe.u.bitrate.value = ((pos[i] & 0x7f) * 500000);
1911                         current_val = iwe_stream_add_value(
1912                                 current_ev, current_val, end_buf, &iwe,
1913                                 IW_EV_PARAM_LEN);
1914                 }
1915                 /* Check if we added any event */
1916                 if ((current_val - current_ev) > IW_EV_LCP_LEN)
1917                         current_ev = current_val;
1918         }
1919
1920         /* TODO: add BeaconInt,resp_rate,atim into BSS table */
1921         buf = kmalloc(MAX_WPA_IE_LEN * 2 + 30, GFP_ATOMIC);
1922         if (buf && scan) {
1923                 memset(&iwe, 0, sizeof(iwe));
1924                 iwe.cmd = IWEVCUSTOM;
1925                 sprintf(buf, "bcn_int=%d", le16_to_cpu(scan->beacon_interval));
1926                 iwe.u.data.length = strlen(buf);
1927                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1928                                                   buf);
1929
1930                 memset(&iwe, 0, sizeof(iwe));
1931                 iwe.cmd = IWEVCUSTOM;
1932                 sprintf(buf, "resp_rate=%d", le16_to_cpu(scan->rate));
1933                 iwe.u.data.length = strlen(buf);
1934                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1935                                                   buf);
1936
1937                 if (local->last_scan_type == PRISM2_HOSTSCAN &&
1938                     (capabilities & WLAN_CAPABILITY_IBSS)) {
1939                         memset(&iwe, 0, sizeof(iwe));
1940                         iwe.cmd = IWEVCUSTOM;
1941                         sprintf(buf, "atim=%d", le16_to_cpu(scan->atim));
1942                         iwe.u.data.length = strlen(buf);
1943                         current_ev = iwe_stream_add_point(current_ev, end_buf,
1944                                                           &iwe, buf);
1945                 }
1946         }
1947         kfree(buf);
1948
1949         if (bss && bss->wpa_ie_len > 0 && bss->wpa_ie_len <= MAX_WPA_IE_LEN) {
1950                 memset(&iwe, 0, sizeof(iwe));
1951                 iwe.cmd = IWEVGENIE;
1952                 iwe.u.data.length = bss->wpa_ie_len;
1953                 current_ev = iwe_stream_add_point(
1954                         current_ev, end_buf, &iwe, bss->wpa_ie);
1955         }
1956
1957         if (bss && bss->rsn_ie_len > 0 && bss->rsn_ie_len <= MAX_WPA_IE_LEN) {
1958                 memset(&iwe, 0, sizeof(iwe));
1959                 iwe.cmd = IWEVGENIE;
1960                 iwe.u.data.length = bss->rsn_ie_len;
1961                 current_ev = iwe_stream_add_point(
1962                         current_ev, end_buf, &iwe, bss->rsn_ie);
1963         }
1964
1965         return current_ev;
1966 }
1967
1968
1969 /* Translate scan data returned from the card to a card independant
1970  * format that the Wireless Tools will understand - Jean II */
1971 static inline int prism2_translate_scan(local_info_t *local,
1972                                         char *buffer, int buflen)
1973 {
1974         struct hfa384x_hostscan_result *scan;
1975         int entry, hostscan;
1976         char *current_ev = buffer;
1977         char *end_buf = buffer + buflen;
1978         struct list_head *ptr;
1979
1980         spin_lock_bh(&local->lock);
1981
1982         list_for_each(ptr, &local->bss_list) {
1983                 struct hostap_bss_info *bss;
1984                 bss = list_entry(ptr, struct hostap_bss_info, list);
1985                 bss->included = 0;
1986         }
1987
1988         hostscan = local->last_scan_type == PRISM2_HOSTSCAN;
1989         for (entry = 0; entry < local->last_scan_results_count; entry++) {
1990                 int found = 0;
1991                 scan = &local->last_scan_results[entry];
1992
1993                 /* Report every SSID if the AP is using multiple SSIDs. If no
1994                  * BSS record is found (e.g., when WPA mode is disabled),
1995                  * report the AP once. */
1996                 list_for_each(ptr, &local->bss_list) {
1997                         struct hostap_bss_info *bss;
1998                         bss = list_entry(ptr, struct hostap_bss_info, list);
1999                         if (memcmp(bss->bssid, scan->bssid, ETH_ALEN) == 0) {
2000                                 bss->included = 1;
2001                                 current_ev = __prism2_translate_scan(
2002                                         local, scan, bss, current_ev, end_buf);
2003                                 found++;
2004                         }
2005                 }
2006                 if (!found) {
2007                         current_ev = __prism2_translate_scan(
2008                                 local, scan, NULL, current_ev, end_buf);
2009                 }
2010                 /* Check if there is space for one more entry */
2011                 if ((end_buf - current_ev) <= IW_EV_ADDR_LEN) {
2012                         /* Ask user space to try again with a bigger buffer */
2013                         spin_unlock_bh(&local->lock);
2014                         return -E2BIG;
2015                 }
2016         }
2017
2018         /* Prism2 firmware has limits (32 at least in some versions) for number
2019          * of BSSes in scan results. Extend this limit by using local BSS list.
2020          */
2021         list_for_each(ptr, &local->bss_list) {
2022                 struct hostap_bss_info *bss;
2023                 bss = list_entry(ptr, struct hostap_bss_info, list);
2024                 if (bss->included)
2025                         continue;
2026                 current_ev = __prism2_translate_scan(local, NULL, bss,
2027                                                      current_ev, end_buf);
2028                 /* Check if there is space for one more entry */
2029                 if ((end_buf - current_ev) <= IW_EV_ADDR_LEN) {
2030                         /* Ask user space to try again with a bigger buffer */
2031                         spin_unlock_bh(&local->lock);
2032                         return -E2BIG;
2033                 }
2034         }
2035
2036         spin_unlock_bh(&local->lock);
2037
2038         return current_ev - buffer;
2039 }
2040 #endif /* PRISM2_NO_STATION_MODES */
2041
2042
2043 static inline int prism2_ioctl_giwscan_sta(struct net_device *dev,
2044                                            struct iw_request_info *info,
2045                                            struct iw_point *data, char *extra)
2046 {
2047 #ifdef PRISM2_NO_STATION_MODES
2048         return -EOPNOTSUPP;
2049 #else /* PRISM2_NO_STATION_MODES */
2050         struct hostap_interface *iface;
2051         local_info_t *local;
2052         int res;
2053
2054         iface = netdev_priv(dev);
2055         local = iface->local;
2056
2057         /* Wait until the scan is finished. We can probably do better
2058          * than that - Jean II */
2059         if (local->scan_timestamp &&
2060             time_before(jiffies, local->scan_timestamp + 3 * HZ)) {
2061                 /* Important note : we don't want to block the caller
2062                  * until results are ready for various reasons.
2063                  * First, managing wait queues is complex and racy
2064                  * (there may be multiple simultaneous callers).
2065                  * Second, we grab some rtnetlink lock before comming
2066                  * here (in dev_ioctl()).
2067                  * Third, the caller can wait on the Wireless Event
2068                  * - Jean II */
2069                 return -EAGAIN;
2070         }
2071         local->scan_timestamp = 0;
2072
2073         res = prism2_translate_scan(local, extra, data->length);
2074
2075         if (res >= 0) {
2076                 data->length = res;
2077                 return 0;
2078         } else {
2079                 data->length = 0;
2080                 return res;
2081         }
2082 #endif /* PRISM2_NO_STATION_MODES */
2083 }
2084
2085
2086 static int prism2_ioctl_giwscan(struct net_device *dev,
2087                                 struct iw_request_info *info,
2088                                 struct iw_point *data, char *extra)
2089 {
2090         struct hostap_interface *iface;
2091         local_info_t *local;
2092         int res;
2093
2094         iface = netdev_priv(dev);
2095         local = iface->local;
2096
2097         if (local->iw_mode == IW_MODE_MASTER) {
2098                 /* In MASTER mode, it doesn't make sense to go around
2099                  * scanning the frequencies and make the stations we serve
2100                  * wait when what the user is really interested about is the
2101                  * list of stations and access points we are talking to.
2102                  * So, just extract results from our cache...
2103                  * Jean II */
2104
2105                 /* Translate to WE format */
2106                 res = prism2_ap_translate_scan(dev, extra);
2107                 if (res >= 0) {
2108                         printk(KERN_DEBUG "Scan result translation succeeded "
2109                                "(length=%d)\n", res);
2110                         data->length = res;
2111                         return 0;
2112                 } else {
2113                         printk(KERN_DEBUG
2114                                "Scan result translation failed (res=%d)\n",
2115                                res);
2116                         data->length = 0;
2117                         return res;
2118                 }
2119         } else {
2120                 /* Station mode */
2121                 return prism2_ioctl_giwscan_sta(dev, info, data, extra);
2122         }
2123 }
2124
2125
2126 static const struct iw_priv_args prism2_priv[] = {
2127         { PRISM2_IOCTL_MONITOR,
2128           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "monitor" },
2129         { PRISM2_IOCTL_READMIF,
2130           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
2131           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, "readmif" },
2132         { PRISM2_IOCTL_WRITEMIF,
2133           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 2, 0, "writemif" },
2134         { PRISM2_IOCTL_RESET,
2135           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "reset" },
2136         { PRISM2_IOCTL_INQUIRE,
2137           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "inquire" },
2138         { PRISM2_IOCTL_SET_RID_WORD,
2139           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "set_rid_word" },
2140         { PRISM2_IOCTL_MACCMD,
2141           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "maccmd" },
2142         { PRISM2_IOCTL_WDS_ADD,
2143           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "wds_add" },
2144         { PRISM2_IOCTL_WDS_DEL,
2145           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "wds_del" },
2146         { PRISM2_IOCTL_ADDMAC,
2147           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "addmac" },
2148         { PRISM2_IOCTL_DELMAC,
2149           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "delmac" },
2150         { PRISM2_IOCTL_KICKMAC,
2151           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "kickmac" },
2152         /* --- raw access to sub-ioctls --- */
2153         { PRISM2_IOCTL_PRISM2_PARAM,
2154           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "prism2_param" },
2155         { PRISM2_IOCTL_GET_PRISM2_PARAM,
2156           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2157           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getprism2_param" },
2158         /* --- sub-ioctls handlers --- */
2159         { PRISM2_IOCTL_PRISM2_PARAM,
2160           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "" },
2161         { PRISM2_IOCTL_GET_PRISM2_PARAM,
2162           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "" },
2163         /* --- sub-ioctls definitions --- */
2164         { PRISM2_PARAM_TXRATECTRL,
2165           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "txratectrl" },
2166         { PRISM2_PARAM_TXRATECTRL,
2167           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gettxratectrl" },
2168         { PRISM2_PARAM_BEACON_INT,
2169           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "beacon_int" },
2170         { PRISM2_PARAM_BEACON_INT,
2171           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbeacon_int" },
2172 #ifndef PRISM2_NO_STATION_MODES
2173         { PRISM2_PARAM_PSEUDO_IBSS,
2174           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "pseudo_ibss" },
2175         { PRISM2_PARAM_PSEUDO_IBSS,
2176           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getpseudo_ibss" },
2177 #endif /* PRISM2_NO_STATION_MODES */
2178         { PRISM2_PARAM_ALC,
2179           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "alc" },
2180         { PRISM2_PARAM_ALC,
2181           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getalc" },
2182         { PRISM2_PARAM_DUMP,
2183           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "dump" },
2184         { PRISM2_PARAM_DUMP,
2185           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdump" },
2186         { PRISM2_PARAM_OTHER_AP_POLICY,
2187           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "other_ap_policy" },
2188         { PRISM2_PARAM_OTHER_AP_POLICY,
2189           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getother_ap_pol" },
2190         { PRISM2_PARAM_AP_MAX_INACTIVITY,
2191           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "max_inactivity" },
2192         { PRISM2_PARAM_AP_MAX_INACTIVITY,
2193           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmax_inactivi" },
2194         { PRISM2_PARAM_AP_BRIDGE_PACKETS,
2195           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "bridge_packets" },
2196         { PRISM2_PARAM_AP_BRIDGE_PACKETS,
2197           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbridge_packe" },
2198         { PRISM2_PARAM_DTIM_PERIOD,
2199           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "dtim_period" },
2200         { PRISM2_PARAM_DTIM_PERIOD,
2201           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdtim_period" },
2202         { PRISM2_PARAM_AP_NULLFUNC_ACK,
2203           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "nullfunc_ack" },
2204         { PRISM2_PARAM_AP_NULLFUNC_ACK,
2205           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getnullfunc_ack" },
2206         { PRISM2_PARAM_MAX_WDS,
2207           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "max_wds" },
2208         { PRISM2_PARAM_MAX_WDS,
2209           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmax_wds" },
2210         { PRISM2_PARAM_AP_AUTOM_AP_WDS,
2211           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "autom_ap_wds" },
2212         { PRISM2_PARAM_AP_AUTOM_AP_WDS,
2213           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getautom_ap_wds" },
2214         { PRISM2_PARAM_AP_AUTH_ALGS,
2215           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ap_auth_algs" },
2216         { PRISM2_PARAM_AP_AUTH_ALGS,
2217           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getap_auth_algs" },
2218         { PRISM2_PARAM_MONITOR_ALLOW_FCSERR,
2219           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "allow_fcserr" },
2220         { PRISM2_PARAM_MONITOR_ALLOW_FCSERR,
2221           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getallow_fcserr" },
2222         { PRISM2_PARAM_HOST_ENCRYPT,
2223           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_encrypt" },
2224         { PRISM2_PARAM_HOST_ENCRYPT,
2225           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_encrypt" },
2226         { PRISM2_PARAM_HOST_DECRYPT,
2227           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_decrypt" },
2228         { PRISM2_PARAM_HOST_DECRYPT,
2229           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_decrypt" },
2230 #ifndef PRISM2_NO_STATION_MODES
2231         { PRISM2_PARAM_HOST_ROAMING,
2232           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_roaming" },
2233         { PRISM2_PARAM_HOST_ROAMING,
2234           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_roaming" },
2235 #endif /* PRISM2_NO_STATION_MODES */
2236         { PRISM2_PARAM_BCRX_STA_KEY,
2237           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "bcrx_sta_key" },
2238         { PRISM2_PARAM_BCRX_STA_KEY,
2239           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbcrx_sta_key" },
2240         { PRISM2_PARAM_IEEE_802_1X,
2241           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ieee_802_1x" },
2242         { PRISM2_PARAM_IEEE_802_1X,
2243           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getieee_802_1x" },
2244         { PRISM2_PARAM_ANTSEL_TX,
2245           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "antsel_tx" },
2246         { PRISM2_PARAM_ANTSEL_TX,
2247           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getantsel_tx" },
2248         { PRISM2_PARAM_ANTSEL_RX,
2249           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "antsel_rx" },
2250         { PRISM2_PARAM_ANTSEL_RX,
2251           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getantsel_rx" },
2252         { PRISM2_PARAM_MONITOR_TYPE,
2253           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "monitor_type" },
2254         { PRISM2_PARAM_MONITOR_TYPE,
2255           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmonitor_type" },
2256         { PRISM2_PARAM_WDS_TYPE,
2257           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wds_type" },
2258         { PRISM2_PARAM_WDS_TYPE,
2259           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getwds_type" },
2260         { PRISM2_PARAM_HOSTSCAN,
2261           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostscan" },
2262         { PRISM2_PARAM_HOSTSCAN,
2263           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostscan" },
2264         { PRISM2_PARAM_AP_SCAN,
2265           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ap_scan" },
2266         { PRISM2_PARAM_AP_SCAN,
2267           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getap_scan" },
2268         { PRISM2_PARAM_ENH_SEC,
2269           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "enh_sec" },
2270         { PRISM2_PARAM_ENH_SEC,
2271           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getenh_sec" },
2272 #ifdef PRISM2_IO_DEBUG
2273         { PRISM2_PARAM_IO_DEBUG,
2274           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "io_debug" },
2275         { PRISM2_PARAM_IO_DEBUG,
2276           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getio_debug" },
2277 #endif /* PRISM2_IO_DEBUG */
2278         { PRISM2_PARAM_BASIC_RATES,
2279           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "basic_rates" },
2280         { PRISM2_PARAM_BASIC_RATES,
2281           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbasic_rates" },
2282         { PRISM2_PARAM_OPER_RATES,
2283           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "oper_rates" },
2284         { PRISM2_PARAM_OPER_RATES,
2285           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getoper_rates" },
2286         { PRISM2_PARAM_HOSTAPD,
2287           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostapd" },
2288         { PRISM2_PARAM_HOSTAPD,
2289           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostapd" },
2290         { PRISM2_PARAM_HOSTAPD_STA,
2291           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostapd_sta" },
2292         { PRISM2_PARAM_HOSTAPD_STA,
2293           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostapd_sta" },
2294         { PRISM2_PARAM_WPA,
2295           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wpa" },
2296         { PRISM2_PARAM_WPA,
2297           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getwpa" },
2298         { PRISM2_PARAM_PRIVACY_INVOKED,
2299           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "privacy_invoked" },
2300         { PRISM2_PARAM_PRIVACY_INVOKED,
2301           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getprivacy_invo" },
2302         { PRISM2_PARAM_TKIP_COUNTERMEASURES,
2303           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "tkip_countermea" },
2304         { PRISM2_PARAM_TKIP_COUNTERMEASURES,
2305           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gettkip_counter" },
2306         { PRISM2_PARAM_DROP_UNENCRYPTED,
2307           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "drop_unencrypte" },
2308         { PRISM2_PARAM_DROP_UNENCRYPTED,
2309           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdrop_unencry" },
2310         { PRISM2_PARAM_SCAN_CHANNEL_MASK,
2311           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "scan_channels" },
2312         { PRISM2_PARAM_SCAN_CHANNEL_MASK,
2313           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getscan_channel" },
2314 };
2315
2316
2317 static int prism2_ioctl_priv_inquire(struct net_device *dev, int *i)
2318 {
2319         struct hostap_interface *iface;
2320         local_info_t *local;
2321
2322         iface = netdev_priv(dev);
2323         local = iface->local;
2324
2325         if (local->func->cmd(dev, HFA384X_CMDCODE_INQUIRE, *i, NULL, NULL))
2326                 return -EOPNOTSUPP;
2327
2328         return 0;
2329 }
2330
2331
2332 static int prism2_ioctl_priv_prism2_param(struct net_device *dev,
2333                                           struct iw_request_info *info,
2334                                           void *wrqu, char *extra)
2335 {
2336         struct hostap_interface *iface;
2337         local_info_t *local;
2338         int *i = (int *) extra;
2339         int param = *i;
2340         int value = *(i + 1);
2341         int ret = 0;
2342         u16 val;
2343
2344         iface = netdev_priv(dev);
2345         local = iface->local;
2346
2347         switch (param) {
2348         case PRISM2_PARAM_TXRATECTRL:
2349                 local->fw_tx_rate_control = value;
2350                 break;
2351
2352         case PRISM2_PARAM_BEACON_INT:
2353                 if (hostap_set_word(dev, HFA384X_RID_CNFBEACONINT, value) ||
2354                     local->func->reset_port(dev))
2355                         ret = -EINVAL;
2356                 else
2357                         local->beacon_int = value;
2358                 break;
2359
2360 #ifndef PRISM2_NO_STATION_MODES
2361         case PRISM2_PARAM_PSEUDO_IBSS:
2362                 if (value == local->pseudo_adhoc)
2363                         break;
2364
2365                 if (value != 0 && value != 1) {
2366                         ret = -EINVAL;
2367                         break;
2368                 }
2369
2370                 printk(KERN_DEBUG "prism2: %s: pseudo IBSS change %d -> %d\n",
2371                        dev->name, local->pseudo_adhoc, value);
2372                 local->pseudo_adhoc = value;
2373                 if (local->iw_mode != IW_MODE_ADHOC)
2374                         break;
2375
2376                 if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2377                                     hostap_get_porttype(local))) {
2378                         ret = -EOPNOTSUPP;
2379                         break;
2380                 }
2381
2382                 if (local->func->reset_port(dev))
2383                         ret = -EINVAL;
2384                 break;
2385 #endif /* PRISM2_NO_STATION_MODES */
2386
2387         case PRISM2_PARAM_ALC:
2388                 printk(KERN_DEBUG "%s: %s ALC\n", dev->name,
2389                        value == 0 ? "Disabling" : "Enabling");
2390                 val = HFA384X_TEST_CFG_BIT_ALC;
2391                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
2392                                  (HFA384X_TEST_CFG_BITS << 8),
2393                                  value == 0 ? 0 : 1, &val, NULL);
2394                 break;
2395
2396         case PRISM2_PARAM_DUMP:
2397                 local->frame_dump = value;
2398                 break;
2399
2400         case PRISM2_PARAM_OTHER_AP_POLICY:
2401                 if (value < 0 || value > 3) {
2402                         ret = -EINVAL;
2403                         break;
2404                 }
2405                 if (local->ap != NULL)
2406                         local->ap->ap_policy = value;
2407                 break;
2408
2409         case PRISM2_PARAM_AP_MAX_INACTIVITY:
2410                 if (value < 0 || value > 7 * 24 * 60 * 60) {
2411                         ret = -EINVAL;
2412                         break;
2413                 }
2414                 if (local->ap != NULL)
2415                         local->ap->max_inactivity = value * HZ;
2416                 break;
2417
2418         case PRISM2_PARAM_AP_BRIDGE_PACKETS:
2419                 if (local->ap != NULL)
2420                         local->ap->bridge_packets = value;
2421                 break;
2422
2423         case PRISM2_PARAM_DTIM_PERIOD:
2424                 if (value < 0 || value > 65535) {
2425                         ret = -EINVAL;
2426                         break;
2427                 }
2428                 if (hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD, value)
2429                     || local->func->reset_port(dev))
2430                         ret = -EINVAL;
2431                 else
2432                         local->dtim_period = value;
2433                 break;
2434
2435         case PRISM2_PARAM_AP_NULLFUNC_ACK:
2436                 if (local->ap != NULL)
2437                         local->ap->nullfunc_ack = value;
2438                 break;
2439
2440         case PRISM2_PARAM_MAX_WDS:
2441                 local->wds_max_connections = value;
2442                 break;
2443
2444         case PRISM2_PARAM_AP_AUTOM_AP_WDS:
2445                 if (local->ap != NULL) {
2446                         if (!local->ap->autom_ap_wds && value) {
2447                                 /* add WDS link to all APs in STA table */
2448                                 hostap_add_wds_links(local);
2449                         }
2450                         local->ap->autom_ap_wds = value;
2451                 }
2452                 break;
2453
2454         case PRISM2_PARAM_AP_AUTH_ALGS:
2455                 local->auth_algs = value;
2456                 if (hostap_set_auth_algs(local))
2457                         ret = -EINVAL;
2458                 break;
2459
2460         case PRISM2_PARAM_MONITOR_ALLOW_FCSERR:
2461                 local->monitor_allow_fcserr = value;
2462                 break;
2463
2464         case PRISM2_PARAM_HOST_ENCRYPT:
2465                 local->host_encrypt = value;
2466                 if (hostap_set_encryption(local) ||
2467                     local->func->reset_port(dev))
2468                         ret = -EINVAL;
2469                 break;
2470
2471         case PRISM2_PARAM_HOST_DECRYPT:
2472                 local->host_decrypt = value;
2473                 if (hostap_set_encryption(local) ||
2474                     local->func->reset_port(dev))
2475                         ret = -EINVAL;
2476                 break;
2477
2478 #ifndef PRISM2_NO_STATION_MODES
2479         case PRISM2_PARAM_HOST_ROAMING:
2480                 if (value < 0 || value > 2) {
2481                         ret = -EINVAL;
2482                         break;
2483                 }
2484                 local->host_roaming = value;
2485                 if (hostap_set_roaming(local) || local->func->reset_port(dev))
2486                         ret = -EINVAL;
2487                 break;
2488 #endif /* PRISM2_NO_STATION_MODES */
2489
2490         case PRISM2_PARAM_BCRX_STA_KEY:
2491                 local->bcrx_sta_key = value;
2492                 break;
2493
2494         case PRISM2_PARAM_IEEE_802_1X:
2495                 local->ieee_802_1x = value;
2496                 break;
2497
2498         case PRISM2_PARAM_ANTSEL_TX:
2499                 if (value < 0 || value > HOSTAP_ANTSEL_HIGH) {
2500                         ret = -EINVAL;
2501                         break;
2502                 }
2503                 local->antsel_tx = value;
2504                 hostap_set_antsel(local);
2505                 break;
2506
2507         case PRISM2_PARAM_ANTSEL_RX:
2508                 if (value < 0 || value > HOSTAP_ANTSEL_HIGH) {
2509                         ret = -EINVAL;
2510                         break;
2511                 }
2512                 local->antsel_rx = value;
2513                 hostap_set_antsel(local);
2514                 break;
2515
2516         case PRISM2_PARAM_MONITOR_TYPE:
2517                 if (value != PRISM2_MONITOR_80211 &&
2518                     value != PRISM2_MONITOR_CAPHDR &&
2519                     value != PRISM2_MONITOR_PRISM) {
2520                         ret = -EINVAL;
2521                         break;
2522                 }
2523                 local->monitor_type = value;
2524                 if (local->iw_mode == IW_MODE_MONITOR)
2525                         hostap_monitor_set_type(local);
2526                 break;
2527
2528         case PRISM2_PARAM_WDS_TYPE:
2529                 local->wds_type = value;
2530                 break;
2531
2532         case PRISM2_PARAM_HOSTSCAN:
2533         {
2534                 struct hfa384x_hostscan_request scan_req;
2535                 u16 rate;
2536
2537                 memset(&scan_req, 0, sizeof(scan_req));
2538                 scan_req.channel_list = __constant_cpu_to_le16(0x3fff);
2539                 switch (value) {
2540                 case 1: rate = HFA384X_RATES_1MBPS; break;
2541                 case 2: rate = HFA384X_RATES_2MBPS; break;
2542                 case 3: rate = HFA384X_RATES_5MBPS; break;
2543                 case 4: rate = HFA384X_RATES_11MBPS; break;
2544                 default: rate = HFA384X_RATES_1MBPS; break;
2545                 }
2546                 scan_req.txrate = cpu_to_le16(rate);
2547                 /* leave SSID empty to accept all SSIDs */
2548
2549                 if (local->iw_mode == IW_MODE_MASTER) {
2550                         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2551                                             HFA384X_PORTTYPE_BSS) ||
2552                             local->func->reset_port(dev))
2553                                 printk(KERN_DEBUG "Leaving Host AP mode "
2554                                        "for HostScan failed\n");
2555                 }
2556
2557                 if (local->func->set_rid(dev, HFA384X_RID_HOSTSCAN, &scan_req,
2558                                          sizeof(scan_req))) {
2559                         printk(KERN_DEBUG "HOSTSCAN failed\n");
2560                         ret = -EINVAL;
2561                 }
2562                 if (local->iw_mode == IW_MODE_MASTER) {
2563                         wait_queue_t __wait;
2564                         init_waitqueue_entry(&__wait, current);
2565                         add_wait_queue(&local->hostscan_wq, &__wait);
2566                         set_current_state(TASK_INTERRUPTIBLE);
2567                         schedule_timeout(HZ);
2568                         if (signal_pending(current))
2569                                 ret = -EINTR;
2570                         set_current_state(TASK_RUNNING);
2571                         remove_wait_queue(&local->hostscan_wq, &__wait);
2572
2573                         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2574                                             HFA384X_PORTTYPE_HOSTAP) ||
2575                             local->func->reset_port(dev))
2576                                 printk(KERN_DEBUG "Returning to Host AP mode "
2577                                        "after HostScan failed\n");
2578                 }
2579                 break;
2580         }
2581
2582         case PRISM2_PARAM_AP_SCAN:
2583                 local->passive_scan_interval = value;
2584                 if (timer_pending(&local->passive_scan_timer))
2585                         del_timer(&local->passive_scan_timer);
2586                 if (value > 0) {
2587                         local->passive_scan_timer.expires = jiffies +
2588                                 local->passive_scan_interval * HZ;
2589                         add_timer(&local->passive_scan_timer);
2590                 }
2591                 break;
2592
2593         case PRISM2_PARAM_ENH_SEC:
2594                 if (value < 0 || value > 3) {
2595                         ret = -EINVAL;
2596                         break;
2597                 }
2598                 local->enh_sec = value;
2599                 if (hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY,
2600                                     local->enh_sec) ||
2601                     local->func->reset_port(dev)) {
2602                         printk(KERN_INFO "%s: cnfEnhSecurity requires STA f/w "
2603                                "1.6.3 or newer\n", dev->name);
2604                         ret = -EOPNOTSUPP;
2605                 }
2606                 break;
2607
2608 #ifdef PRISM2_IO_DEBUG
2609         case PRISM2_PARAM_IO_DEBUG:
2610                 local->io_debug_enabled = value;
2611                 break;
2612 #endif /* PRISM2_IO_DEBUG */
2613
2614         case PRISM2_PARAM_BASIC_RATES:
2615                 if ((value & local->tx_rate_control) != value || value == 0) {
2616                         printk(KERN_INFO "%s: invalid basic rate set - basic "
2617                                "rates must be in supported rate set\n",
2618                                dev->name);
2619                         ret = -EINVAL;
2620                         break;
2621                 }
2622                 local->basic_rates = value;
2623                 if (hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
2624                                     local->basic_rates) ||
2625                     local->func->reset_port(dev))
2626                         ret = -EINVAL;
2627                 break;
2628
2629         case PRISM2_PARAM_OPER_RATES:
2630                 local->tx_rate_control = value;
2631                 if (hostap_set_rate(dev))
2632                         ret = -EINVAL;
2633                 break;
2634
2635         case PRISM2_PARAM_HOSTAPD:
2636                 ret = hostap_set_hostapd(local, value, 1);
2637                 break;
2638
2639         case PRISM2_PARAM_HOSTAPD_STA:
2640                 ret = hostap_set_hostapd_sta(local, value, 1);
2641                 break;
2642
2643         case PRISM2_PARAM_WPA:
2644                 local->wpa = value;
2645                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
2646                         ret = -EOPNOTSUPP;
2647                 else if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE,
2648                                          value ? 1 : 0))
2649                         ret = -EINVAL;
2650                 break;
2651
2652         case PRISM2_PARAM_PRIVACY_INVOKED:
2653                 local->privacy_invoked = value;
2654                 if (hostap_set_encryption(local) ||
2655                     local->func->reset_port(dev))
2656                         ret = -EINVAL;
2657                 break;
2658
2659         case PRISM2_PARAM_TKIP_COUNTERMEASURES:
2660                 local->tkip_countermeasures = value;
2661                 break;
2662
2663         case PRISM2_PARAM_DROP_UNENCRYPTED:
2664                 local->drop_unencrypted = value;
2665                 break;
2666
2667         case PRISM2_PARAM_SCAN_CHANNEL_MASK:
2668                 local->scan_channel_mask = value;
2669                 break;
2670
2671         default:
2672                 printk(KERN_DEBUG "%s: prism2_param: unknown param %d\n",
2673                        dev->name, param);
2674                 ret = -EOPNOTSUPP;
2675                 break;
2676         }
2677
2678         return ret;
2679 }
2680
2681
2682 static int prism2_ioctl_priv_get_prism2_param(struct net_device *dev,
2683                                               struct iw_request_info *info,
2684                                               void *wrqu, char *extra)
2685 {
2686         struct hostap_interface *iface;
2687         local_info_t *local;
2688         int *param = (int *) extra;
2689         int ret = 0;
2690
2691         iface = netdev_priv(dev);
2692         local = iface->local;
2693
2694         switch (*param) {
2695         case PRISM2_PARAM_TXRATECTRL:
2696                 *param = local->fw_tx_rate_control;
2697                 break;
2698
2699         case PRISM2_PARAM_BEACON_INT:
2700                 *param = local->beacon_int;
2701                 break;
2702
2703         case PRISM2_PARAM_PSEUDO_IBSS:
2704                 *param = local->pseudo_adhoc;
2705                 break;
2706
2707         case PRISM2_PARAM_ALC:
2708                 ret = -EOPNOTSUPP; /* FIX */
2709                 break;
2710
2711         case PRISM2_PARAM_DUMP:
2712                 *param = local->frame_dump;
2713                 break;
2714
2715         case PRISM2_PARAM_OTHER_AP_POLICY:
2716                 if (local->ap != NULL)
2717                         *param = local->ap->ap_policy;
2718                 else
2719                         ret = -EOPNOTSUPP;
2720                 break;
2721
2722         case PRISM2_PARAM_AP_MAX_INACTIVITY:
2723                 if (local->ap != NULL)
2724                         *param = local->ap->max_inactivity / HZ;
2725                 else
2726                         ret = -EOPNOTSUPP;
2727                 break;
2728
2729         case PRISM2_PARAM_AP_BRIDGE_PACKETS:
2730                 if (local->ap != NULL)
2731                         *param = local->ap->bridge_packets;
2732                 else
2733                         ret = -EOPNOTSUPP;
2734                 break;
2735
2736         case PRISM2_PARAM_DTIM_PERIOD:
2737                 *param = local->dtim_period;
2738                 break;
2739
2740         case PRISM2_PARAM_AP_NULLFUNC_ACK:
2741                 if (local->ap != NULL)
2742                         *param = local->ap->nullfunc_ack;
2743                 else
2744                         ret = -EOPNOTSUPP;
2745                 break;
2746
2747         case PRISM2_PARAM_MAX_WDS:
2748                 *param = local->wds_max_connections;
2749                 break;
2750
2751         case PRISM2_PARAM_AP_AUTOM_AP_WDS:
2752                 if (local->ap != NULL)
2753                         *param = local->ap->autom_ap_wds;
2754                 else
2755                         ret = -EOPNOTSUPP;
2756                 break;
2757
2758         case PRISM2_PARAM_AP_AUTH_ALGS:
2759                 *param = local->auth_algs;
2760                 break;
2761
2762         case PRISM2_PARAM_MONITOR_ALLOW_FCSERR:
2763                 *param = local->monitor_allow_fcserr;
2764                 break;
2765
2766         case PRISM2_PARAM_HOST_ENCRYPT:
2767                 *param = local->host_encrypt;
2768                 break;
2769
2770         case PRISM2_PARAM_HOST_DECRYPT:
2771                 *param = local->host_decrypt;
2772                 break;
2773
2774         case PRISM2_PARAM_HOST_ROAMING:
2775                 *param = local->host_roaming;
2776                 break;
2777
2778         case PRISM2_PARAM_BCRX_STA_KEY:
2779                 *param = local->bcrx_sta_key;
2780                 break;
2781
2782         case PRISM2_PARAM_IEEE_802_1X:
2783                 *param = local->ieee_802_1x;
2784                 break;
2785
2786         case PRISM2_PARAM_ANTSEL_TX:
2787                 *param = local->antsel_tx;
2788                 break;
2789
2790         case PRISM2_PARAM_ANTSEL_RX:
2791                 *param = local->antsel_rx;
2792                 break;
2793
2794         case PRISM2_PARAM_MONITOR_TYPE:
2795                 *param = local->monitor_type;
2796                 break;
2797
2798         case PRISM2_PARAM_WDS_TYPE:
2799                 *param = local->wds_type;
2800                 break;
2801
2802         case PRISM2_PARAM_HOSTSCAN:
2803                 ret = -EOPNOTSUPP;
2804                 break;
2805
2806         case PRISM2_PARAM_AP_SCAN:
2807                 *param = local->passive_scan_interval;
2808                 break;
2809
2810         case PRISM2_PARAM_ENH_SEC:
2811                 *param = local->enh_sec;
2812                 break;
2813
2814 #ifdef PRISM2_IO_DEBUG
2815         case PRISM2_PARAM_IO_DEBUG:
2816                 *param = local->io_debug_enabled;
2817                 break;
2818 #endif /* PRISM2_IO_DEBUG */
2819
2820         case PRISM2_PARAM_BASIC_RATES:
2821                 *param = local->basic_rates;
2822                 break;
2823
2824         case PRISM2_PARAM_OPER_RATES:
2825                 *param = local->tx_rate_control;
2826                 break;
2827
2828         case PRISM2_PARAM_HOSTAPD:
2829                 *param = local->hostapd;
2830                 break;
2831
2832         case PRISM2_PARAM_HOSTAPD_STA:
2833                 *param = local->hostapd_sta;
2834                 break;
2835
2836         case PRISM2_PARAM_WPA:
2837                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
2838                         ret = -EOPNOTSUPP;
2839                 *param = local->wpa;
2840                 break;
2841
2842         case PRISM2_PARAM_PRIVACY_INVOKED:
2843                 *param = local->privacy_invoked;
2844                 break;
2845
2846         case PRISM2_PARAM_TKIP_COUNTERMEASURES:
2847                 *param = local->tkip_countermeasures;
2848                 break;
2849
2850         case PRISM2_PARAM_DROP_UNENCRYPTED:
2851                 *param = local->drop_unencrypted;
2852                 break;
2853
2854         case PRISM2_PARAM_SCAN_CHANNEL_MASK:
2855                 *param = local->scan_channel_mask;
2856                 break;
2857
2858         default:
2859                 printk(KERN_DEBUG "%s: get_prism2_param: unknown param %d\n",
2860                        dev->name, *param);
2861                 ret = -EOPNOTSUPP;
2862                 break;
2863         }
2864
2865         return ret;
2866 }
2867
2868
2869 static int prism2_ioctl_priv_readmif(struct net_device *dev,
2870                                      struct iw_request_info *info,
2871                                      void *wrqu, char *extra)
2872 {
2873         struct hostap_interface *iface;
2874         local_info_t *local;
2875         u16 resp0;
2876
2877         iface = netdev_priv(dev);
2878         local = iface->local;
2879
2880         if (local->func->cmd(dev, HFA384X_CMDCODE_READMIF, *extra, NULL,
2881                              &resp0))
2882                 return -EOPNOTSUPP;
2883         else
2884                 *extra = resp0;
2885
2886         return 0;
2887 }
2888
2889
2890 static int prism2_ioctl_priv_writemif(struct net_device *dev,
2891                                       struct iw_request_info *info,
2892                                       void *wrqu, char *extra)
2893 {
2894         struct hostap_interface *iface;
2895         local_info_t *local;
2896         u16 cr, val;
2897
2898         iface = netdev_priv(dev);
2899         local = iface->local;
2900
2901         cr = *extra;
2902         val = *(extra + 1);
2903         if (local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF, cr, &val, NULL))
2904                 return -EOPNOTSUPP;
2905
2906         return 0;
2907 }
2908
2909
2910 static int prism2_ioctl_priv_monitor(struct net_device *dev, int *i)
2911 {
2912         struct hostap_interface *iface;
2913         local_info_t *local;
2914         int ret = 0;
2915         u32 mode;
2916
2917         iface = netdev_priv(dev);
2918         local = iface->local;
2919
2920         printk(KERN_DEBUG "%s: process %d (%s) used deprecated iwpriv monitor "
2921                "- update software to use iwconfig mode monitor\n",
2922                dev->name, task_pid_nr(current), current->comm);
2923
2924         /* Backward compatibility code - this can be removed at some point */
2925
2926         if (*i == 0) {
2927                 /* Disable monitor mode - old mode was not saved, so go to
2928                  * Master mode */
2929                 mode = IW_MODE_MASTER;
2930                 ret = prism2_ioctl_siwmode(dev, NULL, &mode, NULL);
2931         } else if (*i == 1) {
2932                 /* netlink socket mode is not supported anymore since it did
2933                  * not separate different devices from each other and was not
2934                  * best method for delivering large amount of packets to
2935                  * user space */
2936                 ret = -EOPNOTSUPP;
2937         } else if (*i == 2 || *i == 3) {
2938                 switch (*i) {
2939                 case 2:
2940                         local->monitor_type = PRISM2_MONITOR_80211;
2941                         break;
2942                 case 3:
2943                         local->monitor_type = PRISM2_MONITOR_PRISM;
2944                         break;
2945                 }
2946                 mode = IW_MODE_MONITOR;
2947                 ret = prism2_ioctl_siwmode(dev, NULL, &mode, NULL);
2948                 hostap_monitor_mode_enable(local);
2949         } else
2950                 ret = -EINVAL;
2951
2952         return ret;
2953 }
2954
2955
2956 static int prism2_ioctl_priv_reset(struct net_device *dev, int *i)
2957 {
2958         struct hostap_interface *iface;
2959         local_info_t *local;
2960
2961         iface = netdev_priv(dev);
2962         local = iface->local;
2963
2964         printk(KERN_DEBUG "%s: manual reset request(%d)\n", dev->name, *i);
2965         switch (*i) {
2966         case 0:
2967                 /* Disable and enable card */
2968                 local->func->hw_shutdown(dev, 1);
2969                 local->func->hw_config(dev, 0);
2970                 break;
2971
2972         case 1:
2973                 /* COR sreset */
2974                 local->func->hw_reset(dev);
2975                 break;
2976
2977         case 2:
2978                 /* Disable and enable port 0 */
2979                 local->func->reset_port(dev);
2980                 break;
2981
2982         case 3:
2983                 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
2984                 if (local->func->cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL,
2985                                      NULL))
2986                         return -EINVAL;
2987                 break;
2988
2989         case 4:
2990                 if (local->func->cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL,
2991                                      NULL))
2992                         return -EINVAL;
2993                 break;
2994
2995         default:
2996                 printk(KERN_DEBUG "Unknown reset request %d\n", *i);
2997                 return -EOPNOTSUPP;
2998         }
2999
3000         return 0;
3001 }
3002
3003
3004 static int prism2_ioctl_priv_set_rid_word(struct net_device *dev, int *i)
3005 {
3006         int rid = *i;
3007         int value = *(i + 1);
3008
3009         printk(KERN_DEBUG "%s: Set RID[0x%X] = %d\n", dev->name, rid, value);
3010
3011         if (hostap_set_word(dev, rid, value))
3012                 return -EINVAL;
3013
3014         return 0;
3015 }
3016
3017
3018 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3019 static int ap_mac_cmd_ioctl(local_info_t *local, int *cmd)
3020 {
3021         int ret = 0;
3022
3023         switch (*cmd) {
3024         case AP_MAC_CMD_POLICY_OPEN:
3025                 local->ap->mac_restrictions.policy = MAC_POLICY_OPEN;
3026                 break;
3027         case AP_MAC_CMD_POLICY_ALLOW:
3028                 local->ap->mac_restrictions.policy = MAC_POLICY_ALLOW;
3029                 break;
3030         case AP_MAC_CMD_POLICY_DENY:
3031                 local->ap->mac_restrictions.policy = MAC_POLICY_DENY;
3032                 break;
3033         case AP_MAC_CMD_FLUSH:
3034                 ap_control_flush_macs(&local->ap->mac_restrictions);
3035                 break;
3036         case AP_MAC_CMD_KICKALL:
3037                 ap_control_kickall(local->ap);
3038                 hostap_deauth_all_stas(local->dev, local->ap, 0);
3039                 break;
3040         default:
3041                 ret = -EOPNOTSUPP;
3042                 break;
3043         }
3044
3045         return ret;
3046 }
3047 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3048
3049
3050 #ifdef PRISM2_DOWNLOAD_SUPPORT
3051 static int prism2_ioctl_priv_download(local_info_t *local, struct iw_point *p)
3052 {
3053         struct prism2_download_param *param;
3054         int ret = 0;
3055
3056         if (p->length < sizeof(struct prism2_download_param) ||
3057             p->length > 1024 || !p->pointer)
3058                 return -EINVAL;
3059
3060         param = (struct prism2_download_param *)
3061                 kmalloc(p->length, GFP_KERNEL);
3062         if (param == NULL)
3063                 return -ENOMEM;
3064
3065         if (copy_from_user(param, p->pointer, p->length)) {
3066                 ret = -EFAULT;
3067                 goto out;
3068         }
3069
3070         if (p->length < sizeof(struct prism2_download_param) +
3071             param->num_areas * sizeof(struct prism2_download_area)) {
3072                 ret = -EINVAL;
3073                 goto out;
3074         }
3075
3076         ret = local->func->download(local, param);
3077
3078  out:
3079         kfree(param);
3080         return ret;
3081 }
3082 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3083
3084
3085 static int prism2_set_genericelement(struct net_device *dev, u8 *elem,
3086                                      size_t len)
3087 {
3088         struct hostap_interface *iface = netdev_priv(dev);
3089         local_info_t *local = iface->local;
3090         u8 *buf;
3091
3092         /*
3093          * Add 16-bit length in the beginning of the buffer because Prism2 RID
3094          * includes it.
3095          */
3096         buf = kmalloc(len + 2, GFP_KERNEL);
3097         if (buf == NULL)
3098                 return -ENOMEM;
3099
3100         *((__le16 *) buf) = cpu_to_le16(len);
3101         memcpy(buf + 2, elem, len);
3102
3103         kfree(local->generic_elem);
3104         local->generic_elem = buf;
3105         local->generic_elem_len = len + 2;
3106
3107         return local->func->set_rid(local->dev, HFA384X_RID_GENERICELEMENT,
3108                                     buf, len + 2);
3109 }
3110
3111
3112 static int prism2_ioctl_siwauth(struct net_device *dev,
3113                                 struct iw_request_info *info,
3114                                 struct iw_param *data, char *extra)
3115 {
3116         struct hostap_interface *iface = netdev_priv(dev);
3117         local_info_t *local = iface->local;
3118
3119         switch (data->flags & IW_AUTH_INDEX) {
3120         case IW_AUTH_WPA_VERSION:
3121         case IW_AUTH_CIPHER_PAIRWISE:
3122         case IW_AUTH_CIPHER_GROUP:
3123         case IW_AUTH_KEY_MGMT:
3124                 /*
3125                  * Host AP driver does not use these parameters and allows
3126                  * wpa_supplicant to control them internally.
3127                  */
3128                 break;
3129         case IW_AUTH_TKIP_COUNTERMEASURES:
3130                 local->tkip_countermeasures = data->value;
3131                 break;
3132         case IW_AUTH_DROP_UNENCRYPTED:
3133                 local->drop_unencrypted = data->value;
3134                 break;
3135         case IW_AUTH_80211_AUTH_ALG:
3136                 local->auth_algs = data->value;
3137                 break;
3138         case IW_AUTH_WPA_ENABLED:
3139                 if (data->value == 0) {
3140                         local->wpa = 0;
3141                         if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
3142                                 break;
3143                         prism2_set_genericelement(dev, "", 0);
3144                         local->host_roaming = 0;
3145                         local->privacy_invoked = 0;
3146                         if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE,
3147                                             0) ||
3148                             hostap_set_roaming(local) ||
3149                             hostap_set_encryption(local) ||
3150                             local->func->reset_port(dev))
3151                                 return -EINVAL;
3152                         break;
3153                 }
3154                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
3155                         return -EOPNOTSUPP;
3156                 local->host_roaming = 2;
3157                 local->privacy_invoked = 1;
3158                 local->wpa = 1;
3159                 if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1) ||
3160                     hostap_set_roaming(local) ||
3161                     hostap_set_encryption(local) ||
3162                     local->func->reset_port(dev))
3163                         return -EINVAL;
3164                 break;
3165         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3166                 local->ieee_802_1x = data->value;
3167                 break;
3168         case IW_AUTH_PRIVACY_INVOKED:
3169                 local->privacy_invoked = data->value;
3170                 break;
3171         default:
3172                 return -EOPNOTSUPP;
3173         }
3174         return 0;
3175 }
3176
3177
3178 static int prism2_ioctl_giwauth(struct net_device *dev,
3179                                 struct iw_request_info *info,
3180                                 struct iw_param *data, char *extra)
3181 {
3182         struct hostap_interface *iface = netdev_priv(dev);
3183         local_info_t *local = iface->local;
3184
3185         switch (data->flags & IW_AUTH_INDEX) {
3186         case IW_AUTH_WPA_VERSION:
3187         case IW_AUTH_CIPHER_PAIRWISE:
3188         case IW_AUTH_CIPHER_GROUP:
3189         case IW_AUTH_KEY_MGMT:
3190                 /*
3191                  * Host AP driver does not use these parameters and allows
3192                  * wpa_supplicant to control them internally.
3193                  */
3194                 return -EOPNOTSUPP;
3195         case IW_AUTH_TKIP_COUNTERMEASURES:
3196                 data->value = local->tkip_countermeasures;
3197                 break;
3198         case IW_AUTH_DROP_UNENCRYPTED:
3199                 data->value = local->drop_unencrypted;
3200                 break;
3201         case IW_AUTH_80211_AUTH_ALG:
3202                 data->value = local->auth_algs;
3203                 break;
3204         case IW_AUTH_WPA_ENABLED:
3205                 data->value = local->wpa;
3206                 break;
3207         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3208                 data->value = local->ieee_802_1x;
3209                 break;
3210         default:
3211                 return -EOPNOTSUPP;
3212         }
3213         return 0;
3214 }
3215
3216
3217 static int prism2_ioctl_siwencodeext(struct net_device *dev,
3218                                      struct iw_request_info *info,
3219                                      struct iw_point *erq, char *extra)
3220 {
3221         struct hostap_interface *iface = netdev_priv(dev);
3222         local_info_t *local = iface->local;
3223         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
3224         int i, ret = 0;
3225         struct ieee80211_crypto_ops *ops;
3226         struct ieee80211_crypt_data **crypt;
3227         void *sta_ptr;
3228         u8 *addr;
3229         const char *alg, *module;
3230
3231         i = erq->flags & IW_ENCODE_INDEX;
3232         if (i > WEP_KEYS)
3233                 return -EINVAL;
3234         if (i < 1 || i > WEP_KEYS)
3235                 i = local->tx_keyidx;
3236         else
3237                 i--;
3238         if (i < 0 || i >= WEP_KEYS)
3239                 return -EINVAL;
3240
3241         addr = ext->addr.sa_data;
3242         if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff &&
3243             addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) {
3244                 sta_ptr = NULL;
3245                 crypt = &local->crypt[i];
3246         } else {
3247                 if (i != 0)
3248                         return -EINVAL;
3249                 sta_ptr = ap_crypt_get_ptrs(local->ap, addr, 0, &crypt);
3250                 if (sta_ptr == NULL) {
3251                         if (local->iw_mode == IW_MODE_INFRA) {
3252                                 /*
3253                                  * TODO: add STA entry for the current AP so
3254                                  * that unicast key can be used. For now, this
3255                                  * is emulated by using default key idx 0.
3256                                  */
3257                                 i = 0;
3258                                 crypt = &local->crypt[i];
3259                         } else
3260                                 return -EINVAL;
3261                 }
3262         }
3263
3264         if ((erq->flags & IW_ENCODE_DISABLED) ||
3265             ext->alg == IW_ENCODE_ALG_NONE) {
3266                 if (*crypt)
3267                         prism2_crypt_delayed_deinit(local, crypt);
3268                 goto done;
3269         }
3270
3271         switch (ext->alg) {
3272         case IW_ENCODE_ALG_WEP:
3273                 alg = "WEP";
3274                 module = "ieee80211_crypt_wep";
3275                 break;
3276         case IW_ENCODE_ALG_TKIP:
3277                 alg = "TKIP";
3278                 module = "ieee80211_crypt_tkip";
3279                 break;
3280         case IW_ENCODE_ALG_CCMP:
3281                 alg = "CCMP";
3282                 module = "ieee80211_crypt_ccmp";
3283                 break;
3284         default:
3285                 printk(KERN_DEBUG "%s: unsupported algorithm %d\n",
3286                        local->dev->name, ext->alg);
3287                 ret = -EOPNOTSUPP;
3288                 goto done;
3289         }
3290
3291         ops = ieee80211_get_crypto_ops(alg);
3292         if (ops == NULL) {
3293                 request_module(module);
3294                 ops = ieee80211_get_crypto_ops(alg);
3295         }
3296         if (ops == NULL) {
3297                 printk(KERN_DEBUG "%s: unknown crypto alg '%s'\n",
3298                        local->dev->name, alg);
3299                 ret = -EOPNOTSUPP;
3300                 goto done;
3301         }
3302
3303         if (sta_ptr || ext->alg != IW_ENCODE_ALG_WEP) {
3304                 /*
3305                  * Per station encryption and other than WEP algorithms
3306                  * require host-based encryption, so force them on
3307                  * automatically.
3308                  */
3309                 local->host_decrypt = local->host_encrypt = 1;
3310         }
3311
3312         if (*crypt == NULL || (*crypt)->ops != ops) {
3313                 struct ieee80211_crypt_data *new_crypt;
3314
3315                 prism2_crypt_delayed_deinit(local, crypt);
3316
3317                 new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
3318                                 GFP_KERNEL);
3319                 if (new_crypt == NULL) {
3320                         ret = -ENOMEM;
3321                         goto done;
3322                 }
3323                 new_crypt->ops = ops;
3324                 new_crypt->priv = new_crypt->ops->init(i);
3325                 if (new_crypt->priv == NULL) {
3326                         kfree(new_crypt);
3327                         ret = -EINVAL;
3328                         goto done;
3329                 }
3330
3331                 *crypt = new_crypt;
3332         }
3333
3334         /*
3335          * TODO: if ext_flags does not have IW_ENCODE_EXT_RX_SEQ_VALID, the
3336          * existing seq# should not be changed.
3337          * TODO: if ext_flags has IW_ENCODE_EXT_TX_SEQ_VALID, next TX seq#
3338          * should be changed to something else than zero.
3339          */
3340         if ((!(ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) || ext->key_len > 0)
3341             && (*crypt)->ops->set_key &&
3342             (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
3343                                    (*crypt)->priv) < 0) {
3344                 printk(KERN_DEBUG "%s: key setting failed\n",
3345                        local->dev->name);
3346                 ret = -EINVAL;
3347                 goto done;
3348         }
3349
3350         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
3351                 if (!sta_ptr)
3352                         local->tx_keyidx = i;
3353         }
3354
3355
3356         if (sta_ptr == NULL && ext->key_len > 0) {
3357                 int first = 1, j;
3358                 for (j = 0; j < WEP_KEYS; j++) {
3359                         if (j != i && local->crypt[j]) {
3360                                 first = 0;
3361                                 break;
3362                         }
3363                 }
3364                 if (first)
3365                         local->tx_keyidx = i;
3366         }
3367
3368  done:
3369         if (sta_ptr)
3370                 hostap_handle_sta_release(sta_ptr);
3371
3372         local->open_wep = erq->flags & IW_ENCODE_OPEN;
3373
3374         /*
3375          * Do not reset port0 if card is in Managed mode since resetting will
3376          * generate new IEEE 802.11 authentication which may end up in looping
3377          * with IEEE 802.1X. Prism2 documentation seem to require port reset
3378          * after WEP configuration. However, keys are apparently changed at
3379          * least in Managed mode.
3380          */
3381         if (ret == 0 &&
3382             (hostap_set_encryption(local) ||
3383              (local->iw_mode != IW_MODE_INFRA &&
3384               local->func->reset_port(local->dev))))
3385                 ret = -EINVAL;
3386
3387         return ret;
3388 }
3389
3390
3391 static int prism2_ioctl_giwencodeext(struct net_device *dev,
3392                                      struct iw_request_info *info,
3393                                      struct iw_point *erq, char *extra)
3394 {
3395         struct hostap_interface *iface = netdev_priv(dev);
3396         local_info_t *local = iface->local;
3397         struct ieee80211_crypt_data **crypt;
3398         void *sta_ptr;
3399         int max_key_len, i;
3400         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
3401         u8 *addr;
3402
3403         max_key_len = erq->length - sizeof(*ext);
3404         if (max_key_len < 0)
3405                 return -EINVAL;
3406
3407         i = erq->flags & IW_ENCODE_INDEX;
3408         if (i < 1 || i > WEP_KEYS)
3409                 i = local->tx_keyidx;
3410         else
3411                 i--;
3412
3413         addr = ext->addr.sa_data;
3414         if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff &&
3415             addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) {
3416                 sta_ptr = NULL;
3417                 crypt = &local->crypt[i];
3418         } else {
3419                 i = 0;
3420                 sta_ptr = ap_crypt_get_ptrs(local->ap, addr, 0, &crypt);
3421                 if (sta_ptr == NULL)
3422                         return -EINVAL;
3423         }
3424         erq->flags = i + 1;
3425         memset(ext, 0, sizeof(*ext));
3426
3427         if (*crypt == NULL || (*crypt)->ops == NULL) {
3428                 ext->alg = IW_ENCODE_ALG_NONE;
3429                 ext->key_len = 0;
3430                 erq->flags |= IW_ENCODE_DISABLED;
3431         } else {
3432                 if (strcmp((*crypt)->ops->name, "WEP") == 0)
3433                         ext->alg = IW_ENCODE_ALG_WEP;
3434                 else if (strcmp((*crypt)->ops->name, "TKIP") == 0)
3435                         ext->alg = IW_ENCODE_ALG_TKIP;
3436                 else if (strcmp((*crypt)->ops->name, "CCMP") == 0)
3437                         ext->alg = IW_ENCODE_ALG_CCMP;
3438                 else
3439                         return -EINVAL;
3440
3441                 if ((*crypt)->ops->get_key) {
3442                         ext->key_len =
3443                                 (*crypt)->ops->get_key(ext->key,
3444                                                        max_key_len,
3445                                                        ext->tx_seq,
3446                                                        (*crypt)->priv);
3447                         if (ext->key_len &&
3448                             (ext->alg == IW_ENCODE_ALG_TKIP ||
3449                              ext->alg == IW_ENCODE_ALG_CCMP))
3450                                 ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
3451                 }
3452         }
3453
3454         if (sta_ptr)
3455                 hostap_handle_sta_release(sta_ptr);
3456
3457         return 0;
3458 }
3459
3460
3461 static int prism2_ioctl_set_encryption(local_info_t *local,
3462                                        struct prism2_hostapd_param *param,
3463                                        int param_len)
3464 {
3465         int ret = 0;
3466         struct ieee80211_crypto_ops *ops;
3467         struct ieee80211_crypt_data **crypt;
3468         void *sta_ptr;
3469
3470         param->u.crypt.err = 0;
3471         param->u.crypt.alg[HOSTAP_CRYPT_ALG_NAME_LEN - 1] = '\0';
3472
3473         if (param_len !=
3474             (int) ((char *) param->u.crypt.key - (char *) param) +
3475             param->u.crypt.key_len)
3476                 return -EINVAL;
3477
3478         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3479             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3480             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3481                 if (param->u.crypt.idx >= WEP_KEYS)
3482                         return -EINVAL;
3483                 sta_ptr = NULL;
3484                 crypt = &local->crypt[param->u.crypt.idx];
3485         } else {
3486                 if (param->u.crypt.idx)
3487                         return -EINVAL;
3488                 sta_ptr = ap_crypt_get_ptrs(
3489                         local->ap, param->sta_addr,
3490                         (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_PERMANENT),
3491                         &crypt);
3492
3493                 if (sta_ptr == NULL) {
3494                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
3495                         return -EINVAL;
3496                 }
3497         }
3498
3499         if (strcmp(param->u.crypt.alg, "none") == 0) {
3500                 if (crypt)
3501                         prism2_crypt_delayed_deinit(local, crypt);
3502                 goto done;
3503         }
3504
3505         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3506         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
3507                 request_module("ieee80211_crypt_wep");
3508                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3509         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
3510                 request_module("ieee80211_crypt_tkip");
3511                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3512         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
3513                 request_module("ieee80211_crypt_ccmp");
3514                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3515         }
3516         if (ops == NULL) {
3517                 printk(KERN_DEBUG "%s: unknown crypto alg '%s'\n",
3518                        local->dev->name, param->u.crypt.alg);
3519                 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ALG;
3520                 ret = -EINVAL;
3521                 goto done;
3522         }
3523
3524         /* station based encryption and other than WEP algorithms require
3525          * host-based encryption, so force them on automatically */
3526         local->host_decrypt = local->host_encrypt = 1;
3527
3528         if (*crypt == NULL || (*crypt)->ops != ops) {
3529                 struct ieee80211_crypt_data *new_crypt;
3530
3531                 prism2_crypt_delayed_deinit(local, crypt);
3532
3533                 new_crypt = kzalloc(sizeof(struct ieee80211_crypt_data),
3534                                 GFP_KERNEL);
3535                 if (new_crypt == NULL) {
3536                         ret = -ENOMEM;
3537                         goto done;
3538                 }
3539                 new_crypt->ops = ops;
3540                 new_crypt->priv = new_crypt->ops->init(param->u.crypt.idx);
3541                 if (new_crypt->priv == NULL) {
3542                         kfree(new_crypt);
3543                         param->u.crypt.err =
3544                                 HOSTAP_CRYPT_ERR_CRYPT_INIT_FAILED;
3545                         ret = -EINVAL;
3546                         goto done;
3547                 }
3548
3549                 *crypt = new_crypt;
3550         }
3551
3552         if ((!(param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) ||
3553              param->u.crypt.key_len > 0) && (*crypt)->ops->set_key &&
3554             (*crypt)->ops->set_key(param->u.crypt.key,
3555                                    param->u.crypt.key_len, param->u.crypt.seq,
3556                                    (*crypt)->priv) < 0) {
3557                 printk(KERN_DEBUG "%s: key setting failed\n",
3558                        local->dev->name);
3559                 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
3560                 ret = -EINVAL;
3561                 goto done;
3562         }
3563
3564         if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
3565                 if (!sta_ptr)
3566                         local->tx_keyidx = param->u.crypt.idx;
3567                 else if (param->u.crypt.idx) {
3568                         printk(KERN_DEBUG "%s: TX key idx setting failed\n",
3569                                local->dev->name);
3570                         param->u.crypt.err =
3571                                 HOSTAP_CRYPT_ERR_TX_KEY_SET_FAILED;
3572                         ret = -EINVAL;
3573                         goto done;
3574                 }
3575         }
3576
3577  done:
3578         if (sta_ptr)
3579                 hostap_handle_sta_release(sta_ptr);
3580
3581         /* Do not reset port0 if card is in Managed mode since resetting will
3582          * generate new IEEE 802.11 authentication which may end up in looping
3583          * with IEEE 802.1X. Prism2 documentation seem to require port reset
3584          * after WEP configuration. However, keys are apparently changed at
3585          * least in Managed mode. */
3586         if (ret == 0 &&
3587             (hostap_set_encryption(local) ||
3588              (local->iw_mode != IW_MODE_INFRA &&
3589               local->func->reset_port(local->dev)))) {
3590                 param->u.crypt.err = HOSTAP_CRYPT_ERR_CARD_CONF_FAILED;
3591                 return -EINVAL;
3592         }
3593
3594         return ret;
3595 }
3596
3597
3598 static int prism2_ioctl_get_encryption(local_info_t *local,
3599                                        struct prism2_hostapd_param *param,
3600                                        int param_len)
3601 {
3602         struct ieee80211_crypt_data **crypt;
3603         void *sta_ptr;
3604         int max_key_len;
3605
3606         param->u.crypt.err = 0;
3607
3608         max_key_len = param_len -
3609                 (int) ((char *) param->u.crypt.key - (char *) param);
3610         if (max_key_len < 0)
3611                 return -EINVAL;
3612
3613         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3614             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3615             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3616                 sta_ptr = NULL;
3617                 if (param->u.crypt.idx >= WEP_KEYS)
3618                         param->u.crypt.idx = local->tx_keyidx;
3619                 crypt = &local->crypt[param->u.crypt.idx];
3620         } else {
3621                 param->u.crypt.idx = 0;
3622                 sta_ptr = ap_crypt_get_ptrs(local->ap, param->sta_addr, 0,
3623                                             &crypt);
3624
3625                 if (sta_ptr == NULL) {
3626                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
3627                         return -EINVAL;
3628                 }
3629         }
3630
3631         if (*crypt == NULL || (*crypt)->ops == NULL) {
3632                 memcpy(param->u.crypt.alg, "none", 5);
3633                 param->u.crypt.key_len = 0;
3634                 param->u.crypt.idx = 0xff;
3635         } else {
3636                 strncpy(param->u.crypt.alg, (*crypt)->ops->name,
3637                         HOSTAP_CRYPT_ALG_NAME_LEN);
3638                 param->u.crypt.key_len = 0;
3639
3640                 memset(param->u.crypt.seq, 0, 8);
3641                 if ((*crypt)->ops->get_key) {
3642                         param->u.crypt.key_len =
3643                                 (*crypt)->ops->get_key(param->u.crypt.key,
3644                                                        max_key_len,
3645                                                        param->u.crypt.seq,
3646                                                        (*crypt)->priv);
3647                 }
3648         }
3649
3650         if (sta_ptr)
3651                 hostap_handle_sta_release(sta_ptr);
3652
3653         return 0;
3654 }
3655
3656
3657 static int prism2_ioctl_get_rid(local_info_t *local,
3658                                 struct prism2_hostapd_param *param,
3659                                 int param_len)
3660 {
3661         int max_len, res;
3662
3663         max_len = param_len - PRISM2_HOSTAPD_RID_HDR_LEN;
3664         if (max_len < 0)
3665                 return -EINVAL;
3666
3667         res = local->func->get_rid(local->dev, param->u.rid.rid,
3668                                    param->u.rid.data, param->u.rid.len, 0);
3669         if (res >= 0) {
3670                 param->u.rid.len = res;
3671                 return 0;
3672         }
3673
3674         return res;
3675 }
3676
3677
3678 static int prism2_ioctl_set_rid(local_info_t *local,
3679                                 struct prism2_hostapd_param *param,
3680                                 int param_len)
3681 {
3682         int max_len;
3683
3684         max_len = param_len - PRISM2_HOSTAPD_RID_HDR_LEN;
3685         if (max_len < 0 || max_len < param->u.rid.len)
3686                 return -EINVAL;
3687
3688         return local->func->set_rid(local->dev, param->u.rid.rid,
3689                                     param->u.rid.data, param->u.rid.len);
3690 }
3691
3692
3693 static int prism2_ioctl_set_assoc_ap_addr(local_info_t *local,
3694                                           struct prism2_hostapd_param *param,
3695                                           int param_len)
3696 {
3697         DECLARE_MAC_BUF(mac);
3698         printk(KERN_DEBUG "%ssta: associated as client with AP "
3699                "%s\n",
3700                local->dev->name, print_mac(mac, param->sta_addr));
3701         memcpy(local->assoc_ap_addr, param->sta_addr, ETH_ALEN);
3702         return 0;
3703 }
3704
3705
3706 static int prism2_ioctl_siwgenie(struct net_device *dev,
3707                                  struct iw_request_info *info,
3708                                  struct iw_point *data, char *extra)
3709 {
3710         return prism2_set_genericelement(dev, extra, data->length);
3711 }
3712
3713
3714 static int prism2_ioctl_giwgenie(struct net_device *dev,
3715                                  struct iw_request_info *info,
3716                                  struct iw_point *data, char *extra)
3717 {
3718         struct hostap_interface *iface = netdev_priv(dev);
3719         local_info_t *local = iface->local;
3720         int len = local->generic_elem_len - 2;
3721
3722         if (len <= 0 || local->generic_elem == NULL) {
3723                 data->length = 0;
3724                 return 0;
3725         }
3726
3727         if (data->length < len)
3728                 return -E2BIG;
3729
3730         data->length = len;
3731         memcpy(extra, local->generic_elem + 2, len);
3732
3733         return 0;
3734 }
3735
3736
3737 static int prism2_ioctl_set_generic_element(local_info_t *local,
3738                                             struct prism2_hostapd_param *param,
3739                                             int param_len)
3740 {
3741         int max_len, len;
3742
3743         len = param->u.generic_elem.len;
3744         max_len = param_len - PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN;
3745         if (max_len < 0 || max_len < len)
3746                 return -EINVAL;
3747
3748         return prism2_set_genericelement(local->dev,
3749                                          param->u.generic_elem.data, len);
3750 }
3751
3752
3753 static int prism2_ioctl_siwmlme(struct net_device *dev,
3754                                 struct iw_request_info *info,
3755                                 struct iw_point *data, char *extra)
3756 {
3757         struct hostap_interface *iface = netdev_priv(dev);
3758         local_info_t *local = iface->local;
3759         struct iw_mlme *mlme = (struct iw_mlme *) extra;
3760         __le16 reason;
3761
3762         reason = cpu_to_le16(mlme->reason_code);
3763
3764         switch (mlme->cmd) {
3765         case IW_MLME_DEAUTH:
3766                 return prism2_sta_send_mgmt(local, mlme->addr.sa_data,
3767                                             IEEE80211_STYPE_DEAUTH,
3768                                             (u8 *) &reason, 2);
3769         case IW_MLME_DISASSOC:
3770                 return prism2_sta_send_mgmt(local, mlme->addr.sa_data,
3771                                             IEEE80211_STYPE_DISASSOC,
3772                                             (u8 *) &reason, 2);
3773         default:
3774                 return -EOPNOTSUPP;
3775         }
3776 }
3777
3778
3779 static int prism2_ioctl_mlme(local_info_t *local,
3780                              struct prism2_hostapd_param *param)
3781 {
3782         __le16 reason;
3783
3784         reason = cpu_to_le16(param->u.mlme.reason_code);
3785         switch (param->u.mlme.cmd) {
3786         case MLME_STA_DEAUTH:
3787                 return prism2_sta_send_mgmt(local, param->sta_addr,
3788                                             IEEE80211_STYPE_DEAUTH,
3789                                             (u8 *) &reason, 2);
3790         case MLME_STA_DISASSOC:
3791                 return prism2_sta_send_mgmt(local, param->sta_addr,
3792                                             IEEE80211_STYPE_DISASSOC,
3793                                             (u8 *) &reason, 2);
3794         default:
3795                 return -EOPNOTSUPP;
3796         }
3797 }
3798
3799
3800 static int prism2_ioctl_scan_req(local_info_t *local,
3801                                  struct prism2_hostapd_param *param)
3802 {
3803 #ifndef PRISM2_NO_STATION_MODES
3804         if ((local->iw_mode != IW_MODE_INFRA &&
3805              local->iw_mode != IW_MODE_ADHOC) ||
3806             (local->sta_fw_ver < PRISM2_FW_VER(1,3,1)))
3807                 return -EOPNOTSUPP;
3808
3809         if (!local->dev_enabled)
3810                 return -ENETDOWN;
3811
3812         return prism2_request_hostscan(local->dev, param->u.scan_req.ssid,
3813                                        param->u.scan_req.ssid_len);
3814 #else /* PRISM2_NO_STATION_MODES */
3815         return -EOPNOTSUPP;
3816 #endif /* PRISM2_NO_STATION_MODES */
3817 }
3818
3819
3820 static int prism2_ioctl_priv_hostapd(local_info_t *local, struct iw_point *p)
3821 {
3822         struct prism2_hostapd_param *param;
3823         int ret = 0;
3824         int ap_ioctl = 0;
3825
3826         if (p->length < sizeof(struct prism2_hostapd_param) ||
3827             p->length > PRISM2_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
3828                 return -EINVAL;
3829
3830         param = kmalloc(p->length, GFP_KERNEL);
3831         if (param == NULL)
3832                 return -ENOMEM;
3833
3834         if (copy_from_user(param, p->pointer, p->length)) {
3835                 ret = -EFAULT;
3836                 goto out;
3837         }
3838
3839         switch (param->cmd) {
3840         case PRISM2_SET_ENCRYPTION:
3841                 ret = prism2_ioctl_set_encryption(local, param, p->length);
3842                 break;
3843         case PRISM2_GET_ENCRYPTION:
3844                 ret = prism2_ioctl_get_encryption(local, param, p->length);
3845                 break;
3846         case PRISM2_HOSTAPD_GET_RID:
3847                 ret = prism2_ioctl_get_rid(local, param, p->length);
3848                 break;
3849         case PRISM2_HOSTAPD_SET_RID:
3850                 ret = prism2_ioctl_set_rid(local, param, p->length);
3851                 break;
3852         case PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR:
3853                 ret = prism2_ioctl_set_assoc_ap_addr(local, param, p->length);
3854                 break;
3855         case PRISM2_HOSTAPD_SET_GENERIC_ELEMENT:
3856                 ret = prism2_ioctl_set_generic_element(local, param,
3857                                                        p->length);
3858                 break;
3859         case PRISM2_HOSTAPD_MLME:
3860                 ret = prism2_ioctl_mlme(local, param);
3861                 break;
3862         case PRISM2_HOSTAPD_SCAN_REQ:
3863                 ret = prism2_ioctl_scan_req(local, param);
3864                 break;
3865         default:
3866                 ret = prism2_hostapd(local->ap, param);
3867                 ap_ioctl = 1;
3868                 break;
3869         }
3870
3871         if (ret == 1 || !ap_ioctl) {
3872                 if (copy_to_user(p->pointer, param, p->length)) {
3873                         ret = -EFAULT;
3874                         goto out;
3875                 } else if (ap_ioctl)
3876                         ret = 0;
3877         }
3878
3879  out:
3880         kfree(param);
3881         return ret;
3882 }
3883
3884
3885 static void prism2_get_drvinfo(struct net_device *dev,
3886                                struct ethtool_drvinfo *info)
3887 {
3888         struct hostap_interface *iface;
3889         local_info_t *local;
3890
3891         iface = netdev_priv(dev);
3892         local = iface->local;
3893
3894         strncpy(info->driver, "hostap", sizeof(info->driver) - 1);
3895         snprintf(info->fw_version, sizeof(info->fw_version) - 1,
3896                  "%d.%d.%d", (local->sta_fw_ver >> 16) & 0xff,
3897                  (local->sta_fw_ver >> 8) & 0xff,
3898                  local->sta_fw_ver & 0xff);
3899 }
3900
3901 const struct ethtool_ops prism2_ethtool_ops = {
3902         .get_drvinfo = prism2_get_drvinfo
3903 };
3904
3905
3906 /* Structures to export the Wireless Handlers */
3907
3908 static const iw_handler prism2_handler[] =
3909 {
3910         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
3911         (iw_handler) prism2_get_name,                   /* SIOCGIWNAME */
3912         (iw_handler) NULL,                              /* SIOCSIWNWID */
3913         (iw_handler) NULL,                              /* SIOCGIWNWID */
3914         (iw_handler) prism2_ioctl_siwfreq,              /* SIOCSIWFREQ */
3915         (iw_handler) prism2_ioctl_giwfreq,              /* SIOCGIWFREQ */
3916         (iw_handler) prism2_ioctl_siwmode,              /* SIOCSIWMODE */
3917         (iw_handler) prism2_ioctl_giwmode,              /* SIOCGIWMODE */
3918         (iw_handler) prism2_ioctl_siwsens,              /* SIOCSIWSENS */
3919         (iw_handler) prism2_ioctl_giwsens,              /* SIOCGIWSENS */
3920         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
3921         (iw_handler) prism2_ioctl_giwrange,             /* SIOCGIWRANGE */
3922         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
3923         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
3924         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
3925         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
3926         iw_handler_set_spy,                             /* SIOCSIWSPY */
3927         iw_handler_get_spy,                             /* SIOCGIWSPY */
3928         iw_handler_set_thrspy,                          /* SIOCSIWTHRSPY */
3929         iw_handler_get_thrspy,                          /* SIOCGIWTHRSPY */
3930         (iw_handler) prism2_ioctl_siwap,                /* SIOCSIWAP */
3931         (iw_handler) prism2_ioctl_giwap,                /* SIOCGIWAP */
3932         (iw_handler) prism2_ioctl_siwmlme,              /* SIOCSIWMLME */
3933         (iw_handler) prism2_ioctl_giwaplist,            /* SIOCGIWAPLIST */
3934         (iw_handler) prism2_ioctl_siwscan,              /* SIOCSIWSCAN */
3935         (iw_handler) prism2_ioctl_giwscan,              /* SIOCGIWSCAN */
3936         (iw_handler) prism2_ioctl_siwessid,             /* SIOCSIWESSID */
3937         (iw_handler) prism2_ioctl_giwessid,             /* SIOCGIWESSID */
3938         (iw_handler) prism2_ioctl_siwnickn,             /* SIOCSIWNICKN */
3939         (iw_handler) prism2_ioctl_giwnickn,             /* SIOCGIWNICKN */
3940         (iw_handler) NULL,                              /* -- hole -- */
3941         (iw_handler) NULL,                              /* -- hole -- */
3942         (iw_handler) prism2_ioctl_siwrate,              /* SIOCSIWRATE */
3943         (iw_handler) prism2_ioctl_giwrate,              /* SIOCGIWRATE */
3944         (iw_handler) prism2_ioctl_siwrts,               /* SIOCSIWRTS */
3945         (iw_handler) prism2_ioctl_giwrts,               /* SIOCGIWRTS */
3946         (iw_handler) prism2_ioctl_siwfrag,              /* SIOCSIWFRAG */
3947         (iw_handler) prism2_ioctl_giwfrag,              /* SIOCGIWFRAG */
3948         (iw_handler) prism2_ioctl_siwtxpow,             /* SIOCSIWTXPOW */
3949         (iw_handler) prism2_ioctl_giwtxpow,             /* SIOCGIWTXPOW */
3950         (iw_handler) prism2_ioctl_siwretry,             /* SIOCSIWRETRY */
3951         (iw_handler) prism2_ioctl_giwretry,             /* SIOCGIWRETRY */
3952         (iw_handler) prism2_ioctl_siwencode,            /* SIOCSIWENCODE */
3953         (iw_handler) prism2_ioctl_giwencode,            /* SIOCGIWENCODE */
3954         (iw_handler) prism2_ioctl_siwpower,             /* SIOCSIWPOWER */
3955         (iw_handler) prism2_ioctl_giwpower,             /* SIOCGIWPOWER */
3956         (iw_handler) NULL,                              /* -- hole -- */
3957         (iw_handler) NULL,                              /* -- hole -- */
3958         (iw_handler) prism2_ioctl_siwgenie,             /* SIOCSIWGENIE */
3959         (iw_handler) prism2_ioctl_giwgenie,             /* SIOCGIWGENIE */
3960         (iw_handler) prism2_ioctl_siwauth,              /* SIOCSIWAUTH */
3961         (iw_handler) prism2_ioctl_giwauth,              /* SIOCGIWAUTH */
3962         (iw_handler) prism2_ioctl_siwencodeext,         /* SIOCSIWENCODEEXT */
3963         (iw_handler) prism2_ioctl_giwencodeext,         /* SIOCGIWENCODEEXT */
3964         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
3965         (iw_handler) NULL,                              /* -- hole -- */
3966 };
3967
3968 static const iw_handler prism2_private_handler[] =
3969 {                                                       /* SIOCIWFIRSTPRIV + */
3970         (iw_handler) prism2_ioctl_priv_prism2_param,    /* 0 */
3971         (iw_handler) prism2_ioctl_priv_get_prism2_param, /* 1 */
3972         (iw_handler) prism2_ioctl_priv_writemif,        /* 2 */
3973         (iw_handler) prism2_ioctl_priv_readmif,         /* 3 */
3974 };
3975
3976 const struct iw_handler_def hostap_iw_handler_def =
3977 {
3978         .num_standard   = ARRAY_SIZE(prism2_handler),
3979         .num_private    = ARRAY_SIZE(prism2_private_handler),
3980         .num_private_args = ARRAY_SIZE(prism2_priv),
3981         .standard       = (iw_handler *) prism2_handler,
3982         .private        = (iw_handler *) prism2_private_handler,
3983         .private_args   = (struct iw_priv_args *) prism2_priv,
3984         .get_wireless_stats = hostap_get_wireless_stats,
3985 };
3986
3987
3988 int hostap_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3989 {
3990         struct iwreq *wrq = (struct iwreq *) ifr;
3991         struct hostap_interface *iface;
3992         local_info_t *local;
3993         int ret = 0;
3994
3995         iface = netdev_priv(dev);
3996         local = iface->local;
3997
3998         switch (cmd) {
3999                 /* Private ioctls (iwpriv) that have not yet been converted
4000                  * into new wireless extensions API */
4001
4002         case PRISM2_IOCTL_INQUIRE:
4003                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4004                 else ret = prism2_ioctl_priv_inquire(dev, (int *) wrq->u.name);
4005                 break;
4006
4007         case PRISM2_IOCTL_MONITOR:
4008                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4009                 else ret = prism2_ioctl_priv_monitor(dev, (int *) wrq->u.name);
4010                 break;
4011
4012         case PRISM2_IOCTL_RESET:
4013                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4014                 else ret = prism2_ioctl_priv_reset(dev, (int *) wrq->u.name);
4015                 break;
4016
4017         case PRISM2_IOCTL_WDS_ADD:
4018                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4019                 else ret = prism2_wds_add(local, wrq->u.ap_addr.sa_data, 1);
4020                 break;
4021
4022         case PRISM2_IOCTL_WDS_DEL:
4023                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4024                 else ret = prism2_wds_del(local, wrq->u.ap_addr.sa_data, 1, 0);
4025                 break;
4026
4027         case PRISM2_IOCTL_SET_RID_WORD:
4028                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4029                 else ret = prism2_ioctl_priv_set_rid_word(dev,
4030                                                           (int *) wrq->u.name);
4031                 break;
4032
4033 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
4034         case PRISM2_IOCTL_MACCMD:
4035                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4036                 else ret = ap_mac_cmd_ioctl(local, (int *) wrq->u.name);
4037                 break;
4038
4039         case PRISM2_IOCTL_ADDMAC:
4040                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4041                 else ret = ap_control_add_mac(&local->ap->mac_restrictions,
4042                                               wrq->u.ap_addr.sa_data);
4043                 break;
4044         case PRISM2_IOCTL_DELMAC:
4045                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4046                 else ret = ap_control_del_mac(&local->ap->mac_restrictions,
4047                                               wrq->u.ap_addr.sa_data);
4048                 break;
4049         case PRISM2_IOCTL_KICKMAC:
4050                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4051                 else ret = ap_control_kick_mac(local->ap, local->dev,
4052                                                wrq->u.ap_addr.sa_data);
4053                 break;
4054 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
4055
4056
4057                 /* Private ioctls that are not used with iwpriv;
4058                  * in SIOCDEVPRIVATE range */
4059
4060 #ifdef PRISM2_DOWNLOAD_SUPPORT
4061         case PRISM2_IOCTL_DOWNLOAD:
4062                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4063                 else ret = prism2_ioctl_priv_download(local, &wrq->u.data);
4064                 break;
4065 #endif /* PRISM2_DOWNLOAD_SUPPORT */
4066
4067         case PRISM2_IOCTL_HOSTAPD:
4068                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4069                 else ret = prism2_ioctl_priv_hostapd(local, &wrq->u.data);
4070                 break;
4071
4072         default:
4073                 ret = -EOPNOTSUPP;
4074                 break;
4075         }
4076
4077         return ret;
4078 }