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