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