[PATCH] libertas: fix sparse-reported problems
[linux-2.6] / drivers / net / wireless / libertas / wext.c
1 /**
2   * This file contains ioctl functions
3   */
4 #include <linux/ctype.h>
5 #include <linux/delay.h>
6 #include <linux/if.h>
7 #include <linux/if_arp.h>
8 #include <linux/wireless.h>
9 #include <linux/bitops.h>
10
11 #include <net/ieee80211.h>
12 #include <net/iw_handler.h>
13
14 #include "host.h"
15 #include "radiotap.h"
16 #include "decl.h"
17 #include "defs.h"
18 #include "dev.h"
19 #include "join.h"
20 #include "wext.h"
21 #include "assoc.h"
22
23
24 /**
25  *  @brief Find the channel frequency power info with specific channel
26  *
27  *  @param adapter      A pointer to wlan_adapter structure
28  *  @param band         it can be BAND_A, BAND_G or BAND_B
29  *  @param channel      the channel for looking
30  *  @return             A pointer to struct chan_freq_power structure or NULL if not find.
31  */
32 struct chan_freq_power *libertas_find_cfp_by_band_and_channel(wlan_adapter * adapter,
33                                                  u8 band, u16 channel)
34 {
35         struct chan_freq_power *cfp = NULL;
36         struct region_channel *rc;
37         int count = sizeof(adapter->region_channel) /
38             sizeof(adapter->region_channel[0]);
39         int i, j;
40
41         for (j = 0; !cfp && (j < count); j++) {
42                 rc = &adapter->region_channel[j];
43
44                 if (adapter->enable11d)
45                         rc = &adapter->universal_channel[j];
46                 if (!rc->valid || !rc->CFP)
47                         continue;
48                 if (rc->band != band)
49                         continue;
50                 for (i = 0; i < rc->nrcfp; i++) {
51                         if (rc->CFP[i].channel == channel) {
52                                 cfp = &rc->CFP[i];
53                                 break;
54                         }
55                 }
56         }
57
58         if (!cfp && channel)
59                 lbs_deb_wext("libertas_find_cfp_by_band_and_channel: can't find "
60                        "cfp by band %d / channel %d\n", band, channel);
61
62         return cfp;
63 }
64
65 /**
66  *  @brief Find the channel frequency power info with specific frequency
67  *
68  *  @param adapter      A pointer to wlan_adapter structure
69  *  @param band         it can be BAND_A, BAND_G or BAND_B
70  *  @param freq         the frequency for looking
71  *  @return             A pointer to struct chan_freq_power structure or NULL if not find.
72  */
73 static struct chan_freq_power *find_cfp_by_band_and_freq(wlan_adapter * adapter,
74                                                      u8 band, u32 freq)
75 {
76         struct chan_freq_power *cfp = NULL;
77         struct region_channel *rc;
78         int count = sizeof(adapter->region_channel) /
79             sizeof(adapter->region_channel[0]);
80         int i, j;
81
82         for (j = 0; !cfp && (j < count); j++) {
83                 rc = &adapter->region_channel[j];
84
85                 if (adapter->enable11d)
86                         rc = &adapter->universal_channel[j];
87                 if (!rc->valid || !rc->CFP)
88                         continue;
89                 if (rc->band != band)
90                         continue;
91                 for (i = 0; i < rc->nrcfp; i++) {
92                         if (rc->CFP[i].freq == freq) {
93                                 cfp = &rc->CFP[i];
94                                 break;
95                         }
96                 }
97         }
98
99         if (!cfp && freq)
100                 lbs_deb_wext("find_cfp_by_band_and_freql: can't find cfp by "
101                        "band %d / freq %d\n", band, freq);
102
103         return cfp;
104 }
105
106
107 /**
108  *  @brief Set Radio On/OFF
109  *
110  *  @param priv                 A pointer to wlan_private structure
111  *  @option                     Radio Option
112  *  @return                     0 --success, otherwise fail
113  */
114 static int wlan_radio_ioctl(wlan_private * priv, u8 option)
115 {
116         int ret = 0;
117         wlan_adapter *adapter = priv->adapter;
118
119         lbs_deb_enter(LBS_DEB_WEXT);
120
121         if (adapter->radioon != option) {
122                 lbs_deb_wext("switching radio %s\n", option ? "on" : "off");
123                 adapter->radioon = option;
124
125                 ret = libertas_prepare_and_send_command(priv,
126                                             CMD_802_11_RADIO_CONTROL,
127                                             CMD_ACT_SET,
128                                             CMD_OPTION_WAITFORRSP, 0, NULL);
129         }
130
131         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
132         return ret;
133 }
134
135 /**
136  *  @brief Copy active data rates based on adapter mode and status
137  *
138  *  @param adapter              A pointer to wlan_adapter structure
139  *  @param rate                 The buf to return the active rates
140  */
141 static void copy_active_data_rates(wlan_adapter * adapter, u8 * rates)
142 {
143         lbs_deb_enter(LBS_DEB_WEXT);
144
145         if (adapter->connect_status != LIBERTAS_CONNECTED)
146                 memcpy(rates, libertas_bg_rates, MAX_RATES);
147         else
148                 memcpy(rates, adapter->curbssparams.rates, MAX_RATES);
149
150         lbs_deb_leave(LBS_DEB_WEXT);
151 }
152
153 static int wlan_get_name(struct net_device *dev, struct iw_request_info *info,
154                          char *cwrq, char *extra)
155 {
156
157         lbs_deb_enter(LBS_DEB_WEXT);
158
159         /* We could add support for 802.11n here as needed. Jean II */
160         snprintf(cwrq, IFNAMSIZ, "IEEE 802.11b/g");
161
162         lbs_deb_leave(LBS_DEB_WEXT);
163         return 0;
164 }
165
166 static int wlan_get_freq(struct net_device *dev, struct iw_request_info *info,
167                          struct iw_freq *fwrq, char *extra)
168 {
169         wlan_private *priv = dev->priv;
170         wlan_adapter *adapter = priv->adapter;
171         struct chan_freq_power *cfp;
172
173         lbs_deb_enter(LBS_DEB_WEXT);
174
175         cfp = libertas_find_cfp_by_band_and_channel(adapter, 0,
176                                            adapter->curbssparams.channel);
177
178         if (!cfp) {
179                 if (adapter->curbssparams.channel)
180                         lbs_deb_wext("invalid channel %d\n",
181                                adapter->curbssparams.channel);
182                 return -EINVAL;
183         }
184
185         fwrq->m = (long)cfp->freq * 100000;
186         fwrq->e = 1;
187
188         lbs_deb_wext("freq %u\n", fwrq->m);
189         lbs_deb_leave(LBS_DEB_WEXT);
190         return 0;
191 }
192
193 static int wlan_get_wap(struct net_device *dev, struct iw_request_info *info,
194                         struct sockaddr *awrq, char *extra)
195 {
196         wlan_private *priv = dev->priv;
197         wlan_adapter *adapter = priv->adapter;
198
199         lbs_deb_enter(LBS_DEB_WEXT);
200
201         if (adapter->connect_status == LIBERTAS_CONNECTED) {
202                 memcpy(awrq->sa_data, adapter->curbssparams.bssid, ETH_ALEN);
203         } else {
204                 memset(awrq->sa_data, 0, ETH_ALEN);
205         }
206         awrq->sa_family = ARPHRD_ETHER;
207
208         lbs_deb_leave(LBS_DEB_WEXT);
209         return 0;
210 }
211
212 static int wlan_set_nick(struct net_device *dev, struct iw_request_info *info,
213                          struct iw_point *dwrq, char *extra)
214 {
215         wlan_private *priv = dev->priv;
216         wlan_adapter *adapter = priv->adapter;
217
218         lbs_deb_enter(LBS_DEB_WEXT);
219
220         /*
221          * Check the size of the string
222          */
223
224         if (dwrq->length > 16) {
225                 return -E2BIG;
226         }
227
228         mutex_lock(&adapter->lock);
229         memset(adapter->nodename, 0, sizeof(adapter->nodename));
230         memcpy(adapter->nodename, extra, dwrq->length);
231         mutex_unlock(&adapter->lock);
232
233         lbs_deb_leave(LBS_DEB_WEXT);
234         return 0;
235 }
236
237 static int wlan_get_nick(struct net_device *dev, struct iw_request_info *info,
238                          struct iw_point *dwrq, char *extra)
239 {
240         const char *cp;
241         char comm[6] = { "COMM-" };
242         char mrvl[6] = { "MRVL-" };
243         int cnt;
244
245         lbs_deb_enter(LBS_DEB_WEXT);
246
247         /*
248          * Nick Name is not used internally in this mode,
249          * therefore return something useful instead. Jean II
250          */
251
252         strcpy(extra, mrvl);
253
254         cp = strstr(libertas_driver_version, comm);
255         if (cp == libertas_driver_version)      //skip leading "COMM-"
256                 cp = libertas_driver_version + strlen(comm);
257         else
258                 cp = libertas_driver_version;
259
260         cnt = strlen(mrvl);
261         extra += cnt;
262         while (cnt < 16 && (*cp != '-')) {
263                 *extra++ = toupper(*cp++);
264                 cnt++;
265         }
266
267         /*
268          * Push it out !
269          */
270         dwrq->length = cnt;
271
272         lbs_deb_leave(LBS_DEB_WEXT);
273         return 0;
274 }
275
276 static int mesh_get_nick(struct net_device *dev, struct iw_request_info *info,
277                          struct iw_point *dwrq, char *extra)
278 {
279         wlan_private *priv = dev->priv;
280         wlan_adapter *adapter = priv->adapter;
281
282         lbs_deb_enter(LBS_DEB_WEXT);
283
284         /* Use nickname to indicate that mesh is on */
285
286         if (adapter->connect_status == LIBERTAS_CONNECTED) {
287                 strncpy(extra, "Mesh", 12);
288                 extra[12] = '\0';
289                 dwrq->length = strlen(extra);
290         }
291
292         else {
293                 extra[0] = '\0';
294                 dwrq->length = 0;
295         }
296
297         lbs_deb_leave(LBS_DEB_WEXT);
298         return 0;
299 }
300 static int wlan_set_rts(struct net_device *dev, struct iw_request_info *info,
301                         struct iw_param *vwrq, char *extra)
302 {
303         int ret = 0;
304         wlan_private *priv = dev->priv;
305         wlan_adapter *adapter = priv->adapter;
306         u32 rthr = vwrq->value;
307
308         lbs_deb_enter(LBS_DEB_WEXT);
309
310         if (vwrq->disabled) {
311                 adapter->rtsthsd = rthr = MRVDRV_RTS_MAX_VALUE;
312         } else {
313                 if (rthr < MRVDRV_RTS_MIN_VALUE || rthr > MRVDRV_RTS_MAX_VALUE)
314                         return -EINVAL;
315                 adapter->rtsthsd = rthr;
316         }
317
318         ret = libertas_prepare_and_send_command(priv, CMD_802_11_SNMP_MIB,
319                                     CMD_ACT_SET, CMD_OPTION_WAITFORRSP,
320                                     OID_802_11_RTS_THRESHOLD, &rthr);
321
322         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
323         return ret;
324 }
325
326 static int wlan_get_rts(struct net_device *dev, struct iw_request_info *info,
327                         struct iw_param *vwrq, char *extra)
328 {
329         int ret = 0;
330         wlan_private *priv = dev->priv;
331         wlan_adapter *adapter = priv->adapter;
332
333         lbs_deb_enter(LBS_DEB_WEXT);
334
335         adapter->rtsthsd = 0;
336         ret = libertas_prepare_and_send_command(priv, CMD_802_11_SNMP_MIB,
337                                     CMD_ACT_GET, CMD_OPTION_WAITFORRSP,
338                                     OID_802_11_RTS_THRESHOLD, NULL);
339         if (ret)
340                 goto out;
341
342         vwrq->value = adapter->rtsthsd;
343         vwrq->disabled = ((vwrq->value < MRVDRV_RTS_MIN_VALUE)
344                           || (vwrq->value > MRVDRV_RTS_MAX_VALUE));
345         vwrq->fixed = 1;
346
347 out:
348         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
349         return ret;
350 }
351
352 static int wlan_set_frag(struct net_device *dev, struct iw_request_info *info,
353                          struct iw_param *vwrq, char *extra)
354 {
355         int ret = 0;
356         u32 fthr = vwrq->value;
357         wlan_private *priv = dev->priv;
358         wlan_adapter *adapter = priv->adapter;
359
360         lbs_deb_enter(LBS_DEB_WEXT);
361
362         if (vwrq->disabled) {
363                 adapter->fragthsd = fthr = MRVDRV_FRAG_MAX_VALUE;
364         } else {
365                 if (fthr < MRVDRV_FRAG_MIN_VALUE
366                     || fthr > MRVDRV_FRAG_MAX_VALUE)
367                         return -EINVAL;
368                 adapter->fragthsd = fthr;
369         }
370
371         ret = libertas_prepare_and_send_command(priv, CMD_802_11_SNMP_MIB,
372                                     CMD_ACT_SET, CMD_OPTION_WAITFORRSP,
373                                     OID_802_11_FRAGMENTATION_THRESHOLD, &fthr);
374
375         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
376         return ret;
377 }
378
379 static int wlan_get_frag(struct net_device *dev, struct iw_request_info *info,
380                          struct iw_param *vwrq, char *extra)
381 {
382         int ret = 0;
383         wlan_private *priv = dev->priv;
384         wlan_adapter *adapter = priv->adapter;
385
386         lbs_deb_enter(LBS_DEB_WEXT);
387
388         adapter->fragthsd = 0;
389         ret = libertas_prepare_and_send_command(priv,
390                                     CMD_802_11_SNMP_MIB,
391                                     CMD_ACT_GET, CMD_OPTION_WAITFORRSP,
392                                     OID_802_11_FRAGMENTATION_THRESHOLD, NULL);
393         if (ret)
394                 goto out;
395
396         vwrq->value = adapter->fragthsd;
397         vwrq->disabled = ((vwrq->value < MRVDRV_FRAG_MIN_VALUE)
398                           || (vwrq->value > MRVDRV_FRAG_MAX_VALUE));
399         vwrq->fixed = 1;
400
401 out:
402         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
403         return ret;
404 }
405
406 static int wlan_get_mode(struct net_device *dev,
407                          struct iw_request_info *info, u32 * uwrq, char *extra)
408 {
409         wlan_private *priv = dev->priv;
410         wlan_adapter *adapter = priv->adapter;
411
412         lbs_deb_enter(LBS_DEB_WEXT);
413
414         *uwrq = adapter->mode;
415
416         lbs_deb_leave(LBS_DEB_WEXT);
417         return 0;
418 }
419
420 static int mesh_wlan_get_mode(struct net_device *dev,
421                               struct iw_request_info *info, u32 * uwrq,
422                               char *extra)
423 {
424         lbs_deb_enter(LBS_DEB_WEXT);
425
426         *uwrq = IW_MODE_REPEAT ;
427
428         lbs_deb_leave(LBS_DEB_WEXT);
429         return 0;
430 }
431
432 static int wlan_get_txpow(struct net_device *dev,
433                           struct iw_request_info *info,
434                           struct iw_param *vwrq, char *extra)
435 {
436         int ret = 0;
437         wlan_private *priv = dev->priv;
438         wlan_adapter *adapter = priv->adapter;
439
440         lbs_deb_enter(LBS_DEB_WEXT);
441
442         ret = libertas_prepare_and_send_command(priv,
443                                     CMD_802_11_RF_TX_POWER,
444                                     CMD_ACT_TX_POWER_OPT_GET,
445                                     CMD_OPTION_WAITFORRSP, 0, NULL);
446
447         if (ret)
448                 goto out;
449
450         lbs_deb_wext("tx power level %d dbm\n", adapter->txpowerlevel);
451         vwrq->value = adapter->txpowerlevel;
452         vwrq->fixed = 1;
453         if (adapter->radioon) {
454                 vwrq->disabled = 0;
455                 vwrq->flags = IW_TXPOW_DBM;
456         } else {
457                 vwrq->disabled = 1;
458         }
459
460 out:
461         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
462         return ret;
463 }
464
465 static int wlan_set_retry(struct net_device *dev, struct iw_request_info *info,
466                           struct iw_param *vwrq, char *extra)
467 {
468         int ret = 0;
469         wlan_private *priv = dev->priv;
470         wlan_adapter *adapter = priv->adapter;
471
472         lbs_deb_enter(LBS_DEB_WEXT);
473
474         if (vwrq->flags == IW_RETRY_LIMIT) {
475                 /* The MAC has a 4-bit Total_Tx_Count register
476                    Total_Tx_Count = 1 + Tx_Retry_Count */
477 #define TX_RETRY_MIN 0
478 #define TX_RETRY_MAX 14
479                 if (vwrq->value < TX_RETRY_MIN || vwrq->value > TX_RETRY_MAX)
480                         return -EINVAL;
481
482                 /* Adding 1 to convert retry count to try count */
483                 adapter->txretrycount = vwrq->value + 1;
484
485                 ret = libertas_prepare_and_send_command(priv, CMD_802_11_SNMP_MIB,
486                                             CMD_ACT_SET,
487                                             CMD_OPTION_WAITFORRSP,
488                                             OID_802_11_TX_RETRYCOUNT, NULL);
489
490                 if (ret)
491                         goto out;
492         } else {
493                 return -EOPNOTSUPP;
494         }
495
496 out:
497         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
498         return ret;
499 }
500
501 static int wlan_get_retry(struct net_device *dev, struct iw_request_info *info,
502                           struct iw_param *vwrq, char *extra)
503 {
504         wlan_private *priv = dev->priv;
505         wlan_adapter *adapter = priv->adapter;
506         int ret = 0;
507
508         lbs_deb_enter(LBS_DEB_WEXT);
509
510         adapter->txretrycount = 0;
511         ret = libertas_prepare_and_send_command(priv,
512                                     CMD_802_11_SNMP_MIB,
513                                     CMD_ACT_GET, CMD_OPTION_WAITFORRSP,
514                                     OID_802_11_TX_RETRYCOUNT, NULL);
515         if (ret)
516                 goto out;
517
518         vwrq->disabled = 0;
519         if (!vwrq->flags) {
520                 vwrq->flags = IW_RETRY_LIMIT;
521                 /* Subtract 1 to convert try count to retry count */
522                 vwrq->value = adapter->txretrycount - 1;
523         }
524
525 out:
526         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
527         return ret;
528 }
529
530 static inline void sort_channels(struct iw_freq *freq, int num)
531 {
532         int i, j;
533         struct iw_freq temp;
534
535         for (i = 0; i < num; i++)
536                 for (j = i + 1; j < num; j++)
537                         if (freq[i].i > freq[j].i) {
538                                 temp.i = freq[i].i;
539                                 temp.m = freq[i].m;
540
541                                 freq[i].i = freq[j].i;
542                                 freq[i].m = freq[j].m;
543
544                                 freq[j].i = temp.i;
545                                 freq[j].m = temp.m;
546                         }
547 }
548
549 /* data rate listing
550         MULTI_BANDS:
551                 abg             a       b       b/g
552    Infra        G(12)           A(8)    B(4)    G(12)
553    Adhoc        A+B(12)         A(8)    B(4)    B(4)
554
555         non-MULTI_BANDS:
556                                         b       b/g
557    Infra                                B(4)    G(12)
558    Adhoc                                B(4)    B(4)
559  */
560 /**
561  *  @brief Get Range Info
562  *
563  *  @param dev                  A pointer to net_device structure
564  *  @param info                 A pointer to iw_request_info structure
565  *  @param vwrq                 A pointer to iw_param structure
566  *  @param extra                A pointer to extra data buf
567  *  @return                     0 --success, otherwise fail
568  */
569 static int wlan_get_range(struct net_device *dev, struct iw_request_info *info,
570                           struct iw_point *dwrq, char *extra)
571 {
572         int i, j;
573         wlan_private *priv = dev->priv;
574         wlan_adapter *adapter = priv->adapter;
575         struct iw_range *range = (struct iw_range *)extra;
576         struct chan_freq_power *cfp;
577         u8 rates[MAX_RATES + 1];
578
579         u8 flag = 0;
580
581         lbs_deb_enter(LBS_DEB_WEXT);
582
583         dwrq->length = sizeof(struct iw_range);
584         memset(range, 0, sizeof(struct iw_range));
585
586         range->min_nwid = 0;
587         range->max_nwid = 0;
588
589         memset(rates, 0, sizeof(rates));
590         copy_active_data_rates(adapter, rates);
591         range->num_bitrates = strnlen(rates, IW_MAX_BITRATES);
592         for (i = 0; i < range->num_bitrates; i++)
593                 range->bitrate[i] = rates[i] * 500000;
594         range->num_bitrates = i;
595         lbs_deb_wext("IW_MAX_BITRATES %d, num_bitrates %d\n", IW_MAX_BITRATES,
596                range->num_bitrates);
597
598         range->num_frequency = 0;
599         if (priv->adapter->enable11d &&
600             adapter->connect_status == LIBERTAS_CONNECTED) {
601                 u8 chan_no;
602                 u8 band;
603
604                 struct parsed_region_chan_11d *parsed_region_chan =
605                     &adapter->parsed_region_chan;
606
607                 if (parsed_region_chan == NULL) {
608                         lbs_deb_wext("11d: parsed_region_chan is NULL\n");
609                         goto out;
610                 }
611                 band = parsed_region_chan->band;
612                 lbs_deb_wext("band %d, nr_char %d\n", band,
613                        parsed_region_chan->nr_chan);
614
615                 for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
616                      && (i < parsed_region_chan->nr_chan); i++) {
617                         chan_no = parsed_region_chan->chanpwr[i].chan;
618                         lbs_deb_wext("chan_no %d\n", chan_no);
619                         range->freq[range->num_frequency].i = (long)chan_no;
620                         range->freq[range->num_frequency].m =
621                             (long)libertas_chan_2_freq(chan_no, band) * 100000;
622                         range->freq[range->num_frequency].e = 1;
623                         range->num_frequency++;
624                 }
625                 flag = 1;
626         }
627         if (!flag) {
628                 for (j = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
629                      && (j < sizeof(adapter->region_channel)
630                          / sizeof(adapter->region_channel[0])); j++) {
631                         cfp = adapter->region_channel[j].CFP;
632                         for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
633                              && adapter->region_channel[j].valid
634                              && cfp
635                              && (i < adapter->region_channel[j].nrcfp); i++) {
636                                 range->freq[range->num_frequency].i =
637                                     (long)cfp->channel;
638                                 range->freq[range->num_frequency].m =
639                                     (long)cfp->freq * 100000;
640                                 range->freq[range->num_frequency].e = 1;
641                                 cfp++;
642                                 range->num_frequency++;
643                         }
644                 }
645         }
646
647         lbs_deb_wext("IW_MAX_FREQUENCIES %d, num_frequency %d\n",
648                IW_MAX_FREQUENCIES, range->num_frequency);
649
650         range->num_channels = range->num_frequency;
651
652         sort_channels(&range->freq[0], range->num_frequency);
653
654         /*
655          * Set an indication of the max TCP throughput in bit/s that we can
656          * expect using this interface
657          */
658         if (i > 2)
659                 range->throughput = 5000 * 1000;
660         else
661                 range->throughput = 1500 * 1000;
662
663         range->min_rts = MRVDRV_RTS_MIN_VALUE;
664         range->max_rts = MRVDRV_RTS_MAX_VALUE;
665         range->min_frag = MRVDRV_FRAG_MIN_VALUE;
666         range->max_frag = MRVDRV_FRAG_MAX_VALUE;
667
668         range->encoding_size[0] = 5;
669         range->encoding_size[1] = 13;
670         range->num_encoding_sizes = 2;
671         range->max_encoding_tokens = 4;
672
673         range->min_pmp = 1000000;
674         range->max_pmp = 120000000;
675         range->min_pmt = 1000;
676         range->max_pmt = 1000000;
677         range->pmp_flags = IW_POWER_PERIOD;
678         range->pmt_flags = IW_POWER_TIMEOUT;
679         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
680
681         /*
682          * Minimum version we recommend
683          */
684         range->we_version_source = 15;
685
686         /*
687          * Version we are compiled with
688          */
689         range->we_version_compiled = WIRELESS_EXT;
690
691         range->retry_capa = IW_RETRY_LIMIT;
692         range->retry_flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
693
694         range->min_retry = TX_RETRY_MIN;
695         range->max_retry = TX_RETRY_MAX;
696
697         /*
698          * Set the qual, level and noise range values
699          */
700         range->max_qual.qual = 100;
701         range->max_qual.level = 0;
702         range->max_qual.noise = 0;
703         range->max_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
704
705         range->avg_qual.qual = 70;
706         /* TODO: Find real 'good' to 'bad' threshold value for RSSI */
707         range->avg_qual.level = 0;
708         range->avg_qual.noise = 0;
709         range->avg_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
710
711         range->sensitivity = 0;
712
713         /*
714          * Setup the supported power level ranges
715          */
716         memset(range->txpower, 0, sizeof(range->txpower));
717         range->txpower[0] = 5;
718         range->txpower[1] = 7;
719         range->txpower[2] = 9;
720         range->txpower[3] = 11;
721         range->txpower[4] = 13;
722         range->txpower[5] = 15;
723         range->txpower[6] = 17;
724         range->txpower[7] = 19;
725
726         range->num_txpower = 8;
727         range->txpower_capa = IW_TXPOW_DBM;
728         range->txpower_capa |= IW_TXPOW_RANGE;
729
730         range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
731                                 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
732                                 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
733         range->event_capa[1] = IW_EVENT_CAPA_K_1;
734
735         if (adapter->fwcapinfo & FW_CAPINFO_WPA) {
736                 range->enc_capa =   IW_ENC_CAPA_WPA
737                                   | IW_ENC_CAPA_WPA2
738                                   | IW_ENC_CAPA_CIPHER_TKIP
739                                   | IW_ENC_CAPA_CIPHER_CCMP;
740         }
741
742 out:
743         lbs_deb_leave(LBS_DEB_WEXT);
744         return 0;
745 }
746
747 static int wlan_set_power(struct net_device *dev, struct iw_request_info *info,
748                           struct iw_param *vwrq, char *extra)
749 {
750         wlan_private *priv = dev->priv;
751         wlan_adapter *adapter = priv->adapter;
752
753         lbs_deb_enter(LBS_DEB_WEXT);
754
755         /* PS is currently supported only in Infrastructure mode
756          * Remove this check if it is to be supported in IBSS mode also
757          */
758
759         if (vwrq->disabled) {
760                 adapter->psmode = WLAN802_11POWERMODECAM;
761                 if (adapter->psstate != PS_STATE_FULL_POWER) {
762                         libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
763                 }
764
765                 return 0;
766         }
767
768         if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
769                 lbs_deb_wext(
770                        "setting power timeout is not supported\n");
771                 return -EINVAL;
772         } else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
773                 lbs_deb_wext("setting power period not supported\n");
774                 return -EINVAL;
775         }
776
777         if (adapter->psmode != WLAN802_11POWERMODECAM) {
778                 return 0;
779         }
780
781         adapter->psmode = WLAN802_11POWERMODEMAX_PSP;
782
783         if (adapter->connect_status == LIBERTAS_CONNECTED) {
784                 libertas_ps_sleep(priv, CMD_OPTION_WAITFORRSP);
785         }
786
787         lbs_deb_leave(LBS_DEB_WEXT);
788         return 0;
789 }
790
791 static int wlan_get_power(struct net_device *dev, struct iw_request_info *info,
792                           struct iw_param *vwrq, char *extra)
793 {
794         wlan_private *priv = dev->priv;
795         wlan_adapter *adapter = priv->adapter;
796         int mode;
797
798         lbs_deb_enter(LBS_DEB_WEXT);
799
800         mode = adapter->psmode;
801
802         if ((vwrq->disabled = (mode == WLAN802_11POWERMODECAM))
803             || adapter->connect_status == LIBERTAS_DISCONNECTED)
804         {
805                 goto out;
806         }
807
808         vwrq->value = 0;
809
810 out:
811         lbs_deb_leave(LBS_DEB_WEXT);
812         return 0;
813 }
814
815 static struct iw_statistics *wlan_get_wireless_stats(struct net_device *dev)
816 {
817         enum {
818                 POOR = 30,
819                 FAIR = 60,
820                 GOOD = 80,
821                 VERY_GOOD = 90,
822                 EXCELLENT = 95,
823                 PERFECT = 100
824         };
825         wlan_private *priv = dev->priv;
826         wlan_adapter *adapter = priv->adapter;
827         u32 rssi_qual;
828         u32 tx_qual;
829         u32 quality = 0;
830         int stats_valid = 0;
831         u8 rssi;
832         u32 tx_retries;
833
834         lbs_deb_enter(LBS_DEB_WEXT);
835
836         priv->wstats.status = adapter->mode;
837
838         /* If we're not associated, all quality values are meaningless */
839         if (adapter->connect_status != LIBERTAS_CONNECTED)
840                 goto out;
841
842         /* Quality by RSSI */
843         priv->wstats.qual.level =
844             CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_NOAVG],
845              adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
846
847         if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
848                 priv->wstats.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
849         } else {
850                 priv->wstats.qual.noise =
851                     CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
852         }
853
854         lbs_deb_wext("signal level %#x\n", priv->wstats.qual.level);
855         lbs_deb_wext("noise %#x\n", priv->wstats.qual.noise);
856
857         rssi = priv->wstats.qual.level - priv->wstats.qual.noise;
858         if (rssi < 15)
859                 rssi_qual = rssi * POOR / 10;
860         else if (rssi < 20)
861                 rssi_qual = (rssi - 15) * (FAIR - POOR) / 5 + POOR;
862         else if (rssi < 30)
863                 rssi_qual = (rssi - 20) * (GOOD - FAIR) / 5 + FAIR;
864         else if (rssi < 40)
865                 rssi_qual = (rssi - 30) * (VERY_GOOD - GOOD) /
866                     10 + GOOD;
867         else
868                 rssi_qual = (rssi - 40) * (PERFECT - VERY_GOOD) /
869                     10 + VERY_GOOD;
870         quality = rssi_qual;
871
872         /* Quality by TX errors */
873         priv->wstats.discard.retries = priv->stats.tx_errors;
874
875         tx_retries = le32_to_cpu(adapter->logmsg.retry);
876
877         if (tx_retries > 75)
878                 tx_qual = (90 - tx_retries) * POOR / 15;
879         else if (tx_retries > 70)
880                 tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
881         else if (tx_retries > 65)
882                 tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
883         else if (tx_retries > 50)
884                 tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
885                     15 + GOOD;
886         else
887                 tx_qual = (50 - tx_retries) *
888                     (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
889         quality = min(quality, tx_qual);
890
891         priv->wstats.discard.code = le32_to_cpu(adapter->logmsg.wepundecryptable);
892         priv->wstats.discard.fragment = le32_to_cpu(adapter->logmsg.rxfrag);
893         priv->wstats.discard.retries = tx_retries;
894         priv->wstats.discard.misc = le32_to_cpu(adapter->logmsg.ackfailure);
895
896         /* Calculate quality */
897         priv->wstats.qual.qual = min_t(u8, quality, 100);
898         priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
899         stats_valid = 1;
900
901         /* update stats asynchronously for future calls */
902         libertas_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
903                                         0, 0, NULL);
904         libertas_prepare_and_send_command(priv, CMD_802_11_GET_LOG, 0,
905                                         0, 0, NULL);
906 out:
907         if (!stats_valid) {
908                 priv->wstats.miss.beacon = 0;
909                 priv->wstats.discard.retries = 0;
910                 priv->wstats.qual.qual = 0;
911                 priv->wstats.qual.level = 0;
912                 priv->wstats.qual.noise = 0;
913                 priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED;
914                 priv->wstats.qual.updated |= IW_QUAL_NOISE_INVALID |
915                     IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
916         }
917
918         lbs_deb_leave(LBS_DEB_WEXT);
919         return &priv->wstats;
920
921
922 }
923
924 static int wlan_set_freq(struct net_device *dev, struct iw_request_info *info,
925                   struct iw_freq *fwrq, char *extra)
926 {
927         int ret = -EINVAL;
928         wlan_private *priv = dev->priv;
929         wlan_adapter *adapter = priv->adapter;
930         struct chan_freq_power *cfp;
931         struct assoc_request * assoc_req;
932
933         lbs_deb_enter(LBS_DEB_WEXT);
934
935         mutex_lock(&adapter->lock);
936         assoc_req = wlan_get_association_request(adapter);
937         if (!assoc_req) {
938                 ret = -ENOMEM;
939                 goto out;
940         }
941
942         /* If setting by frequency, convert to a channel */
943         if (fwrq->e == 1) {
944                 long f = fwrq->m / 100000;
945
946                 cfp = find_cfp_by_band_and_freq(adapter, 0, f);
947                 if (!cfp) {
948                         lbs_deb_wext("invalid freq %ld\n", f);
949                         goto out;
950                 }
951
952                 fwrq->e = 0;
953                 fwrq->m = (int) cfp->channel;
954         }
955
956         /* Setting by channel number */
957         if (fwrq->m > 1000 || fwrq->e > 0) {
958                 goto out;
959         }
960
961         cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, fwrq->m);
962         if (!cfp) {
963                 goto out;
964         }
965
966         assoc_req->channel = fwrq->m;
967         ret = 0;
968
969 out:
970         if (ret == 0) {
971                 set_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags);
972                 wlan_postpone_association_work(priv);
973         } else {
974                 wlan_cancel_association_work(priv);
975         }
976         mutex_unlock(&adapter->lock);
977
978         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
979         return ret;
980 }
981
982 static int wlan_set_rate(struct net_device *dev, struct iw_request_info *info,
983                   struct iw_param *vwrq, char *extra)
984 {
985         wlan_private *priv = dev->priv;
986         wlan_adapter *adapter = priv->adapter;
987         u32 new_rate;
988         u16 action;
989         int ret = -EINVAL;
990         u8 rates[MAX_RATES + 1];
991
992         lbs_deb_enter(LBS_DEB_WEXT);
993         lbs_deb_wext("vwrq->value %d\n", vwrq->value);
994
995         /* Auto rate? */
996         if (vwrq->value == -1) {
997                 action = CMD_ACT_SET_TX_AUTO;
998                 adapter->auto_rate = 1;
999                 adapter->cur_rate = 0;
1000         } else {
1001                 if (vwrq->value % 100000)
1002                         goto out;
1003
1004                 memset(rates, 0, sizeof(rates));
1005                 copy_active_data_rates(adapter, rates);
1006                 new_rate = vwrq->value / 500000;
1007                 if (!memchr(rates, new_rate, sizeof(rates))) {
1008                         lbs_pr_alert("fixed data rate 0x%X out of range\n",
1009                                 new_rate);
1010                         goto out;
1011                 }
1012
1013                 adapter->cur_rate = new_rate;
1014                 action = CMD_ACT_SET_TX_FIX_RATE;
1015                 adapter->auto_rate = 0;
1016         }
1017
1018         ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
1019                                     action, CMD_OPTION_WAITFORRSP, 0, NULL);
1020
1021 out:
1022         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1023         return ret;
1024 }
1025
1026 static int wlan_get_rate(struct net_device *dev, struct iw_request_info *info,
1027                   struct iw_param *vwrq, char *extra)
1028 {
1029         wlan_private *priv = dev->priv;
1030         wlan_adapter *adapter = priv->adapter;
1031
1032         lbs_deb_enter(LBS_DEB_WEXT);
1033
1034         if (adapter->connect_status == LIBERTAS_CONNECTED) {
1035                 vwrq->value = adapter->cur_rate * 500000;
1036
1037                 if (adapter->auto_rate)
1038                         vwrq->fixed = 0;
1039                 else
1040                         vwrq->fixed = 1;
1041
1042         } else {
1043                 vwrq->fixed = 0;
1044                 vwrq->value = 0;
1045         }
1046
1047         lbs_deb_leave(LBS_DEB_WEXT);
1048         return 0;
1049 }
1050
1051 static int wlan_set_mode(struct net_device *dev,
1052                   struct iw_request_info *info, u32 * uwrq, char *extra)
1053 {
1054         int ret = 0;
1055         wlan_private *priv = dev->priv;
1056         wlan_adapter *adapter = priv->adapter;
1057         struct assoc_request * assoc_req;
1058
1059         lbs_deb_enter(LBS_DEB_WEXT);
1060
1061         if (   (*uwrq != IW_MODE_ADHOC)
1062             && (*uwrq != IW_MODE_INFRA)
1063             && (*uwrq != IW_MODE_AUTO)) {
1064                 lbs_deb_wext("Invalid mode: 0x%x\n", *uwrq);
1065                 ret = -EINVAL;
1066                 goto out;
1067         }
1068
1069         mutex_lock(&adapter->lock);
1070         assoc_req = wlan_get_association_request(adapter);
1071         if (!assoc_req) {
1072                 ret = -ENOMEM;
1073                 wlan_cancel_association_work(priv);
1074         } else {
1075                 assoc_req->mode = *uwrq;
1076                 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1077                 wlan_postpone_association_work(priv);
1078                 lbs_deb_wext("Switching to mode: 0x%x\n", *uwrq);
1079         }
1080         mutex_unlock(&adapter->lock);
1081
1082 out:
1083         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1084         return ret;
1085 }
1086
1087
1088 /**
1089  *  @brief Get Encryption key
1090  *
1091  *  @param dev                  A pointer to net_device structure
1092  *  @param info                 A pointer to iw_request_info structure
1093  *  @param vwrq                 A pointer to iw_param structure
1094  *  @param extra                A pointer to extra data buf
1095  *  @return                     0 --success, otherwise fail
1096  */
1097 static int wlan_get_encode(struct net_device *dev,
1098                            struct iw_request_info *info,
1099                            struct iw_point *dwrq, u8 * extra)
1100 {
1101         wlan_private *priv = dev->priv;
1102         wlan_adapter *adapter = priv->adapter;
1103         int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
1104
1105         lbs_deb_enter(LBS_DEB_WEXT);
1106
1107         lbs_deb_wext("flags 0x%x, index %d, length %d, wep_tx_keyidx %d\n",
1108                dwrq->flags, index, dwrq->length, adapter->wep_tx_keyidx);
1109
1110         dwrq->flags = 0;
1111
1112         /* Authentication method */
1113         switch (adapter->secinfo.auth_mode) {
1114         case IW_AUTH_ALG_OPEN_SYSTEM:
1115                 dwrq->flags = IW_ENCODE_OPEN;
1116                 break;
1117
1118         case IW_AUTH_ALG_SHARED_KEY:
1119         case IW_AUTH_ALG_LEAP:
1120                 dwrq->flags = IW_ENCODE_RESTRICTED;
1121                 break;
1122         default:
1123                 dwrq->flags = IW_ENCODE_DISABLED | IW_ENCODE_OPEN;
1124                 break;
1125         }
1126
1127         if (   adapter->secinfo.wep_enabled
1128             || adapter->secinfo.WPAenabled
1129             || adapter->secinfo.WPA2enabled) {
1130                 dwrq->flags &= ~IW_ENCODE_DISABLED;
1131         } else {
1132                 dwrq->flags |= IW_ENCODE_DISABLED;
1133         }
1134
1135         memset(extra, 0, 16);
1136
1137         mutex_lock(&adapter->lock);
1138
1139         /* Default to returning current transmit key */
1140         if (index < 0)
1141                 index = adapter->wep_tx_keyidx;
1142
1143         if ((adapter->wep_keys[index].len) && adapter->secinfo.wep_enabled) {
1144                 memcpy(extra, adapter->wep_keys[index].key,
1145                        adapter->wep_keys[index].len);
1146                 dwrq->length = adapter->wep_keys[index].len;
1147
1148                 dwrq->flags |= (index + 1);
1149                 /* Return WEP enabled */
1150                 dwrq->flags &= ~IW_ENCODE_DISABLED;
1151         } else if ((adapter->secinfo.WPAenabled)
1152                    || (adapter->secinfo.WPA2enabled)) {
1153                 /* return WPA enabled */
1154                 dwrq->flags &= ~IW_ENCODE_DISABLED;
1155         } else {
1156                 dwrq->flags |= IW_ENCODE_DISABLED;
1157         }
1158
1159         mutex_unlock(&adapter->lock);
1160
1161         dwrq->flags |= IW_ENCODE_NOKEY;
1162
1163         lbs_deb_wext("key: " MAC_FMT ", keylen %d\n",
1164                extra[0], extra[1], extra[2],
1165                extra[3], extra[4], extra[5], dwrq->length);
1166
1167         lbs_deb_wext("return flags 0x%x\n", dwrq->flags);
1168
1169         lbs_deb_leave(LBS_DEB_WEXT);
1170         return 0;
1171 }
1172
1173 /**
1174  *  @brief Set Encryption key (internal)
1175  *
1176  *  @param priv                 A pointer to private card structure
1177  *  @param key_material         A pointer to key material
1178  *  @param key_length           length of key material
1179  *  @param index                key index to set
1180  *  @param set_tx_key           Force set TX key (1 = yes, 0 = no)
1181  *  @return                     0 --success, otherwise fail
1182  */
1183 static int wlan_set_wep_key(struct assoc_request *assoc_req,
1184                             const char *key_material,
1185                             u16 key_length,
1186                             u16 index,
1187                             int set_tx_key)
1188 {
1189         int ret = 0;
1190         struct enc_key *pkey;
1191
1192         lbs_deb_enter(LBS_DEB_WEXT);
1193
1194         /* Paranoid validation of key index */
1195         if (index > 3) {
1196                 ret = -EINVAL;
1197                 goto out;
1198         }
1199
1200         /* validate max key length */
1201         if (key_length > KEY_LEN_WEP_104) {
1202                 ret = -EINVAL;
1203                 goto out;
1204         }
1205
1206         pkey = &assoc_req->wep_keys[index];
1207
1208         if (key_length > 0) {
1209                 memset(pkey, 0, sizeof(struct enc_key));
1210                 pkey->type = KEY_TYPE_ID_WEP;
1211
1212                 /* Standardize the key length */
1213                 pkey->len = (key_length > KEY_LEN_WEP_40) ?
1214                                 KEY_LEN_WEP_104 : KEY_LEN_WEP_40;
1215                 memcpy(pkey->key, key_material, key_length);
1216         }
1217
1218         if (set_tx_key) {
1219                 /* Ensure the chosen key is valid */
1220                 if (!pkey->len) {
1221                         lbs_deb_wext("key not set, so cannot enable it\n");
1222                         ret = -EINVAL;
1223                         goto out;
1224                 }
1225                 assoc_req->wep_tx_keyidx = index;
1226         }
1227
1228         assoc_req->secinfo.wep_enabled = 1;
1229
1230 out:
1231         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1232         return ret;
1233 }
1234
1235 static int validate_key_index(u16 def_index, u16 raw_index,
1236                               u16 *out_index, u16 *is_default)
1237 {
1238         if (!out_index || !is_default)
1239                 return -EINVAL;
1240
1241         /* Verify index if present, otherwise use default TX key index */
1242         if (raw_index > 0) {
1243                 if (raw_index > 4)
1244                         return -EINVAL;
1245                 *out_index = raw_index - 1;
1246         } else {
1247                 *out_index = def_index;
1248                 *is_default = 1;
1249         }
1250         return 0;
1251 }
1252
1253 static void disable_wep(struct assoc_request *assoc_req)
1254 {
1255         int i;
1256
1257         lbs_deb_enter(LBS_DEB_WEXT);
1258
1259         /* Set Open System auth mode */
1260         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1261
1262         /* Clear WEP keys and mark WEP as disabled */
1263         assoc_req->secinfo.wep_enabled = 0;
1264         for (i = 0; i < 4; i++)
1265                 assoc_req->wep_keys[i].len = 0;
1266
1267         set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1268         set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1269
1270         lbs_deb_leave(LBS_DEB_WEXT);
1271 }
1272
1273 static void disable_wpa(struct assoc_request *assoc_req)
1274 {
1275         lbs_deb_enter(LBS_DEB_WEXT);
1276
1277         memset(&assoc_req->wpa_mcast_key, 0, sizeof (struct enc_key));
1278         assoc_req->wpa_mcast_key.flags = KEY_INFO_WPA_MCAST;
1279         set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1280
1281         memset(&assoc_req->wpa_unicast_key, 0, sizeof (struct enc_key));
1282         assoc_req->wpa_unicast_key.flags = KEY_INFO_WPA_UNICAST;
1283         set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1284
1285         assoc_req->secinfo.WPAenabled = 0;
1286         assoc_req->secinfo.WPA2enabled = 0;
1287         set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1288
1289         lbs_deb_leave(LBS_DEB_WEXT);
1290 }
1291
1292 /**
1293  *  @brief Set Encryption key
1294  *
1295  *  @param dev                  A pointer to net_device structure
1296  *  @param info                 A pointer to iw_request_info structure
1297  *  @param vwrq                 A pointer to iw_param structure
1298  *  @param extra                A pointer to extra data buf
1299  *  @return                     0 --success, otherwise fail
1300  */
1301 static int wlan_set_encode(struct net_device *dev,
1302                     struct iw_request_info *info,
1303                     struct iw_point *dwrq, char *extra)
1304 {
1305         int ret = 0;
1306         wlan_private *priv = dev->priv;
1307         wlan_adapter *adapter = priv->adapter;
1308         struct assoc_request * assoc_req;
1309         u16 is_default = 0, index = 0, set_tx_key = 0;
1310
1311         lbs_deb_enter(LBS_DEB_WEXT);
1312
1313         mutex_lock(&adapter->lock);
1314         assoc_req = wlan_get_association_request(adapter);
1315         if (!assoc_req) {
1316                 ret = -ENOMEM;
1317                 goto out;
1318         }
1319
1320         if (dwrq->flags & IW_ENCODE_DISABLED) {
1321                 disable_wep (assoc_req);
1322                 disable_wpa (assoc_req);
1323                 goto out;
1324         }
1325
1326         ret = validate_key_index(assoc_req->wep_tx_keyidx,
1327                                  (dwrq->flags & IW_ENCODE_INDEX),
1328                                  &index, &is_default);
1329         if (ret) {
1330                 ret = -EINVAL;
1331                 goto out;
1332         }
1333
1334         /* If WEP isn't enabled, or if there is no key data but a valid
1335          * index, set the TX key.
1336          */
1337         if (!assoc_req->secinfo.wep_enabled || (dwrq->length == 0 && !is_default))
1338                 set_tx_key = 1;
1339
1340         ret = wlan_set_wep_key(assoc_req, extra, dwrq->length, index, set_tx_key);
1341         if (ret)
1342                 goto out;
1343
1344         if (dwrq->length)
1345                 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1346         if (set_tx_key)
1347                 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1348
1349         if (dwrq->flags & IW_ENCODE_RESTRICTED) {
1350                 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1351         } else if (dwrq->flags & IW_ENCODE_OPEN) {
1352                 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1353         }
1354
1355 out:
1356         if (ret == 0) {
1357                 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1358                 wlan_postpone_association_work(priv);
1359         } else {
1360                 wlan_cancel_association_work(priv);
1361         }
1362         mutex_unlock(&adapter->lock);
1363
1364         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1365         return ret;
1366 }
1367
1368 /**
1369  *  @brief Get Extended Encryption key (WPA/802.1x and WEP)
1370  *
1371  *  @param dev                  A pointer to net_device structure
1372  *  @param info                 A pointer to iw_request_info structure
1373  *  @param vwrq                 A pointer to iw_param structure
1374  *  @param extra                A pointer to extra data buf
1375  *  @return                     0 on success, otherwise failure
1376  */
1377 static int wlan_get_encodeext(struct net_device *dev,
1378                               struct iw_request_info *info,
1379                               struct iw_point *dwrq,
1380                               char *extra)
1381 {
1382         int ret = -EINVAL;
1383         wlan_private *priv = dev->priv;
1384         wlan_adapter *adapter = priv->adapter;
1385         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1386         int index, max_key_len;
1387
1388         lbs_deb_enter(LBS_DEB_WEXT);
1389
1390         max_key_len = dwrq->length - sizeof(*ext);
1391         if (max_key_len < 0)
1392                 goto out;
1393
1394         index = dwrq->flags & IW_ENCODE_INDEX;
1395         if (index) {
1396                 if (index < 1 || index > 4)
1397                         goto out;
1398                 index--;
1399         } else {
1400                 index = adapter->wep_tx_keyidx;
1401         }
1402
1403         if (!ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY &&
1404             ext->alg != IW_ENCODE_ALG_WEP) {
1405                 if (index != 0 || adapter->mode != IW_MODE_INFRA)
1406                         goto out;
1407         }
1408
1409         dwrq->flags = index + 1;
1410         memset(ext, 0, sizeof(*ext));
1411
1412         if (   !adapter->secinfo.wep_enabled
1413             && !adapter->secinfo.WPAenabled
1414             && !adapter->secinfo.WPA2enabled) {
1415                 ext->alg = IW_ENCODE_ALG_NONE;
1416                 ext->key_len = 0;
1417                 dwrq->flags |= IW_ENCODE_DISABLED;
1418         } else {
1419                 u8 *key = NULL;
1420
1421                 if (   adapter->secinfo.wep_enabled
1422                     && !adapter->secinfo.WPAenabled
1423                     && !adapter->secinfo.WPA2enabled) {
1424                         /* WEP */
1425                         ext->alg = IW_ENCODE_ALG_WEP;
1426                         ext->key_len = adapter->wep_keys[index].len;
1427                         key = &adapter->wep_keys[index].key[0];
1428                 } else if (   !adapter->secinfo.wep_enabled
1429                            && (adapter->secinfo.WPAenabled ||
1430                                adapter->secinfo.WPA2enabled)) {
1431                         /* WPA */
1432                         struct enc_key * pkey = NULL;
1433
1434                         if (   adapter->wpa_mcast_key.len
1435                             && (adapter->wpa_mcast_key.flags & KEY_INFO_WPA_ENABLED))
1436                                 pkey = &adapter->wpa_mcast_key;
1437                         else if (   adapter->wpa_unicast_key.len
1438                                  && (adapter->wpa_unicast_key.flags & KEY_INFO_WPA_ENABLED))
1439                                 pkey = &adapter->wpa_unicast_key;
1440
1441                         if (pkey) {
1442                                 if (pkey->type == KEY_TYPE_ID_AES) {
1443                                         ext->alg = IW_ENCODE_ALG_CCMP;
1444                                 } else {
1445                                         ext->alg = IW_ENCODE_ALG_TKIP;
1446                                 }
1447                                 ext->key_len = pkey->len;
1448                                 key = &pkey->key[0];
1449                         } else {
1450                                 ext->alg = IW_ENCODE_ALG_TKIP;
1451                                 ext->key_len = 0;
1452                         }
1453                 } else {
1454                         goto out;
1455                 }
1456
1457                 if (ext->key_len > max_key_len) {
1458                         ret = -E2BIG;
1459                         goto out;
1460                 }
1461
1462                 if (ext->key_len)
1463                         memcpy(ext->key, key, ext->key_len);
1464                 else
1465                         dwrq->flags |= IW_ENCODE_NOKEY;
1466                 dwrq->flags |= IW_ENCODE_ENABLED;
1467         }
1468         ret = 0;
1469
1470 out:
1471         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1472         return ret;
1473 }
1474
1475 /**
1476  *  @brief Set Encryption key Extended (WPA/802.1x and WEP)
1477  *
1478  *  @param dev                  A pointer to net_device structure
1479  *  @param info                 A pointer to iw_request_info structure
1480  *  @param vwrq                 A pointer to iw_param structure
1481  *  @param extra                A pointer to extra data buf
1482  *  @return                     0 --success, otherwise fail
1483  */
1484 static int wlan_set_encodeext(struct net_device *dev,
1485                               struct iw_request_info *info,
1486                               struct iw_point *dwrq,
1487                               char *extra)
1488 {
1489         int ret = 0;
1490         wlan_private *priv = dev->priv;
1491         wlan_adapter *adapter = priv->adapter;
1492         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1493         int alg = ext->alg;
1494         struct assoc_request * assoc_req;
1495
1496         lbs_deb_enter(LBS_DEB_WEXT);
1497
1498         mutex_lock(&adapter->lock);
1499         assoc_req = wlan_get_association_request(adapter);
1500         if (!assoc_req) {
1501                 ret = -ENOMEM;
1502                 goto out;
1503         }
1504
1505         if ((alg == IW_ENCODE_ALG_NONE) || (dwrq->flags & IW_ENCODE_DISABLED)) {
1506                 disable_wep (assoc_req);
1507                 disable_wpa (assoc_req);
1508         } else if (alg == IW_ENCODE_ALG_WEP) {
1509                 u16 is_default = 0, index, set_tx_key = 0;
1510
1511                 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1512                                          (dwrq->flags & IW_ENCODE_INDEX),
1513                                          &index, &is_default);
1514                 if (ret)
1515                         goto out;
1516
1517                 /* If WEP isn't enabled, or if there is no key data but a valid
1518                  * index, or if the set-TX-key flag was passed, set the TX key.
1519                  */
1520                 if (   !assoc_req->secinfo.wep_enabled
1521                     || (dwrq->length == 0 && !is_default)
1522                     || (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY))
1523                         set_tx_key = 1;
1524
1525                 /* Copy key to driver */
1526                 ret = wlan_set_wep_key (assoc_req, ext->key, ext->key_len, index,
1527                                         set_tx_key);
1528                 if (ret)
1529                         goto out;
1530
1531                 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
1532                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1533                 } else if (dwrq->flags & IW_ENCODE_OPEN) {
1534                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1535                 }
1536
1537                 /* Mark the various WEP bits as modified */
1538                 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1539                 if (dwrq->length)
1540                         set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1541                 if (set_tx_key)
1542                         set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1543         } else if ((alg == IW_ENCODE_ALG_TKIP) || (alg == IW_ENCODE_ALG_CCMP)) {
1544                 struct enc_key * pkey;
1545
1546                 /* validate key length */
1547                 if (((alg == IW_ENCODE_ALG_TKIP)
1548                         && (ext->key_len != KEY_LEN_WPA_TKIP))
1549                     || ((alg == IW_ENCODE_ALG_CCMP)
1550                         && (ext->key_len != KEY_LEN_WPA_AES))) {
1551                                 lbs_deb_wext("invalid size %d for key of alg"
1552                                        "type %d\n",
1553                                        ext->key_len,
1554                                        alg);
1555                                 ret = -EINVAL;
1556                                 goto out;
1557                 }
1558
1559                 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1560                         pkey = &assoc_req->wpa_mcast_key;
1561                         set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1562                 } else {
1563                         pkey = &assoc_req->wpa_unicast_key;
1564                         set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1565                 }
1566
1567                 memset(pkey, 0, sizeof (struct enc_key));
1568                 memcpy(pkey->key, ext->key, ext->key_len);
1569                 pkey->len = ext->key_len;
1570                 if (pkey->len)
1571                         pkey->flags |= KEY_INFO_WPA_ENABLED;
1572
1573                 /* Do this after zeroing key structure */
1574                 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1575                         pkey->flags |= KEY_INFO_WPA_MCAST;
1576                 } else {
1577                         pkey->flags |= KEY_INFO_WPA_UNICAST;
1578                 }
1579
1580                 if (alg == IW_ENCODE_ALG_TKIP) {
1581                         pkey->type = KEY_TYPE_ID_TKIP;
1582                 } else if (alg == IW_ENCODE_ALG_CCMP) {
1583                         pkey->type = KEY_TYPE_ID_AES;
1584                 }
1585
1586                 /* If WPA isn't enabled yet, do that now */
1587                 if (   assoc_req->secinfo.WPAenabled == 0
1588                     && assoc_req->secinfo.WPA2enabled == 0) {
1589                         assoc_req->secinfo.WPAenabled = 1;
1590                         assoc_req->secinfo.WPA2enabled = 1;
1591                         set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1592                 }
1593
1594                 disable_wep (assoc_req);
1595         }
1596
1597 out:
1598         if (ret == 0) {
1599                 wlan_postpone_association_work(priv);
1600         } else {
1601                 wlan_cancel_association_work(priv);
1602         }
1603         mutex_unlock(&adapter->lock);
1604
1605         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1606         return ret;
1607 }
1608
1609
1610 static int wlan_set_genie(struct net_device *dev,
1611                           struct iw_request_info *info,
1612                           struct iw_point *dwrq,
1613                           char *extra)
1614 {
1615         wlan_private *priv = dev->priv;
1616         wlan_adapter *adapter = priv->adapter;
1617         int ret = 0;
1618         struct assoc_request * assoc_req;
1619
1620         lbs_deb_enter(LBS_DEB_WEXT);
1621
1622         mutex_lock(&adapter->lock);
1623         assoc_req = wlan_get_association_request(adapter);
1624         if (!assoc_req) {
1625                 ret = -ENOMEM;
1626                 goto out;
1627         }
1628
1629         if (dwrq->length > MAX_WPA_IE_LEN ||
1630             (dwrq->length && extra == NULL)) {
1631                 ret = -EINVAL;
1632                 goto out;
1633         }
1634
1635         if (dwrq->length) {
1636                 memcpy(&assoc_req->wpa_ie[0], extra, dwrq->length);
1637                 assoc_req->wpa_ie_len = dwrq->length;
1638         } else {
1639                 memset(&assoc_req->wpa_ie[0], 0, sizeof(adapter->wpa_ie));
1640                 assoc_req->wpa_ie_len = 0;
1641         }
1642
1643 out:
1644         if (ret == 0) {
1645                 set_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags);
1646                 wlan_postpone_association_work(priv);
1647         } else {
1648                 wlan_cancel_association_work(priv);
1649         }
1650         mutex_unlock(&adapter->lock);
1651
1652         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1653         return ret;
1654 }
1655
1656 static int wlan_get_genie(struct net_device *dev,
1657                           struct iw_request_info *info,
1658                           struct iw_point *dwrq,
1659                           char *extra)
1660 {
1661         int ret = 0;
1662         wlan_private *priv = dev->priv;
1663         wlan_adapter *adapter = priv->adapter;
1664
1665         lbs_deb_enter(LBS_DEB_WEXT);
1666
1667         if (adapter->wpa_ie_len == 0) {
1668                 dwrq->length = 0;
1669                 goto out;
1670         }
1671
1672         if (dwrq->length < adapter->wpa_ie_len) {
1673                 ret = -E2BIG;
1674                 goto out;
1675         }
1676
1677         dwrq->length = adapter->wpa_ie_len;
1678         memcpy(extra, &adapter->wpa_ie[0], adapter->wpa_ie_len);
1679
1680 out:
1681         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1682         return ret;
1683 }
1684
1685
1686 static int wlan_set_auth(struct net_device *dev,
1687                          struct iw_request_info *info,
1688                          struct iw_param *dwrq,
1689                          char *extra)
1690 {
1691         wlan_private *priv = dev->priv;
1692         wlan_adapter *adapter = priv->adapter;
1693         struct assoc_request * assoc_req;
1694         int ret = 0;
1695         int updated = 0;
1696
1697         lbs_deb_enter(LBS_DEB_WEXT);
1698
1699         mutex_lock(&adapter->lock);
1700         assoc_req = wlan_get_association_request(adapter);
1701         if (!assoc_req) {
1702                 ret = -ENOMEM;
1703                 goto out;
1704         }
1705
1706         switch (dwrq->flags & IW_AUTH_INDEX) {
1707         case IW_AUTH_TKIP_COUNTERMEASURES:
1708         case IW_AUTH_CIPHER_PAIRWISE:
1709         case IW_AUTH_CIPHER_GROUP:
1710         case IW_AUTH_KEY_MGMT:
1711         case IW_AUTH_DROP_UNENCRYPTED:
1712                 /*
1713                  * libertas does not use these parameters
1714                  */
1715                 break;
1716
1717         case IW_AUTH_WPA_VERSION:
1718                 if (dwrq->value & IW_AUTH_WPA_VERSION_DISABLED) {
1719                         assoc_req->secinfo.WPAenabled = 0;
1720                         assoc_req->secinfo.WPA2enabled = 0;
1721                         disable_wpa (assoc_req);
1722                 }
1723                 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA) {
1724                         assoc_req->secinfo.WPAenabled = 1;
1725                         assoc_req->secinfo.wep_enabled = 0;
1726                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1727                 }
1728                 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA2) {
1729                         assoc_req->secinfo.WPA2enabled = 1;
1730                         assoc_req->secinfo.wep_enabled = 0;
1731                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1732                 }
1733                 updated = 1;
1734                 break;
1735
1736         case IW_AUTH_80211_AUTH_ALG:
1737                 if (dwrq->value & IW_AUTH_ALG_SHARED_KEY) {
1738                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1739                 } else if (dwrq->value & IW_AUTH_ALG_OPEN_SYSTEM) {
1740                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1741                 } else if (dwrq->value & IW_AUTH_ALG_LEAP) {
1742                         assoc_req->secinfo.auth_mode = IW_AUTH_ALG_LEAP;
1743                 } else {
1744                         ret = -EINVAL;
1745                 }
1746                 updated = 1;
1747                 break;
1748
1749         case IW_AUTH_WPA_ENABLED:
1750                 if (dwrq->value) {
1751                         if (!assoc_req->secinfo.WPAenabled &&
1752                             !assoc_req->secinfo.WPA2enabled) {
1753                                 assoc_req->secinfo.WPAenabled = 1;
1754                                 assoc_req->secinfo.WPA2enabled = 1;
1755                                 assoc_req->secinfo.wep_enabled = 0;
1756                                 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1757                         }
1758                 } else {
1759                         assoc_req->secinfo.WPAenabled = 0;
1760                         assoc_req->secinfo.WPA2enabled = 0;
1761                         disable_wpa (assoc_req);
1762                 }
1763                 updated = 1;
1764                 break;
1765
1766         default:
1767                 ret = -EOPNOTSUPP;
1768                 break;
1769         }
1770
1771 out:
1772         if (ret == 0) {
1773                 if (updated)
1774                         set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1775                 wlan_postpone_association_work(priv);
1776         } else if (ret != -EOPNOTSUPP) {
1777                 wlan_cancel_association_work(priv);
1778         }
1779         mutex_unlock(&adapter->lock);
1780
1781         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1782         return ret;
1783 }
1784
1785 static int wlan_get_auth(struct net_device *dev,
1786                          struct iw_request_info *info,
1787                          struct iw_param *dwrq,
1788                          char *extra)
1789 {
1790         int ret = 0;
1791         wlan_private *priv = dev->priv;
1792         wlan_adapter *adapter = priv->adapter;
1793
1794         lbs_deb_enter(LBS_DEB_WEXT);
1795
1796         switch (dwrq->flags & IW_AUTH_INDEX) {
1797         case IW_AUTH_WPA_VERSION:
1798                 dwrq->value = 0;
1799                 if (adapter->secinfo.WPAenabled)
1800                         dwrq->value |= IW_AUTH_WPA_VERSION_WPA;
1801                 if (adapter->secinfo.WPA2enabled)
1802                         dwrq->value |= IW_AUTH_WPA_VERSION_WPA2;
1803                 if (!dwrq->value)
1804                         dwrq->value |= IW_AUTH_WPA_VERSION_DISABLED;
1805                 break;
1806
1807         case IW_AUTH_80211_AUTH_ALG:
1808                 dwrq->value = adapter->secinfo.auth_mode;
1809                 break;
1810
1811         case IW_AUTH_WPA_ENABLED:
1812                 if (adapter->secinfo.WPAenabled && adapter->secinfo.WPA2enabled)
1813                         dwrq->value = 1;
1814                 break;
1815
1816         default:
1817                 ret = -EOPNOTSUPP;
1818         }
1819
1820         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1821         return ret;
1822 }
1823
1824
1825 static int wlan_set_txpow(struct net_device *dev, struct iw_request_info *info,
1826                    struct iw_param *vwrq, char *extra)
1827 {
1828         int ret = 0;
1829         wlan_private *priv = dev->priv;
1830         wlan_adapter *adapter = priv->adapter;
1831
1832         u16 dbm;
1833
1834         lbs_deb_enter(LBS_DEB_WEXT);
1835
1836         if (vwrq->disabled) {
1837                 wlan_radio_ioctl(priv, RADIO_OFF);
1838                 return 0;
1839         }
1840
1841         adapter->preamble = CMD_TYPE_AUTO_PREAMBLE;
1842
1843         wlan_radio_ioctl(priv, RADIO_ON);
1844
1845         /* Userspace check in iwrange if it should use dBm or mW,
1846          * therefore this should never happen... Jean II */
1847         if ((vwrq->flags & IW_TXPOW_TYPE) == IW_TXPOW_MWATT) {
1848                 return -EOPNOTSUPP;
1849         } else
1850                 dbm = (u16) vwrq->value;
1851
1852         /* auto tx power control */
1853
1854         if (vwrq->fixed == 0)
1855                 dbm = 0xffff;
1856
1857         lbs_deb_wext("txpower set %d dbm\n", dbm);
1858
1859         ret = libertas_prepare_and_send_command(priv,
1860                                     CMD_802_11_RF_TX_POWER,
1861                                     CMD_ACT_TX_POWER_OPT_SET_LOW,
1862                                     CMD_OPTION_WAITFORRSP, 0, (void *)&dbm);
1863
1864         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1865         return ret;
1866 }
1867
1868 static int wlan_get_essid(struct net_device *dev, struct iw_request_info *info,
1869                    struct iw_point *dwrq, char *extra)
1870 {
1871         wlan_private *priv = dev->priv;
1872         wlan_adapter *adapter = priv->adapter;
1873
1874         lbs_deb_enter(LBS_DEB_WEXT);
1875
1876         /*
1877          * Note : if dwrq->flags != 0, we should get the relevant SSID from
1878          * the SSID list...
1879          */
1880
1881         /*
1882          * Get the current SSID
1883          */
1884         if (adapter->connect_status == LIBERTAS_CONNECTED) {
1885                 memcpy(extra, adapter->curbssparams.ssid,
1886                        adapter->curbssparams.ssid_len);
1887                 extra[adapter->curbssparams.ssid_len] = '\0';
1888         } else {
1889                 memset(extra, 0, 32);
1890                 extra[adapter->curbssparams.ssid_len] = '\0';
1891         }
1892         /*
1893          * If none, we may want to get the one that was set
1894          */
1895
1896         dwrq->length = adapter->curbssparams.ssid_len;
1897
1898         dwrq->flags = 1;        /* active */
1899
1900         lbs_deb_leave(LBS_DEB_WEXT);
1901         return 0;
1902 }
1903
1904 static int wlan_set_essid(struct net_device *dev, struct iw_request_info *info,
1905                    struct iw_point *dwrq, char *extra)
1906 {
1907         wlan_private *priv = dev->priv;
1908         wlan_adapter *adapter = priv->adapter;
1909         int ret = 0;
1910         u8 ssid[IW_ESSID_MAX_SIZE];
1911         u8 ssid_len = 0;
1912         struct assoc_request * assoc_req;
1913         int in_ssid_len = dwrq->length;
1914
1915         lbs_deb_enter(LBS_DEB_WEXT);
1916
1917         /* Check the size of the string */
1918         if (in_ssid_len > IW_ESSID_MAX_SIZE) {
1919                 ret = -E2BIG;
1920                 goto out;
1921         }
1922
1923         memset(&ssid, 0, sizeof(ssid));
1924
1925         if (!dwrq->flags || !in_ssid_len) {
1926                 /* "any" SSID requested; leave SSID blank */
1927         } else {
1928                 /* Specific SSID requested */
1929                 memcpy(&ssid, extra, in_ssid_len);
1930                 ssid_len = in_ssid_len;
1931         }
1932
1933         if (!ssid_len) {
1934                 lbs_deb_wext("requested any SSID\n");
1935         } else {
1936                 lbs_deb_wext("requested SSID '%s'\n",
1937                              escape_essid(ssid, ssid_len));
1938         }
1939
1940 out:
1941         mutex_lock(&adapter->lock);
1942         if (ret == 0) {
1943                 /* Get or create the current association request */
1944                 assoc_req = wlan_get_association_request(adapter);
1945                 if (!assoc_req) {
1946                         ret = -ENOMEM;
1947                 } else {
1948                         /* Copy the SSID to the association request */
1949                         memcpy(&assoc_req->ssid, &ssid, IW_ESSID_MAX_SIZE);
1950                         assoc_req->ssid_len = ssid_len;
1951                         set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1952                         wlan_postpone_association_work(priv);
1953                 }
1954         }
1955
1956         /* Cancel the association request if there was an error */
1957         if (ret != 0) {
1958                 wlan_cancel_association_work(priv);
1959         }
1960
1961         mutex_unlock(&adapter->lock);
1962
1963         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1964         return ret;
1965 }
1966
1967 /**
1968  *  @brief Connect to the AP or Ad-hoc Network with specific bssid
1969  *
1970  *  @param dev          A pointer to net_device structure
1971  *  @param info         A pointer to iw_request_info structure
1972  *  @param awrq         A pointer to iw_param structure
1973  *  @param extra        A pointer to extra data buf
1974  *  @return             0 --success, otherwise fail
1975  */
1976 static int wlan_set_wap(struct net_device *dev, struct iw_request_info *info,
1977                  struct sockaddr *awrq, char *extra)
1978 {
1979         wlan_private *priv = dev->priv;
1980         wlan_adapter *adapter = priv->adapter;
1981         struct assoc_request * assoc_req;
1982         int ret = 0;
1983
1984         lbs_deb_enter(LBS_DEB_WEXT);
1985
1986         if (awrq->sa_family != ARPHRD_ETHER)
1987                 return -EINVAL;
1988
1989         lbs_deb_wext("ASSOC: WAP: sa_data " MAC_FMT "\n", MAC_ARG(awrq->sa_data));
1990
1991         mutex_lock(&adapter->lock);
1992
1993         /* Get or create the current association request */
1994         assoc_req = wlan_get_association_request(adapter);
1995         if (!assoc_req) {
1996                 wlan_cancel_association_work(priv);
1997                 ret = -ENOMEM;
1998         } else {
1999                 /* Copy the BSSID to the association request */
2000                 memcpy(&assoc_req->bssid, awrq->sa_data, ETH_ALEN);
2001                 set_bit(ASSOC_FLAG_BSSID, &assoc_req->flags);
2002                 wlan_postpone_association_work(priv);
2003         }
2004
2005         mutex_unlock(&adapter->lock);
2006
2007         return ret;
2008 }
2009
2010 void libertas_get_fwversion(wlan_adapter * adapter, char *fwversion, int maxlen)
2011 {
2012         char fwver[32];
2013
2014         mutex_lock(&adapter->lock);
2015
2016         if (adapter->fwreleasenumber[3] == 0)
2017                 sprintf(fwver, "%u.%u.%u",
2018                         adapter->fwreleasenumber[2],
2019                         adapter->fwreleasenumber[1],
2020                         adapter->fwreleasenumber[0]);
2021         else
2022                 sprintf(fwver, "%u.%u.%u.p%u",
2023                         adapter->fwreleasenumber[2],
2024                         adapter->fwreleasenumber[1],
2025                         adapter->fwreleasenumber[0],
2026                         adapter->fwreleasenumber[3]);
2027
2028         mutex_unlock(&adapter->lock);
2029         snprintf(fwversion, maxlen, fwver);
2030 }
2031
2032
2033 /*
2034  * iwconfig settable callbacks
2035  */
2036 static const iw_handler wlan_handler[] = {
2037         (iw_handler) NULL,      /* SIOCSIWCOMMIT */
2038         (iw_handler) wlan_get_name,     /* SIOCGIWNAME */
2039         (iw_handler) NULL,      /* SIOCSIWNWID */
2040         (iw_handler) NULL,      /* SIOCGIWNWID */
2041         (iw_handler) wlan_set_freq,     /* SIOCSIWFREQ */
2042         (iw_handler) wlan_get_freq,     /* SIOCGIWFREQ */
2043         (iw_handler) wlan_set_mode,     /* SIOCSIWMODE */
2044         (iw_handler) wlan_get_mode,     /* SIOCGIWMODE */
2045         (iw_handler) NULL,      /* SIOCSIWSENS */
2046         (iw_handler) NULL,      /* SIOCGIWSENS */
2047         (iw_handler) NULL,      /* SIOCSIWRANGE */
2048         (iw_handler) wlan_get_range,    /* SIOCGIWRANGE */
2049         (iw_handler) NULL,      /* SIOCSIWPRIV */
2050         (iw_handler) NULL,      /* SIOCGIWPRIV */
2051         (iw_handler) NULL,      /* SIOCSIWSTATS */
2052         (iw_handler) NULL,      /* SIOCGIWSTATS */
2053         iw_handler_set_spy,     /* SIOCSIWSPY */
2054         iw_handler_get_spy,     /* SIOCGIWSPY */
2055         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
2056         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
2057         (iw_handler) wlan_set_wap,      /* SIOCSIWAP */
2058         (iw_handler) wlan_get_wap,      /* SIOCGIWAP */
2059         (iw_handler) NULL,      /* SIOCSIWMLME */
2060         (iw_handler) NULL,      /* SIOCGIWAPLIST - deprecated */
2061         (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2062         (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2063         (iw_handler) wlan_set_essid,    /* SIOCSIWESSID */
2064         (iw_handler) wlan_get_essid,    /* SIOCGIWESSID */
2065         (iw_handler) wlan_set_nick,     /* SIOCSIWNICKN */
2066         (iw_handler) wlan_get_nick,     /* SIOCGIWNICKN */
2067         (iw_handler) NULL,      /* -- hole -- */
2068         (iw_handler) NULL,      /* -- hole -- */
2069         (iw_handler) wlan_set_rate,     /* SIOCSIWRATE */
2070         (iw_handler) wlan_get_rate,     /* SIOCGIWRATE */
2071         (iw_handler) wlan_set_rts,      /* SIOCSIWRTS */
2072         (iw_handler) wlan_get_rts,      /* SIOCGIWRTS */
2073         (iw_handler) wlan_set_frag,     /* SIOCSIWFRAG */
2074         (iw_handler) wlan_get_frag,     /* SIOCGIWFRAG */
2075         (iw_handler) wlan_set_txpow,    /* SIOCSIWTXPOW */
2076         (iw_handler) wlan_get_txpow,    /* SIOCGIWTXPOW */
2077         (iw_handler) wlan_set_retry,    /* SIOCSIWRETRY */
2078         (iw_handler) wlan_get_retry,    /* SIOCGIWRETRY */
2079         (iw_handler) wlan_set_encode,   /* SIOCSIWENCODE */
2080         (iw_handler) wlan_get_encode,   /* SIOCGIWENCODE */
2081         (iw_handler) wlan_set_power,    /* SIOCSIWPOWER */
2082         (iw_handler) wlan_get_power,    /* SIOCGIWPOWER */
2083         (iw_handler) NULL,      /* -- hole -- */
2084         (iw_handler) NULL,      /* -- hole -- */
2085         (iw_handler) wlan_set_genie,    /* SIOCSIWGENIE */
2086         (iw_handler) wlan_get_genie,    /* SIOCGIWGENIE */
2087         (iw_handler) wlan_set_auth,     /* SIOCSIWAUTH */
2088         (iw_handler) wlan_get_auth,     /* SIOCGIWAUTH */
2089         (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2090         (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2091         (iw_handler) NULL,              /* SIOCSIWPMKSA */
2092 };
2093
2094 static const iw_handler mesh_wlan_handler[] = {
2095         (iw_handler) NULL,      /* SIOCSIWCOMMIT */
2096         (iw_handler) wlan_get_name,     /* SIOCGIWNAME */
2097         (iw_handler) NULL,      /* SIOCSIWNWID */
2098         (iw_handler) NULL,      /* SIOCGIWNWID */
2099         (iw_handler) wlan_set_freq,     /* SIOCSIWFREQ */
2100         (iw_handler) wlan_get_freq,     /* SIOCGIWFREQ */
2101         (iw_handler) NULL,              /* SIOCSIWMODE */
2102         (iw_handler) mesh_wlan_get_mode,        /* SIOCGIWMODE */
2103         (iw_handler) NULL,      /* SIOCSIWSENS */
2104         (iw_handler) NULL,      /* SIOCGIWSENS */
2105         (iw_handler) NULL,      /* SIOCSIWRANGE */
2106         (iw_handler) wlan_get_range,    /* SIOCGIWRANGE */
2107         (iw_handler) NULL,      /* SIOCSIWPRIV */
2108         (iw_handler) NULL,      /* SIOCGIWPRIV */
2109         (iw_handler) NULL,      /* SIOCSIWSTATS */
2110         (iw_handler) NULL,      /* SIOCGIWSTATS */
2111         iw_handler_set_spy,     /* SIOCSIWSPY */
2112         iw_handler_get_spy,     /* SIOCGIWSPY */
2113         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
2114         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
2115         (iw_handler) NULL,      /* SIOCSIWAP */
2116         (iw_handler) NULL,      /* SIOCGIWAP */
2117         (iw_handler) NULL,      /* SIOCSIWMLME */
2118         (iw_handler) NULL,      /* SIOCGIWAPLIST - deprecated */
2119         (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2120         (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2121         (iw_handler) NULL,              /* SIOCSIWESSID */
2122         (iw_handler) NULL,              /* SIOCGIWESSID */
2123         (iw_handler) NULL,              /* SIOCSIWNICKN */
2124         (iw_handler) mesh_get_nick,     /* SIOCGIWNICKN */
2125         (iw_handler) NULL,      /* -- hole -- */
2126         (iw_handler) NULL,      /* -- hole -- */
2127         (iw_handler) wlan_set_rate,     /* SIOCSIWRATE */
2128         (iw_handler) wlan_get_rate,     /* SIOCGIWRATE */
2129         (iw_handler) wlan_set_rts,      /* SIOCSIWRTS */
2130         (iw_handler) wlan_get_rts,      /* SIOCGIWRTS */
2131         (iw_handler) wlan_set_frag,     /* SIOCSIWFRAG */
2132         (iw_handler) wlan_get_frag,     /* SIOCGIWFRAG */
2133         (iw_handler) wlan_set_txpow,    /* SIOCSIWTXPOW */
2134         (iw_handler) wlan_get_txpow,    /* SIOCGIWTXPOW */
2135         (iw_handler) wlan_set_retry,    /* SIOCSIWRETRY */
2136         (iw_handler) wlan_get_retry,    /* SIOCGIWRETRY */
2137         (iw_handler) wlan_set_encode,   /* SIOCSIWENCODE */
2138         (iw_handler) wlan_get_encode,   /* SIOCGIWENCODE */
2139         (iw_handler) wlan_set_power,    /* SIOCSIWPOWER */
2140         (iw_handler) wlan_get_power,    /* SIOCGIWPOWER */
2141         (iw_handler) NULL,      /* -- hole -- */
2142         (iw_handler) NULL,      /* -- hole -- */
2143         (iw_handler) wlan_set_genie,    /* SIOCSIWGENIE */
2144         (iw_handler) wlan_get_genie,    /* SIOCGIWGENIE */
2145         (iw_handler) wlan_set_auth,     /* SIOCSIWAUTH */
2146         (iw_handler) wlan_get_auth,     /* SIOCGIWAUTH */
2147         (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2148         (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2149         (iw_handler) NULL,              /* SIOCSIWPMKSA */
2150 };
2151 struct iw_handler_def libertas_handler_def = {
2152         .num_standard   = sizeof(wlan_handler) / sizeof(iw_handler),
2153         .standard       = (iw_handler *) wlan_handler,
2154         .get_wireless_stats = wlan_get_wireless_stats,
2155 };
2156
2157 struct iw_handler_def mesh_handler_def = {
2158         .num_standard   = sizeof(mesh_wlan_handler) / sizeof(iw_handler),
2159         .standard       = (iw_handler *) mesh_wlan_handler,
2160         .get_wireless_stats = wlan_get_wireless_stats,
2161 };