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