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