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