]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/libertas/wext.c
40dd08018b49657264af3cb8f312f3d041d3922c
[mirror_ubuntu-bionic-kernel.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 /* Set Open System auth mode */
1502 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1503
1504 /* Clear WEP keys and mark WEP as disabled */
1505 assoc_req->secinfo.wep_enabled = 0;
1506 for (i = 0; i < 4; i++)
1507 assoc_req->wep_keys[i].len = 0;
1508
1509 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1510 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1511 }
1512
1513 /**
1514 * @brief Set Encryption key
1515 *
1516 * @param dev A pointer to net_device structure
1517 * @param info A pointer to iw_request_info structure
1518 * @param vwrq A pointer to iw_param structure
1519 * @param extra A pointer to extra data buf
1520 * @return 0 --success, otherwise fail
1521 */
1522 static int wlan_set_encode(struct net_device *dev,
1523 struct iw_request_info *info,
1524 struct iw_point *dwrq, char *extra)
1525 {
1526 int ret = 0;
1527 wlan_private *priv = dev->priv;
1528 wlan_adapter *adapter = priv->adapter;
1529 struct assoc_request * assoc_req;
1530 u16 is_default = 0, index = 0, set_tx_key = 0;
1531
1532 lbs_deb_enter(LBS_DEB_WEXT);
1533
1534 mutex_lock(&adapter->lock);
1535 assoc_req = wlan_get_association_request(adapter);
1536 if (!assoc_req) {
1537 ret = -ENOMEM;
1538 goto out;
1539 }
1540
1541 if (dwrq->flags & IW_ENCODE_DISABLED) {
1542 disable_wep (assoc_req);
1543 goto out;
1544 }
1545
1546 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1547 (dwrq->flags & IW_ENCODE_INDEX),
1548 &index, &is_default);
1549 if (ret) {
1550 ret = -EINVAL;
1551 goto out;
1552 }
1553
1554 /* If WEP isn't enabled, or if there is no key data but a valid
1555 * index, set the TX key.
1556 */
1557 if (!assoc_req->secinfo.wep_enabled || (dwrq->length == 0 && !is_default))
1558 set_tx_key = 1;
1559
1560 ret = wlan_set_wep_key(assoc_req, extra, dwrq->length, index, set_tx_key);
1561 if (ret)
1562 goto out;
1563
1564 if (dwrq->length)
1565 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1566 if (set_tx_key)
1567 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1568
1569 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
1570 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1571 } else if (dwrq->flags & IW_ENCODE_OPEN) {
1572 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1573 }
1574
1575 out:
1576 if (ret == 0) {
1577 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1578 wlan_postpone_association_work(priv);
1579 } else {
1580 wlan_cancel_association_work(priv);
1581 }
1582 mutex_unlock(&adapter->lock);
1583
1584 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1585 return ret;
1586 }
1587
1588 /**
1589 * @brief Get Extended Encryption key (WPA/802.1x and WEP)
1590 *
1591 * @param dev A pointer to net_device structure
1592 * @param info A pointer to iw_request_info structure
1593 * @param vwrq A pointer to iw_param structure
1594 * @param extra A pointer to extra data buf
1595 * @return 0 on success, otherwise failure
1596 */
1597 static int wlan_get_encodeext(struct net_device *dev,
1598 struct iw_request_info *info,
1599 struct iw_point *dwrq,
1600 char *extra)
1601 {
1602 int ret = -EINVAL;
1603 wlan_private *priv = dev->priv;
1604 wlan_adapter *adapter = priv->adapter;
1605 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1606 int index, max_key_len;
1607
1608 lbs_deb_enter(LBS_DEB_WEXT);
1609
1610 max_key_len = dwrq->length - sizeof(*ext);
1611 if (max_key_len < 0)
1612 goto out;
1613
1614 index = dwrq->flags & IW_ENCODE_INDEX;
1615 if (index) {
1616 if (index < 1 || index > 4)
1617 goto out;
1618 index--;
1619 } else {
1620 index = adapter->wep_tx_keyidx;
1621 }
1622
1623 if (!ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY &&
1624 ext->alg != IW_ENCODE_ALG_WEP) {
1625 if (index != 0 || adapter->mode != IW_MODE_INFRA)
1626 goto out;
1627 }
1628
1629 dwrq->flags = index + 1;
1630 memset(ext, 0, sizeof(*ext));
1631
1632 if ( !adapter->secinfo.wep_enabled
1633 && !adapter->secinfo.WPAenabled
1634 && !adapter->secinfo.WPA2enabled) {
1635 ext->alg = IW_ENCODE_ALG_NONE;
1636 ext->key_len = 0;
1637 dwrq->flags |= IW_ENCODE_DISABLED;
1638 } else {
1639 u8 *key = NULL;
1640
1641 if ( adapter->secinfo.wep_enabled
1642 && !adapter->secinfo.WPAenabled
1643 && !adapter->secinfo.WPA2enabled) {
1644 ext->alg = IW_ENCODE_ALG_WEP;
1645 ext->key_len = adapter->wep_keys[index].len;
1646 key = &adapter->wep_keys[index].key[0];
1647 } else if ( !adapter->secinfo.wep_enabled
1648 && (adapter->secinfo.WPAenabled ||
1649 adapter->secinfo.WPA2enabled)) {
1650 /* WPA */
1651 ext->alg = IW_ENCODE_ALG_TKIP;
1652 ext->key_len = 0;
1653 } else {
1654 goto out;
1655 }
1656
1657 if (ext->key_len > max_key_len) {
1658 ret = -E2BIG;
1659 goto out;
1660 }
1661
1662 if (ext->key_len)
1663 memcpy(ext->key, key, ext->key_len);
1664 else
1665 dwrq->flags |= IW_ENCODE_NOKEY;
1666 dwrq->flags |= IW_ENCODE_ENABLED;
1667 }
1668 ret = 0;
1669
1670 out:
1671 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1672 return ret;
1673 }
1674
1675 /**
1676 * @brief Set Encryption key Extended (WPA/802.1x and WEP)
1677 *
1678 * @param dev A pointer to net_device structure
1679 * @param info A pointer to iw_request_info structure
1680 * @param vwrq A pointer to iw_param structure
1681 * @param extra A pointer to extra data buf
1682 * @return 0 --success, otherwise fail
1683 */
1684 static int wlan_set_encodeext(struct net_device *dev,
1685 struct iw_request_info *info,
1686 struct iw_point *dwrq,
1687 char *extra)
1688 {
1689 int ret = 0;
1690 wlan_private *priv = dev->priv;
1691 wlan_adapter *adapter = priv->adapter;
1692 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1693 int alg = ext->alg;
1694 struct assoc_request * assoc_req;
1695
1696 lbs_deb_enter(LBS_DEB_WEXT);
1697
1698 mutex_lock(&adapter->lock);
1699 assoc_req = wlan_get_association_request(adapter);
1700 if (!assoc_req) {
1701 ret = -ENOMEM;
1702 goto out;
1703 }
1704
1705 if ((alg == IW_ENCODE_ALG_NONE) || (dwrq->flags & IW_ENCODE_DISABLED)) {
1706 disable_wep (assoc_req);
1707 } else if (alg == IW_ENCODE_ALG_WEP) {
1708 u16 is_default = 0, index, set_tx_key = 0;
1709
1710 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1711 (dwrq->flags & IW_ENCODE_INDEX),
1712 &index, &is_default);
1713 if (ret)
1714 goto out;
1715
1716 /* If WEP isn't enabled, or if there is no key data but a valid
1717 * index, or if the set-TX-key flag was passed, set the TX key.
1718 */
1719 if ( !assoc_req->secinfo.wep_enabled
1720 || (dwrq->length == 0 && !is_default)
1721 || (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY))
1722 set_tx_key = 1;
1723
1724 /* Copy key to driver */
1725 ret = wlan_set_wep_key (assoc_req, ext->key, ext->key_len, index,
1726 set_tx_key);
1727 if (ret)
1728 goto out;
1729
1730 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
1731 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1732 } else if (dwrq->flags & IW_ENCODE_OPEN) {
1733 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1734 }
1735
1736 /* Mark the various WEP bits as modified */
1737 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1738 if (dwrq->length)
1739 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1740 if (set_tx_key)
1741 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1742
1743 } else if ((alg == IW_ENCODE_ALG_TKIP) || (alg == IW_ENCODE_ALG_CCMP)) {
1744 struct WLAN_802_11_KEY * pkey;
1745
1746 /* validate key length */
1747 if (((alg == IW_ENCODE_ALG_TKIP)
1748 && (ext->key_len != KEY_LEN_WPA_TKIP))
1749 || ((alg == IW_ENCODE_ALG_CCMP)
1750 && (ext->key_len != KEY_LEN_WPA_AES))) {
1751 lbs_deb_wext("invalid size %d for key of alg"
1752 "type %d\n",
1753 ext->key_len,
1754 alg);
1755 ret = -EINVAL;
1756 goto out;
1757 }
1758
1759 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
1760 pkey = &assoc_req->wpa_mcast_key;
1761 else
1762 pkey = &assoc_req->wpa_unicast_key;
1763
1764 memset(pkey, 0, sizeof (struct WLAN_802_11_KEY));
1765 memcpy(pkey->key, ext->key, ext->key_len);
1766 pkey->len = ext->key_len;
1767 pkey->flags = KEY_INFO_WPA_ENABLED;
1768
1769 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1770 pkey->flags |= KEY_INFO_WPA_MCAST;
1771 set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1772 } else {
1773 pkey->flags |= KEY_INFO_WPA_UNICAST;
1774 set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1775 }
1776
1777 if (alg == IW_ENCODE_ALG_TKIP)
1778 pkey->type = KEY_TYPE_ID_TKIP;
1779 else if (alg == IW_ENCODE_ALG_CCMP)
1780 pkey->type = KEY_TYPE_ID_AES;
1781
1782 /* If WPA isn't enabled yet, do that now */
1783 if ( assoc_req->secinfo.WPAenabled == 0
1784 && assoc_req->secinfo.WPA2enabled == 0) {
1785 assoc_req->secinfo.WPAenabled = 1;
1786 assoc_req->secinfo.WPA2enabled = 1;
1787 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1788 }
1789
1790 disable_wep (assoc_req);
1791 }
1792
1793 out:
1794 if (ret == 0) {
1795 wlan_postpone_association_work(priv);
1796 } else {
1797 wlan_cancel_association_work(priv);
1798 }
1799 mutex_unlock(&adapter->lock);
1800
1801 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1802 return ret;
1803 }
1804
1805
1806 static int wlan_set_genie(struct net_device *dev,
1807 struct iw_request_info *info,
1808 struct iw_point *dwrq,
1809 char *extra)
1810 {
1811 wlan_private *priv = dev->priv;
1812 wlan_adapter *adapter = priv->adapter;
1813 int ret = 0;
1814 struct assoc_request * assoc_req;
1815
1816 lbs_deb_enter(LBS_DEB_WEXT);
1817
1818 mutex_lock(&adapter->lock);
1819 assoc_req = wlan_get_association_request(adapter);
1820 if (!assoc_req) {
1821 ret = -ENOMEM;
1822 goto out;
1823 }
1824
1825 if (dwrq->length > MAX_WPA_IE_LEN ||
1826 (dwrq->length && extra == NULL)) {
1827 ret = -EINVAL;
1828 goto out;
1829 }
1830
1831 if (dwrq->length) {
1832 memcpy(&assoc_req->wpa_ie[0], extra, dwrq->length);
1833 assoc_req->wpa_ie_len = dwrq->length;
1834 } else {
1835 memset(&assoc_req->wpa_ie[0], 0, sizeof(adapter->wpa_ie));
1836 assoc_req->wpa_ie_len = 0;
1837 }
1838
1839 out:
1840 if (ret == 0) {
1841 set_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags);
1842 wlan_postpone_association_work(priv);
1843 } else {
1844 wlan_cancel_association_work(priv);
1845 }
1846 mutex_unlock(&adapter->lock);
1847
1848 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1849 return ret;
1850 }
1851
1852 static int wlan_get_genie(struct net_device *dev,
1853 struct iw_request_info *info,
1854 struct iw_point *dwrq,
1855 char *extra)
1856 {
1857 int ret = 0;
1858 wlan_private *priv = dev->priv;
1859 wlan_adapter *adapter = priv->adapter;
1860
1861 lbs_deb_enter(LBS_DEB_WEXT);
1862
1863 if (adapter->wpa_ie_len == 0) {
1864 dwrq->length = 0;
1865 goto out;
1866 }
1867
1868 if (dwrq->length < adapter->wpa_ie_len) {
1869 ret = -E2BIG;
1870 goto out;
1871 }
1872
1873 dwrq->length = adapter->wpa_ie_len;
1874 memcpy(extra, &adapter->wpa_ie[0], adapter->wpa_ie_len);
1875
1876 out:
1877 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1878 return ret;
1879 }
1880
1881
1882 static int wlan_set_auth(struct net_device *dev,
1883 struct iw_request_info *info,
1884 struct iw_param *dwrq,
1885 char *extra)
1886 {
1887 wlan_private *priv = dev->priv;
1888 wlan_adapter *adapter = priv->adapter;
1889 struct assoc_request * assoc_req;
1890 int ret = 0;
1891 int updated = 0;
1892
1893 lbs_deb_enter(LBS_DEB_WEXT);
1894
1895 mutex_lock(&adapter->lock);
1896 assoc_req = wlan_get_association_request(adapter);
1897 if (!assoc_req) {
1898 ret = -ENOMEM;
1899 goto out;
1900 }
1901
1902 switch (dwrq->flags & IW_AUTH_INDEX) {
1903 case IW_AUTH_TKIP_COUNTERMEASURES:
1904 case IW_AUTH_CIPHER_PAIRWISE:
1905 case IW_AUTH_CIPHER_GROUP:
1906 case IW_AUTH_KEY_MGMT:
1907 /*
1908 * libertas does not use these parameters
1909 */
1910 break;
1911
1912 case IW_AUTH_WPA_VERSION:
1913 if (dwrq->value & IW_AUTH_WPA_VERSION_DISABLED) {
1914 assoc_req->secinfo.WPAenabled = 0;
1915 assoc_req->secinfo.WPA2enabled = 0;
1916 }
1917 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA) {
1918 assoc_req->secinfo.WPAenabled = 1;
1919 assoc_req->secinfo.wep_enabled = 0;
1920 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1921 }
1922 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA2) {
1923 assoc_req->secinfo.WPA2enabled = 1;
1924 assoc_req->secinfo.wep_enabled = 0;
1925 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1926 }
1927 updated = 1;
1928 break;
1929
1930 case IW_AUTH_DROP_UNENCRYPTED:
1931 if (dwrq->value) {
1932 adapter->currentpacketfilter |=
1933 cmd_act_mac_strict_protection_enable;
1934 } else {
1935 adapter->currentpacketfilter &=
1936 ~cmd_act_mac_strict_protection_enable;
1937 }
1938 updated = 1;
1939 break;
1940
1941 case IW_AUTH_80211_AUTH_ALG:
1942 if (dwrq->value & IW_AUTH_ALG_SHARED_KEY) {
1943 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1944 } else if (dwrq->value & IW_AUTH_ALG_OPEN_SYSTEM) {
1945 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1946 } else if (dwrq->value & IW_AUTH_ALG_LEAP) {
1947 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_LEAP;
1948 } else {
1949 ret = -EINVAL;
1950 }
1951 updated = 1;
1952 break;
1953
1954 case IW_AUTH_WPA_ENABLED:
1955 if (dwrq->value) {
1956 if (!assoc_req->secinfo.WPAenabled &&
1957 !assoc_req->secinfo.WPA2enabled) {
1958 assoc_req->secinfo.WPAenabled = 1;
1959 assoc_req->secinfo.WPA2enabled = 1;
1960 assoc_req->secinfo.wep_enabled = 0;
1961 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1962 }
1963 } else {
1964 assoc_req->secinfo.WPAenabled = 0;
1965 assoc_req->secinfo.WPA2enabled = 0;
1966 }
1967 updated = 1;
1968 break;
1969
1970 default:
1971 ret = -EOPNOTSUPP;
1972 break;
1973 }
1974
1975 out:
1976 if (ret == 0) {
1977 if (updated)
1978 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1979 wlan_postpone_association_work(priv);
1980 } else if (ret != -EOPNOTSUPP) {
1981 wlan_cancel_association_work(priv);
1982 }
1983 mutex_unlock(&adapter->lock);
1984
1985 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1986 return ret;
1987 }
1988
1989 static int wlan_get_auth(struct net_device *dev,
1990 struct iw_request_info *info,
1991 struct iw_param *dwrq,
1992 char *extra)
1993 {
1994 int ret = 0;
1995 wlan_private *priv = dev->priv;
1996 wlan_adapter *adapter = priv->adapter;
1997
1998 lbs_deb_enter(LBS_DEB_WEXT);
1999
2000 switch (dwrq->flags & IW_AUTH_INDEX) {
2001 case IW_AUTH_WPA_VERSION:
2002 dwrq->value = 0;
2003 if (adapter->secinfo.WPAenabled)
2004 dwrq->value |= IW_AUTH_WPA_VERSION_WPA;
2005 if (adapter->secinfo.WPA2enabled)
2006 dwrq->value |= IW_AUTH_WPA_VERSION_WPA2;
2007 if (!dwrq->value)
2008 dwrq->value |= IW_AUTH_WPA_VERSION_DISABLED;
2009 break;
2010
2011 case IW_AUTH_DROP_UNENCRYPTED:
2012 dwrq->value = 0;
2013 if (adapter->currentpacketfilter &
2014 cmd_act_mac_strict_protection_enable)
2015 dwrq->value = 1;
2016 break;
2017
2018 case IW_AUTH_80211_AUTH_ALG:
2019 dwrq->value = adapter->secinfo.auth_mode;
2020 break;
2021
2022 case IW_AUTH_WPA_ENABLED:
2023 if (adapter->secinfo.WPAenabled && adapter->secinfo.WPA2enabled)
2024 dwrq->value = 1;
2025 break;
2026
2027 default:
2028 ret = -EOPNOTSUPP;
2029 }
2030
2031 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2032 return ret;
2033 }
2034
2035
2036 static int wlan_set_txpow(struct net_device *dev, struct iw_request_info *info,
2037 struct iw_param *vwrq, char *extra)
2038 {
2039 int ret = 0;
2040 wlan_private *priv = dev->priv;
2041 wlan_adapter *adapter = priv->adapter;
2042
2043 u16 dbm;
2044
2045 lbs_deb_enter(LBS_DEB_WEXT);
2046
2047 if (vwrq->disabled) {
2048 wlan_radio_ioctl(priv, RADIO_OFF);
2049 return 0;
2050 }
2051
2052 adapter->preamble = cmd_type_auto_preamble;
2053
2054 wlan_radio_ioctl(priv, RADIO_ON);
2055
2056 if ((vwrq->flags & IW_TXPOW_TYPE) == IW_TXPOW_MWATT) {
2057 dbm = (u16) mw_to_dbm(vwrq->value);
2058 } else
2059 dbm = (u16) vwrq->value;
2060
2061 /* auto tx power control */
2062
2063 if (vwrq->fixed == 0)
2064 dbm = 0xffff;
2065
2066 lbs_deb_wext("txpower set %d dbm\n", dbm);
2067
2068 ret = libertas_prepare_and_send_command(priv,
2069 cmd_802_11_rf_tx_power,
2070 cmd_act_tx_power_opt_set_low,
2071 cmd_option_waitforrsp, 0, (void *)&dbm);
2072
2073 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2074 return ret;
2075 }
2076
2077 static int wlan_get_essid(struct net_device *dev, struct iw_request_info *info,
2078 struct iw_point *dwrq, char *extra)
2079 {
2080 wlan_private *priv = dev->priv;
2081 wlan_adapter *adapter = priv->adapter;
2082
2083 lbs_deb_enter(LBS_DEB_WEXT);
2084
2085 /*
2086 * Note : if dwrq->flags != 0, we should get the relevant SSID from
2087 * the SSID list...
2088 */
2089
2090 /*
2091 * Get the current SSID
2092 */
2093 if (adapter->connect_status == libertas_connected) {
2094 memcpy(extra, adapter->curbssparams.ssid.ssid,
2095 adapter->curbssparams.ssid.ssidlength);
2096 extra[adapter->curbssparams.ssid.ssidlength] = '\0';
2097 } else {
2098 memset(extra, 0, 32);
2099 extra[adapter->curbssparams.ssid.ssidlength] = '\0';
2100 }
2101 /*
2102 * If none, we may want to get the one that was set
2103 */
2104
2105 /* To make the driver backward compatible with WPA supplicant v0.2.4 */
2106 if (dwrq->length == 32) /* check with WPA supplicant buffer size */
2107 dwrq->length = min_t(size_t, adapter->curbssparams.ssid.ssidlength,
2108 IW_ESSID_MAX_SIZE);
2109 else
2110 dwrq->length = adapter->curbssparams.ssid.ssidlength + 1;
2111
2112 dwrq->flags = 1; /* active */
2113
2114 lbs_deb_leave(LBS_DEB_WEXT);
2115 return 0;
2116 }
2117
2118 static int wlan_set_essid(struct net_device *dev, struct iw_request_info *info,
2119 struct iw_point *dwrq, char *extra)
2120 {
2121 wlan_private *priv = dev->priv;
2122 wlan_adapter *adapter = priv->adapter;
2123 int ret = 0;
2124 struct WLAN_802_11_SSID ssid;
2125 struct assoc_request * assoc_req;
2126 int ssid_len = dwrq->length;
2127
2128 lbs_deb_enter(LBS_DEB_WEXT);
2129
2130 /*
2131 * WE-20 and earlier NULL pad the end of the SSID and increment
2132 * SSID length so it can be used like a string. WE-21 and later don't,
2133 * but some userspace tools aren't able to cope with the change.
2134 */
2135 if ((ssid_len > 0) && (extra[ssid_len - 1] == '\0'))
2136 ssid_len--;
2137
2138 /* Check the size of the string */
2139 if (ssid_len > IW_ESSID_MAX_SIZE) {
2140 ret = -E2BIG;
2141 goto out;
2142 }
2143
2144 memset(&ssid, 0, sizeof(struct WLAN_802_11_SSID));
2145
2146 if (!dwrq->flags || !ssid_len) {
2147 /* "any" SSID requested; leave SSID blank */
2148 } else {
2149 /* Specific SSID requested */
2150 memcpy(&ssid.ssid, extra, ssid_len);
2151 ssid.ssidlength = ssid_len;
2152 }
2153
2154 lbs_deb_wext("requested new SSID '%s'\n",
2155 (ssid.ssidlength > 0) ? (char *)ssid.ssid : "any");
2156
2157 out:
2158 mutex_lock(&adapter->lock);
2159 if (ret == 0) {
2160 /* Get or create the current association request */
2161 assoc_req = wlan_get_association_request(adapter);
2162 if (!assoc_req) {
2163 ret = -ENOMEM;
2164 } else {
2165 /* Copy the SSID to the association request */
2166 memcpy(&assoc_req->ssid, &ssid, sizeof(struct WLAN_802_11_SSID));
2167 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
2168 wlan_postpone_association_work(priv);
2169 }
2170 }
2171
2172 /* Cancel the association request if there was an error */
2173 if (ret != 0) {
2174 wlan_cancel_association_work(priv);
2175 }
2176
2177 mutex_unlock(&adapter->lock);
2178
2179 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2180 return ret;
2181 }
2182
2183 /**
2184 * @brief Connect to the AP or Ad-hoc Network with specific bssid
2185 *
2186 * @param dev A pointer to net_device structure
2187 * @param info A pointer to iw_request_info structure
2188 * @param awrq A pointer to iw_param structure
2189 * @param extra A pointer to extra data buf
2190 * @return 0 --success, otherwise fail
2191 */
2192 static int wlan_set_wap(struct net_device *dev, struct iw_request_info *info,
2193 struct sockaddr *awrq, char *extra)
2194 {
2195 wlan_private *priv = dev->priv;
2196 wlan_adapter *adapter = priv->adapter;
2197 struct assoc_request * assoc_req;
2198 int ret = 0;
2199
2200 lbs_deb_enter(LBS_DEB_WEXT);
2201
2202 if (awrq->sa_family != ARPHRD_ETHER)
2203 return -EINVAL;
2204
2205 lbs_deb_wext("ASSOC: WAP: sa_data " MAC_FMT "\n", MAC_ARG(awrq->sa_data));
2206
2207 mutex_lock(&adapter->lock);
2208
2209 /* Get or create the current association request */
2210 assoc_req = wlan_get_association_request(adapter);
2211 if (!assoc_req) {
2212 wlan_cancel_association_work(priv);
2213 ret = -ENOMEM;
2214 } else {
2215 /* Copy the BSSID to the association request */
2216 memcpy(&assoc_req->bssid, awrq->sa_data, ETH_ALEN);
2217 set_bit(ASSOC_FLAG_BSSID, &assoc_req->flags);
2218 wlan_postpone_association_work(priv);
2219 }
2220
2221 mutex_unlock(&adapter->lock);
2222
2223 return ret;
2224 }
2225
2226 void libertas_get_fwversion(wlan_adapter * adapter, char *fwversion, int maxlen)
2227 {
2228 union {
2229 u32 l;
2230 u8 c[4];
2231 } ver;
2232 char fwver[32];
2233
2234 mutex_lock(&adapter->lock);
2235 ver.l = adapter->fwreleasenumber;
2236 mutex_unlock(&adapter->lock);
2237
2238 if (ver.c[3] == 0)
2239 sprintf(fwver, "%u.%u.%u", ver.c[2], ver.c[1], ver.c[0]);
2240 else
2241 sprintf(fwver, "%u.%u.%u.p%u",
2242 ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
2243
2244 snprintf(fwversion, maxlen, fwver);
2245 }
2246
2247
2248 /*
2249 * iwconfig settable callbacks
2250 */
2251 static const iw_handler wlan_handler[] = {
2252 (iw_handler) NULL, /* SIOCSIWCOMMIT */
2253 (iw_handler) wlan_get_name, /* SIOCGIWNAME */
2254 (iw_handler) NULL, /* SIOCSIWNWID */
2255 (iw_handler) NULL, /* SIOCGIWNWID */
2256 (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
2257 (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
2258 (iw_handler) wlan_set_mode, /* SIOCSIWMODE */
2259 (iw_handler) wlan_get_mode, /* SIOCGIWMODE */
2260 (iw_handler) NULL, /* SIOCSIWSENS */
2261 (iw_handler) NULL, /* SIOCGIWSENS */
2262 (iw_handler) NULL, /* SIOCSIWRANGE */
2263 (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
2264 (iw_handler) NULL, /* SIOCSIWPRIV */
2265 (iw_handler) NULL, /* SIOCGIWPRIV */
2266 (iw_handler) NULL, /* SIOCSIWSTATS */
2267 (iw_handler) NULL, /* SIOCGIWSTATS */
2268 iw_handler_set_spy, /* SIOCSIWSPY */
2269 iw_handler_get_spy, /* SIOCGIWSPY */
2270 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
2271 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
2272 (iw_handler) wlan_set_wap, /* SIOCSIWAP */
2273 (iw_handler) wlan_get_wap, /* SIOCGIWAP */
2274 (iw_handler) NULL, /* SIOCSIWMLME */
2275 (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
2276 (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2277 (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2278 (iw_handler) wlan_set_essid, /* SIOCSIWESSID */
2279 (iw_handler) wlan_get_essid, /* SIOCGIWESSID */
2280 (iw_handler) wlan_set_nick, /* SIOCSIWNICKN */
2281 (iw_handler) wlan_get_nick, /* SIOCGIWNICKN */
2282 (iw_handler) NULL, /* -- hole -- */
2283 (iw_handler) NULL, /* -- hole -- */
2284 (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
2285 (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
2286 (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
2287 (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
2288 (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
2289 (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
2290 (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
2291 (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
2292 (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
2293 (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
2294 (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
2295 (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
2296 (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
2297 (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
2298 (iw_handler) NULL, /* -- hole -- */
2299 (iw_handler) NULL, /* -- hole -- */
2300 (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
2301 (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
2302 (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
2303 (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
2304 (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2305 (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2306 (iw_handler) NULL, /* SIOCSIWPMKSA */
2307 };
2308
2309 struct iw_handler_def libertas_handler_def = {
2310 .num_standard = sizeof(wlan_handler) / sizeof(iw_handler),
2311 .num_private = sizeof(wlan_private_handler) / sizeof(iw_handler),
2312 .num_private_args = sizeof(wlan_private_args) /
2313 sizeof(struct iw_priv_args),
2314 .standard = (iw_handler *) wlan_handler,
2315 .private = (iw_handler *) wlan_private_handler,
2316 .private_args = (struct iw_priv_args *)wlan_private_args,
2317 .get_wireless_stats = wlan_get_wireless_stats,
2318 };