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