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