Add HostAP wireless driver.
[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 = __constant_cpu_to_le16(local->channel_mask);
1668         scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
1669         if (ssid) {
1670                 if (ssid_len > 32)
1671                         return -EINVAL;
1672                 scan_req.target_ssid_len = cpu_to_le16(ssid_len);
1673                 memcpy(scan_req.target_ssid, ssid, ssid_len);
1674         }
1675
1676         if (local->func->set_rid(dev, HFA384X_RID_HOSTSCAN, &scan_req,
1677                                  sizeof(scan_req))) {
1678                 printk(KERN_DEBUG "%s: HOSTSCAN failed\n", dev->name);
1679                 return -EINVAL;
1680         }
1681         return 0;
1682 }
1683
1684
1685 static int prism2_request_scan(struct net_device *dev)
1686 {
1687         struct hostap_interface *iface;
1688         local_info_t *local;
1689         struct hfa384x_scan_request scan_req;
1690         int ret = 0;
1691
1692         iface = netdev_priv(dev);
1693         local = iface->local;
1694
1695         memset(&scan_req, 0, sizeof(scan_req));
1696         scan_req.channel_list = __constant_cpu_to_le16(local->channel_mask);
1697         scan_req.txrate = __constant_cpu_to_le16(HFA384X_RATES_1MBPS);
1698
1699         /* FIX:
1700          * It seems to be enough to set roaming mode for a short moment to
1701          * host-based and then setup scanrequest data and return the mode to
1702          * firmware-based.
1703          *
1704          * Master mode would need to drop to Managed mode for a short while
1705          * to make scanning work.. Or sweep through the different channels and
1706          * use passive scan based on beacons. */
1707
1708         if (!local->host_roaming)
1709                 hostap_set_word(dev, HFA384X_RID_CNFROAMINGMODE,
1710                                 HFA384X_ROAMING_HOST);
1711
1712         if (local->func->set_rid(dev, HFA384X_RID_SCANREQUEST, &scan_req,
1713                                  sizeof(scan_req))) {
1714                 printk(KERN_DEBUG "SCANREQUEST failed\n");
1715                 ret = -EINVAL;
1716         }
1717
1718         if (!local->host_roaming)
1719                 hostap_set_word(dev, HFA384X_RID_CNFROAMINGMODE,
1720                                 HFA384X_ROAMING_FIRMWARE);
1721
1722         return 0;
1723 }
1724
1725 #else /* !PRISM2_NO_STATION_MODES */
1726
1727 static inline int prism2_request_hostscan(struct net_device *dev,
1728                                           u8 *ssid, u8 ssid_len)
1729 {
1730         return -EOPNOTSUPP;
1731 }
1732
1733
1734 static inline int prism2_request_scan(struct net_device *dev)
1735 {
1736         return -EOPNOTSUPP;
1737 }
1738
1739 #endif /* !PRISM2_NO_STATION_MODES */
1740
1741
1742 static int prism2_ioctl_siwscan(struct net_device *dev,
1743                                 struct iw_request_info *info,
1744                                 struct iw_point *data, char *extra)
1745 {
1746         struct hostap_interface *iface;
1747         local_info_t *local;
1748         int ret;
1749         u8 *ssid = NULL, ssid_len = 0;
1750         struct iw_scan_req *req = (struct iw_scan_req *) extra;
1751
1752         iface = netdev_priv(dev);
1753         local = iface->local;
1754
1755         if (data->length < sizeof(struct iw_scan_req))
1756                 req = NULL;
1757
1758         if (local->iw_mode == IW_MODE_MASTER) {
1759                 /* In master mode, we just return the results of our local
1760                  * tables, so we don't need to start anything...
1761                  * Jean II */
1762                 data->length = 0;
1763                 return 0;
1764         }
1765
1766         if (!local->dev_enabled)
1767                 return -ENETDOWN;
1768
1769         if (req && data->flags & IW_SCAN_THIS_ESSID) {
1770                 ssid = req->essid;
1771                 ssid_len = req->essid_len;
1772
1773                 if (ssid_len &&
1774                     ((local->iw_mode != IW_MODE_INFRA &&
1775                       local->iw_mode != IW_MODE_ADHOC) ||
1776                      (local->sta_fw_ver < PRISM2_FW_VER(1,3,1))))
1777                         return -EOPNOTSUPP;
1778         }
1779
1780         if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1))
1781                 ret = prism2_request_hostscan(dev, ssid, ssid_len);
1782         else
1783                 ret = prism2_request_scan(dev);
1784
1785         if (ret == 0)
1786                 local->scan_timestamp = jiffies;
1787
1788         /* Could inquire F101, F103 or wait for SIOCGIWSCAN and read RID */
1789
1790         return ret;
1791 }
1792
1793
1794 #ifndef PRISM2_NO_STATION_MODES
1795 static char * __prism2_translate_scan(local_info_t *local,
1796                                       struct hfa384x_scan_result *scan,
1797                                       struct hfa384x_hostscan_result *hscan,
1798                                       int hostscan,
1799                                       struct hostap_bss_info *bss, u8 *bssid,
1800                                       char *current_ev, char *end_buf)
1801 {
1802         int i, chan;
1803         struct iw_event iwe;
1804         char *current_val;
1805         u16 capabilities;
1806         u8 *pos;
1807         u8 *ssid;
1808         size_t ssid_len;
1809         char *buf;
1810
1811         if (bss) {
1812                 ssid = bss->ssid;
1813                 ssid_len = bss->ssid_len;
1814         } else {
1815                 ssid = hostscan ? hscan->ssid : scan->ssid;
1816                 ssid_len = le16_to_cpu(hostscan ? hscan->ssid_len :
1817                                        scan->ssid_len);
1818         }
1819         if (ssid_len > 32)
1820                 ssid_len = 32;
1821
1822         /* First entry *MUST* be the AP MAC address */
1823         memset(&iwe, 0, sizeof(iwe));
1824         iwe.cmd = SIOCGIWAP;
1825         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1826         memcpy(iwe.u.ap_addr.sa_data, bssid, ETH_ALEN);
1827         /* FIX:
1828          * I do not know how this is possible, but iwe_stream_add_event
1829          * seems to re-order memcpy execution so that len is set only
1830          * after copying.. Pre-setting len here "fixes" this, but real
1831          * problems should be solved (after which these iwe.len
1832          * settings could be removed from this function). */
1833         iwe.len = IW_EV_ADDR_LEN;
1834         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1835                                           IW_EV_ADDR_LEN);
1836
1837         /* Other entries will be displayed in the order we give them */
1838
1839         memset(&iwe, 0, sizeof(iwe));
1840         iwe.cmd = SIOCGIWESSID;
1841         iwe.u.data.length = ssid_len;
1842         iwe.u.data.flags = 1;
1843         iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1844         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, ssid);
1845
1846         memset(&iwe, 0, sizeof(iwe));
1847         iwe.cmd = SIOCGIWMODE;
1848         if (bss) {
1849                 capabilities = bss->capab_info;
1850         } else {
1851                 capabilities = le16_to_cpu(hostscan ? hscan->capability :
1852                                            scan->capability);
1853         }
1854         if (capabilities & (WLAN_CAPABILITY_ESS |
1855                             WLAN_CAPABILITY_IBSS)) {
1856                 if (capabilities & WLAN_CAPABILITY_ESS)
1857                         iwe.u.mode = IW_MODE_MASTER;
1858                 else
1859                         iwe.u.mode = IW_MODE_ADHOC;
1860                 iwe.len = IW_EV_UINT_LEN;
1861                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1862                                                   IW_EV_UINT_LEN);
1863         }
1864
1865         memset(&iwe, 0, sizeof(iwe));
1866         iwe.cmd = SIOCGIWFREQ;
1867         if (hscan || scan) {
1868                 chan = hostscan ? hscan->chid : scan->chid;
1869         } else if (bss) {
1870                 chan = bss->chan;
1871         } else {
1872                 chan = 0;
1873         }
1874
1875         if (chan > 0) {
1876                 iwe.u.freq.m = freq_list[le16_to_cpu(chan - 1)] * 100000;
1877                 iwe.u.freq.e = 1;
1878                 iwe.len = IW_EV_FREQ_LEN;
1879                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1880                                                   IW_EV_FREQ_LEN);
1881         }
1882
1883         if (scan || hscan) {
1884                 memset(&iwe, 0, sizeof(iwe));
1885                 iwe.cmd = IWEVQUAL;
1886                 if (hostscan) {
1887                         iwe.u.qual.level = le16_to_cpu(hscan->sl);
1888                         iwe.u.qual.noise = le16_to_cpu(hscan->anl);
1889                 } else {
1890                         iwe.u.qual.level =
1891                                 HFA384X_LEVEL_TO_dBm(le16_to_cpu(scan->sl));
1892                         iwe.u.qual.noise =
1893                                 HFA384X_LEVEL_TO_dBm(le16_to_cpu(scan->anl));
1894                 }
1895                 iwe.len = IW_EV_QUAL_LEN;
1896                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
1897                                                   IW_EV_QUAL_LEN);
1898         }
1899
1900         memset(&iwe, 0, sizeof(iwe));
1901         iwe.cmd = SIOCGIWENCODE;
1902         if (capabilities & WLAN_CAPABILITY_PRIVACY)
1903                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1904         else
1905                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1906         iwe.u.data.length = 0;
1907         iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
1908         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
1909
1910         /* TODO: add SuppRates into BSS table */
1911         if (scan || hscan) {
1912                 memset(&iwe, 0, sizeof(iwe));
1913                 iwe.cmd = SIOCGIWRATE;
1914                 current_val = current_ev + IW_EV_LCP_LEN;
1915                 pos = hostscan ? hscan->sup_rates : scan->sup_rates;
1916                 for (i = 0; i < sizeof(scan->sup_rates); i++) {
1917                         if (pos[i] == 0)
1918                                 break;
1919                         /* Bit rate given in 500 kb/s units (+ 0x80) */
1920                         iwe.u.bitrate.value = ((pos[i] & 0x7f) * 500000);
1921                         current_val = iwe_stream_add_value(
1922                                 current_ev, current_val, end_buf, &iwe,
1923                                 IW_EV_PARAM_LEN);
1924                 }
1925                 /* Check if we added any event */
1926                 if ((current_val - current_ev) > IW_EV_LCP_LEN)
1927                         current_ev = current_val;
1928         }
1929
1930         /* TODO: add BeaconInt,resp_rate,atim into BSS table */
1931         buf = kmalloc(MAX_WPA_IE_LEN * 2 + 30, GFP_KERNEL);
1932         if (buf && (scan || hscan)) {
1933                 memset(&iwe, 0, sizeof(iwe));
1934                 iwe.cmd = IWEVCUSTOM;
1935                 sprintf(buf, "bcn_int=%d",
1936                         le16_to_cpu(hostscan ? hscan->beacon_interval :
1937                                     scan->beacon_interval));
1938                 iwe.u.data.length = strlen(buf);
1939                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1940                                                   buf);
1941
1942                 memset(&iwe, 0, sizeof(iwe));
1943                 iwe.cmd = IWEVCUSTOM;
1944                 sprintf(buf, "resp_rate=%d", le16_to_cpu(hostscan ?
1945                                                          hscan->rate :
1946                                                          scan->rate));
1947                 iwe.u.data.length = strlen(buf);
1948                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
1949                                                   buf);
1950
1951                 if (hostscan && (capabilities & WLAN_CAPABILITY_IBSS)) {
1952                         memset(&iwe, 0, sizeof(iwe));
1953                         iwe.cmd = IWEVCUSTOM;
1954                         sprintf(buf, "atim=%d", le16_to_cpu(hscan->atim));
1955                         iwe.u.data.length = strlen(buf);
1956                         current_ev = iwe_stream_add_point(current_ev, end_buf,
1957                                                           &iwe, buf);
1958                 }
1959         }
1960         kfree(buf);
1961
1962         if (bss && bss->wpa_ie_len > 0 && bss->wpa_ie_len <= MAX_WPA_IE_LEN) {
1963                 memset(&iwe, 0, sizeof(iwe));
1964                 iwe.cmd = IWEVGENIE;
1965                 iwe.u.data.length = bss->wpa_ie_len;
1966                 current_ev = iwe_stream_add_point(
1967                         current_ev, end_buf, &iwe, bss->wpa_ie);
1968         }
1969
1970         if (bss && bss->rsn_ie_len > 0 && bss->rsn_ie_len <= MAX_WPA_IE_LEN) {
1971                 memset(&iwe, 0, sizeof(iwe));
1972                 iwe.cmd = IWEVGENIE;
1973                 iwe.u.data.length = bss->rsn_ie_len;
1974                 current_ev = iwe_stream_add_point(
1975                         current_ev, end_buf, &iwe, bss->rsn_ie);
1976         }
1977
1978         return current_ev;
1979 }
1980
1981
1982 /* Translate scan data returned from the card to a card independant
1983  * format that the Wireless Tools will understand - Jean II */
1984 static inline int prism2_translate_scan(local_info_t *local,
1985                                         char *buffer, int buflen)
1986 {
1987         struct hfa384x_scan_result *scan;
1988         struct hfa384x_hostscan_result *hscan;
1989         int entries, entry, hostscan;
1990         char *current_ev = buffer;
1991         char *end_buf = buffer + buflen;
1992         u8 *bssid;
1993         struct list_head *ptr;
1994
1995         spin_lock_bh(&local->lock);
1996
1997         list_for_each(ptr, &local->bss_list) {
1998                 struct hostap_bss_info *bss;
1999                 bss = list_entry(ptr, struct hostap_bss_info, list);
2000                 bss->included = 0;
2001         }
2002
2003         hostscan = local->last_scan_type == PRISM2_HOSTSCAN;
2004         entries = hostscan ? local->last_hostscan_results_count :
2005                 local->last_scan_results_count;
2006         for (entry = 0; entry < entries; entry++) {
2007                 int found = 0;
2008                 scan = &local->last_scan_results[entry];
2009                 hscan = &local->last_hostscan_results[entry];
2010
2011                 bssid = hostscan ? hscan->bssid : scan->bssid;
2012
2013                 /* Report every SSID if the AP is using multiple SSIDs. If no
2014                  * BSS record is found (e.g., when WPA mode is disabled),
2015                  * report the AP once. */
2016                 list_for_each(ptr, &local->bss_list) {
2017                         struct hostap_bss_info *bss;
2018                         bss = list_entry(ptr, struct hostap_bss_info, list);
2019                         if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
2020                                 bss->included = 1;
2021                                 current_ev = __prism2_translate_scan(
2022                                         local, scan, hscan, hostscan, bss,
2023                                         bssid, current_ev, end_buf);
2024                                 found++;
2025                         }
2026                 }
2027                 if (!found) {
2028                         current_ev = __prism2_translate_scan(
2029                                 local, scan, hscan, hostscan, NULL, bssid,
2030                                 current_ev, end_buf);
2031                 }
2032                 /* Check if there is space for one more entry */
2033                 if ((end_buf - current_ev) <= IW_EV_ADDR_LEN) {
2034                         /* Ask user space to try again with a bigger buffer */
2035                         spin_unlock_bh(&local->lock);
2036                         return -E2BIG;
2037                 }
2038         }
2039
2040         /* Prism2 firmware has limits (32 at least in some versions) for number
2041          * of BSSes in scan results. Extend this limit by using local BSS list.
2042          */
2043         list_for_each(ptr, &local->bss_list) {
2044                 struct hostap_bss_info *bss;
2045                 bss = list_entry(ptr, struct hostap_bss_info, list);
2046                 if (bss->included)
2047                         continue;
2048                 current_ev = __prism2_translate_scan(local, NULL, NULL, 0, bss,
2049                                                      bss->bssid, current_ev,
2050                                                      end_buf);
2051                 /* Check if there is space for one more entry */
2052                 if ((end_buf - current_ev) <= IW_EV_ADDR_LEN) {
2053                         /* Ask user space to try again with a bigger buffer */
2054                         spin_unlock_bh(&local->lock);
2055                         return -E2BIG;
2056                 }
2057         }
2058
2059         spin_unlock_bh(&local->lock);
2060
2061         return current_ev - buffer;
2062 }
2063 #endif /* PRISM2_NO_STATION_MODES */
2064
2065
2066 static inline int prism2_ioctl_giwscan_sta(struct net_device *dev,
2067                                            struct iw_request_info *info,
2068                                            struct iw_point *data, char *extra)
2069 {
2070 #ifdef PRISM2_NO_STATION_MODES
2071         return -EOPNOTSUPP;
2072 #else /* PRISM2_NO_STATION_MODES */
2073         struct hostap_interface *iface;
2074         local_info_t *local;
2075         int res;
2076
2077         iface = netdev_priv(dev);
2078         local = iface->local;
2079
2080         /* Wait until the scan is finished. We can probably do better
2081          * than that - Jean II */
2082         if (local->scan_timestamp &&
2083             time_before(jiffies, local->scan_timestamp + 3 * HZ)) {
2084                 /* Important note : we don't want to block the caller
2085                  * until results are ready for various reasons.
2086                  * First, managing wait queues is complex and racy
2087                  * (there may be multiple simultaneous callers).
2088                  * Second, we grab some rtnetlink lock before comming
2089                  * here (in dev_ioctl()).
2090                  * Third, the caller can wait on the Wireless Event
2091                  * - Jean II */
2092                 return -EAGAIN;
2093         }
2094         local->scan_timestamp = 0;
2095
2096         res = prism2_translate_scan(local, extra, data->length);
2097
2098         if (res >= 0) {
2099                 data->length = res;
2100                 return 0;
2101         } else {
2102                 data->length = 0;
2103                 return res;
2104         }
2105 #endif /* PRISM2_NO_STATION_MODES */
2106 }
2107
2108
2109 static int prism2_ioctl_giwscan(struct net_device *dev,
2110                                 struct iw_request_info *info,
2111                                 struct iw_point *data, char *extra)
2112 {
2113         struct hostap_interface *iface;
2114         local_info_t *local;
2115         int res;
2116
2117         iface = netdev_priv(dev);
2118         local = iface->local;
2119
2120         if (local->iw_mode == IW_MODE_MASTER) {
2121                 /* In MASTER mode, it doesn't make sense to go around
2122                  * scanning the frequencies and make the stations we serve
2123                  * wait when what the user is really interested about is the
2124                  * list of stations and access points we are talking to.
2125                  * So, just extract results from our cache...
2126                  * Jean II */
2127
2128                 /* Translate to WE format */
2129                 res = prism2_ap_translate_scan(dev, extra);
2130                 if (res >= 0) {
2131                         printk(KERN_DEBUG "Scan result translation succeeded "
2132                                "(length=%d)\n", res);
2133                         data->length = res;
2134                         return 0;
2135                 } else {
2136                         printk(KERN_DEBUG
2137                                "Scan result translation failed (res=%d)\n",
2138                                res);
2139                         data->length = 0;
2140                         return res;
2141                 }
2142         } else {
2143                 /* Station mode */
2144                 return prism2_ioctl_giwscan_sta(dev, info, data, extra);
2145         }
2146 }
2147
2148
2149 static const struct iw_priv_args prism2_priv[] = {
2150         { PRISM2_IOCTL_MONITOR,
2151           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "monitor" },
2152         { PRISM2_IOCTL_READMIF,
2153           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
2154           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, "readmif" },
2155         { PRISM2_IOCTL_WRITEMIF,
2156           IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 2, 0, "writemif" },
2157         { PRISM2_IOCTL_RESET,
2158           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "reset" },
2159         { PRISM2_IOCTL_INQUIRE,
2160           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "inquire" },
2161         { PRISM2_IOCTL_SET_RID_WORD,
2162           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "set_rid_word" },
2163         { PRISM2_IOCTL_MACCMD,
2164           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "maccmd" },
2165         { PRISM2_IOCTL_WDS_ADD,
2166           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "wds_add" },
2167         { PRISM2_IOCTL_WDS_DEL,
2168           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "wds_del" },
2169         { PRISM2_IOCTL_ADDMAC,
2170           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "addmac" },
2171         { PRISM2_IOCTL_DELMAC,
2172           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "delmac" },
2173         { PRISM2_IOCTL_KICKMAC,
2174           IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 0, "kickmac" },
2175         /* --- raw access to sub-ioctls --- */
2176         { PRISM2_IOCTL_PRISM2_PARAM,
2177           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "prism2_param" },
2178         { PRISM2_IOCTL_GET_PRISM2_PARAM,
2179           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2180           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getprism2_param" },
2181         /* --- sub-ioctls handlers --- */
2182         { PRISM2_IOCTL_PRISM2_PARAM,
2183           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "" },
2184         { PRISM2_IOCTL_GET_PRISM2_PARAM,
2185           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "" },
2186         /* --- sub-ioctls definitions --- */
2187         { PRISM2_PARAM_TXRATECTRL,
2188           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "txratectrl" },
2189         { PRISM2_PARAM_TXRATECTRL,
2190           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gettxratectrl" },
2191         { PRISM2_PARAM_BEACON_INT,
2192           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "beacon_int" },
2193         { PRISM2_PARAM_BEACON_INT,
2194           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbeacon_int" },
2195 #ifndef PRISM2_NO_STATION_MODES
2196         { PRISM2_PARAM_PSEUDO_IBSS,
2197           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "pseudo_ibss" },
2198         { PRISM2_PARAM_PSEUDO_IBSS,
2199           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getpseudo_ibss" },
2200 #endif /* PRISM2_NO_STATION_MODES */
2201         { PRISM2_PARAM_ALC,
2202           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "alc" },
2203         { PRISM2_PARAM_ALC,
2204           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getalc" },
2205         { PRISM2_PARAM_DUMP,
2206           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "dump" },
2207         { PRISM2_PARAM_DUMP,
2208           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdump" },
2209         { PRISM2_PARAM_OTHER_AP_POLICY,
2210           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "other_ap_policy" },
2211         { PRISM2_PARAM_OTHER_AP_POLICY,
2212           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getother_ap_pol" },
2213         { PRISM2_PARAM_AP_MAX_INACTIVITY,
2214           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "max_inactivity" },
2215         { PRISM2_PARAM_AP_MAX_INACTIVITY,
2216           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmax_inactivi" },
2217         { PRISM2_PARAM_AP_BRIDGE_PACKETS,
2218           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "bridge_packets" },
2219         { PRISM2_PARAM_AP_BRIDGE_PACKETS,
2220           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbridge_packe" },
2221         { PRISM2_PARAM_DTIM_PERIOD,
2222           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "dtim_period" },
2223         { PRISM2_PARAM_DTIM_PERIOD,
2224           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdtim_period" },
2225         { PRISM2_PARAM_AP_NULLFUNC_ACK,
2226           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "nullfunc_ack" },
2227         { PRISM2_PARAM_AP_NULLFUNC_ACK,
2228           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getnullfunc_ack" },
2229         { PRISM2_PARAM_MAX_WDS,
2230           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "max_wds" },
2231         { PRISM2_PARAM_MAX_WDS,
2232           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmax_wds" },
2233         { PRISM2_PARAM_AP_AUTOM_AP_WDS,
2234           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "autom_ap_wds" },
2235         { PRISM2_PARAM_AP_AUTOM_AP_WDS,
2236           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getautom_ap_wds" },
2237         { PRISM2_PARAM_AP_AUTH_ALGS,
2238           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ap_auth_algs" },
2239         { PRISM2_PARAM_AP_AUTH_ALGS,
2240           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getap_auth_algs" },
2241         { PRISM2_PARAM_MONITOR_ALLOW_FCSERR,
2242           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "allow_fcserr" },
2243         { PRISM2_PARAM_MONITOR_ALLOW_FCSERR,
2244           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getallow_fcserr" },
2245         { PRISM2_PARAM_HOST_ENCRYPT,
2246           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_encrypt" },
2247         { PRISM2_PARAM_HOST_ENCRYPT,
2248           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_encrypt" },
2249         { PRISM2_PARAM_HOST_DECRYPT,
2250           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_decrypt" },
2251         { PRISM2_PARAM_HOST_DECRYPT,
2252           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_decrypt" },
2253         { PRISM2_PARAM_BUS_MASTER_THRESHOLD_RX,
2254           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "busmaster_rx" },
2255         { PRISM2_PARAM_BUS_MASTER_THRESHOLD_RX,
2256           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbusmaster_rx" },
2257         { PRISM2_PARAM_BUS_MASTER_THRESHOLD_TX,
2258           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "busmaster_tx" },
2259         { PRISM2_PARAM_BUS_MASTER_THRESHOLD_TX,
2260           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbusmaster_tx" },
2261 #ifndef PRISM2_NO_STATION_MODES
2262         { PRISM2_PARAM_HOST_ROAMING,
2263           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "host_roaming" },
2264         { PRISM2_PARAM_HOST_ROAMING,
2265           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethost_roaming" },
2266 #endif /* PRISM2_NO_STATION_MODES */
2267         { PRISM2_PARAM_BCRX_STA_KEY,
2268           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "bcrx_sta_key" },
2269         { PRISM2_PARAM_BCRX_STA_KEY,
2270           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbcrx_sta_key" },
2271         { PRISM2_PARAM_IEEE_802_1X,
2272           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ieee_802_1x" },
2273         { PRISM2_PARAM_IEEE_802_1X,
2274           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getieee_802_1x" },
2275         { PRISM2_PARAM_ANTSEL_TX,
2276           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "antsel_tx" },
2277         { PRISM2_PARAM_ANTSEL_TX,
2278           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getantsel_tx" },
2279         { PRISM2_PARAM_ANTSEL_RX,
2280           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "antsel_rx" },
2281         { PRISM2_PARAM_ANTSEL_RX,
2282           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getantsel_rx" },
2283         { PRISM2_PARAM_MONITOR_TYPE,
2284           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "monitor_type" },
2285         { PRISM2_PARAM_MONITOR_TYPE,
2286           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmonitor_type" },
2287         { PRISM2_PARAM_WDS_TYPE,
2288           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wds_type" },
2289         { PRISM2_PARAM_WDS_TYPE,
2290           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getwds_type" },
2291         { PRISM2_PARAM_HOSTSCAN,
2292           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostscan" },
2293         { PRISM2_PARAM_HOSTSCAN,
2294           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostscan" },
2295         { PRISM2_PARAM_AP_SCAN,
2296           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "ap_scan" },
2297         { PRISM2_PARAM_AP_SCAN,
2298           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getap_scan" },
2299         { PRISM2_PARAM_ENH_SEC,
2300           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "enh_sec" },
2301         { PRISM2_PARAM_ENH_SEC,
2302           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getenh_sec" },
2303 #ifdef PRISM2_IO_DEBUG
2304         { PRISM2_PARAM_IO_DEBUG,
2305           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "io_debug" },
2306         { PRISM2_PARAM_IO_DEBUG,
2307           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getio_debug" },
2308 #endif /* PRISM2_IO_DEBUG */
2309         { PRISM2_PARAM_BASIC_RATES,
2310           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "basic_rates" },
2311         { PRISM2_PARAM_BASIC_RATES,
2312           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getbasic_rates" },
2313         { PRISM2_PARAM_OPER_RATES,
2314           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "oper_rates" },
2315         { PRISM2_PARAM_OPER_RATES,
2316           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getoper_rates" },
2317         { PRISM2_PARAM_HOSTAPD,
2318           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostapd" },
2319         { PRISM2_PARAM_HOSTAPD,
2320           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostapd" },
2321         { PRISM2_PARAM_HOSTAPD_STA,
2322           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "hostapd_sta" },
2323         { PRISM2_PARAM_HOSTAPD_STA,
2324           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostapd_sta" },
2325         { PRISM2_PARAM_WPA,
2326           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wpa" },
2327         { PRISM2_PARAM_WPA,
2328           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getwpa" },
2329         { PRISM2_PARAM_PRIVACY_INVOKED,
2330           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "privacy_invoked" },
2331         { PRISM2_PARAM_PRIVACY_INVOKED,
2332           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getprivacy_invo" },
2333         { PRISM2_PARAM_TKIP_COUNTERMEASURES,
2334           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "tkip_countermea" },
2335         { PRISM2_PARAM_TKIP_COUNTERMEASURES,
2336           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gettkip_counter" },
2337         { PRISM2_PARAM_DROP_UNENCRYPTED,
2338           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "drop_unencrypte" },
2339         { PRISM2_PARAM_DROP_UNENCRYPTED,
2340           0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getdrop_unencry" },
2341 };
2342
2343
2344 static int prism2_ioctl_priv_inquire(struct net_device *dev, int *i)
2345 {
2346         struct hostap_interface *iface;
2347         local_info_t *local;
2348
2349         iface = netdev_priv(dev);
2350         local = iface->local;
2351
2352         if (local->func->cmd(dev, HFA384X_CMDCODE_INQUIRE, *i, NULL, NULL))
2353                 return -EOPNOTSUPP;
2354
2355         return 0;
2356 }
2357
2358
2359 static int prism2_ioctl_priv_prism2_param(struct net_device *dev,
2360                                           struct iw_request_info *info,
2361                                           void *wrqu, char *extra)
2362 {
2363         struct hostap_interface *iface;
2364         local_info_t *local;
2365         int *i = (int *) extra;
2366         int param = *i;
2367         int value = *(i + 1);
2368         int ret = 0;
2369         u16 val;
2370
2371         iface = netdev_priv(dev);
2372         local = iface->local;
2373
2374         switch (param) {
2375         case PRISM2_PARAM_TXRATECTRL:
2376                 local->fw_tx_rate_control = value;
2377                 break;
2378
2379         case PRISM2_PARAM_BEACON_INT:
2380                 if (hostap_set_word(dev, HFA384X_RID_CNFBEACONINT, value) ||
2381                     local->func->reset_port(dev))
2382                         ret = -EINVAL;
2383                 else
2384                         local->beacon_int = value;
2385                 break;
2386
2387 #ifndef PRISM2_NO_STATION_MODES
2388         case PRISM2_PARAM_PSEUDO_IBSS:
2389                 if (value == local->pseudo_adhoc)
2390                         break;
2391
2392                 if (value != 0 && value != 1) {
2393                         ret = -EINVAL;
2394                         break;
2395                 }
2396
2397                 printk(KERN_DEBUG "prism2: %s: pseudo IBSS change %d -> %d\n",
2398                        dev->name, local->pseudo_adhoc, value);
2399                 local->pseudo_adhoc = value;
2400                 if (local->iw_mode != IW_MODE_ADHOC)
2401                         break;
2402
2403                 if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2404                                     hostap_get_porttype(local))) {
2405                         ret = -EOPNOTSUPP;
2406                         break;
2407                 }
2408
2409                 if (local->func->reset_port(dev))
2410                         ret = -EINVAL;
2411                 break;
2412 #endif /* PRISM2_NO_STATION_MODES */
2413
2414         case PRISM2_PARAM_ALC:
2415                 printk(KERN_DEBUG "%s: %s ALC\n", dev->name,
2416                        value == 0 ? "Disabling" : "Enabling");
2417                 val = HFA384X_TEST_CFG_BIT_ALC;
2418                 local->func->cmd(dev, HFA384X_CMDCODE_TEST |
2419                                  (HFA384X_TEST_CFG_BITS << 8),
2420                                  value == 0 ? 0 : 1, &val, NULL);
2421                 break;
2422
2423         case PRISM2_PARAM_DUMP:
2424                 local->frame_dump = value;
2425                 break;
2426
2427         case PRISM2_PARAM_OTHER_AP_POLICY:
2428                 if (value < 0 || value > 3) {
2429                         ret = -EINVAL;
2430                         break;
2431                 }
2432                 if (local->ap != NULL)
2433                         local->ap->ap_policy = value;
2434                 break;
2435
2436         case PRISM2_PARAM_AP_MAX_INACTIVITY:
2437                 if (value < 0 || value > 7 * 24 * 60 * 60) {
2438                         ret = -EINVAL;
2439                         break;
2440                 }
2441                 if (local->ap != NULL)
2442                         local->ap->max_inactivity = value * HZ;
2443                 break;
2444
2445         case PRISM2_PARAM_AP_BRIDGE_PACKETS:
2446                 if (local->ap != NULL)
2447                         local->ap->bridge_packets = value;
2448                 break;
2449
2450         case PRISM2_PARAM_DTIM_PERIOD:
2451                 if (value < 0 || value > 65535) {
2452                         ret = -EINVAL;
2453                         break;
2454                 }
2455                 if (hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD, value)
2456                     || local->func->reset_port(dev))
2457                         ret = -EINVAL;
2458                 else
2459                         local->dtim_period = value;
2460                 break;
2461
2462         case PRISM2_PARAM_AP_NULLFUNC_ACK:
2463                 if (local->ap != NULL)
2464                         local->ap->nullfunc_ack = value;
2465                 break;
2466
2467         case PRISM2_PARAM_MAX_WDS:
2468                 local->wds_max_connections = value;
2469                 break;
2470
2471         case PRISM2_PARAM_AP_AUTOM_AP_WDS:
2472                 if (local->ap != NULL) {
2473                         if (!local->ap->autom_ap_wds && value) {
2474                                 /* add WDS link to all APs in STA table */
2475                                 hostap_add_wds_links(local);
2476                         }
2477                         local->ap->autom_ap_wds = value;
2478                 }
2479                 break;
2480
2481         case PRISM2_PARAM_AP_AUTH_ALGS:
2482                 local->auth_algs = value;
2483                 if (hostap_set_auth_algs(local))
2484                         ret = -EINVAL;
2485                 break;
2486
2487         case PRISM2_PARAM_MONITOR_ALLOW_FCSERR:
2488                 local->monitor_allow_fcserr = value;
2489                 break;
2490
2491         case PRISM2_PARAM_HOST_ENCRYPT:
2492                 local->host_encrypt = value;
2493                 if (hostap_set_encryption(local) ||
2494                     local->func->reset_port(dev))
2495                         ret = -EINVAL;
2496                 break;
2497
2498         case PRISM2_PARAM_HOST_DECRYPT:
2499                 local->host_decrypt = value;
2500                 if (hostap_set_encryption(local) ||
2501                     local->func->reset_port(dev))
2502                         ret = -EINVAL;
2503                 break;
2504
2505         case PRISM2_PARAM_BUS_MASTER_THRESHOLD_RX:
2506                 local->bus_master_threshold_rx = value;
2507                 break;
2508
2509         case PRISM2_PARAM_BUS_MASTER_THRESHOLD_TX:
2510                 local->bus_master_threshold_tx = value;
2511                 break;
2512
2513 #ifndef PRISM2_NO_STATION_MODES
2514         case PRISM2_PARAM_HOST_ROAMING:
2515                 if (value < 0 || value > 2) {
2516                         ret = -EINVAL;
2517                         break;
2518                 }
2519                 local->host_roaming = value;
2520                 if (hostap_set_roaming(local) || local->func->reset_port(dev))
2521                         ret = -EINVAL;
2522                 break;
2523 #endif /* PRISM2_NO_STATION_MODES */
2524
2525         case PRISM2_PARAM_BCRX_STA_KEY:
2526                 local->bcrx_sta_key = value;
2527                 break;
2528
2529         case PRISM2_PARAM_IEEE_802_1X:
2530                 local->ieee_802_1x = value;
2531                 break;
2532
2533         case PRISM2_PARAM_ANTSEL_TX:
2534                 if (value < 0 || value > HOSTAP_ANTSEL_HIGH) {
2535                         ret = -EINVAL;
2536                         break;
2537                 }
2538                 local->antsel_tx = value;
2539                 hostap_set_antsel(local);
2540                 break;
2541
2542         case PRISM2_PARAM_ANTSEL_RX:
2543                 if (value < 0 || value > HOSTAP_ANTSEL_HIGH) {
2544                         ret = -EINVAL;
2545                         break;
2546                 }
2547                 local->antsel_rx = value;
2548                 hostap_set_antsel(local);
2549                 break;
2550
2551         case PRISM2_PARAM_MONITOR_TYPE:
2552                 if (value != PRISM2_MONITOR_80211 &&
2553                     value != PRISM2_MONITOR_CAPHDR &&
2554                     value != PRISM2_MONITOR_PRISM) {
2555                         ret = -EINVAL;
2556                         break;
2557                 }
2558                 local->monitor_type = value;
2559                 if (local->iw_mode == IW_MODE_MONITOR)
2560                         hostap_monitor_set_type(local);
2561                 break;
2562
2563         case PRISM2_PARAM_WDS_TYPE:
2564                 local->wds_type = value;
2565                 break;
2566
2567         case PRISM2_PARAM_HOSTSCAN:
2568         {
2569                 struct hfa384x_hostscan_request scan_req;
2570                 u16 rate;
2571
2572                 memset(&scan_req, 0, sizeof(scan_req));
2573                 scan_req.channel_list = __constant_cpu_to_le16(0x3fff);
2574                 switch (value) {
2575                 case 1: rate = HFA384X_RATES_1MBPS; break;
2576                 case 2: rate = HFA384X_RATES_2MBPS; break;
2577                 case 3: rate = HFA384X_RATES_5MBPS; break;
2578                 case 4: rate = HFA384X_RATES_11MBPS; break;
2579                 default: rate = HFA384X_RATES_1MBPS; break;
2580                 }
2581                 scan_req.txrate = cpu_to_le16(rate);
2582                 /* leave SSID empty to accept all SSIDs */
2583
2584                 if (local->iw_mode == IW_MODE_MASTER) {
2585                         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2586                                             HFA384X_PORTTYPE_BSS) ||
2587                             local->func->reset_port(dev))
2588                                 printk(KERN_DEBUG "Leaving Host AP mode "
2589                                        "for HostScan failed\n");
2590                 }
2591
2592                 if (local->func->set_rid(dev, HFA384X_RID_HOSTSCAN, &scan_req,
2593                                          sizeof(scan_req))) {
2594                         printk(KERN_DEBUG "HOSTSCAN failed\n");
2595                         ret = -EINVAL;
2596                 }
2597                 if (local->iw_mode == IW_MODE_MASTER) {
2598                         wait_queue_t __wait;
2599                         init_waitqueue_entry(&__wait, current);
2600                         add_wait_queue(&local->hostscan_wq, &__wait);
2601                         set_current_state(TASK_INTERRUPTIBLE);
2602                         schedule_timeout(HZ);
2603                         if (signal_pending(current))
2604                                 ret = -EINTR;
2605                         set_current_state(TASK_RUNNING);
2606                         remove_wait_queue(&local->hostscan_wq, &__wait);
2607
2608                         if (hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE,
2609                                             HFA384X_PORTTYPE_HOSTAP) ||
2610                             local->func->reset_port(dev))
2611                                 printk(KERN_DEBUG "Returning to Host AP mode "
2612                                        "after HostScan failed\n");
2613                 }
2614                 break;
2615         }
2616
2617         case PRISM2_PARAM_AP_SCAN:
2618                 local->passive_scan_interval = value;
2619                 if (timer_pending(&local->passive_scan_timer))
2620                         del_timer(&local->passive_scan_timer);
2621                 if (value > 0) {
2622                         local->passive_scan_timer.expires = jiffies +
2623                                 local->passive_scan_interval * HZ;
2624                         add_timer(&local->passive_scan_timer);
2625                 }
2626                 break;
2627
2628         case PRISM2_PARAM_ENH_SEC:
2629                 if (value < 0 || value > 3) {
2630                         ret = -EINVAL;
2631                         break;
2632                 }
2633                 local->enh_sec = value;
2634                 if (hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY,
2635                                     local->enh_sec) ||
2636                     local->func->reset_port(dev)) {
2637                         printk(KERN_INFO "%s: cnfEnhSecurity requires STA f/w "
2638                                "1.6.3 or newer\n", dev->name);
2639                         ret = -EOPNOTSUPP;
2640                 }
2641                 break;
2642
2643 #ifdef PRISM2_IO_DEBUG
2644         case PRISM2_PARAM_IO_DEBUG:
2645                 local->io_debug_enabled = value;
2646                 break;
2647 #endif /* PRISM2_IO_DEBUG */
2648
2649         case PRISM2_PARAM_BASIC_RATES:
2650                 if ((value & local->tx_rate_control) != value || value == 0) {
2651                         printk(KERN_INFO "%s: invalid basic rate set - basic "
2652                                "rates must be in supported rate set\n",
2653                                dev->name);
2654                         ret = -EINVAL;
2655                         break;
2656                 }
2657                 local->basic_rates = value;
2658                 if (hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
2659                                     local->basic_rates) ||
2660                     local->func->reset_port(dev))
2661                         ret = -EINVAL;
2662                 break;
2663
2664         case PRISM2_PARAM_OPER_RATES:
2665                 local->tx_rate_control = value;
2666                 if (hostap_set_rate(dev))
2667                         ret = -EINVAL;
2668                 break;
2669
2670         case PRISM2_PARAM_HOSTAPD:
2671                 ret = hostap_set_hostapd(local, value, 1);
2672                 break;
2673
2674         case PRISM2_PARAM_HOSTAPD_STA:
2675                 ret = hostap_set_hostapd_sta(local, value, 1);
2676                 break;
2677
2678         case PRISM2_PARAM_WPA:
2679                 local->wpa = value;
2680                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
2681                         ret = -EOPNOTSUPP;
2682                 else if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE,
2683                                          value ? 1 : 0))
2684                         ret = -EINVAL;
2685                 break;
2686
2687         case PRISM2_PARAM_PRIVACY_INVOKED:
2688                 local->privacy_invoked = value;
2689                 if (hostap_set_encryption(local) ||
2690                     local->func->reset_port(dev))
2691                         ret = -EINVAL;
2692                 break;
2693
2694         case PRISM2_PARAM_TKIP_COUNTERMEASURES:
2695                 local->tkip_countermeasures = value;
2696                 break;
2697
2698         case PRISM2_PARAM_DROP_UNENCRYPTED:
2699                 local->drop_unencrypted = value;
2700                 break;
2701
2702         default:
2703                 printk(KERN_DEBUG "%s: prism2_param: unknown param %d\n",
2704                        dev->name, param);
2705                 ret = -EOPNOTSUPP;
2706                 break;
2707         }
2708
2709         return ret;
2710 }
2711
2712
2713 static int prism2_ioctl_priv_get_prism2_param(struct net_device *dev,
2714                                               struct iw_request_info *info,
2715                                               void *wrqu, char *extra)
2716 {
2717         struct hostap_interface *iface;
2718         local_info_t *local;
2719         int *param = (int *) extra;
2720         int ret = 0;
2721
2722         iface = netdev_priv(dev);
2723         local = iface->local;
2724
2725         switch (*param) {
2726         case PRISM2_PARAM_TXRATECTRL:
2727                 *param = local->fw_tx_rate_control;
2728                 break;
2729
2730         case PRISM2_PARAM_BEACON_INT:
2731                 *param = local->beacon_int;
2732                 break;
2733
2734         case PRISM2_PARAM_PSEUDO_IBSS:
2735                 *param = local->pseudo_adhoc;
2736                 break;
2737
2738         case PRISM2_PARAM_ALC:
2739                 ret = -EOPNOTSUPP; /* FIX */
2740                 break;
2741
2742         case PRISM2_PARAM_DUMP:
2743                 *param = local->frame_dump;
2744                 break;
2745
2746         case PRISM2_PARAM_OTHER_AP_POLICY:
2747                 if (local->ap != NULL)
2748                         *param = local->ap->ap_policy;
2749                 else
2750                         ret = -EOPNOTSUPP;
2751                 break;
2752
2753         case PRISM2_PARAM_AP_MAX_INACTIVITY:
2754                 if (local->ap != NULL)
2755                         *param = local->ap->max_inactivity / HZ;
2756                 else
2757                         ret = -EOPNOTSUPP;
2758                 break;
2759
2760         case PRISM2_PARAM_AP_BRIDGE_PACKETS:
2761                 if (local->ap != NULL)
2762                         *param = local->ap->bridge_packets;
2763                 else
2764                         ret = -EOPNOTSUPP;
2765                 break;
2766
2767         case PRISM2_PARAM_DTIM_PERIOD:
2768                 *param = local->dtim_period;
2769                 break;
2770
2771         case PRISM2_PARAM_AP_NULLFUNC_ACK:
2772                 if (local->ap != NULL)
2773                         *param = local->ap->nullfunc_ack;
2774                 else
2775                         ret = -EOPNOTSUPP;
2776                 break;
2777
2778         case PRISM2_PARAM_MAX_WDS:
2779                 *param = local->wds_max_connections;
2780                 break;
2781
2782         case PRISM2_PARAM_AP_AUTOM_AP_WDS:
2783                 if (local->ap != NULL)
2784                         *param = local->ap->autom_ap_wds;
2785                 else
2786                         ret = -EOPNOTSUPP;
2787                 break;
2788
2789         case PRISM2_PARAM_AP_AUTH_ALGS:
2790                 *param = local->auth_algs;
2791                 break;
2792
2793         case PRISM2_PARAM_MONITOR_ALLOW_FCSERR:
2794                 *param = local->monitor_allow_fcserr;
2795                 break;
2796
2797         case PRISM2_PARAM_HOST_ENCRYPT:
2798                 *param = local->host_encrypt;
2799                 break;
2800
2801         case PRISM2_PARAM_HOST_DECRYPT:
2802                 *param = local->host_decrypt;
2803                 break;
2804
2805         case PRISM2_PARAM_BUS_MASTER_THRESHOLD_RX:
2806                 *param = local->bus_master_threshold_rx;
2807                 break;
2808
2809         case PRISM2_PARAM_BUS_MASTER_THRESHOLD_TX:
2810                 *param = local->bus_master_threshold_tx;
2811                 break;
2812
2813         case PRISM2_PARAM_HOST_ROAMING:
2814                 *param = local->host_roaming;
2815                 break;
2816
2817         case PRISM2_PARAM_BCRX_STA_KEY:
2818                 *param = local->bcrx_sta_key;
2819                 break;
2820
2821         case PRISM2_PARAM_IEEE_802_1X:
2822                 *param = local->ieee_802_1x;
2823                 break;
2824
2825         case PRISM2_PARAM_ANTSEL_TX:
2826                 *param = local->antsel_tx;
2827                 break;
2828
2829         case PRISM2_PARAM_ANTSEL_RX:
2830                 *param = local->antsel_rx;
2831                 break;
2832
2833         case PRISM2_PARAM_MONITOR_TYPE:
2834                 *param = local->monitor_type;
2835                 break;
2836
2837         case PRISM2_PARAM_WDS_TYPE:
2838                 *param = local->wds_type;
2839                 break;
2840
2841         case PRISM2_PARAM_HOSTSCAN:
2842                 ret = -EOPNOTSUPP;
2843                 break;
2844
2845         case PRISM2_PARAM_AP_SCAN:
2846                 *param = local->passive_scan_interval;
2847                 break;
2848
2849         case PRISM2_PARAM_ENH_SEC:
2850                 *param = local->enh_sec;
2851                 break;
2852
2853 #ifdef PRISM2_IO_DEBUG
2854         case PRISM2_PARAM_IO_DEBUG:
2855                 *param = local->io_debug_enabled;
2856                 break;
2857 #endif /* PRISM2_IO_DEBUG */
2858
2859         case PRISM2_PARAM_BASIC_RATES:
2860                 *param = local->basic_rates;
2861                 break;
2862
2863         case PRISM2_PARAM_OPER_RATES:
2864                 *param = local->tx_rate_control;
2865                 break;
2866
2867         case PRISM2_PARAM_HOSTAPD:
2868                 *param = local->hostapd;
2869                 break;
2870
2871         case PRISM2_PARAM_HOSTAPD_STA:
2872                 *param = local->hostapd_sta;
2873                 break;
2874
2875         case PRISM2_PARAM_WPA:
2876                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
2877                         ret = -EOPNOTSUPP;
2878                 *param = local->wpa;
2879                 break;
2880
2881         case PRISM2_PARAM_PRIVACY_INVOKED:
2882                 *param = local->privacy_invoked;
2883                 break;
2884
2885         case PRISM2_PARAM_TKIP_COUNTERMEASURES:
2886                 *param = local->tkip_countermeasures;
2887                 break;
2888
2889         case PRISM2_PARAM_DROP_UNENCRYPTED:
2890                 *param = local->drop_unencrypted;
2891                 break;
2892
2893         default:
2894                 printk(KERN_DEBUG "%s: get_prism2_param: unknown param %d\n",
2895                        dev->name, *param);
2896                 ret = -EOPNOTSUPP;
2897                 break;
2898         }
2899
2900         return ret;
2901 }
2902
2903
2904 static int prism2_ioctl_priv_readmif(struct net_device *dev,
2905                                      struct iw_request_info *info,
2906                                      void *wrqu, char *extra)
2907 {
2908         struct hostap_interface *iface;
2909         local_info_t *local;
2910         u16 resp0;
2911
2912         iface = netdev_priv(dev);
2913         local = iface->local;
2914
2915         if (local->func->cmd(dev, HFA384X_CMDCODE_READMIF, *extra, NULL,
2916                              &resp0))
2917                 return -EOPNOTSUPP;
2918         else
2919                 *extra = resp0;
2920
2921         return 0;
2922 }
2923
2924
2925 static int prism2_ioctl_priv_writemif(struct net_device *dev,
2926                                       struct iw_request_info *info,
2927                                       void *wrqu, char *extra)
2928 {
2929         struct hostap_interface *iface;
2930         local_info_t *local;
2931         u16 cr, val;
2932
2933         iface = netdev_priv(dev);
2934         local = iface->local;
2935
2936         cr = *extra;
2937         val = *(extra + 1);
2938         if (local->func->cmd(dev, HFA384X_CMDCODE_WRITEMIF, cr, &val, NULL))
2939                 return -EOPNOTSUPP;
2940
2941         return 0;
2942 }
2943
2944
2945 static int prism2_ioctl_priv_monitor(struct net_device *dev, int *i)
2946 {
2947         struct hostap_interface *iface;
2948         local_info_t *local;
2949         int ret = 0;
2950         u32 mode;
2951
2952         iface = netdev_priv(dev);
2953         local = iface->local;
2954
2955         printk(KERN_DEBUG "%s: process %d (%s) used deprecated iwpriv monitor "
2956                "- update software to use iwconfig mode monitor\n",
2957                dev->name, current->pid, current->comm);
2958
2959         /* Backward compatibility code - this can be removed at some point */
2960
2961         if (*i == 0) {
2962                 /* Disable monitor mode - old mode was not saved, so go to
2963                  * Master mode */
2964                 mode = IW_MODE_MASTER;
2965                 ret = prism2_ioctl_siwmode(dev, NULL, &mode, NULL);
2966         } else if (*i == 1) {
2967                 /* netlink socket mode is not supported anymore since it did
2968                  * not separate different devices from each other and was not
2969                  * best method for delivering large amount of packets to
2970                  * user space */
2971                 ret = -EOPNOTSUPP;
2972         } else if (*i == 2 || *i == 3) {
2973                 switch (*i) {
2974                 case 2:
2975                         local->monitor_type = PRISM2_MONITOR_80211;
2976                         break;
2977                 case 3:
2978                         local->monitor_type = PRISM2_MONITOR_PRISM;
2979                         break;
2980                 }
2981                 mode = IW_MODE_MONITOR;
2982                 ret = prism2_ioctl_siwmode(dev, NULL, &mode, NULL);
2983                 hostap_monitor_mode_enable(local);
2984         } else
2985                 ret = -EINVAL;
2986
2987         return ret;
2988 }
2989
2990
2991 static int prism2_ioctl_priv_reset(struct net_device *dev, int *i)
2992 {
2993         struct hostap_interface *iface;
2994         local_info_t *local;
2995
2996         iface = netdev_priv(dev);
2997         local = iface->local;
2998
2999         printk(KERN_DEBUG "%s: manual reset request(%d)\n", dev->name, *i);
3000         switch (*i) {
3001         case 0:
3002                 /* Disable and enable card */
3003                 local->func->hw_shutdown(dev, 1);
3004                 local->func->hw_config(dev, 0);
3005                 break;
3006
3007         case 1:
3008                 /* COR sreset */
3009                 local->func->hw_reset(dev);
3010                 break;
3011
3012         case 2:
3013                 /* Disable and enable port 0 */
3014                 local->func->reset_port(dev);
3015                 break;
3016
3017         case 3:
3018                 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
3019                 if (local->func->cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL,
3020                                      NULL))
3021                         return -EINVAL;
3022                 break;
3023
3024         case 4:
3025                 if (local->func->cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL,
3026                                      NULL))
3027                         return -EINVAL;
3028                 break;
3029
3030         default:
3031                 printk(KERN_DEBUG "Unknown reset request %d\n", *i);
3032                 return -EOPNOTSUPP;
3033         }
3034
3035         return 0;
3036 }
3037
3038
3039 static int prism2_ioctl_priv_set_rid_word(struct net_device *dev, int *i)
3040 {
3041         int rid = *i;
3042         int value = *(i + 1);
3043
3044         printk(KERN_DEBUG "%s: Set RID[0x%X] = %d\n", dev->name, rid, value);
3045
3046         if (hostap_set_word(dev, rid, value))
3047                 return -EINVAL;
3048
3049         return 0;
3050 }
3051
3052
3053 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3054 static int ap_mac_cmd_ioctl(local_info_t *local, int *cmd)
3055 {
3056         int ret = 0;
3057
3058         switch (*cmd) {
3059         case AP_MAC_CMD_POLICY_OPEN:
3060                 local->ap->mac_restrictions.policy = MAC_POLICY_OPEN;
3061                 break;
3062         case AP_MAC_CMD_POLICY_ALLOW:
3063                 local->ap->mac_restrictions.policy = MAC_POLICY_ALLOW;
3064                 break;
3065         case AP_MAC_CMD_POLICY_DENY:
3066                 local->ap->mac_restrictions.policy = MAC_POLICY_DENY;
3067                 break;
3068         case AP_MAC_CMD_FLUSH:
3069                 ap_control_flush_macs(&local->ap->mac_restrictions);
3070                 break;
3071         case AP_MAC_CMD_KICKALL:
3072                 ap_control_kickall(local->ap);
3073                 hostap_deauth_all_stas(local->dev, local->ap, 0);
3074                 break;
3075         default:
3076                 ret = -EOPNOTSUPP;
3077                 break;
3078         }
3079
3080         return ret;
3081 }
3082 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3083
3084
3085 #ifdef PRISM2_DOWNLOAD_SUPPORT
3086 static int prism2_ioctl_priv_download(local_info_t *local, struct iw_point *p)
3087 {
3088         struct prism2_download_param *param;
3089         int ret = 0;
3090
3091         if (p->length < sizeof(struct prism2_download_param) ||
3092             p->length > 1024 || !p->pointer)
3093                 return -EINVAL;
3094
3095         param = (struct prism2_download_param *)
3096                 kmalloc(p->length, GFP_KERNEL);
3097         if (param == NULL)
3098                 return -ENOMEM;
3099
3100         if (copy_from_user(param, p->pointer, p->length)) {
3101                 ret = -EFAULT;
3102                 goto out;
3103         }
3104
3105         if (p->length < sizeof(struct prism2_download_param) +
3106             param->num_areas * sizeof(struct prism2_download_area)) {
3107                 ret = -EINVAL;
3108                 goto out;
3109         }
3110
3111         ret = local->func->download(local, param);
3112
3113  out:
3114         if (param != NULL)
3115                 kfree(param);
3116
3117         return ret;
3118 }
3119 #endif /* PRISM2_DOWNLOAD_SUPPORT */
3120
3121
3122 static int prism2_set_genericelement(struct net_device *dev, u8 *elem,
3123                                      size_t len)
3124 {
3125         struct hostap_interface *iface = dev->priv;
3126         local_info_t *local = iface->local;
3127         u8 *buf;
3128
3129         /*
3130          * Add 16-bit length in the beginning of the buffer because Prism2 RID
3131          * includes it.
3132          */
3133         buf = kmalloc(len + 2, GFP_KERNEL);
3134         if (buf == NULL)
3135                 return -ENOMEM;
3136
3137         *((u16 *) buf) = cpu_to_le16(len);
3138         memcpy(buf + 2, elem, len);
3139
3140         kfree(local->generic_elem);
3141         local->generic_elem = buf;
3142         local->generic_elem_len = len + 2;
3143
3144         return local->func->set_rid(local->dev, HFA384X_RID_GENERICELEMENT,
3145                                     buf, len + 2);
3146 }
3147
3148
3149 static int prism2_ioctl_siwauth(struct net_device *dev,
3150                                 struct iw_request_info *info,
3151                                 struct iw_param *data, char *extra)
3152 {
3153         struct hostap_interface *iface = dev->priv;
3154         local_info_t *local = iface->local;
3155
3156         switch (data->flags & IW_AUTH_INDEX) {
3157         case IW_AUTH_WPA_VERSION:
3158         case IW_AUTH_CIPHER_PAIRWISE:
3159         case IW_AUTH_CIPHER_GROUP:
3160         case IW_AUTH_KEY_MGMT:
3161                 /*
3162                  * Host AP driver does not use these parameters and allows
3163                  * wpa_supplicant to control them internally.
3164                  */
3165                 break;
3166         case IW_AUTH_TKIP_COUNTERMEASURES:
3167                 local->tkip_countermeasures = data->value;
3168                 break;
3169         case IW_AUTH_DROP_UNENCRYPTED:
3170                 local->drop_unencrypted = data->value;
3171                 break;
3172         case IW_AUTH_80211_AUTH_ALG:
3173                 local->auth_algs = data->value;
3174                 break;
3175         case IW_AUTH_WPA_ENABLED:
3176                 if (data->value == 0) {
3177                         local->wpa = 0;
3178                         if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
3179                                 break;
3180                         prism2_set_genericelement(dev, "", 0);
3181                         local->host_roaming = 0;
3182                         local->privacy_invoked = 0;
3183                         if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE,
3184                                             0) ||
3185                             hostap_set_roaming(local) ||
3186                             hostap_set_encryption(local) ||
3187                             local->func->reset_port(dev))
3188                                 return -EINVAL;
3189                         break;
3190                 }
3191                 if (local->sta_fw_ver < PRISM2_FW_VER(1,7,0))
3192                         return -EOPNOTSUPP;
3193                 local->host_roaming = 2;
3194                 local->privacy_invoked = 1;
3195                 local->wpa = 1;
3196                 if (hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1) ||
3197                     hostap_set_roaming(local) ||
3198                     hostap_set_encryption(local) ||
3199                     local->func->reset_port(dev))
3200                         return -EINVAL;
3201                 break;
3202         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3203                 local->ieee_802_1x = data->value;
3204                 break;
3205         case IW_AUTH_PRIVACY_INVOKED:
3206                 local->privacy_invoked = data->value;
3207                 break;
3208         default:
3209                 return -EOPNOTSUPP;
3210         }
3211         return 0;
3212 }
3213
3214
3215 static int prism2_ioctl_giwauth(struct net_device *dev,
3216                                 struct iw_request_info *info,
3217                                 struct iw_param *data, char *extra)
3218 {
3219         struct hostap_interface *iface = dev->priv;
3220         local_info_t *local = iface->local;
3221
3222         switch (data->flags & IW_AUTH_INDEX) {
3223         case IW_AUTH_WPA_VERSION:
3224         case IW_AUTH_CIPHER_PAIRWISE:
3225         case IW_AUTH_CIPHER_GROUP:
3226         case IW_AUTH_KEY_MGMT:
3227                 /*
3228                  * Host AP driver does not use these parameters and allows
3229                  * wpa_supplicant to control them internally.
3230                  */
3231                 return -EOPNOTSUPP;
3232         case IW_AUTH_TKIP_COUNTERMEASURES:
3233                 data->value = local->tkip_countermeasures;
3234                 break;
3235         case IW_AUTH_DROP_UNENCRYPTED:
3236                 data->value = local->drop_unencrypted;
3237                 break;
3238         case IW_AUTH_80211_AUTH_ALG:
3239                 data->value = local->auth_algs;
3240                 break;
3241         case IW_AUTH_WPA_ENABLED:
3242                 data->value = local->wpa;
3243                 break;
3244         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3245                 data->value = local->ieee_802_1x;
3246                 break;
3247         default:
3248                 return -EOPNOTSUPP;
3249         }
3250         return 0;
3251 }
3252
3253
3254 static int prism2_ioctl_siwencodeext(struct net_device *dev,
3255                                      struct iw_request_info *info,
3256                                      struct iw_point *erq, char *extra)
3257 {
3258         struct hostap_interface *iface = dev->priv;
3259         local_info_t *local = iface->local;
3260         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
3261         int i, ret = 0;
3262         struct hostap_crypto_ops *ops;
3263         struct prism2_crypt_data **crypt;
3264         void *sta_ptr;
3265         u8 *addr;
3266         const char *alg, *module;
3267
3268         i = erq->flags & IW_ENCODE_INDEX;
3269         if (i > WEP_KEYS)
3270                 return -EINVAL;
3271         if (i < 1 || i > WEP_KEYS)
3272                 i = local->tx_keyidx;
3273         else
3274                 i--;
3275         if (i < 0 || i >= WEP_KEYS)
3276                 return -EINVAL;
3277
3278         addr = ext->addr.sa_data;
3279         if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff &&
3280             addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) {
3281                 sta_ptr = NULL;
3282                 crypt = &local->crypt[i];
3283         } else {
3284                 if (i != 0)
3285                         return -EINVAL;
3286                 sta_ptr = ap_crypt_get_ptrs(local->ap, addr, 0, &crypt);
3287                 if (sta_ptr == NULL) {
3288                         if (local->iw_mode == IW_MODE_INFRA) {
3289                                 /*
3290                                  * TODO: add STA entry for the current AP so
3291                                  * that unicast key can be used. For now, this
3292                                  * is emulated by using default key idx 0.
3293                                  */
3294                                 i = 0;
3295                                 crypt = &local->crypt[i];
3296                         } else
3297                                 return -EINVAL;
3298                 }
3299         }
3300
3301         if ((erq->flags & IW_ENCODE_DISABLED) ||
3302             ext->alg == IW_ENCODE_ALG_NONE) {
3303                 if (*crypt)
3304                         prism2_crypt_delayed_deinit(local, crypt);
3305                 goto done;
3306         }
3307
3308         switch (ext->alg) {
3309         case IW_ENCODE_ALG_WEP:
3310                 alg = "WEP";
3311                 module = "hostap_crypt_wep";
3312                 break;
3313         case IW_ENCODE_ALG_TKIP:
3314                 alg = "TKIP";
3315                 module = "hostap_crypt_tkip";
3316                 break;
3317         case IW_ENCODE_ALG_CCMP:
3318                 alg = "CCMP";
3319                 module = "hostap_crypt_ccmp";
3320                 break;
3321         default:
3322                 printk(KERN_DEBUG "%s: unsupported algorithm %d\n",
3323                        local->dev->name, ext->alg);
3324                 ret = -EOPNOTSUPP;
3325                 goto done;
3326         }
3327
3328         ops = hostap_get_crypto_ops(alg);
3329         if (ops == NULL) {
3330                 request_module(module);
3331                 ops = hostap_get_crypto_ops(alg);
3332         }
3333         if (ops == NULL) {
3334                 printk(KERN_DEBUG "%s: unknown crypto alg '%s'\n",
3335                        local->dev->name, alg);
3336                 ret = -EOPNOTSUPP;
3337                 goto done;
3338         }
3339
3340         if (sta_ptr || ext->alg != IW_ENCODE_ALG_WEP) {
3341                 /*
3342                  * Per station encryption and other than WEP algorithms
3343                  * require host-based encryption, so force them on
3344                  * automatically.
3345                  */
3346                 local->host_decrypt = local->host_encrypt = 1;
3347         }
3348
3349         if (*crypt == NULL || (*crypt)->ops != ops) {
3350                 struct prism2_crypt_data *new_crypt;
3351
3352                 prism2_crypt_delayed_deinit(local, crypt);
3353
3354                 new_crypt = (struct prism2_crypt_data *)
3355                         kmalloc(sizeof(struct prism2_crypt_data), GFP_KERNEL);
3356                 if (new_crypt == NULL) {
3357                         ret = -ENOMEM;
3358                         goto done;
3359                 }
3360                 memset(new_crypt, 0, sizeof(struct prism2_crypt_data));
3361                 new_crypt->ops = ops;
3362                 new_crypt->priv = new_crypt->ops->init(i);
3363                 if (new_crypt->priv == NULL) {
3364                         kfree(new_crypt);
3365                         ret = -EINVAL;
3366                         goto done;
3367                 }
3368
3369                 *crypt = new_crypt;
3370         }
3371
3372         /*
3373          * TODO: if ext_flags does not have IW_ENCODE_EXT_RX_SEQ_VALID, the
3374          * existing seq# should not be changed.
3375          * TODO: if ext_flags has IW_ENCODE_EXT_TX_SEQ_VALID, next TX seq#
3376          * should be changed to something else than zero.
3377          */
3378         if ((!(ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) || ext->key_len > 0)
3379             && (*crypt)->ops->set_key &&
3380             (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
3381                                    (*crypt)->priv) < 0) {
3382                 printk(KERN_DEBUG "%s: key setting failed\n",
3383                        local->dev->name);
3384                 ret = -EINVAL;
3385                 goto done;
3386         }
3387
3388         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
3389                 if (!sta_ptr)
3390                         local->tx_keyidx = i;
3391                 else if (i) {
3392                         ret = -EINVAL;
3393                         goto done;
3394                 }
3395         }
3396
3397
3398         if (sta_ptr == NULL && ext->key_len > 0) {
3399                 int first = 1, j;
3400                 for (j = 0; j < WEP_KEYS; j++) {
3401                         if (j != i && local->crypt[j]) {
3402                                 first = 0;
3403                                 break;
3404                         }
3405                 }
3406                 if (first)
3407                         local->tx_keyidx = i;
3408         }
3409
3410  done:
3411         if (sta_ptr)
3412                 hostap_handle_sta_release(sta_ptr);
3413
3414         local->open_wep = erq->flags & IW_ENCODE_OPEN;
3415
3416         /*
3417          * Do not reset port0 if card is in Managed mode since resetting will
3418          * generate new IEEE 802.11 authentication which may end up in looping
3419          * with IEEE 802.1X. Prism2 documentation seem to require port reset
3420          * after WEP configuration. However, keys are apparently changed at
3421          * least in Managed mode.
3422          */
3423         if (ret == 0 &&
3424             (hostap_set_encryption(local) ||
3425              (local->iw_mode != IW_MODE_INFRA &&
3426               local->func->reset_port(local->dev))))
3427                 ret = -EINVAL;
3428
3429         return ret;
3430 }
3431
3432
3433 static int prism2_ioctl_giwencodeext(struct net_device *dev,
3434                                      struct iw_request_info *info,
3435                                      struct iw_point *erq, char *extra)
3436 {
3437         struct hostap_interface *iface = dev->priv;
3438         local_info_t *local = iface->local;
3439         struct prism2_crypt_data **crypt;
3440         void *sta_ptr;
3441         int max_key_len, i;
3442         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
3443         u8 *addr;
3444
3445         max_key_len = erq->length - sizeof(*ext);
3446         if (max_key_len < 0)
3447                 return -EINVAL;
3448
3449         i = erq->flags & IW_ENCODE_INDEX;
3450         if (i < 1 || i > WEP_KEYS)
3451                 i = local->tx_keyidx;
3452         else
3453                 i--;
3454
3455         addr = ext->addr.sa_data;
3456         if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff &&
3457             addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) {
3458                 sta_ptr = NULL;
3459                 crypt = &local->crypt[i];
3460         } else {
3461                 i = 0;
3462                 sta_ptr = ap_crypt_get_ptrs(local->ap, addr, 0, &crypt);
3463                 if (sta_ptr == NULL)
3464                         return -EINVAL;
3465         }
3466         erq->flags = i + 1;
3467         memset(ext, 0, sizeof(*ext));
3468
3469         if (*crypt == NULL || (*crypt)->ops == NULL) {
3470                 ext->alg = IW_ENCODE_ALG_NONE;
3471                 ext->key_len = 0;
3472                 erq->flags |= IW_ENCODE_DISABLED;
3473         } else {
3474                 if (strcmp((*crypt)->ops->name, "WEP") == 0)
3475                         ext->alg = IW_ENCODE_ALG_WEP;
3476                 else if (strcmp((*crypt)->ops->name, "TKIP") == 0)
3477                         ext->alg = IW_ENCODE_ALG_TKIP;
3478                 else if (strcmp((*crypt)->ops->name, "CCMP") == 0)
3479                         ext->alg = IW_ENCODE_ALG_CCMP;
3480                 else
3481                         return -EINVAL;
3482
3483                 if ((*crypt)->ops->get_key) {
3484                         ext->key_len =
3485                                 (*crypt)->ops->get_key(ext->key,
3486                                                        max_key_len,
3487                                                        ext->tx_seq,
3488                                                        (*crypt)->priv);
3489                         if (ext->key_len &&
3490                             (ext->alg == IW_ENCODE_ALG_TKIP ||
3491                              ext->alg == IW_ENCODE_ALG_CCMP))
3492                                 ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
3493                 }
3494         }
3495
3496         if (sta_ptr)
3497                 hostap_handle_sta_release(sta_ptr);
3498
3499         return 0;
3500 }
3501
3502
3503 static int prism2_ioctl_set_encryption(local_info_t *local,
3504                                        struct prism2_hostapd_param *param,
3505                                        int param_len)
3506 {
3507         int ret = 0;
3508         struct hostap_crypto_ops *ops;
3509         struct prism2_crypt_data **crypt;
3510         void *sta_ptr;
3511
3512         param->u.crypt.err = 0;
3513         param->u.crypt.alg[HOSTAP_CRYPT_ALG_NAME_LEN - 1] = '\0';
3514
3515         if (param_len !=
3516             (int) ((char *) param->u.crypt.key - (char *) param) +
3517             param->u.crypt.key_len)
3518                 return -EINVAL;
3519
3520         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3521             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3522             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3523                 if (param->u.crypt.idx >= WEP_KEYS)
3524                         return -EINVAL;
3525                 sta_ptr = NULL;
3526                 crypt = &local->crypt[param->u.crypt.idx];
3527         } else {
3528                 if (param->u.crypt.idx)
3529                         return -EINVAL;
3530                 sta_ptr = ap_crypt_get_ptrs(
3531                         local->ap, param->sta_addr,
3532                         (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_PERMANENT),
3533                         &crypt);
3534
3535                 if (sta_ptr == NULL) {
3536                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
3537                         return -EINVAL;
3538                 }
3539         }
3540
3541         if (strcmp(param->u.crypt.alg, "none") == 0) {
3542                 if (crypt)
3543                         prism2_crypt_delayed_deinit(local, crypt);
3544                 goto done;
3545         }
3546
3547         ops = hostap_get_crypto_ops(param->u.crypt.alg);
3548         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
3549                 request_module("hostap_crypt_wep");
3550                 ops = hostap_get_crypto_ops(param->u.crypt.alg);
3551         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
3552                 request_module("hostap_crypt_tkip");
3553                 ops = hostap_get_crypto_ops(param->u.crypt.alg);
3554         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
3555                 request_module("hostap_crypt_ccmp");
3556                 ops = hostap_get_crypto_ops(param->u.crypt.alg);
3557         }
3558         if (ops == NULL) {
3559                 printk(KERN_DEBUG "%s: unknown crypto alg '%s'\n",
3560                        local->dev->name, param->u.crypt.alg);
3561                 param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ALG;
3562                 ret = -EINVAL;
3563                 goto done;
3564         }
3565
3566         /* station based encryption and other than WEP algorithms require
3567          * host-based encryption, so force them on automatically */
3568         local->host_decrypt = local->host_encrypt = 1;
3569
3570         if (*crypt == NULL || (*crypt)->ops != ops) {
3571                 struct prism2_crypt_data *new_crypt;
3572
3573                 prism2_crypt_delayed_deinit(local, crypt);
3574
3575                 new_crypt = (struct prism2_crypt_data *)
3576                         kmalloc(sizeof(struct prism2_crypt_data), GFP_KERNEL);
3577                 if (new_crypt == NULL) {
3578                         ret = -ENOMEM;
3579                         goto done;
3580                 }
3581                 memset(new_crypt, 0, sizeof(struct prism2_crypt_data));
3582                 new_crypt->ops = ops;
3583                 new_crypt->priv = new_crypt->ops->init(param->u.crypt.idx);
3584                 if (new_crypt->priv == NULL) {
3585                         kfree(new_crypt);
3586                         param->u.crypt.err =
3587                                 HOSTAP_CRYPT_ERR_CRYPT_INIT_FAILED;
3588                         ret = -EINVAL;
3589                         goto done;
3590                 }
3591
3592                 *crypt = new_crypt;
3593         }
3594
3595         if ((!(param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) ||
3596              param->u.crypt.key_len > 0) && (*crypt)->ops->set_key &&
3597             (*crypt)->ops->set_key(param->u.crypt.key,
3598                                    param->u.crypt.key_len, param->u.crypt.seq,
3599                                    (*crypt)->priv) < 0) {
3600                 printk(KERN_DEBUG "%s: key setting failed\n",
3601                        local->dev->name);
3602                 param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
3603                 ret = -EINVAL;
3604                 goto done;
3605         }
3606
3607         if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY) {
3608                 if (!sta_ptr)
3609                         local->tx_keyidx = param->u.crypt.idx;
3610                 else if (param->u.crypt.idx) {
3611                         printk(KERN_DEBUG "%s: TX key idx setting failed\n",
3612                                local->dev->name);
3613                         param->u.crypt.err =
3614                                 HOSTAP_CRYPT_ERR_TX_KEY_SET_FAILED;
3615                         ret = -EINVAL;
3616                         goto done;
3617                 }
3618         }
3619
3620  done:
3621         if (sta_ptr)
3622                 hostap_handle_sta_release(sta_ptr);
3623
3624         /* Do not reset port0 if card is in Managed mode since resetting will
3625          * generate new IEEE 802.11 authentication which may end up in looping
3626          * with IEEE 802.1X. Prism2 documentation seem to require port reset
3627          * after WEP configuration. However, keys are apparently changed at
3628          * least in Managed mode. */
3629         if (ret == 0 &&
3630             (hostap_set_encryption(local) ||
3631              (local->iw_mode != IW_MODE_INFRA &&
3632               local->func->reset_port(local->dev)))) {
3633                 param->u.crypt.err = HOSTAP_CRYPT_ERR_CARD_CONF_FAILED;
3634                 return -EINVAL;
3635         }
3636
3637         return ret;
3638 }
3639
3640
3641 static int prism2_ioctl_get_encryption(local_info_t *local,
3642                                        struct prism2_hostapd_param *param,
3643                                        int param_len)
3644 {
3645         struct prism2_crypt_data **crypt;
3646         void *sta_ptr;
3647         int max_key_len;
3648
3649         param->u.crypt.err = 0;
3650
3651         max_key_len = param_len -
3652                 (int) ((char *) param->u.crypt.key - (char *) param);
3653         if (max_key_len < 0)
3654                 return -EINVAL;
3655
3656         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3657             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3658             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3659                 sta_ptr = NULL;
3660                 if (param->u.crypt.idx >= WEP_KEYS)
3661                         param->u.crypt.idx = local->tx_keyidx;
3662                 crypt = &local->crypt[param->u.crypt.idx];
3663         } else {
3664                 param->u.crypt.idx = 0;
3665                 sta_ptr = ap_crypt_get_ptrs(local->ap, param->sta_addr, 0,
3666                                             &crypt);
3667
3668                 if (sta_ptr == NULL) {
3669                         param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
3670                         return -EINVAL;
3671                 }
3672         }
3673
3674         if (*crypt == NULL || (*crypt)->ops == NULL) {
3675                 memcpy(param->u.crypt.alg, "none", 5);
3676                 param->u.crypt.key_len = 0;
3677                 param->u.crypt.idx = 0xff;
3678         } else {
3679                 strncpy(param->u.crypt.alg, (*crypt)->ops->name,
3680                         HOSTAP_CRYPT_ALG_NAME_LEN);
3681                 param->u.crypt.key_len = 0;
3682
3683                 memset(param->u.crypt.seq, 0, 8);
3684                 if ((*crypt)->ops->get_key) {
3685                         param->u.crypt.key_len =
3686                                 (*crypt)->ops->get_key(param->u.crypt.key,
3687                                                        max_key_len,
3688                                                        param->u.crypt.seq,
3689                                                        (*crypt)->priv);
3690                 }
3691         }
3692
3693         if (sta_ptr)
3694                 hostap_handle_sta_release(sta_ptr);
3695
3696         return 0;
3697 }
3698
3699
3700 static int prism2_ioctl_get_rid(local_info_t *local,
3701                                 struct prism2_hostapd_param *param,
3702                                 int param_len)
3703 {
3704         int max_len, res;
3705
3706         max_len = param_len - PRISM2_HOSTAPD_RID_HDR_LEN;
3707         if (max_len < 0)
3708                 return -EINVAL;
3709
3710         res = local->func->get_rid(local->dev, param->u.rid.rid,
3711                                    param->u.rid.data, param->u.rid.len, 0);
3712         if (res >= 0) {
3713                 param->u.rid.len = res;
3714                 return 0;
3715         }
3716
3717         return res;
3718 }
3719
3720
3721 static int prism2_ioctl_set_rid(local_info_t *local,
3722                                 struct prism2_hostapd_param *param,
3723                                 int param_len)
3724 {
3725         int max_len;
3726
3727         max_len = param_len - PRISM2_HOSTAPD_RID_HDR_LEN;
3728         if (max_len < 0 || max_len < param->u.rid.len)
3729                 return -EINVAL;
3730
3731         return local->func->set_rid(local->dev, param->u.rid.rid,
3732                                     param->u.rid.data, param->u.rid.len);
3733 }
3734
3735
3736 static int prism2_ioctl_set_assoc_ap_addr(local_info_t *local,
3737                                           struct prism2_hostapd_param *param,
3738                                           int param_len)
3739 {
3740         printk(KERN_DEBUG "%ssta: associated as client with AP " MACSTR "\n",
3741                local->dev->name, MAC2STR(param->sta_addr));
3742         memcpy(local->assoc_ap_addr, param->sta_addr, ETH_ALEN);
3743         return 0;
3744 }
3745
3746
3747 static int prism2_ioctl_siwgenie(struct net_device *dev,
3748                                  struct iw_request_info *info,
3749                                  struct iw_point *data, char *extra)
3750 {
3751         return prism2_set_genericelement(dev, extra, data->length);
3752 }
3753
3754
3755 static int prism2_ioctl_giwgenie(struct net_device *dev,
3756                                  struct iw_request_info *info,
3757                                  struct iw_point *data, char *extra)
3758 {
3759         struct hostap_interface *iface = dev->priv;
3760         local_info_t *local = iface->local;
3761         int len = local->generic_elem_len - 2;
3762
3763         if (len <= 0 || local->generic_elem == NULL) {
3764                 data->length = 0;
3765                 return 0;
3766         }
3767
3768         if (data->length < len)
3769                 return -E2BIG;
3770
3771         data->length = len;
3772         memcpy(extra, local->generic_elem + 2, len);
3773
3774         return 0;
3775 }
3776
3777
3778 static int prism2_ioctl_set_generic_element(local_info_t *local,
3779                                             struct prism2_hostapd_param *param,
3780                                             int param_len)
3781 {
3782         int max_len, len;
3783
3784         len = param->u.generic_elem.len;
3785         max_len = param_len - PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN;
3786         if (max_len < 0 || max_len < len)
3787                 return -EINVAL;
3788
3789         return prism2_set_genericelement(local->dev,
3790                                          param->u.generic_elem.data, len);
3791 }
3792
3793
3794 static int prism2_ioctl_siwmlme(struct net_device *dev,
3795                                 struct iw_request_info *info,
3796                                 struct iw_point *data, char *extra)
3797 {
3798         struct hostap_interface *iface = dev->priv;
3799         local_info_t *local = iface->local;
3800         struct iw_mlme *mlme = (struct iw_mlme *) extra;
3801         u16 reason;
3802
3803         reason = cpu_to_le16(mlme->reason_code);
3804
3805         switch (mlme->cmd) {
3806         case IW_MLME_DEAUTH:
3807                 return prism2_sta_send_mgmt(local, mlme->addr.sa_data,
3808                                             WLAN_FC_STYPE_DEAUTH,
3809                                             (u8 *) &reason, 2);
3810         case IW_MLME_DISASSOC:
3811                 return prism2_sta_send_mgmt(local, mlme->addr.sa_data,
3812                                             WLAN_FC_STYPE_DISASSOC,
3813                                             (u8 *) &reason, 2);
3814         default:
3815                 return -EOPNOTSUPP;
3816         }
3817 }
3818
3819
3820 static int prism2_ioctl_mlme(local_info_t *local,
3821                              struct prism2_hostapd_param *param)
3822 {
3823         u16 reason;
3824
3825         reason = cpu_to_le16(param->u.mlme.reason_code);
3826         switch (param->u.mlme.cmd) {
3827         case MLME_STA_DEAUTH:
3828                 return prism2_sta_send_mgmt(local, param->sta_addr,
3829                                             WLAN_FC_STYPE_DEAUTH,
3830                                             (u8 *) &reason, 2);
3831         case MLME_STA_DISASSOC:
3832                 return prism2_sta_send_mgmt(local, param->sta_addr,
3833                                             WLAN_FC_STYPE_DISASSOC,
3834                                             (u8 *) &reason, 2);
3835         default:
3836                 return -EOPNOTSUPP;
3837         }
3838 }
3839
3840
3841 static int prism2_ioctl_scan_req(local_info_t *local,
3842                                  struct prism2_hostapd_param *param)
3843 {
3844 #ifndef PRISM2_NO_STATION_MODES
3845         if ((local->iw_mode != IW_MODE_INFRA &&
3846              local->iw_mode != IW_MODE_ADHOC) ||
3847             (local->sta_fw_ver < PRISM2_FW_VER(1,3,1)))
3848                 return -EOPNOTSUPP;
3849
3850         if (!local->dev_enabled)
3851                 return -ENETDOWN;
3852
3853         return prism2_request_hostscan(local->dev, param->u.scan_req.ssid,
3854                                        param->u.scan_req.ssid_len);
3855 #else /* PRISM2_NO_STATION_MODES */
3856         return -EOPNOTSUPP;
3857 #endif /* PRISM2_NO_STATION_MODES */
3858 }
3859
3860
3861 static int prism2_ioctl_priv_hostapd(local_info_t *local, struct iw_point *p)
3862 {
3863         struct prism2_hostapd_param *param;
3864         int ret = 0;
3865         int ap_ioctl = 0;
3866
3867         if (p->length < sizeof(struct prism2_hostapd_param) ||
3868             p->length > PRISM2_HOSTAPD_MAX_BUF_SIZE || !p->pointer)
3869                 return -EINVAL;
3870
3871         param = (struct prism2_hostapd_param *) kmalloc(p->length, GFP_KERNEL);
3872         if (param == NULL)
3873                 return -ENOMEM;
3874
3875         if (copy_from_user(param, p->pointer, p->length)) {
3876                 ret = -EFAULT;
3877                 goto out;
3878         }
3879
3880         switch (param->cmd) {
3881         case PRISM2_SET_ENCRYPTION:
3882                 ret = prism2_ioctl_set_encryption(local, param, p->length);
3883                 break;
3884         case PRISM2_GET_ENCRYPTION:
3885                 ret = prism2_ioctl_get_encryption(local, param, p->length);
3886                 break;
3887         case PRISM2_HOSTAPD_GET_RID:
3888                 ret = prism2_ioctl_get_rid(local, param, p->length);
3889                 break;
3890         case PRISM2_HOSTAPD_SET_RID:
3891                 ret = prism2_ioctl_set_rid(local, param, p->length);
3892                 break;
3893         case PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR:
3894                 ret = prism2_ioctl_set_assoc_ap_addr(local, param, p->length);
3895                 break;
3896         case PRISM2_HOSTAPD_SET_GENERIC_ELEMENT:
3897                 ret = prism2_ioctl_set_generic_element(local, param,
3898                                                        p->length);
3899                 break;
3900         case PRISM2_HOSTAPD_MLME:
3901                 ret = prism2_ioctl_mlme(local, param);
3902                 break;
3903         case PRISM2_HOSTAPD_SCAN_REQ:
3904                 ret = prism2_ioctl_scan_req(local, param);
3905                 break;
3906         default:
3907                 ret = prism2_hostapd(local->ap, param);
3908                 ap_ioctl = 1;
3909                 break;
3910         }
3911
3912         if (ret == 1 || !ap_ioctl) {
3913                 if (copy_to_user(p->pointer, param, p->length)) {
3914                         ret = -EFAULT;
3915                         goto out;
3916                 } else if (ap_ioctl)
3917                         ret = 0;
3918         }
3919
3920  out:
3921         if (param != NULL)
3922                 kfree(param);
3923
3924         return ret;
3925 }
3926
3927
3928 static void prism2_get_drvinfo(struct net_device *dev,
3929                                struct ethtool_drvinfo *info)
3930 {
3931         struct hostap_interface *iface;
3932         local_info_t *local;
3933
3934         iface = netdev_priv(dev);
3935         local = iface->local;
3936
3937         strncpy(info->driver, "hostap", sizeof(info->driver) - 1);
3938         strncpy(info->version, PRISM2_VERSION,
3939                 sizeof(info->version) - 1);
3940         snprintf(info->fw_version, sizeof(info->fw_version) - 1,
3941                  "%d.%d.%d", (local->sta_fw_ver >> 16) & 0xff,
3942                  (local->sta_fw_ver >> 8) & 0xff,
3943                  local->sta_fw_ver & 0xff);
3944 }
3945
3946 static struct ethtool_ops prism2_ethtool_ops = {
3947         .get_drvinfo = prism2_get_drvinfo
3948 };
3949
3950
3951 /* Structures to export the Wireless Handlers */
3952
3953 static const iw_handler prism2_handler[] =
3954 {
3955         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
3956         (iw_handler) prism2_get_name,                   /* SIOCGIWNAME */
3957         (iw_handler) NULL,                              /* SIOCSIWNWID */
3958         (iw_handler) NULL,                              /* SIOCGIWNWID */
3959         (iw_handler) prism2_ioctl_siwfreq,              /* SIOCSIWFREQ */
3960         (iw_handler) prism2_ioctl_giwfreq,              /* SIOCGIWFREQ */
3961         (iw_handler) prism2_ioctl_siwmode,              /* SIOCSIWMODE */
3962         (iw_handler) prism2_ioctl_giwmode,              /* SIOCGIWMODE */
3963         (iw_handler) prism2_ioctl_siwsens,              /* SIOCSIWSENS */
3964         (iw_handler) prism2_ioctl_giwsens,              /* SIOCGIWSENS */
3965         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
3966         (iw_handler) prism2_ioctl_giwrange,             /* SIOCGIWRANGE */
3967         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
3968         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
3969         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
3970         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
3971         iw_handler_set_spy,                             /* SIOCSIWSPY */
3972         iw_handler_get_spy,                             /* SIOCGIWSPY */
3973         iw_handler_set_thrspy,                          /* SIOCSIWTHRSPY */
3974         iw_handler_get_thrspy,                          /* SIOCGIWTHRSPY */
3975         (iw_handler) prism2_ioctl_siwap,                /* SIOCSIWAP */
3976         (iw_handler) prism2_ioctl_giwap,                /* SIOCGIWAP */
3977         (iw_handler) prism2_ioctl_siwmlme,              /* SIOCSIWMLME */
3978         (iw_handler) prism2_ioctl_giwaplist,            /* SIOCGIWAPLIST */
3979         (iw_handler) prism2_ioctl_siwscan,              /* SIOCSIWSCAN */
3980         (iw_handler) prism2_ioctl_giwscan,              /* SIOCGIWSCAN */
3981         (iw_handler) prism2_ioctl_siwessid,             /* SIOCSIWESSID */
3982         (iw_handler) prism2_ioctl_giwessid,             /* SIOCGIWESSID */
3983         (iw_handler) prism2_ioctl_siwnickn,             /* SIOCSIWNICKN */
3984         (iw_handler) prism2_ioctl_giwnickn,             /* SIOCGIWNICKN */
3985         (iw_handler) NULL,                              /* -- hole -- */
3986         (iw_handler) NULL,                              /* -- hole -- */
3987         (iw_handler) prism2_ioctl_siwrate,              /* SIOCSIWRATE */
3988         (iw_handler) prism2_ioctl_giwrate,              /* SIOCGIWRATE */
3989         (iw_handler) prism2_ioctl_siwrts,               /* SIOCSIWRTS */
3990         (iw_handler) prism2_ioctl_giwrts,               /* SIOCGIWRTS */
3991         (iw_handler) prism2_ioctl_siwfrag,              /* SIOCSIWFRAG */
3992         (iw_handler) prism2_ioctl_giwfrag,              /* SIOCGIWFRAG */
3993         (iw_handler) prism2_ioctl_siwtxpow,             /* SIOCSIWTXPOW */
3994         (iw_handler) prism2_ioctl_giwtxpow,             /* SIOCGIWTXPOW */
3995         (iw_handler) prism2_ioctl_siwretry,             /* SIOCSIWRETRY */
3996         (iw_handler) prism2_ioctl_giwretry,             /* SIOCGIWRETRY */
3997         (iw_handler) prism2_ioctl_siwencode,            /* SIOCSIWENCODE */
3998         (iw_handler) prism2_ioctl_giwencode,            /* SIOCGIWENCODE */
3999         (iw_handler) prism2_ioctl_siwpower,             /* SIOCSIWPOWER */
4000         (iw_handler) prism2_ioctl_giwpower,             /* SIOCGIWPOWER */
4001         (iw_handler) NULL,                              /* -- hole -- */
4002         (iw_handler) NULL,                              /* -- hole -- */
4003         (iw_handler) prism2_ioctl_siwgenie,             /* SIOCSIWGENIE */
4004         (iw_handler) prism2_ioctl_giwgenie,             /* SIOCGIWGENIE */
4005         (iw_handler) prism2_ioctl_siwauth,              /* SIOCSIWAUTH */
4006         (iw_handler) prism2_ioctl_giwauth,              /* SIOCGIWAUTH */
4007         (iw_handler) prism2_ioctl_siwencodeext,         /* SIOCSIWENCODEEXT */
4008         (iw_handler) prism2_ioctl_giwencodeext,         /* SIOCGIWENCODEEXT */
4009         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
4010         (iw_handler) NULL,                              /* -- hole -- */
4011 };
4012
4013 static const iw_handler prism2_private_handler[] =
4014 {                                                       /* SIOCIWFIRSTPRIV + */
4015         (iw_handler) prism2_ioctl_priv_prism2_param,    /* 0 */
4016         (iw_handler) prism2_ioctl_priv_get_prism2_param, /* 1 */
4017         (iw_handler) prism2_ioctl_priv_writemif,        /* 2 */
4018         (iw_handler) prism2_ioctl_priv_readmif,         /* 3 */
4019 };
4020
4021 static const struct iw_handler_def hostap_iw_handler_def =
4022 {
4023         .num_standard   = sizeof(prism2_handler) / sizeof(iw_handler),
4024         .num_private    = sizeof(prism2_private_handler) / sizeof(iw_handler),
4025         .num_private_args = sizeof(prism2_priv) / sizeof(struct iw_priv_args),
4026         .standard       = (iw_handler *) prism2_handler,
4027         .private        = (iw_handler *) prism2_private_handler,
4028         .private_args   = (struct iw_priv_args *) prism2_priv,
4029         .get_wireless_stats = hostap_get_wireless_stats,
4030 };
4031
4032
4033 int hostap_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4034 {
4035         struct iwreq *wrq = (struct iwreq *) ifr;
4036         struct hostap_interface *iface;
4037         local_info_t *local;
4038         int ret = 0;
4039
4040         iface = netdev_priv(dev);
4041         local = iface->local;
4042
4043         switch (cmd) {
4044                 /* Private ioctls (iwpriv) that have not yet been converted
4045                  * into new wireless extensions API */
4046
4047         case PRISM2_IOCTL_INQUIRE:
4048                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4049                 else ret = prism2_ioctl_priv_inquire(dev, (int *) wrq->u.name);
4050                 break;
4051
4052         case PRISM2_IOCTL_MONITOR:
4053                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4054                 else ret = prism2_ioctl_priv_monitor(dev, (int *) wrq->u.name);
4055                 break;
4056
4057         case PRISM2_IOCTL_RESET:
4058                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4059                 else ret = prism2_ioctl_priv_reset(dev, (int *) wrq->u.name);
4060                 break;
4061
4062         case PRISM2_IOCTL_WDS_ADD:
4063                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4064                 else ret = prism2_wds_add(local, wrq->u.ap_addr.sa_data, 1);
4065                 break;
4066
4067         case PRISM2_IOCTL_WDS_DEL:
4068                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4069                 else ret = prism2_wds_del(local, wrq->u.ap_addr.sa_data, 1, 0);
4070                 break;
4071
4072         case PRISM2_IOCTL_SET_RID_WORD:
4073                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4074                 else ret = prism2_ioctl_priv_set_rid_word(dev,
4075                                                           (int *) wrq->u.name);
4076                 break;
4077
4078 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
4079         case PRISM2_IOCTL_MACCMD:
4080                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4081                 else ret = ap_mac_cmd_ioctl(local, (int *) wrq->u.name);
4082                 break;
4083
4084         case PRISM2_IOCTL_ADDMAC:
4085                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4086                 else ret = ap_control_add_mac(&local->ap->mac_restrictions,
4087                                               wrq->u.ap_addr.sa_data);
4088                 break;
4089         case PRISM2_IOCTL_DELMAC:
4090                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4091                 else ret = ap_control_del_mac(&local->ap->mac_restrictions,
4092                                               wrq->u.ap_addr.sa_data);
4093                 break;
4094         case PRISM2_IOCTL_KICKMAC:
4095                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4096                 else ret = ap_control_kick_mac(local->ap, local->dev,
4097                                                wrq->u.ap_addr.sa_data);
4098                 break;
4099 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
4100
4101
4102                 /* Private ioctls that are not used with iwpriv;
4103                  * in SIOCDEVPRIVATE range */
4104
4105 #ifdef PRISM2_DOWNLOAD_SUPPORT
4106         case PRISM2_IOCTL_DOWNLOAD:
4107                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4108                 else ret = prism2_ioctl_priv_download(local, &wrq->u.data);
4109                 break;
4110 #endif /* PRISM2_DOWNLOAD_SUPPORT */
4111
4112         case PRISM2_IOCTL_HOSTAPD:
4113                 if (!capable(CAP_NET_ADMIN)) ret = -EPERM;
4114                 else ret = prism2_ioctl_priv_hostapd(local, &wrq->u.data);
4115                 break;
4116
4117         default:
4118                 ret = -EOPNOTSUPP;
4119                 break;
4120         }
4121
4122         return ret;
4123 }