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