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