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