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