orinoco: Change set_tkip to use orinoco_private instead of hermes_t
[safe/jmp/linux-2.6] / drivers / net / wireless / orinoco / wext.c
1 /* Wireless extensions support.
2  *
3  * See copyright notice in main.c
4  */
5 #include <linux/kernel.h>
6 #include <linux/if_arp.h>
7 #include <linux/wireless.h>
8 #include <linux/ieee80211.h>
9 #include <net/iw_handler.h>
10
11 #include "hermes.h"
12 #include "hermes_rid.h"
13 #include "orinoco.h"
14
15 #include "hw.h"
16 #include "mic.h"
17 #include "scan.h"
18 #include "main.h"
19
20 #include "wext.h"
21
22 #define MAX_RID_LEN 1024
23
24 static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
25 {
26         struct orinoco_private *priv = netdev_priv(dev);
27         hermes_t *hw = &priv->hw;
28         struct iw_statistics *wstats = &priv->wstats;
29         int err;
30         unsigned long flags;
31
32         if (!netif_device_present(dev)) {
33                 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
34                        dev->name);
35                 return NULL; /* FIXME: Can we do better than this? */
36         }
37
38         /* If busy, return the old stats.  Returning NULL may cause
39          * the interface to disappear from /proc/net/wireless */
40         if (orinoco_lock(priv, &flags) != 0)
41                 return wstats;
42
43         /* We can't really wait for the tallies inquiry command to
44          * complete, so we just use the previous results and trigger
45          * a new tallies inquiry command for next time - Jean II */
46         /* FIXME: Really we should wait for the inquiry to come back -
47          * as it is the stats we give don't make a whole lot of sense.
48          * Unfortunately, it's not clear how to do that within the
49          * wireless extensions framework: I think we're in user
50          * context, but a lock seems to be held by the time we get in
51          * here so we're not safe to sleep here. */
52         hermes_inquire(hw, HERMES_INQ_TALLIES);
53
54         if (priv->iw_mode == IW_MODE_ADHOC) {
55                 memset(&wstats->qual, 0, sizeof(wstats->qual));
56                 /* If a spy address is defined, we report stats of the
57                  * first spy address - Jean II */
58                 if (SPY_NUMBER(priv)) {
59                         wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
60                         wstats->qual.level = priv->spy_data.spy_stat[0].level;
61                         wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
62                         wstats->qual.updated =
63                                 priv->spy_data.spy_stat[0].updated;
64                 }
65         } else {
66                 struct {
67                         __le16 qual, signal, noise, unused;
68                 } __attribute__ ((packed)) cq;
69
70                 err = HERMES_READ_RECORD(hw, USER_BAP,
71                                          HERMES_RID_COMMSQUALITY, &cq);
72
73                 if (!err) {
74                         wstats->qual.qual = (int)le16_to_cpu(cq.qual);
75                         wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
76                         wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
77                         wstats->qual.updated =
78                                 IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
79                 }
80         }
81
82         orinoco_unlock(priv, &flags);
83         return wstats;
84 }
85
86 /********************************************************************/
87 /* Wireless extensions                                              */
88 /********************************************************************/
89
90 static int orinoco_ioctl_getname(struct net_device *dev,
91                                  struct iw_request_info *info,
92                                  char *name,
93                                  char *extra)
94 {
95         struct orinoco_private *priv = netdev_priv(dev);
96         int numrates;
97         int err;
98
99         err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
100
101         if (!err && (numrates > 2))
102                 strcpy(name, "IEEE 802.11b");
103         else
104                 strcpy(name, "IEEE 802.11-DS");
105
106         return 0;
107 }
108
109 static int orinoco_ioctl_setwap(struct net_device *dev,
110                                 struct iw_request_info *info,
111                                 struct sockaddr *ap_addr,
112                                 char *extra)
113 {
114         struct orinoco_private *priv = netdev_priv(dev);
115         int err = -EINPROGRESS;         /* Call commit handler */
116         unsigned long flags;
117         static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
118         static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
119
120         if (orinoco_lock(priv, &flags) != 0)
121                 return -EBUSY;
122
123         /* Enable automatic roaming - no sanity checks are needed */
124         if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
125             memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
126                 priv->bssid_fixed = 0;
127                 memset(priv->desired_bssid, 0, ETH_ALEN);
128
129                 /* "off" means keep existing connection */
130                 if (ap_addr->sa_data[0] == 0) {
131                         __orinoco_hw_set_wap(priv);
132                         err = 0;
133                 }
134                 goto out;
135         }
136
137         if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
138                 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
139                        "support manual roaming\n",
140                        dev->name);
141                 err = -EOPNOTSUPP;
142                 goto out;
143         }
144
145         if (priv->iw_mode != IW_MODE_INFRA) {
146                 printk(KERN_WARNING "%s: Manual roaming supported only in "
147                        "managed mode\n", dev->name);
148                 err = -EOPNOTSUPP;
149                 goto out;
150         }
151
152         /* Intersil firmware hangs without Desired ESSID */
153         if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
154             strlen(priv->desired_essid) == 0) {
155                 printk(KERN_WARNING "%s: Desired ESSID must be set for "
156                        "manual roaming\n", dev->name);
157                 err = -EOPNOTSUPP;
158                 goto out;
159         }
160
161         /* Finally, enable manual roaming */
162         priv->bssid_fixed = 1;
163         memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
164
165  out:
166         orinoco_unlock(priv, &flags);
167         return err;
168 }
169
170 static int orinoco_ioctl_getwap(struct net_device *dev,
171                                 struct iw_request_info *info,
172                                 struct sockaddr *ap_addr,
173                                 char *extra)
174 {
175         struct orinoco_private *priv = netdev_priv(dev);
176
177         hermes_t *hw = &priv->hw;
178         int err = 0;
179         unsigned long flags;
180
181         if (orinoco_lock(priv, &flags) != 0)
182                 return -EBUSY;
183
184         ap_addr->sa_family = ARPHRD_ETHER;
185         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
186                               ETH_ALEN, NULL, ap_addr->sa_data);
187
188         orinoco_unlock(priv, &flags);
189
190         return err;
191 }
192
193 static int orinoco_ioctl_setmode(struct net_device *dev,
194                                  struct iw_request_info *info,
195                                  u32 *mode,
196                                  char *extra)
197 {
198         struct orinoco_private *priv = netdev_priv(dev);
199         int err = -EINPROGRESS;         /* Call commit handler */
200         unsigned long flags;
201
202         if (priv->iw_mode == *mode)
203                 return 0;
204
205         if (orinoco_lock(priv, &flags) != 0)
206                 return -EBUSY;
207
208         switch (*mode) {
209         case IW_MODE_ADHOC:
210                 if (!priv->has_ibss && !priv->has_port3)
211                         err = -EOPNOTSUPP;
212                 break;
213
214         case IW_MODE_INFRA:
215                 break;
216
217         case IW_MODE_MONITOR:
218                 if (priv->broken_monitor && !force_monitor) {
219                         printk(KERN_WARNING "%s: Monitor mode support is "
220                                "buggy in this firmware, not enabling\n",
221                                dev->name);
222                         err = -EOPNOTSUPP;
223                 }
224                 break;
225
226         default:
227                 err = -EOPNOTSUPP;
228                 break;
229         }
230
231         if (err == -EINPROGRESS) {
232                 priv->iw_mode = *mode;
233                 set_port_type(priv);
234         }
235
236         orinoco_unlock(priv, &flags);
237
238         return err;
239 }
240
241 static int orinoco_ioctl_getmode(struct net_device *dev,
242                                  struct iw_request_info *info,
243                                  u32 *mode,
244                                  char *extra)
245 {
246         struct orinoco_private *priv = netdev_priv(dev);
247
248         *mode = priv->iw_mode;
249         return 0;
250 }
251
252 static int orinoco_ioctl_getiwrange(struct net_device *dev,
253                                     struct iw_request_info *info,
254                                     struct iw_point *rrq,
255                                     char *extra)
256 {
257         struct orinoco_private *priv = netdev_priv(dev);
258         int err = 0;
259         struct iw_range *range = (struct iw_range *) extra;
260         int numrates;
261         int i, k;
262
263         rrq->length = sizeof(struct iw_range);
264         memset(range, 0, sizeof(struct iw_range));
265
266         range->we_version_compiled = WIRELESS_EXT;
267         range->we_version_source = 22;
268
269         /* Set available channels/frequencies */
270         range->num_channels = NUM_CHANNELS;
271         k = 0;
272         for (i = 0; i < NUM_CHANNELS; i++) {
273                 if (priv->channel_mask & (1 << i)) {
274                         range->freq[k].i = i + 1;
275                         range->freq[k].m = (ieee80211_dsss_chan_to_freq(i + 1) *
276                                             100000);
277                         range->freq[k].e = 1;
278                         k++;
279                 }
280
281                 if (k >= IW_MAX_FREQUENCIES)
282                         break;
283         }
284         range->num_frequency = k;
285         range->sensitivity = 3;
286
287         if (priv->has_wep) {
288                 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
289                 range->encoding_size[0] = SMALL_KEY_SIZE;
290                 range->num_encoding_sizes = 1;
291
292                 if (priv->has_big_wep) {
293                         range->encoding_size[1] = LARGE_KEY_SIZE;
294                         range->num_encoding_sizes = 2;
295                 }
296         }
297
298         if (priv->has_wpa)
299                 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
300
301         if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))) {
302                 /* Quality stats meaningless in ad-hoc mode */
303         } else {
304                 range->max_qual.qual = 0x8b - 0x2f;
305                 range->max_qual.level = 0x2f - 0x95 - 1;
306                 range->max_qual.noise = 0x2f - 0x95 - 1;
307                 /* Need to get better values */
308                 range->avg_qual.qual = 0x24;
309                 range->avg_qual.level = 0xC2;
310                 range->avg_qual.noise = 0x9E;
311         }
312
313         err = orinoco_hw_get_bitratelist(priv, &numrates,
314                                          range->bitrate, IW_MAX_BITRATES);
315         if (err)
316                 return err;
317         range->num_bitrates = numrates;
318
319         /* Set an indication of the max TCP throughput in bit/s that we can
320          * expect using this interface. May be use for QoS stuff...
321          * Jean II */
322         if (numrates > 2)
323                 range->throughput = 5 * 1000 * 1000;    /* ~5 Mb/s */
324         else
325                 range->throughput = 1.5 * 1000 * 1000;  /* ~1.5 Mb/s */
326
327         range->min_rts = 0;
328         range->max_rts = 2347;
329         range->min_frag = 256;
330         range->max_frag = 2346;
331
332         range->min_pmp = 0;
333         range->max_pmp = 65535000;
334         range->min_pmt = 0;
335         range->max_pmt = 65535 * 1000;  /* ??? */
336         range->pmp_flags = IW_POWER_PERIOD;
337         range->pmt_flags = IW_POWER_TIMEOUT;
338         range->pm_capa = (IW_POWER_PERIOD | IW_POWER_TIMEOUT |
339                           IW_POWER_UNICAST_R);
340
341         range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
342         range->retry_flags = IW_RETRY_LIMIT;
343         range->r_time_flags = IW_RETRY_LIFETIME;
344         range->min_retry = 0;
345         range->max_retry = 65535;       /* ??? */
346         range->min_r_time = 0;
347         range->max_r_time = 65535 * 1000;       /* ??? */
348
349         if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
350                 range->scan_capa = IW_SCAN_CAPA_ESSID;
351         else
352                 range->scan_capa = IW_SCAN_CAPA_NONE;
353
354         /* Event capability (kernel) */
355         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
356         /* Event capability (driver) */
357         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
358         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
359         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
360         IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
361
362         return 0;
363 }
364
365 static int orinoco_ioctl_setiwencode(struct net_device *dev,
366                                      struct iw_request_info *info,
367                                      struct iw_point *erq,
368                                      char *keybuf)
369 {
370         struct orinoco_private *priv = netdev_priv(dev);
371         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
372         int setindex = priv->tx_key;
373         int encode_alg = priv->encode_alg;
374         int restricted = priv->wep_restrict;
375         u16 xlen = 0;
376         int err = -EINPROGRESS;         /* Call commit handler */
377         unsigned long flags;
378
379         if (!priv->has_wep)
380                 return -EOPNOTSUPP;
381
382         if (erq->pointer) {
383                 /* We actually have a key to set - check its length */
384                 if (erq->length > LARGE_KEY_SIZE)
385                         return -E2BIG;
386
387                 if ((erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep)
388                         return -E2BIG;
389         }
390
391         if (orinoco_lock(priv, &flags) != 0)
392                 return -EBUSY;
393
394         /* Clear any TKIP key we have */
395         if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
396                 (void) orinoco_clear_tkip_key(priv, setindex);
397
398         if (erq->length > 0) {
399                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
400                         index = priv->tx_key;
401
402                 /* Adjust key length to a supported value */
403                 if (erq->length > SMALL_KEY_SIZE)
404                         xlen = LARGE_KEY_SIZE;
405                 else if (erq->length > 0)
406                         xlen = SMALL_KEY_SIZE;
407                 else
408                         xlen = 0;
409
410                 /* Switch on WEP if off */
411                 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
412                         setindex = index;
413                         encode_alg = IW_ENCODE_ALG_WEP;
414                 }
415         } else {
416                 /* Important note : if the user do "iwconfig eth0 enc off",
417                  * we will arrive there with an index of -1. This is valid
418                  * but need to be taken care off... Jean II */
419                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
420                         if ((index != -1) || (erq->flags == 0)) {
421                                 err = -EINVAL;
422                                 goto out;
423                         }
424                 } else {
425                         /* Set the index : Check that the key is valid */
426                         if (priv->keys[index].len == 0) {
427                                 err = -EINVAL;
428                                 goto out;
429                         }
430                         setindex = index;
431                 }
432         }
433
434         if (erq->flags & IW_ENCODE_DISABLED)
435                 encode_alg = IW_ENCODE_ALG_NONE;
436         if (erq->flags & IW_ENCODE_OPEN)
437                 restricted = 0;
438         if (erq->flags & IW_ENCODE_RESTRICTED)
439                 restricted = 1;
440
441         if (erq->pointer && erq->length > 0) {
442                 priv->keys[index].len = cpu_to_le16(xlen);
443                 memset(priv->keys[index].data, 0,
444                        sizeof(priv->keys[index].data));
445                 memcpy(priv->keys[index].data, keybuf, erq->length);
446         }
447         priv->tx_key = setindex;
448
449         /* Try fast key change if connected and only keys are changed */
450         if ((priv->encode_alg == encode_alg) &&
451             (priv->wep_restrict == restricted) &&
452             netif_carrier_ok(dev)) {
453                 err = __orinoco_hw_setup_wepkeys(priv);
454                 /* No need to commit if successful */
455                 goto out;
456         }
457
458         priv->encode_alg = encode_alg;
459         priv->wep_restrict = restricted;
460
461  out:
462         orinoco_unlock(priv, &flags);
463
464         return err;
465 }
466
467 static int orinoco_ioctl_getiwencode(struct net_device *dev,
468                                      struct iw_request_info *info,
469                                      struct iw_point *erq,
470                                      char *keybuf)
471 {
472         struct orinoco_private *priv = netdev_priv(dev);
473         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
474         u16 xlen = 0;
475         unsigned long flags;
476
477         if (!priv->has_wep)
478                 return -EOPNOTSUPP;
479
480         if (orinoco_lock(priv, &flags) != 0)
481                 return -EBUSY;
482
483         if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
484                 index = priv->tx_key;
485
486         erq->flags = 0;
487         if (!priv->encode_alg)
488                 erq->flags |= IW_ENCODE_DISABLED;
489         erq->flags |= index + 1;
490
491         if (priv->wep_restrict)
492                 erq->flags |= IW_ENCODE_RESTRICTED;
493         else
494                 erq->flags |= IW_ENCODE_OPEN;
495
496         xlen = le16_to_cpu(priv->keys[index].len);
497
498         erq->length = xlen;
499
500         memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
501
502         orinoco_unlock(priv, &flags);
503         return 0;
504 }
505
506 static int orinoco_ioctl_setessid(struct net_device *dev,
507                                   struct iw_request_info *info,
508                                   struct iw_point *erq,
509                                   char *essidbuf)
510 {
511         struct orinoco_private *priv = netdev_priv(dev);
512         unsigned long flags;
513
514         /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
515          * anyway... - Jean II */
516
517         /* Hum... Should not use Wireless Extension constant (may change),
518          * should use our own... - Jean II */
519         if (erq->length > IW_ESSID_MAX_SIZE)
520                 return -E2BIG;
521
522         if (orinoco_lock(priv, &flags) != 0)
523                 return -EBUSY;
524
525         /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
526         memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
527
528         /* If not ANY, get the new ESSID */
529         if (erq->flags)
530                 memcpy(priv->desired_essid, essidbuf, erq->length);
531
532         orinoco_unlock(priv, &flags);
533
534         return -EINPROGRESS;            /* Call commit handler */
535 }
536
537 static int orinoco_ioctl_getessid(struct net_device *dev,
538                                   struct iw_request_info *info,
539                                   struct iw_point *erq,
540                                   char *essidbuf)
541 {
542         struct orinoco_private *priv = netdev_priv(dev);
543         int active;
544         int err = 0;
545         unsigned long flags;
546
547         if (netif_running(dev)) {
548                 err = orinoco_hw_get_essid(priv, &active, essidbuf);
549                 if (err < 0)
550                         return err;
551                 erq->length = err;
552         } else {
553                 if (orinoco_lock(priv, &flags) != 0)
554                         return -EBUSY;
555                 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
556                 erq->length = strlen(priv->desired_essid);
557                 orinoco_unlock(priv, &flags);
558         }
559
560         erq->flags = 1;
561
562         return 0;
563 }
564
565 static int orinoco_ioctl_setnick(struct net_device *dev,
566                                  struct iw_request_info *info,
567                                  struct iw_point *nrq,
568                                  char *nickbuf)
569 {
570         struct orinoco_private *priv = netdev_priv(dev);
571         unsigned long flags;
572
573         if (nrq->length > IW_ESSID_MAX_SIZE)
574                 return -E2BIG;
575
576         if (orinoco_lock(priv, &flags) != 0)
577                 return -EBUSY;
578
579         memset(priv->nick, 0, sizeof(priv->nick));
580         memcpy(priv->nick, nickbuf, nrq->length);
581
582         orinoco_unlock(priv, &flags);
583
584         return -EINPROGRESS;            /* Call commit handler */
585 }
586
587 static int orinoco_ioctl_getnick(struct net_device *dev,
588                                  struct iw_request_info *info,
589                                  struct iw_point *nrq,
590                                  char *nickbuf)
591 {
592         struct orinoco_private *priv = netdev_priv(dev);
593         unsigned long flags;
594
595         if (orinoco_lock(priv, &flags) != 0)
596                 return -EBUSY;
597
598         memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
599         orinoco_unlock(priv, &flags);
600
601         nrq->length = strlen(priv->nick);
602
603         return 0;
604 }
605
606 static int orinoco_ioctl_setfreq(struct net_device *dev,
607                                  struct iw_request_info *info,
608                                  struct iw_freq *frq,
609                                  char *extra)
610 {
611         struct orinoco_private *priv = netdev_priv(dev);
612         int chan = -1;
613         unsigned long flags;
614         int err = -EINPROGRESS;         /* Call commit handler */
615
616         /* In infrastructure mode the AP sets the channel */
617         if (priv->iw_mode == IW_MODE_INFRA)
618                 return -EBUSY;
619
620         if ((frq->e == 0) && (frq->m <= 1000)) {
621                 /* Setting by channel number */
622                 chan = frq->m;
623         } else {
624                 /* Setting by frequency */
625                 int denom = 1;
626                 int i;
627
628                 /* Calculate denominator to rescale to MHz */
629                 for (i = 0; i < (6 - frq->e); i++)
630                         denom *= 10;
631
632                 chan = ieee80211_freq_to_dsss_chan(frq->m / denom);
633         }
634
635         if ((chan < 1) || (chan > NUM_CHANNELS) ||
636              !(priv->channel_mask & (1 << (chan-1))))
637                 return -EINVAL;
638
639         if (orinoco_lock(priv, &flags) != 0)
640                 return -EBUSY;
641
642         priv->channel = chan;
643         if (priv->iw_mode == IW_MODE_MONITOR) {
644                 /* Fast channel change - no commit if successful */
645                 hermes_t *hw = &priv->hw;
646                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
647                                             HERMES_TEST_SET_CHANNEL,
648                                         chan, NULL);
649         }
650         orinoco_unlock(priv, &flags);
651
652         return err;
653 }
654
655 static int orinoco_ioctl_getfreq(struct net_device *dev,
656                                  struct iw_request_info *info,
657                                  struct iw_freq *frq,
658                                  char *extra)
659 {
660         struct orinoco_private *priv = netdev_priv(dev);
661         int tmp;
662
663         /* Locking done in there */
664         tmp = orinoco_hw_get_freq(priv);
665         if (tmp < 0)
666                 return tmp;
667
668         frq->m = tmp * 100000;
669         frq->e = 1;
670
671         return 0;
672 }
673
674 static int orinoco_ioctl_getsens(struct net_device *dev,
675                                  struct iw_request_info *info,
676                                  struct iw_param *srq,
677                                  char *extra)
678 {
679         struct orinoco_private *priv = netdev_priv(dev);
680         hermes_t *hw = &priv->hw;
681         u16 val;
682         int err;
683         unsigned long flags;
684
685         if (!priv->has_sensitivity)
686                 return -EOPNOTSUPP;
687
688         if (orinoco_lock(priv, &flags) != 0)
689                 return -EBUSY;
690         err = hermes_read_wordrec(hw, USER_BAP,
691                                   HERMES_RID_CNFSYSTEMSCALE, &val);
692         orinoco_unlock(priv, &flags);
693
694         if (err)
695                 return err;
696
697         srq->value = val;
698         srq->fixed = 0; /* auto */
699
700         return 0;
701 }
702
703 static int orinoco_ioctl_setsens(struct net_device *dev,
704                                  struct iw_request_info *info,
705                                  struct iw_param *srq,
706                                  char *extra)
707 {
708         struct orinoco_private *priv = netdev_priv(dev);
709         int val = srq->value;
710         unsigned long flags;
711
712         if (!priv->has_sensitivity)
713                 return -EOPNOTSUPP;
714
715         if ((val < 1) || (val > 3))
716                 return -EINVAL;
717
718         if (orinoco_lock(priv, &flags) != 0)
719                 return -EBUSY;
720         priv->ap_density = val;
721         orinoco_unlock(priv, &flags);
722
723         return -EINPROGRESS;            /* Call commit handler */
724 }
725
726 static int orinoco_ioctl_setrts(struct net_device *dev,
727                                 struct iw_request_info *info,
728                                 struct iw_param *rrq,
729                                 char *extra)
730 {
731         struct orinoco_private *priv = netdev_priv(dev);
732         int val = rrq->value;
733         unsigned long flags;
734
735         if (rrq->disabled)
736                 val = 2347;
737
738         if ((val < 0) || (val > 2347))
739                 return -EINVAL;
740
741         if (orinoco_lock(priv, &flags) != 0)
742                 return -EBUSY;
743
744         priv->rts_thresh = val;
745         orinoco_unlock(priv, &flags);
746
747         return -EINPROGRESS;            /* Call commit handler */
748 }
749
750 static int orinoco_ioctl_getrts(struct net_device *dev,
751                                 struct iw_request_info *info,
752                                 struct iw_param *rrq,
753                                 char *extra)
754 {
755         struct orinoco_private *priv = netdev_priv(dev);
756
757         rrq->value = priv->rts_thresh;
758         rrq->disabled = (rrq->value == 2347);
759         rrq->fixed = 1;
760
761         return 0;
762 }
763
764 static int orinoco_ioctl_setfrag(struct net_device *dev,
765                                  struct iw_request_info *info,
766                                  struct iw_param *frq,
767                                  char *extra)
768 {
769         struct orinoco_private *priv = netdev_priv(dev);
770         int err = -EINPROGRESS;         /* Call commit handler */
771         unsigned long flags;
772
773         if (orinoco_lock(priv, &flags) != 0)
774                 return -EBUSY;
775
776         if (priv->has_mwo) {
777                 if (frq->disabled)
778                         priv->mwo_robust = 0;
779                 else {
780                         if (frq->fixed)
781                                 printk(KERN_WARNING "%s: Fixed fragmentation "
782                                        "is not supported on this firmware. "
783                                        "Using MWO robust instead.\n",
784                                        dev->name);
785                         priv->mwo_robust = 1;
786                 }
787         } else {
788                 if (frq->disabled)
789                         priv->frag_thresh = 2346;
790                 else {
791                         if ((frq->value < 256) || (frq->value > 2346))
792                                 err = -EINVAL;
793                         else
794                                 /* must be even */
795                                 priv->frag_thresh = frq->value & ~0x1;
796                 }
797         }
798
799         orinoco_unlock(priv, &flags);
800
801         return err;
802 }
803
804 static int orinoco_ioctl_getfrag(struct net_device *dev,
805                                  struct iw_request_info *info,
806                                  struct iw_param *frq,
807                                  char *extra)
808 {
809         struct orinoco_private *priv = netdev_priv(dev);
810         hermes_t *hw = &priv->hw;
811         int err;
812         u16 val;
813         unsigned long flags;
814
815         if (orinoco_lock(priv, &flags) != 0)
816                 return -EBUSY;
817
818         if (priv->has_mwo) {
819                 err = hermes_read_wordrec(hw, USER_BAP,
820                                           HERMES_RID_CNFMWOROBUST_AGERE,
821                                           &val);
822                 if (err)
823                         val = 0;
824
825                 frq->value = val ? 2347 : 0;
826                 frq->disabled = !val;
827                 frq->fixed = 0;
828         } else {
829                 err = hermes_read_wordrec(hw, USER_BAP,
830                                           HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
831                                           &val);
832                 if (err)
833                         val = 0;
834
835                 frq->value = val;
836                 frq->disabled = (val >= 2346);
837                 frq->fixed = 1;
838         }
839
840         orinoco_unlock(priv, &flags);
841
842         return err;
843 }
844
845 static int orinoco_ioctl_setrate(struct net_device *dev,
846                                  struct iw_request_info *info,
847                                  struct iw_param *rrq,
848                                  char *extra)
849 {
850         struct orinoco_private *priv = netdev_priv(dev);
851         int ratemode;
852         int bitrate; /* 100s of kilobits */
853         unsigned long flags;
854
855         /* As the user space doesn't know our highest rate, it uses -1
856          * to ask us to set the highest rate.  Test it using "iwconfig
857          * ethX rate auto" - Jean II */
858         if (rrq->value == -1)
859                 bitrate = 110;
860         else {
861                 if (rrq->value % 100000)
862                         return -EINVAL;
863                 bitrate = rrq->value / 100000;
864         }
865
866         ratemode = orinoco_get_bitratemode(bitrate, !rrq->fixed);
867
868         if (ratemode == -1)
869                 return -EINVAL;
870
871         if (orinoco_lock(priv, &flags) != 0)
872                 return -EBUSY;
873         priv->bitratemode = ratemode;
874         orinoco_unlock(priv, &flags);
875
876         return -EINPROGRESS;
877 }
878
879 static int orinoco_ioctl_getrate(struct net_device *dev,
880                                  struct iw_request_info *info,
881                                  struct iw_param *rrq,
882                                  char *extra)
883 {
884         struct orinoco_private *priv = netdev_priv(dev);
885         int err = 0;
886         int bitrate, automatic;
887         unsigned long flags;
888
889         if (orinoco_lock(priv, &flags) != 0)
890                 return -EBUSY;
891
892         orinoco_get_ratemode_cfg(priv->bitratemode, &bitrate, &automatic);
893
894         /* If the interface is running we try to find more about the
895            current mode */
896         if (netif_running(dev))
897                 err = orinoco_hw_get_act_bitrate(priv, &bitrate);
898
899         orinoco_unlock(priv, &flags);
900
901         rrq->value = bitrate;
902         rrq->fixed = !automatic;
903         rrq->disabled = 0;
904
905         return err;
906 }
907
908 static int orinoco_ioctl_setpower(struct net_device *dev,
909                                   struct iw_request_info *info,
910                                   struct iw_param *prq,
911                                   char *extra)
912 {
913         struct orinoco_private *priv = netdev_priv(dev);
914         int err = -EINPROGRESS;         /* Call commit handler */
915         unsigned long flags;
916
917         if (orinoco_lock(priv, &flags) != 0)
918                 return -EBUSY;
919
920         if (prq->disabled) {
921                 priv->pm_on = 0;
922         } else {
923                 switch (prq->flags & IW_POWER_MODE) {
924                 case IW_POWER_UNICAST_R:
925                         priv->pm_mcast = 0;
926                         priv->pm_on = 1;
927                         break;
928                 case IW_POWER_ALL_R:
929                         priv->pm_mcast = 1;
930                         priv->pm_on = 1;
931                         break;
932                 case IW_POWER_ON:
933                         /* No flags : but we may have a value - Jean II */
934                         break;
935                 default:
936                         err = -EINVAL;
937                         goto out;
938                 }
939
940                 if (prq->flags & IW_POWER_TIMEOUT) {
941                         priv->pm_on = 1;
942                         priv->pm_timeout = prq->value / 1000;
943                 }
944                 if (prq->flags & IW_POWER_PERIOD) {
945                         priv->pm_on = 1;
946                         priv->pm_period = prq->value / 1000;
947                 }
948                 /* It's valid to not have a value if we are just toggling
949                  * the flags... Jean II */
950                 if (!priv->pm_on) {
951                         err = -EINVAL;
952                         goto out;
953                 }
954         }
955
956  out:
957         orinoco_unlock(priv, &flags);
958
959         return err;
960 }
961
962 static int orinoco_ioctl_getpower(struct net_device *dev,
963                                   struct iw_request_info *info,
964                                   struct iw_param *prq,
965                                   char *extra)
966 {
967         struct orinoco_private *priv = netdev_priv(dev);
968         hermes_t *hw = &priv->hw;
969         int err = 0;
970         u16 enable, period, timeout, mcast;
971         unsigned long flags;
972
973         if (orinoco_lock(priv, &flags) != 0)
974                 return -EBUSY;
975
976         err = hermes_read_wordrec(hw, USER_BAP,
977                                   HERMES_RID_CNFPMENABLED, &enable);
978         if (err)
979                 goto out;
980
981         err = hermes_read_wordrec(hw, USER_BAP,
982                                   HERMES_RID_CNFMAXSLEEPDURATION, &period);
983         if (err)
984                 goto out;
985
986         err = hermes_read_wordrec(hw, USER_BAP,
987                                   HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
988         if (err)
989                 goto out;
990
991         err = hermes_read_wordrec(hw, USER_BAP,
992                                   HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
993         if (err)
994                 goto out;
995
996         prq->disabled = !enable;
997         /* Note : by default, display the period */
998         if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
999                 prq->flags = IW_POWER_TIMEOUT;
1000                 prq->value = timeout * 1000;
1001         } else {
1002                 prq->flags = IW_POWER_PERIOD;
1003                 prq->value = period * 1000;
1004         }
1005         if (mcast)
1006                 prq->flags |= IW_POWER_ALL_R;
1007         else
1008                 prq->flags |= IW_POWER_UNICAST_R;
1009
1010  out:
1011         orinoco_unlock(priv, &flags);
1012
1013         return err;
1014 }
1015
1016 static int orinoco_ioctl_set_encodeext(struct net_device *dev,
1017                                        struct iw_request_info *info,
1018                                        union iwreq_data *wrqu,
1019                                        char *extra)
1020 {
1021         struct orinoco_private *priv = netdev_priv(dev);
1022         struct iw_point *encoding = &wrqu->encoding;
1023         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1024         int idx, alg = ext->alg, set_key = 1;
1025         unsigned long flags;
1026         int err = -EINVAL;
1027         u16 key_len;
1028
1029         if (orinoco_lock(priv, &flags) != 0)
1030                 return -EBUSY;
1031
1032         /* Determine and validate the key index */
1033         idx = encoding->flags & IW_ENCODE_INDEX;
1034         if (idx) {
1035                 if ((idx < 1) || (idx > 4))
1036                         goto out;
1037                 idx--;
1038         } else
1039                 idx = priv->tx_key;
1040
1041         if (encoding->flags & IW_ENCODE_DISABLED)
1042                 alg = IW_ENCODE_ALG_NONE;
1043
1044         if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
1045                 /* Clear any TKIP TX key we had */
1046                 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
1047         }
1048
1049         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
1050                 priv->tx_key = idx;
1051                 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
1052                            (ext->key_len > 0)) ? 1 : 0;
1053         }
1054
1055         if (set_key) {
1056                 /* Set the requested key first */
1057                 switch (alg) {
1058                 case IW_ENCODE_ALG_NONE:
1059                         priv->encode_alg = alg;
1060                         priv->keys[idx].len = 0;
1061                         break;
1062
1063                 case IW_ENCODE_ALG_WEP:
1064                         if (ext->key_len > SMALL_KEY_SIZE)
1065                                 key_len = LARGE_KEY_SIZE;
1066                         else if (ext->key_len > 0)
1067                                 key_len = SMALL_KEY_SIZE;
1068                         else
1069                                 goto out;
1070
1071                         priv->encode_alg = alg;
1072                         priv->keys[idx].len = cpu_to_le16(key_len);
1073
1074                         key_len = min(ext->key_len, key_len);
1075
1076                         memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
1077                         memcpy(priv->keys[idx].data, ext->key, key_len);
1078                         break;
1079
1080                 case IW_ENCODE_ALG_TKIP:
1081                 {
1082                         u8 *tkip_iv = NULL;
1083
1084                         if (!priv->has_wpa ||
1085                             (ext->key_len > sizeof(priv->tkip_key[0])))
1086                                 goto out;
1087
1088                         priv->encode_alg = alg;
1089                         memset(&priv->tkip_key[idx], 0,
1090                                sizeof(priv->tkip_key[idx]));
1091                         memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
1092
1093                         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
1094                                 tkip_iv = &ext->rx_seq[0];
1095
1096                         err = __orinoco_hw_set_tkip_key(priv, idx,
1097                                  ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
1098                                  (u8 *) &priv->tkip_key[idx],
1099                                  tkip_iv, NULL);
1100                         if (err)
1101                                 printk(KERN_ERR "%s: Error %d setting TKIP key"
1102                                        "\n", dev->name, err);
1103
1104                         goto out;
1105                 }
1106                 default:
1107                         goto out;
1108                 }
1109         }
1110         err = -EINPROGRESS;
1111  out:
1112         orinoco_unlock(priv, &flags);
1113
1114         return err;
1115 }
1116
1117 static int orinoco_ioctl_get_encodeext(struct net_device *dev,
1118                                        struct iw_request_info *info,
1119                                        union iwreq_data *wrqu,
1120                                        char *extra)
1121 {
1122         struct orinoco_private *priv = netdev_priv(dev);
1123         struct iw_point *encoding = &wrqu->encoding;
1124         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1125         int idx, max_key_len;
1126         unsigned long flags;
1127         int err;
1128
1129         if (orinoco_lock(priv, &flags) != 0)
1130                 return -EBUSY;
1131
1132         err = -EINVAL;
1133         max_key_len = encoding->length - sizeof(*ext);
1134         if (max_key_len < 0)
1135                 goto out;
1136
1137         idx = encoding->flags & IW_ENCODE_INDEX;
1138         if (idx) {
1139                 if ((idx < 1) || (idx > 4))
1140                         goto out;
1141                 idx--;
1142         } else
1143                 idx = priv->tx_key;
1144
1145         encoding->flags = idx + 1;
1146         memset(ext, 0, sizeof(*ext));
1147
1148         ext->alg = priv->encode_alg;
1149         switch (priv->encode_alg) {
1150         case IW_ENCODE_ALG_NONE:
1151                 ext->key_len = 0;
1152                 encoding->flags |= IW_ENCODE_DISABLED;
1153                 break;
1154         case IW_ENCODE_ALG_WEP:
1155                 ext->key_len = min_t(u16, le16_to_cpu(priv->keys[idx].len),
1156                                      max_key_len);
1157                 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
1158                 encoding->flags |= IW_ENCODE_ENABLED;
1159                 break;
1160         case IW_ENCODE_ALG_TKIP:
1161                 ext->key_len = min_t(u16, sizeof(struct orinoco_tkip_key),
1162                                      max_key_len);
1163                 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
1164                 encoding->flags |= IW_ENCODE_ENABLED;
1165                 break;
1166         }
1167
1168         err = 0;
1169  out:
1170         orinoco_unlock(priv, &flags);
1171
1172         return err;
1173 }
1174
1175 static int orinoco_ioctl_set_auth(struct net_device *dev,
1176                                   struct iw_request_info *info,
1177                                   union iwreq_data *wrqu, char *extra)
1178 {
1179         struct orinoco_private *priv = netdev_priv(dev);
1180         hermes_t *hw = &priv->hw;
1181         struct iw_param *param = &wrqu->param;
1182         unsigned long flags;
1183         int ret = -EINPROGRESS;
1184
1185         if (orinoco_lock(priv, &flags) != 0)
1186                 return -EBUSY;
1187
1188         switch (param->flags & IW_AUTH_INDEX) {
1189         case IW_AUTH_WPA_VERSION:
1190         case IW_AUTH_CIPHER_PAIRWISE:
1191         case IW_AUTH_CIPHER_GROUP:
1192         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1193         case IW_AUTH_PRIVACY_INVOKED:
1194         case IW_AUTH_DROP_UNENCRYPTED:
1195                 /*
1196                  * orinoco does not use these parameters
1197                  */
1198                 break;
1199
1200         case IW_AUTH_KEY_MGMT:
1201                 /* wl_lkm implies value 2 == PSK for Hermes I
1202                  * which ties in with WEXT
1203                  * no other hints tho :(
1204                  */
1205                 priv->key_mgmt = param->value;
1206                 break;
1207
1208         case IW_AUTH_TKIP_COUNTERMEASURES:
1209                 /* When countermeasures are enabled, shut down the
1210                  * card; when disabled, re-enable the card. This must
1211                  * take effect immediately.
1212                  *
1213                  * TODO: Make sure that the EAPOL message is getting
1214                  *       out before card disabled
1215                  */
1216                 if (param->value) {
1217                         priv->tkip_cm_active = 1;
1218                         ret = hermes_enable_port(hw, 0);
1219                 } else {
1220                         priv->tkip_cm_active = 0;
1221                         ret = hermes_disable_port(hw, 0);
1222                 }
1223                 break;
1224
1225         case IW_AUTH_80211_AUTH_ALG:
1226                 if (param->value & IW_AUTH_ALG_SHARED_KEY)
1227                         priv->wep_restrict = 1;
1228                 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
1229                         priv->wep_restrict = 0;
1230                 else
1231                         ret = -EINVAL;
1232                 break;
1233
1234         case IW_AUTH_WPA_ENABLED:
1235                 if (priv->has_wpa) {
1236                         priv->wpa_enabled = param->value ? 1 : 0;
1237                 } else {
1238                         if (param->value)
1239                                 ret = -EOPNOTSUPP;
1240                         /* else silently accept disable of WPA */
1241                         priv->wpa_enabled = 0;
1242                 }
1243                 break;
1244
1245         default:
1246                 ret = -EOPNOTSUPP;
1247         }
1248
1249         orinoco_unlock(priv, &flags);
1250         return ret;
1251 }
1252
1253 static int orinoco_ioctl_get_auth(struct net_device *dev,
1254                                   struct iw_request_info *info,
1255                                   union iwreq_data *wrqu, char *extra)
1256 {
1257         struct orinoco_private *priv = netdev_priv(dev);
1258         struct iw_param *param = &wrqu->param;
1259         unsigned long flags;
1260         int ret = 0;
1261
1262         if (orinoco_lock(priv, &flags) != 0)
1263                 return -EBUSY;
1264
1265         switch (param->flags & IW_AUTH_INDEX) {
1266         case IW_AUTH_KEY_MGMT:
1267                 param->value = priv->key_mgmt;
1268                 break;
1269
1270         case IW_AUTH_TKIP_COUNTERMEASURES:
1271                 param->value = priv->tkip_cm_active;
1272                 break;
1273
1274         case IW_AUTH_80211_AUTH_ALG:
1275                 if (priv->wep_restrict)
1276                         param->value = IW_AUTH_ALG_SHARED_KEY;
1277                 else
1278                         param->value = IW_AUTH_ALG_OPEN_SYSTEM;
1279                 break;
1280
1281         case IW_AUTH_WPA_ENABLED:
1282                 param->value = priv->wpa_enabled;
1283                 break;
1284
1285         default:
1286                 ret = -EOPNOTSUPP;
1287         }
1288
1289         orinoco_unlock(priv, &flags);
1290         return ret;
1291 }
1292
1293 static int orinoco_ioctl_set_genie(struct net_device *dev,
1294                                    struct iw_request_info *info,
1295                                    union iwreq_data *wrqu, char *extra)
1296 {
1297         struct orinoco_private *priv = netdev_priv(dev);
1298         u8 *buf;
1299         unsigned long flags;
1300
1301         /* cut off at IEEE80211_MAX_DATA_LEN */
1302         if ((wrqu->data.length > IEEE80211_MAX_DATA_LEN) ||
1303             (wrqu->data.length && (extra == NULL)))
1304                 return -EINVAL;
1305
1306         if (wrqu->data.length) {
1307                 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
1308                 if (buf == NULL)
1309                         return -ENOMEM;
1310
1311                 memcpy(buf, extra, wrqu->data.length);
1312         } else
1313                 buf = NULL;
1314
1315         if (orinoco_lock(priv, &flags) != 0) {
1316                 kfree(buf);
1317                 return -EBUSY;
1318         }
1319
1320         kfree(priv->wpa_ie);
1321         priv->wpa_ie = buf;
1322         priv->wpa_ie_len = wrqu->data.length;
1323
1324         if (priv->wpa_ie) {
1325                 /* Looks like wl_lkm wants to check the auth alg, and
1326                  * somehow pass it to the firmware.
1327                  * Instead it just calls the key mgmt rid
1328                  *   - we do this in set auth.
1329                  */
1330         }
1331
1332         orinoco_unlock(priv, &flags);
1333         return 0;
1334 }
1335
1336 static int orinoco_ioctl_get_genie(struct net_device *dev,
1337                                    struct iw_request_info *info,
1338                                    union iwreq_data *wrqu, char *extra)
1339 {
1340         struct orinoco_private *priv = netdev_priv(dev);
1341         unsigned long flags;
1342         int err = 0;
1343
1344         if (orinoco_lock(priv, &flags) != 0)
1345                 return -EBUSY;
1346
1347         if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
1348                 wrqu->data.length = 0;
1349                 goto out;
1350         }
1351
1352         if (wrqu->data.length < priv->wpa_ie_len) {
1353                 err = -E2BIG;
1354                 goto out;
1355         }
1356
1357         wrqu->data.length = priv->wpa_ie_len;
1358         memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1359
1360 out:
1361         orinoco_unlock(priv, &flags);
1362         return err;
1363 }
1364
1365 static int orinoco_ioctl_set_mlme(struct net_device *dev,
1366                                   struct iw_request_info *info,
1367                                   union iwreq_data *wrqu, char *extra)
1368 {
1369         struct orinoco_private *priv = netdev_priv(dev);
1370         hermes_t *hw = &priv->hw;
1371         struct iw_mlme *mlme = (struct iw_mlme *)extra;
1372         unsigned long flags;
1373         int ret = 0;
1374
1375         if (orinoco_lock(priv, &flags) != 0)
1376                 return -EBUSY;
1377
1378         switch (mlme->cmd) {
1379         case IW_MLME_DEAUTH:
1380                 /* silently ignore */
1381                 break;
1382
1383         case IW_MLME_DISASSOC:
1384         {
1385                 struct {
1386                         u8 addr[ETH_ALEN];
1387                         __le16 reason_code;
1388                 } __attribute__ ((packed)) buf;
1389
1390                 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
1391                 buf.reason_code = cpu_to_le16(mlme->reason_code);
1392                 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
1393                                           HERMES_RID_CNFDISASSOCIATE,
1394                                           &buf);
1395                 break;
1396         }
1397         default:
1398                 ret = -EOPNOTSUPP;
1399         }
1400
1401         orinoco_unlock(priv, &flags);
1402         return ret;
1403 }
1404
1405 static int orinoco_ioctl_getretry(struct net_device *dev,
1406                                   struct iw_request_info *info,
1407                                   struct iw_param *rrq,
1408                                   char *extra)
1409 {
1410         struct orinoco_private *priv = netdev_priv(dev);
1411         hermes_t *hw = &priv->hw;
1412         int err = 0;
1413         u16 short_limit, long_limit, lifetime;
1414         unsigned long flags;
1415
1416         if (orinoco_lock(priv, &flags) != 0)
1417                 return -EBUSY;
1418
1419         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
1420                                   &short_limit);
1421         if (err)
1422                 goto out;
1423
1424         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
1425                                   &long_limit);
1426         if (err)
1427                 goto out;
1428
1429         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
1430                                   &lifetime);
1431         if (err)
1432                 goto out;
1433
1434         rrq->disabled = 0;              /* Can't be disabled */
1435
1436         /* Note : by default, display the retry number */
1437         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
1438                 rrq->flags = IW_RETRY_LIFETIME;
1439                 rrq->value = lifetime * 1000;   /* ??? */
1440         } else {
1441                 /* By default, display the min number */
1442                 if ((rrq->flags & IW_RETRY_LONG)) {
1443                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
1444                         rrq->value = long_limit;
1445                 } else {
1446                         rrq->flags = IW_RETRY_LIMIT;
1447                         rrq->value = short_limit;
1448                         if (short_limit != long_limit)
1449                                 rrq->flags |= IW_RETRY_SHORT;
1450                 }
1451         }
1452
1453  out:
1454         orinoco_unlock(priv, &flags);
1455
1456         return err;
1457 }
1458
1459 static int orinoco_ioctl_reset(struct net_device *dev,
1460                                struct iw_request_info *info,
1461                                void *wrqu,
1462                                char *extra)
1463 {
1464         struct orinoco_private *priv = netdev_priv(dev);
1465
1466         if (!capable(CAP_NET_ADMIN))
1467                 return -EPERM;
1468
1469         if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
1470                 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
1471
1472                 /* Firmware reset */
1473                 orinoco_reset(&priv->reset_work);
1474         } else {
1475                 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
1476
1477                 schedule_work(&priv->reset_work);
1478         }
1479
1480         return 0;
1481 }
1482
1483 static int orinoco_ioctl_setibssport(struct net_device *dev,
1484                                      struct iw_request_info *info,
1485                                      void *wrqu,
1486                                      char *extra)
1487
1488 {
1489         struct orinoco_private *priv = netdev_priv(dev);
1490         int val = *((int *) extra);
1491         unsigned long flags;
1492
1493         if (orinoco_lock(priv, &flags) != 0)
1494                 return -EBUSY;
1495
1496         priv->ibss_port = val ;
1497
1498         /* Actually update the mode we are using */
1499         set_port_type(priv);
1500
1501         orinoco_unlock(priv, &flags);
1502         return -EINPROGRESS;            /* Call commit handler */
1503 }
1504
1505 static int orinoco_ioctl_getibssport(struct net_device *dev,
1506                                      struct iw_request_info *info,
1507                                      void *wrqu,
1508                                      char *extra)
1509 {
1510         struct orinoco_private *priv = netdev_priv(dev);
1511         int *val = (int *) extra;
1512
1513         *val = priv->ibss_port;
1514         return 0;
1515 }
1516
1517 static int orinoco_ioctl_setport3(struct net_device *dev,
1518                                   struct iw_request_info *info,
1519                                   void *wrqu,
1520                                   char *extra)
1521 {
1522         struct orinoco_private *priv = netdev_priv(dev);
1523         int val = *((int *) extra);
1524         int err = 0;
1525         unsigned long flags;
1526
1527         if (orinoco_lock(priv, &flags) != 0)
1528                 return -EBUSY;
1529
1530         switch (val) {
1531         case 0: /* Try to do IEEE ad-hoc mode */
1532                 if (!priv->has_ibss) {
1533                         err = -EINVAL;
1534                         break;
1535                 }
1536                 priv->prefer_port3 = 0;
1537
1538                 break;
1539
1540         case 1: /* Try to do Lucent proprietary ad-hoc mode */
1541                 if (!priv->has_port3) {
1542                         err = -EINVAL;
1543                         break;
1544                 }
1545                 priv->prefer_port3 = 1;
1546                 break;
1547
1548         default:
1549                 err = -EINVAL;
1550         }
1551
1552         if (!err) {
1553                 /* Actually update the mode we are using */
1554                 set_port_type(priv);
1555                 err = -EINPROGRESS;
1556         }
1557
1558         orinoco_unlock(priv, &flags);
1559
1560         return err;
1561 }
1562
1563 static int orinoco_ioctl_getport3(struct net_device *dev,
1564                                   struct iw_request_info *info,
1565                                   void *wrqu,
1566                                   char *extra)
1567 {
1568         struct orinoco_private *priv = netdev_priv(dev);
1569         int *val = (int *) extra;
1570
1571         *val = priv->prefer_port3;
1572         return 0;
1573 }
1574
1575 static int orinoco_ioctl_setpreamble(struct net_device *dev,
1576                                      struct iw_request_info *info,
1577                                      void *wrqu,
1578                                      char *extra)
1579 {
1580         struct orinoco_private *priv = netdev_priv(dev);
1581         unsigned long flags;
1582         int val;
1583
1584         if (!priv->has_preamble)
1585                 return -EOPNOTSUPP;
1586
1587         /* 802.11b has recently defined some short preamble.
1588          * Basically, the Phy header has been reduced in size.
1589          * This increase performance, especially at high rates
1590          * (the preamble is transmitted at 1Mb/s), unfortunately
1591          * this give compatibility troubles... - Jean II */
1592         val = *((int *) extra);
1593
1594         if (orinoco_lock(priv, &flags) != 0)
1595                 return -EBUSY;
1596
1597         if (val)
1598                 priv->preamble = 1;
1599         else
1600                 priv->preamble = 0;
1601
1602         orinoco_unlock(priv, &flags);
1603
1604         return -EINPROGRESS;            /* Call commit handler */
1605 }
1606
1607 static int orinoco_ioctl_getpreamble(struct net_device *dev,
1608                                      struct iw_request_info *info,
1609                                      void *wrqu,
1610                                      char *extra)
1611 {
1612         struct orinoco_private *priv = netdev_priv(dev);
1613         int *val = (int *) extra;
1614
1615         if (!priv->has_preamble)
1616                 return -EOPNOTSUPP;
1617
1618         *val = priv->preamble;
1619         return 0;
1620 }
1621
1622 /* ioctl interface to hermes_read_ltv()
1623  * To use with iwpriv, pass the RID as the token argument, e.g.
1624  * iwpriv get_rid [0xfc00]
1625  * At least Wireless Tools 25 is required to use iwpriv.
1626  * For Wireless Tools 25 and 26 append "dummy" are the end. */
1627 static int orinoco_ioctl_getrid(struct net_device *dev,
1628                                 struct iw_request_info *info,
1629                                 struct iw_point *data,
1630                                 char *extra)
1631 {
1632         struct orinoco_private *priv = netdev_priv(dev);
1633         hermes_t *hw = &priv->hw;
1634         int rid = data->flags;
1635         u16 length;
1636         int err;
1637         unsigned long flags;
1638
1639         /* It's a "get" function, but we don't want users to access the
1640          * WEP key and other raw firmware data */
1641         if (!capable(CAP_NET_ADMIN))
1642                 return -EPERM;
1643
1644         if (rid < 0xfc00 || rid > 0xffff)
1645                 return -EINVAL;
1646
1647         if (orinoco_lock(priv, &flags) != 0)
1648                 return -EBUSY;
1649
1650         err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
1651                               extra);
1652         if (err)
1653                 goto out;
1654
1655         data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
1656                              MAX_RID_LEN);
1657
1658  out:
1659         orinoco_unlock(priv, &flags);
1660         return err;
1661 }
1662
1663 /* Trigger a scan (look for other cells in the vicinity) */
1664 static int orinoco_ioctl_setscan(struct net_device *dev,
1665                                  struct iw_request_info *info,
1666                                  struct iw_point *srq,
1667                                  char *extra)
1668 {
1669         struct orinoco_private *priv = netdev_priv(dev);
1670         hermes_t *hw = &priv->hw;
1671         struct iw_scan_req *si = (struct iw_scan_req *) extra;
1672         int err = 0;
1673         unsigned long flags;
1674
1675         /* Note : you may have realised that, as this is a SET operation,
1676          * this is privileged and therefore a normal user can't
1677          * perform scanning.
1678          * This is not an error, while the device perform scanning,
1679          * traffic doesn't flow, so it's a perfect DoS...
1680          * Jean II */
1681
1682         if (orinoco_lock(priv, &flags) != 0)
1683                 return -EBUSY;
1684
1685         /* Scanning with port 0 disabled would fail */
1686         if (!netif_running(dev)) {
1687                 err = -ENETDOWN;
1688                 goto out;
1689         }
1690
1691         /* In monitor mode, the scan results are always empty.
1692          * Probe responses are passed to the driver as received
1693          * frames and could be processed in software. */
1694         if (priv->iw_mode == IW_MODE_MONITOR) {
1695                 err = -EOPNOTSUPP;
1696                 goto out;
1697         }
1698
1699         /* Note : because we don't lock out the irq handler, the way
1700          * we access scan variables in priv is critical.
1701          *      o scan_inprogress : not touched by irq handler
1702          *      o scan_mode : not touched by irq handler
1703          * Before modifying anything on those variables, please think hard !
1704          * Jean II */
1705
1706         /* Save flags */
1707         priv->scan_mode = srq->flags;
1708
1709         /* Always trigger scanning, even if it's in progress.
1710          * This way, if the info frame get lost, we will recover somewhat
1711          * gracefully  - Jean II */
1712
1713         if (priv->has_hostscan) {
1714                 switch (priv->firmware_type) {
1715                 case FIRMWARE_TYPE_SYMBOL:
1716                         err = hermes_write_wordrec(hw, USER_BAP,
1717                                                 HERMES_RID_CNFHOSTSCAN_SYMBOL,
1718                                                 HERMES_HOSTSCAN_SYMBOL_ONCE |
1719                                                 HERMES_HOSTSCAN_SYMBOL_BCAST);
1720                         break;
1721                 case FIRMWARE_TYPE_INTERSIL: {
1722                         __le16 req[3];
1723
1724                         req[0] = cpu_to_le16(0x3fff);   /* All channels */
1725                         req[1] = cpu_to_le16(0x0001);   /* rate 1 Mbps */
1726                         req[2] = 0;                     /* Any ESSID */
1727                         err = HERMES_WRITE_RECORD(hw, USER_BAP,
1728                                                   HERMES_RID_CNFHOSTSCAN, &req);
1729                 }
1730                 break;
1731                 case FIRMWARE_TYPE_AGERE:
1732                         if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
1733                                 struct hermes_idstring idbuf;
1734                                 size_t len = min(sizeof(idbuf.val),
1735                                                  (size_t) si->essid_len);
1736                                 idbuf.len = cpu_to_le16(len);
1737                                 memcpy(idbuf.val, si->essid, len);
1738
1739                                 err = hermes_write_ltv(hw, USER_BAP,
1740                                                HERMES_RID_CNFSCANSSID_AGERE,
1741                                                HERMES_BYTES_TO_RECLEN(len + 2),
1742                                                &idbuf);
1743                         } else
1744                                 err = hermes_write_wordrec(hw, USER_BAP,
1745                                                    HERMES_RID_CNFSCANSSID_AGERE,
1746                                                    0);  /* Any ESSID */
1747                         if (err)
1748                                 break;
1749
1750                         if (priv->has_ext_scan) {
1751                                 /* Clear scan results at the start of
1752                                  * an extended scan */
1753                                 orinoco_clear_scan_results(priv,
1754                                                 msecs_to_jiffies(15000));
1755
1756                                 /* TODO: Is this available on older firmware?
1757                                  *   Can we use it to scan specific channels
1758                                  *   for IW_SCAN_THIS_FREQ? */
1759                                 err = hermes_write_wordrec(hw, USER_BAP,
1760                                                 HERMES_RID_CNFSCANCHANNELS2GHZ,
1761                                                 0x7FFF);
1762                                 if (err)
1763                                         goto out;
1764
1765                                 err = hermes_inquire(hw,
1766                                                      HERMES_INQ_CHANNELINFO);
1767                         } else
1768                                 err = hermes_inquire(hw, HERMES_INQ_SCAN);
1769                         break;
1770                 }
1771         } else
1772                 err = hermes_inquire(hw, HERMES_INQ_SCAN);
1773
1774         /* One more client */
1775         if (!err)
1776                 priv->scan_inprogress = 1;
1777
1778  out:
1779         orinoco_unlock(priv, &flags);
1780         return err;
1781 }
1782
1783 #define MAX_CUSTOM_LEN 64
1784
1785 /* Translate scan data returned from the card to a card independant
1786  * format that the Wireless Tools will understand - Jean II */
1787 static inline char *orinoco_translate_scan(struct net_device *dev,
1788                                            struct iw_request_info *info,
1789                                            char *current_ev,
1790                                            char *end_buf,
1791                                            union hermes_scan_info *bss,
1792                                            unsigned long last_scanned)
1793 {
1794         struct orinoco_private *priv = netdev_priv(dev);
1795         u16                     capabilities;
1796         u16                     channel;
1797         struct iw_event         iwe;            /* Temporary buffer */
1798         char custom[MAX_CUSTOM_LEN];
1799
1800         memset(&iwe, 0, sizeof(iwe));
1801
1802         /* First entry *MUST* be the AP MAC address */
1803         iwe.cmd = SIOCGIWAP;
1804         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1805         memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
1806         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1807                                           &iwe, IW_EV_ADDR_LEN);
1808
1809         /* Other entries will be displayed in the order we give them */
1810
1811         /* Add the ESSID */
1812         iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
1813         if (iwe.u.data.length > 32)
1814                 iwe.u.data.length = 32;
1815         iwe.cmd = SIOCGIWESSID;
1816         iwe.u.data.flags = 1;
1817         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1818                                           &iwe, bss->a.essid);
1819
1820         /* Add mode */
1821         iwe.cmd = SIOCGIWMODE;
1822         capabilities = le16_to_cpu(bss->a.capabilities);
1823         if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
1824                 if (capabilities & WLAN_CAPABILITY_ESS)
1825                         iwe.u.mode = IW_MODE_MASTER;
1826                 else
1827                         iwe.u.mode = IW_MODE_ADHOC;
1828                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1829                                                   &iwe, IW_EV_UINT_LEN);
1830         }
1831
1832         channel = bss->s.channel;
1833         if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
1834                 /* Add channel and frequency */
1835                 iwe.cmd = SIOCGIWFREQ;
1836                 iwe.u.freq.m = channel;
1837                 iwe.u.freq.e = 0;
1838                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1839                                                   &iwe, IW_EV_FREQ_LEN);
1840
1841                 iwe.u.freq.m = ieee80211_dsss_chan_to_freq(channel) * 100000;
1842                 iwe.u.freq.e = 1;
1843                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1844                                                   &iwe, IW_EV_FREQ_LEN);
1845         }
1846
1847         /* Add quality statistics. level and noise in dB. No link quality */
1848         iwe.cmd = IWEVQUAL;
1849         iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
1850         iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
1851         iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
1852         /* Wireless tools prior to 27.pre22 will show link quality
1853          * anyway, so we provide a reasonable value. */
1854         if (iwe.u.qual.level > iwe.u.qual.noise)
1855                 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
1856         else
1857                 iwe.u.qual.qual = 0;
1858         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1859                                           &iwe, IW_EV_QUAL_LEN);
1860
1861         /* Add encryption capability */
1862         iwe.cmd = SIOCGIWENCODE;
1863         if (capabilities & WLAN_CAPABILITY_PRIVACY)
1864                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1865         else
1866                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1867         iwe.u.data.length = 0;
1868         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1869                                           &iwe, NULL);
1870
1871         /* Bit rate is not available in Lucent/Agere firmwares */
1872         if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
1873                 char *current_val = current_ev + iwe_stream_lcp_len(info);
1874                 int i;
1875                 int step;
1876
1877                 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
1878                         step = 2;
1879                 else
1880                         step = 1;
1881
1882                 iwe.cmd = SIOCGIWRATE;
1883                 /* Those two flags are ignored... */
1884                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1885                 /* Max 10 values */
1886                 for (i = 0; i < 10; i += step) {
1887                         /* NULL terminated */
1888                         if (bss->p.rates[i] == 0x0)
1889                                 break;
1890                         /* Bit rate given in 500 kb/s units (+ 0x80) */
1891                         iwe.u.bitrate.value =
1892                                 ((bss->p.rates[i] & 0x7f) * 500000);
1893                         current_val = iwe_stream_add_value(info, current_ev,
1894                                                            current_val,
1895                                                            end_buf, &iwe,
1896                                                            IW_EV_PARAM_LEN);
1897                 }
1898                 /* Check if we added any event */
1899                 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
1900                         current_ev = current_val;
1901         }
1902
1903         /* Beacon interval */
1904         iwe.cmd = IWEVCUSTOM;
1905         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
1906                                      "bcn_int=%d",
1907                                      le16_to_cpu(bss->a.beacon_interv));
1908         if (iwe.u.data.length)
1909                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1910                                                   &iwe, custom);
1911
1912         /* Capabilites */
1913         iwe.cmd = IWEVCUSTOM;
1914         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
1915                                      "capab=0x%04x",
1916                                      capabilities);
1917         if (iwe.u.data.length)
1918                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1919                                                   &iwe, custom);
1920
1921         /* Add EXTRA: Age to display seconds since last beacon/probe response
1922          * for given network. */
1923         iwe.cmd = IWEVCUSTOM;
1924         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
1925                                      " Last beacon: %dms ago",
1926                                      jiffies_to_msecs(jiffies - last_scanned));
1927         if (iwe.u.data.length)
1928                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1929                                                   &iwe, custom);
1930
1931         return current_ev;
1932 }
1933
1934 static inline char *orinoco_translate_ext_scan(struct net_device *dev,
1935                                                struct iw_request_info *info,
1936                                                char *current_ev,
1937                                                char *end_buf,
1938                                                struct agere_ext_scan_info *bss,
1939                                                unsigned long last_scanned)
1940 {
1941         u16                     capabilities;
1942         u16                     channel;
1943         struct iw_event         iwe;            /* Temporary buffer */
1944         char custom[MAX_CUSTOM_LEN];
1945         u8 *ie;
1946
1947         memset(&iwe, 0, sizeof(iwe));
1948
1949         /* First entry *MUST* be the AP MAC address */
1950         iwe.cmd = SIOCGIWAP;
1951         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1952         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
1953         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1954                                           &iwe, IW_EV_ADDR_LEN);
1955
1956         /* Other entries will be displayed in the order we give them */
1957
1958         /* Add the ESSID */
1959         ie = bss->data;
1960         iwe.u.data.length = ie[1];
1961         if (iwe.u.data.length) {
1962                 if (iwe.u.data.length > 32)
1963                         iwe.u.data.length = 32;
1964                 iwe.cmd = SIOCGIWESSID;
1965                 iwe.u.data.flags = 1;
1966                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1967                                                   &iwe, &ie[2]);
1968         }
1969
1970         /* Add mode */
1971         capabilities = le16_to_cpu(bss->capabilities);
1972         if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
1973                 iwe.cmd = SIOCGIWMODE;
1974                 if (capabilities & WLAN_CAPABILITY_ESS)
1975                         iwe.u.mode = IW_MODE_MASTER;
1976                 else
1977                         iwe.u.mode = IW_MODE_ADHOC;
1978                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1979                                                   &iwe, IW_EV_UINT_LEN);
1980         }
1981
1982         ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_DS_PARAMS);
1983         channel = ie ? ie[2] : 0;
1984         if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
1985                 /* Add channel and frequency */
1986                 iwe.cmd = SIOCGIWFREQ;
1987                 iwe.u.freq.m = channel;
1988                 iwe.u.freq.e = 0;
1989                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1990                                                   &iwe, IW_EV_FREQ_LEN);
1991
1992                 iwe.u.freq.m = ieee80211_dsss_chan_to_freq(channel) * 100000;
1993                 iwe.u.freq.e = 1;
1994                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1995                                                   &iwe, IW_EV_FREQ_LEN);
1996         }
1997
1998         /* Add quality statistics. level and noise in dB. No link quality */
1999         iwe.cmd = IWEVQUAL;
2000         iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
2001         iwe.u.qual.level = bss->level - 0x95;
2002         iwe.u.qual.noise = bss->noise - 0x95;
2003         /* Wireless tools prior to 27.pre22 will show link quality
2004          * anyway, so we provide a reasonable value. */
2005         if (iwe.u.qual.level > iwe.u.qual.noise)
2006                 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
2007         else
2008                 iwe.u.qual.qual = 0;
2009         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2010                                           &iwe, IW_EV_QUAL_LEN);
2011
2012         /* Add encryption capability */
2013         iwe.cmd = SIOCGIWENCODE;
2014         if (capabilities & WLAN_CAPABILITY_PRIVACY)
2015                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2016         else
2017                 iwe.u.data.flags = IW_ENCODE_DISABLED;
2018         iwe.u.data.length = 0;
2019         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2020                                           &iwe, NULL);
2021
2022         /* WPA IE */
2023         ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
2024         if (ie) {
2025                 iwe.cmd = IWEVGENIE;
2026                 iwe.u.data.length = ie[1] + 2;
2027                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2028                                                   &iwe, ie);
2029         }
2030
2031         /* RSN IE */
2032         ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_RSN);
2033         if (ie) {
2034                 iwe.cmd = IWEVGENIE;
2035                 iwe.u.data.length = ie[1] + 2;
2036                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2037                                                   &iwe, ie);
2038         }
2039
2040         ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_SUPP_RATES);
2041         if (ie) {
2042                 char *p = current_ev + iwe_stream_lcp_len(info);
2043                 int i;
2044
2045                 iwe.cmd = SIOCGIWRATE;
2046                 /* Those two flags are ignored... */
2047                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2048
2049                 for (i = 2; i < (ie[1] + 2); i++) {
2050                         iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
2051                         p = iwe_stream_add_value(info, current_ev, p, end_buf,
2052                                                  &iwe, IW_EV_PARAM_LEN);
2053                 }
2054                 /* Check if we added any event */
2055                 if (p > (current_ev + iwe_stream_lcp_len(info)))
2056                         current_ev = p;
2057         }
2058
2059         /* Timestamp */
2060         iwe.cmd = IWEVCUSTOM;
2061         iwe.u.data.length =
2062                 snprintf(custom, MAX_CUSTOM_LEN, "tsf=%016llx",
2063                          (unsigned long long) le64_to_cpu(bss->timestamp));
2064         if (iwe.u.data.length)
2065                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2066                                                   &iwe, custom);
2067
2068         /* Beacon interval */
2069         iwe.cmd = IWEVCUSTOM;
2070         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
2071                                      "bcn_int=%d",
2072                                      le16_to_cpu(bss->beacon_interval));
2073         if (iwe.u.data.length)
2074                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2075                                                   &iwe, custom);
2076
2077         /* Capabilites */
2078         iwe.cmd = IWEVCUSTOM;
2079         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
2080                                      "capab=0x%04x",
2081                                      capabilities);
2082         if (iwe.u.data.length)
2083                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2084                                                   &iwe, custom);
2085
2086         /* Add EXTRA: Age to display seconds since last beacon/probe response
2087          * for given network. */
2088         iwe.cmd = IWEVCUSTOM;
2089         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
2090                                      " Last beacon: %dms ago",
2091                                      jiffies_to_msecs(jiffies - last_scanned));
2092         if (iwe.u.data.length)
2093                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
2094                                                   &iwe, custom);
2095
2096         return current_ev;
2097 }
2098
2099 /* Return results of a scan */
2100 static int orinoco_ioctl_getscan(struct net_device *dev,
2101                                  struct iw_request_info *info,
2102                                  struct iw_point *srq,
2103                                  char *extra)
2104 {
2105         struct orinoco_private *priv = netdev_priv(dev);
2106         int err = 0;
2107         unsigned long flags;
2108         char *current_ev = extra;
2109
2110         if (orinoco_lock(priv, &flags) != 0)
2111                 return -EBUSY;
2112
2113         if (priv->scan_inprogress) {
2114                 /* Important note : we don't want to block the caller
2115                  * until results are ready for various reasons.
2116                  * First, managing wait queues is complex and racy.
2117                  * Second, we grab some rtnetlink lock before comming
2118                  * here (in dev_ioctl()).
2119                  * Third, we generate an Wireless Event, so the
2120                  * caller can wait itself on that - Jean II */
2121                 err = -EAGAIN;
2122                 goto out;
2123         }
2124
2125         if (priv->has_ext_scan) {
2126                 struct xbss_element *bss;
2127
2128                 list_for_each_entry(bss, &priv->bss_list, list) {
2129                         /* Translate this entry to WE format */
2130                         current_ev =
2131                                 orinoco_translate_ext_scan(dev, info,
2132                                                            current_ev,
2133                                                            extra + srq->length,
2134                                                            &bss->bss,
2135                                                            bss->last_scanned);
2136
2137                         /* Check if there is space for one more entry */
2138                         if ((extra + srq->length - current_ev)
2139                             <= IW_EV_ADDR_LEN) {
2140                                 /* Ask user space to try again with a
2141                                  * bigger buffer */
2142                                 err = -E2BIG;
2143                                 goto out;
2144                         }
2145                 }
2146
2147         } else {
2148                 struct bss_element *bss;
2149
2150                 list_for_each_entry(bss, &priv->bss_list, list) {
2151                         /* Translate this entry to WE format */
2152                         current_ev = orinoco_translate_scan(dev, info,
2153                                                             current_ev,
2154                                                             extra + srq->length,
2155                                                             &bss->bss,
2156                                                             bss->last_scanned);
2157
2158                         /* Check if there is space for one more entry */
2159                         if ((extra + srq->length - current_ev)
2160                             <= IW_EV_ADDR_LEN) {
2161                                 /* Ask user space to try again with a
2162                                  * bigger buffer */
2163                                 err = -E2BIG;
2164                                 goto out;
2165                         }
2166                 }
2167         }
2168
2169         srq->length = (current_ev - extra);
2170         srq->flags = (__u16) priv->scan_mode;
2171
2172 out:
2173         orinoco_unlock(priv, &flags);
2174         return err;
2175 }
2176
2177 /* Commit handler, called after set operations */
2178 static int orinoco_ioctl_commit(struct net_device *dev,
2179                                 struct iw_request_info *info,
2180                                 void *wrqu,
2181                                 char *extra)
2182 {
2183         struct orinoco_private *priv = netdev_priv(dev);
2184         struct hermes *hw = &priv->hw;
2185         unsigned long flags;
2186         int err = 0;
2187
2188         if (!priv->open)
2189                 return 0;
2190
2191         if (priv->broken_disableport) {
2192                 orinoco_reset(&priv->reset_work);
2193                 return 0;
2194         }
2195
2196         if (orinoco_lock(priv, &flags) != 0)
2197                 return err;
2198
2199         err = hermes_disable_port(hw, 0);
2200         if (err) {
2201                 printk(KERN_WARNING "%s: Unable to disable port "
2202                        "while reconfiguring card\n", dev->name);
2203                 priv->broken_disableport = 1;
2204                 goto out;
2205         }
2206
2207         err = __orinoco_program_rids(dev);
2208         if (err) {
2209                 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
2210                        dev->name);
2211                 goto out;
2212         }
2213
2214         err = hermes_enable_port(hw, 0);
2215         if (err) {
2216                 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
2217                        dev->name);
2218                 goto out;
2219         }
2220
2221  out:
2222         if (err) {
2223                 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
2224                 schedule_work(&priv->reset_work);
2225                 err = 0;
2226         }
2227
2228         orinoco_unlock(priv, &flags);
2229         return err;
2230 }
2231
2232 static const struct iw_priv_args orinoco_privtab[] = {
2233         { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
2234         { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
2235         { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2236           0, "set_port3" },
2237         { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2238           "get_port3" },
2239         { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2240           0, "set_preamble" },
2241         { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2242           "get_preamble" },
2243         { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2244           0, "set_ibssport" },
2245         { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2246           "get_ibssport" },
2247         { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
2248           "get_rid" },
2249 };
2250
2251
2252 /*
2253  * Structures to export the Wireless Handlers
2254  */
2255
2256 #define STD_IW_HANDLER(id, func) \
2257         [IW_IOCTL_IDX(id)] = (iw_handler) func
2258 static const iw_handler orinoco_handler[] = {
2259         STD_IW_HANDLER(SIOCSIWCOMMIT,   orinoco_ioctl_commit),
2260         STD_IW_HANDLER(SIOCGIWNAME,     orinoco_ioctl_getname),
2261         STD_IW_HANDLER(SIOCSIWFREQ,     orinoco_ioctl_setfreq),
2262         STD_IW_HANDLER(SIOCGIWFREQ,     orinoco_ioctl_getfreq),
2263         STD_IW_HANDLER(SIOCSIWMODE,     orinoco_ioctl_setmode),
2264         STD_IW_HANDLER(SIOCGIWMODE,     orinoco_ioctl_getmode),
2265         STD_IW_HANDLER(SIOCSIWSENS,     orinoco_ioctl_setsens),
2266         STD_IW_HANDLER(SIOCGIWSENS,     orinoco_ioctl_getsens),
2267         STD_IW_HANDLER(SIOCGIWRANGE,    orinoco_ioctl_getiwrange),
2268         STD_IW_HANDLER(SIOCSIWSPY,      iw_handler_set_spy),
2269         STD_IW_HANDLER(SIOCGIWSPY,      iw_handler_get_spy),
2270         STD_IW_HANDLER(SIOCSIWTHRSPY,   iw_handler_set_thrspy),
2271         STD_IW_HANDLER(SIOCGIWTHRSPY,   iw_handler_get_thrspy),
2272         STD_IW_HANDLER(SIOCSIWAP,       orinoco_ioctl_setwap),
2273         STD_IW_HANDLER(SIOCGIWAP,       orinoco_ioctl_getwap),
2274         STD_IW_HANDLER(SIOCSIWSCAN,     orinoco_ioctl_setscan),
2275         STD_IW_HANDLER(SIOCGIWSCAN,     orinoco_ioctl_getscan),
2276         STD_IW_HANDLER(SIOCSIWESSID,    orinoco_ioctl_setessid),
2277         STD_IW_HANDLER(SIOCGIWESSID,    orinoco_ioctl_getessid),
2278         STD_IW_HANDLER(SIOCSIWNICKN,    orinoco_ioctl_setnick),
2279         STD_IW_HANDLER(SIOCGIWNICKN,    orinoco_ioctl_getnick),
2280         STD_IW_HANDLER(SIOCSIWRATE,     orinoco_ioctl_setrate),
2281         STD_IW_HANDLER(SIOCGIWRATE,     orinoco_ioctl_getrate),
2282         STD_IW_HANDLER(SIOCSIWRTS,      orinoco_ioctl_setrts),
2283         STD_IW_HANDLER(SIOCGIWRTS,      orinoco_ioctl_getrts),
2284         STD_IW_HANDLER(SIOCSIWFRAG,     orinoco_ioctl_setfrag),
2285         STD_IW_HANDLER(SIOCGIWFRAG,     orinoco_ioctl_getfrag),
2286         STD_IW_HANDLER(SIOCGIWRETRY,    orinoco_ioctl_getretry),
2287         STD_IW_HANDLER(SIOCSIWENCODE,   orinoco_ioctl_setiwencode),
2288         STD_IW_HANDLER(SIOCGIWENCODE,   orinoco_ioctl_getiwencode),
2289         STD_IW_HANDLER(SIOCSIWPOWER,    orinoco_ioctl_setpower),
2290         STD_IW_HANDLER(SIOCGIWPOWER,    orinoco_ioctl_getpower),
2291         STD_IW_HANDLER(SIOCSIWGENIE,    orinoco_ioctl_set_genie),
2292         STD_IW_HANDLER(SIOCGIWGENIE,    orinoco_ioctl_get_genie),
2293         STD_IW_HANDLER(SIOCSIWMLME,     orinoco_ioctl_set_mlme),
2294         STD_IW_HANDLER(SIOCSIWAUTH,     orinoco_ioctl_set_auth),
2295         STD_IW_HANDLER(SIOCGIWAUTH,     orinoco_ioctl_get_auth),
2296         STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
2297         STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
2298 };
2299
2300
2301 /*
2302   Added typecasting since we no longer use iwreq_data -- Moustafa
2303  */
2304 static const iw_handler orinoco_private_handler[] = {
2305         [0] = (iw_handler) orinoco_ioctl_reset,
2306         [1] = (iw_handler) orinoco_ioctl_reset,
2307         [2] = (iw_handler) orinoco_ioctl_setport3,
2308         [3] = (iw_handler) orinoco_ioctl_getport3,
2309         [4] = (iw_handler) orinoco_ioctl_setpreamble,
2310         [5] = (iw_handler) orinoco_ioctl_getpreamble,
2311         [6] = (iw_handler) orinoco_ioctl_setibssport,
2312         [7] = (iw_handler) orinoco_ioctl_getibssport,
2313         [9] = (iw_handler) orinoco_ioctl_getrid,
2314 };
2315
2316 const struct iw_handler_def orinoco_handler_def = {
2317         .num_standard = ARRAY_SIZE(orinoco_handler),
2318         .num_private = ARRAY_SIZE(orinoco_private_handler),
2319         .num_private_args = ARRAY_SIZE(orinoco_privtab),
2320         .standard = orinoco_handler,
2321         .private = orinoco_private_handler,
2322         .private_args = orinoco_privtab,
2323         .get_wireless_stats = orinoco_get_wireless_stats,
2324 };