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