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