[PATCH] libertas: first pass at fixing up endianness issues
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / libertas / wext.c
blob1abb4bcecc66fb0e2ef32e7502ccdb0a1aa4dd96
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>
11 #include <net/ieee80211.h>
12 #include <net/iw_handler.h>
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"
24 /**
25 * the rates supported by the card
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
32 /**
33 * @brief Convert mw value to dbm value
35 * @param mw the value of mw
36 * @return the value of dbm
38 static int mw_to_dbm(int mw)
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;
78 /**
79 * @brief Find the channel frequency power info with specific channel
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.
86 struct chan_freq_power *libertas_find_cfp_by_band_and_channel(wlan_adapter * adapter,
87 u8 band, u16 channel)
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;
95 for (j = 0; !cfp && (j < count); j++) {
96 rc = &adapter->region_channel[j];
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;
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);
116 return cfp;
120 * @brief Find the channel frequency power info with specific frequency
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.
127 static struct chan_freq_power *find_cfp_by_band_and_freq(wlan_adapter * adapter,
128 u8 band, u32 freq)
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;
136 for (j = 0; !cfp && (j < count); j++) {
137 rc = &adapter->region_channel[j];
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;
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);
157 return cfp;
162 * @brief Set Radio On/OFF
164 * @param priv A pointer to wlan_private structure
165 * @option Radio Option
166 * @return 0 --success, otherwise fail
168 int wlan_radio_ioctl(wlan_private * priv, u8 option)
170 int ret = 0;
171 wlan_adapter *adapter = priv->adapter;
173 lbs_deb_enter(LBS_DEB_WEXT);
175 if (adapter->radioon != option) {
176 lbs_deb_wext("switching radio %s\n", option ? "on" : "off");
177 adapter->radioon = option;
179 ret = libertas_prepare_and_send_command(priv,
180 cmd_802_11_radio_control,
181 cmd_act_set,
182 cmd_option_waitforrsp, 0, NULL);
185 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
186 return ret;
190 * @brief Copy rates
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
197 static inline int copyrates(u8 * dest, int pos, u8 * src, int len)
199 int i;
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];
207 return pos;
211 * @brief Get active data rates
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
217 static int get_active_data_rates(wlan_adapter * adapter,
218 u8* rates)
220 int k = 0;
222 lbs_deb_enter(LBS_DEB_WEXT);
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));
234 } else {
235 k = copyrates(rates, 0, adapter->curbssparams.datarates,
236 adapter->curbssparams.numofrates);
239 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", k);
240 return k;
243 static int wlan_get_name(struct net_device *dev, struct iw_request_info *info,
244 char *cwrq, char *extra)
246 const char *cp;
247 char comm[6] = { "COMM-" };
248 char mrvl[6] = { "MRVL-" };
249 int cnt;
251 lbs_deb_enter(LBS_DEB_WEXT);
253 strcpy(cwrq, mrvl);
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;
261 cnt = strlen(mrvl);
262 cwrq += cnt;
263 while (cnt < 16 && (*cp != '-')) {
264 *cwrq++ = toupper(*cp++);
265 cnt++;
267 *cwrq = '\0';
269 lbs_deb_leave(LBS_DEB_WEXT);
270 return 0;
273 static int wlan_get_freq(struct net_device *dev, struct iw_request_info *info,
274 struct iw_freq *fwrq, char *extra)
276 wlan_private *priv = dev->priv;
277 wlan_adapter *adapter = priv->adapter;
278 struct chan_freq_power *cfp;
280 lbs_deb_enter(LBS_DEB_WEXT);
282 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0,
283 adapter->curbssparams.channel);
285 if (!cfp) {
286 if (adapter->curbssparams.channel)
287 lbs_deb_wext("invalid channel %d\n",
288 adapter->curbssparams.channel);
289 return -EINVAL;
292 fwrq->m = (long)cfp->freq * 100000;
293 fwrq->e = 1;
295 lbs_deb_wext("freq %u\n", fwrq->m);
296 lbs_deb_leave(LBS_DEB_WEXT);
297 return 0;
300 static int wlan_get_wap(struct net_device *dev, struct iw_request_info *info,
301 struct sockaddr *awrq, char *extra)
303 wlan_private *priv = dev->priv;
304 wlan_adapter *adapter = priv->adapter;
306 lbs_deb_enter(LBS_DEB_WEXT);
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);
313 awrq->sa_family = ARPHRD_ETHER;
315 lbs_deb_leave(LBS_DEB_WEXT);
316 return 0;
319 static int wlan_set_nick(struct net_device *dev, struct iw_request_info *info,
320 struct iw_point *dwrq, char *extra)
322 wlan_private *priv = dev->priv;
323 wlan_adapter *adapter = priv->adapter;
325 lbs_deb_enter(LBS_DEB_WEXT);
328 * Check the size of the string
331 if (dwrq->length > 16) {
332 return -E2BIG;
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);
340 lbs_deb_leave(LBS_DEB_WEXT);
341 return 0;
344 static int wlan_get_nick(struct net_device *dev, struct iw_request_info *info,
345 struct iw_point *dwrq, char *extra)
347 wlan_private *priv = dev->priv;
348 wlan_adapter *adapter = priv->adapter;
350 lbs_deb_enter(LBS_DEB_WEXT);
353 * Get the Nick Name saved
356 mutex_lock(&adapter->lock);
357 strncpy(extra, adapter->nodename, 16);
358 mutex_unlock(&adapter->lock);
360 extra[16] = '\0';
363 * If none, we may want to get the one that was set
367 * Push it out !
369 dwrq->length = strlen(extra) + 1;
371 lbs_deb_leave(LBS_DEB_WEXT);
372 return 0;
375 static int mesh_get_nick(struct net_device *dev, struct iw_request_info *info,
376 struct iw_point *dwrq, char *extra)
378 wlan_private *priv = dev->priv;
379 wlan_adapter *adapter = priv->adapter;
381 lbs_deb_enter(LBS_DEB_WEXT);
383 /* Use nickname to indicate that mesh is on */
385 if (adapter->connect_status == libertas_connected) {
386 strncpy(extra, "Mesh", 12);
387 extra[12] = '\0';
388 dwrq->length = strlen(extra) + 1;
391 else {
392 extra[0] = '\0';
393 dwrq->length = 1 ;
396 lbs_deb_leave(LBS_DEB_WEXT);
397 return 0;
399 static int wlan_set_rts(struct net_device *dev, struct iw_request_info *info,
400 struct iw_param *vwrq, char *extra)
402 int ret = 0;
403 wlan_private *priv = dev->priv;
404 wlan_adapter *adapter = priv->adapter;
405 u32 rthr = vwrq->value;
407 lbs_deb_enter(LBS_DEB_WEXT);
409 if (vwrq->disabled) {
410 adapter->rtsthsd = rthr = MRVDRV_RTS_MAX_VALUE;
411 } else {
412 if (rthr < MRVDRV_RTS_MIN_VALUE || rthr > MRVDRV_RTS_MAX_VALUE)
413 return -EINVAL;
414 adapter->rtsthsd = rthr;
417 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
418 cmd_act_set, cmd_option_waitforrsp,
419 OID_802_11_RTS_THRESHOLD, &rthr);
421 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
422 return ret;
425 static int wlan_get_rts(struct net_device *dev, struct iw_request_info *info,
426 struct iw_param *vwrq, char *extra)
428 int ret = 0;
429 wlan_private *priv = dev->priv;
430 wlan_adapter *adapter = priv->adapter;
432 lbs_deb_enter(LBS_DEB_WEXT);
434 adapter->rtsthsd = 0;
435 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
436 cmd_act_get, cmd_option_waitforrsp,
437 OID_802_11_RTS_THRESHOLD, NULL);
438 if (ret)
439 goto out;
441 vwrq->value = adapter->rtsthsd;
442 vwrq->disabled = ((vwrq->value < MRVDRV_RTS_MIN_VALUE)
443 || (vwrq->value > MRVDRV_RTS_MAX_VALUE));
444 vwrq->fixed = 1;
446 out:
447 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
448 return ret;
451 static int wlan_set_frag(struct net_device *dev, struct iw_request_info *info,
452 struct iw_param *vwrq, char *extra)
454 int ret = 0;
455 u32 fthr = vwrq->value;
456 wlan_private *priv = dev->priv;
457 wlan_adapter *adapter = priv->adapter;
459 lbs_deb_enter(LBS_DEB_WEXT);
461 if (vwrq->disabled) {
462 adapter->fragthsd = fthr = MRVDRV_FRAG_MAX_VALUE;
463 } else {
464 if (fthr < MRVDRV_FRAG_MIN_VALUE
465 || fthr > MRVDRV_FRAG_MAX_VALUE)
466 return -EINVAL;
467 adapter->fragthsd = fthr;
470 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
471 cmd_act_set, cmd_option_waitforrsp,
472 OID_802_11_FRAGMENTATION_THRESHOLD, &fthr);
474 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
475 return ret;
478 static int wlan_get_frag(struct net_device *dev, struct iw_request_info *info,
479 struct iw_param *vwrq, char *extra)
481 int ret = 0;
482 wlan_private *priv = dev->priv;
483 wlan_adapter *adapter = priv->adapter;
485 lbs_deb_enter(LBS_DEB_WEXT);
487 adapter->fragthsd = 0;
488 ret = libertas_prepare_and_send_command(priv,
489 cmd_802_11_snmp_mib,
490 cmd_act_get, cmd_option_waitforrsp,
491 OID_802_11_FRAGMENTATION_THRESHOLD, NULL);
492 if (ret)
493 goto out;
495 vwrq->value = adapter->fragthsd;
496 vwrq->disabled = ((vwrq->value < MRVDRV_FRAG_MIN_VALUE)
497 || (vwrq->value > MRVDRV_FRAG_MAX_VALUE));
498 vwrq->fixed = 1;
500 out:
501 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
502 return ret;
505 static int wlan_get_mode(struct net_device *dev,
506 struct iw_request_info *info, u32 * uwrq, char *extra)
508 wlan_private *priv = dev->priv;
509 wlan_adapter *adapter = priv->adapter;
511 lbs_deb_enter(LBS_DEB_WEXT);
513 *uwrq = adapter->mode;
515 lbs_deb_leave(LBS_DEB_WEXT);
516 return 0;
519 static int mesh_wlan_get_mode(struct net_device *dev,
520 struct iw_request_info *info, u32 * uwrq,
521 char *extra)
523 lbs_deb_enter(LBS_DEB_WEXT);
525 *uwrq = IW_MODE_REPEAT ;
527 lbs_deb_leave(LBS_DEB_WEXT);
528 return 0;
531 static int wlan_get_txpow(struct net_device *dev,
532 struct iw_request_info *info,
533 struct iw_param *vwrq, char *extra)
535 int ret = 0;
536 wlan_private *priv = dev->priv;
537 wlan_adapter *adapter = priv->adapter;
539 lbs_deb_enter(LBS_DEB_WEXT);
541 ret = libertas_prepare_and_send_command(priv,
542 cmd_802_11_rf_tx_power,
543 cmd_act_tx_power_opt_get,
544 cmd_option_waitforrsp, 0, NULL);
546 if (ret)
547 goto out;
549 lbs_deb_wext("tx power level %d dbm\n", adapter->txpowerlevel);
550 vwrq->value = adapter->txpowerlevel;
551 vwrq->fixed = 1;
552 if (adapter->radioon) {
553 vwrq->disabled = 0;
554 vwrq->flags = IW_TXPOW_DBM;
555 } else {
556 vwrq->disabled = 1;
559 out:
560 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
561 return ret;
564 static int wlan_set_retry(struct net_device *dev, struct iw_request_info *info,
565 struct iw_param *vwrq, char *extra)
567 int ret = 0;
568 wlan_private *priv = dev->priv;
569 wlan_adapter *adapter = priv->adapter;
571 lbs_deb_enter(LBS_DEB_WEXT);
573 if (vwrq->flags == IW_RETRY_LIMIT) {
574 /* The MAC has a 4-bit Total_Tx_Count register
575 Total_Tx_Count = 1 + Tx_Retry_Count */
576 #define TX_RETRY_MIN 0
577 #define TX_RETRY_MAX 14
578 if (vwrq->value < TX_RETRY_MIN || vwrq->value > TX_RETRY_MAX)
579 return -EINVAL;
581 /* Adding 1 to convert retry count to try count */
582 adapter->txretrycount = vwrq->value + 1;
584 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
585 cmd_act_set,
586 cmd_option_waitforrsp,
587 OID_802_11_TX_RETRYCOUNT, NULL);
589 if (ret)
590 goto out;
591 } else {
592 return -EOPNOTSUPP;
595 out:
596 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
597 return ret;
600 static int wlan_get_retry(struct net_device *dev, struct iw_request_info *info,
601 struct iw_param *vwrq, char *extra)
603 wlan_private *priv = dev->priv;
604 wlan_adapter *adapter = priv->adapter;
605 int ret = 0;
607 lbs_deb_enter(LBS_DEB_WEXT);
609 adapter->txretrycount = 0;
610 ret = libertas_prepare_and_send_command(priv,
611 cmd_802_11_snmp_mib,
612 cmd_act_get, cmd_option_waitforrsp,
613 OID_802_11_TX_RETRYCOUNT, NULL);
614 if (ret)
615 goto out;
617 vwrq->disabled = 0;
618 if (!vwrq->flags) {
619 vwrq->flags = IW_RETRY_LIMIT;
620 /* Subtract 1 to convert try count to retry count */
621 vwrq->value = adapter->txretrycount - 1;
624 out:
625 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
626 return ret;
629 static inline void sort_channels(struct iw_freq *freq, int num)
631 int i, j;
632 struct iw_freq temp;
634 for (i = 0; i < num; i++)
635 for (j = i + 1; j < num; j++)
636 if (freq[i].i > freq[j].i) {
637 temp.i = freq[i].i;
638 temp.m = freq[i].m;
640 freq[i].i = freq[j].i;
641 freq[i].m = freq[j].m;
643 freq[j].i = temp.i;
644 freq[j].m = temp.m;
648 /* data rate listing
649 MULTI_BANDS:
650 abg a b b/g
651 Infra G(12) A(8) B(4) G(12)
652 Adhoc A+B(12) A(8) B(4) B(4)
654 non-MULTI_BANDS:
655 b b/g
656 Infra B(4) G(12)
657 Adhoc B(4) B(4)
660 * @brief Get Range Info
662 * @param dev A pointer to net_device structure
663 * @param info A pointer to iw_request_info structure
664 * @param vwrq A pointer to iw_param structure
665 * @param extra A pointer to extra data buf
666 * @return 0 --success, otherwise fail
668 static int wlan_get_range(struct net_device *dev, struct iw_request_info *info,
669 struct iw_point *dwrq, char *extra)
671 int i, j;
672 wlan_private *priv = dev->priv;
673 wlan_adapter *adapter = priv->adapter;
674 struct iw_range *range = (struct iw_range *)extra;
675 struct chan_freq_power *cfp;
676 u8 rates[WLAN_SUPPORTED_RATES];
678 u8 flag = 0;
680 lbs_deb_enter(LBS_DEB_WEXT);
682 dwrq->length = sizeof(struct iw_range);
683 memset(range, 0, sizeof(struct iw_range));
685 range->min_nwid = 0;
686 range->max_nwid = 0;
688 memset(rates, 0, sizeof(rates));
689 range->num_bitrates = get_active_data_rates(adapter, rates);
691 for (i = 0; i < min_t(__u8, range->num_bitrates, IW_MAX_BITRATES) && rates[i];
692 i++) {
693 range->bitrate[i] = (rates[i] & 0x7f) * 500000;
695 range->num_bitrates = i;
696 lbs_deb_wext("IW_MAX_BITRATES %d, num_bitrates %d\n", IW_MAX_BITRATES,
697 range->num_bitrates);
699 range->num_frequency = 0;
700 if (priv->adapter->enable11d &&
701 adapter->connect_status == libertas_connected) {
702 u8 chan_no;
703 u8 band;
705 struct parsed_region_chan_11d *parsed_region_chan =
706 &adapter->parsed_region_chan;
708 if (parsed_region_chan == NULL) {
709 lbs_deb_wext("11d: parsed_region_chan is NULL\n");
710 goto out;
712 band = parsed_region_chan->band;
713 lbs_deb_wext("band %d, nr_char %d\n", band,
714 parsed_region_chan->nr_chan);
716 for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
717 && (i < parsed_region_chan->nr_chan); i++) {
718 chan_no = parsed_region_chan->chanpwr[i].chan;
719 lbs_deb_wext("chan_no %d\n", chan_no);
720 range->freq[range->num_frequency].i = (long)chan_no;
721 range->freq[range->num_frequency].m =
722 (long)libertas_chan_2_freq(chan_no, band) * 100000;
723 range->freq[range->num_frequency].e = 1;
724 range->num_frequency++;
726 flag = 1;
728 if (!flag) {
729 for (j = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
730 && (j < sizeof(adapter->region_channel)
731 / sizeof(adapter->region_channel[0])); j++) {
732 cfp = adapter->region_channel[j].CFP;
733 for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
734 && adapter->region_channel[j].valid
735 && cfp
736 && (i < adapter->region_channel[j].nrcfp); i++) {
737 range->freq[range->num_frequency].i =
738 (long)cfp->channel;
739 range->freq[range->num_frequency].m =
740 (long)cfp->freq * 100000;
741 range->freq[range->num_frequency].e = 1;
742 cfp++;
743 range->num_frequency++;
748 lbs_deb_wext("IW_MAX_FREQUENCIES %d, num_frequency %d\n",
749 IW_MAX_FREQUENCIES, range->num_frequency);
751 range->num_channels = range->num_frequency;
753 sort_channels(&range->freq[0], range->num_frequency);
756 * Set an indication of the max TCP throughput in bit/s that we can
757 * expect using this interface
759 if (i > 2)
760 range->throughput = 5000 * 1000;
761 else
762 range->throughput = 1500 * 1000;
764 range->min_rts = MRVDRV_RTS_MIN_VALUE;
765 range->max_rts = MRVDRV_RTS_MAX_VALUE;
766 range->min_frag = MRVDRV_FRAG_MIN_VALUE;
767 range->max_frag = MRVDRV_FRAG_MAX_VALUE;
769 range->encoding_size[0] = 5;
770 range->encoding_size[1] = 13;
771 range->num_encoding_sizes = 2;
772 range->max_encoding_tokens = 4;
774 range->min_pmp = 1000000;
775 range->max_pmp = 120000000;
776 range->min_pmt = 1000;
777 range->max_pmt = 1000000;
778 range->pmp_flags = IW_POWER_PERIOD;
779 range->pmt_flags = IW_POWER_TIMEOUT;
780 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
783 * Minimum version we recommend
785 range->we_version_source = 15;
788 * Version we are compiled with
790 range->we_version_compiled = WIRELESS_EXT;
792 range->retry_capa = IW_RETRY_LIMIT;
793 range->retry_flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
795 range->min_retry = TX_RETRY_MIN;
796 range->max_retry = TX_RETRY_MAX;
799 * Set the qual, level and noise range values
801 range->max_qual.qual = 100;
802 range->max_qual.level = 0;
803 range->max_qual.noise = 0;
804 range->max_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
806 range->avg_qual.qual = 70;
807 /* TODO: Find real 'good' to 'bad' threshold value for RSSI */
808 range->avg_qual.level = 0;
809 range->avg_qual.noise = 0;
810 range->avg_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
812 range->sensitivity = 0;
815 * Setup the supported power level ranges
817 memset(range->txpower, 0, sizeof(range->txpower));
818 range->txpower[0] = 5;
819 range->txpower[1] = 7;
820 range->txpower[2] = 9;
821 range->txpower[3] = 11;
822 range->txpower[4] = 13;
823 range->txpower[5] = 15;
824 range->txpower[6] = 17;
825 range->txpower[7] = 19;
827 range->num_txpower = 8;
828 range->txpower_capa = IW_TXPOW_DBM;
829 range->txpower_capa |= IW_TXPOW_RANGE;
831 range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
832 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
833 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
834 range->event_capa[1] = IW_EVENT_CAPA_K_1;
836 if (adapter->fwcapinfo & FW_CAPINFO_WPA) {
837 range->enc_capa = IW_ENC_CAPA_WPA
838 | IW_ENC_CAPA_WPA2
839 | IW_ENC_CAPA_CIPHER_TKIP
840 | IW_ENC_CAPA_CIPHER_CCMP;
843 out:
844 lbs_deb_leave(LBS_DEB_WEXT);
845 return 0;
848 static int wlan_set_power(struct net_device *dev, struct iw_request_info *info,
849 struct iw_param *vwrq, char *extra)
851 wlan_private *priv = dev->priv;
852 wlan_adapter *adapter = priv->adapter;
854 lbs_deb_enter(LBS_DEB_WEXT);
856 /* PS is currently supported only in Infrastructure mode
857 * Remove this check if it is to be supported in IBSS mode also
860 if (vwrq->disabled) {
861 adapter->psmode = wlan802_11powermodecam;
862 if (adapter->psstate != PS_STATE_FULL_POWER) {
863 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
866 return 0;
869 if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
870 lbs_deb_wext(
871 "setting power timeout is not supported\n");
872 return -EINVAL;
873 } else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
874 lbs_deb_wext("setting power period not supported\n");
875 return -EINVAL;
878 if (adapter->psmode != wlan802_11powermodecam) {
879 return 0;
882 adapter->psmode = wlan802_11powermodemax_psp;
884 if (adapter->connect_status == libertas_connected) {
885 libertas_ps_sleep(priv, cmd_option_waitforrsp);
888 lbs_deb_leave(LBS_DEB_WEXT);
889 return 0;
892 static int wlan_get_power(struct net_device *dev, struct iw_request_info *info,
893 struct iw_param *vwrq, char *extra)
895 wlan_private *priv = dev->priv;
896 wlan_adapter *adapter = priv->adapter;
897 int mode;
899 lbs_deb_enter(LBS_DEB_WEXT);
901 mode = adapter->psmode;
903 if ((vwrq->disabled = (mode == wlan802_11powermodecam))
904 || adapter->connect_status == libertas_disconnected)
906 goto out;
909 vwrq->value = 0;
911 out:
912 lbs_deb_leave(LBS_DEB_WEXT);
913 return 0;
917 * iwpriv settable callbacks
920 static const iw_handler wlan_private_handler[] = {
921 NULL, /* SIOCIWFIRSTPRIV */
924 static const struct iw_priv_args wlan_private_args[] = {
926 * { cmd, set_args, get_args, name }
928 /* Using iwpriv sub-command feature */
930 WLAN_SETONEINT_GETNONE, /* IOCTL: 24 */
931 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
932 IW_PRIV_TYPE_NONE,
933 ""},
935 WLANSETREGION,
936 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
937 IW_PRIV_TYPE_NONE,
938 "setregioncode"},
940 WLAN_SUBCMD_MESH_SET_TTL,
941 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
942 IW_PRIV_TYPE_NONE,
943 "mesh_set_ttl"},
945 WLAN_SETNONE_GETONEINT,
946 IW_PRIV_TYPE_NONE,
947 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
948 ""},
950 WLANGETREGION,
951 IW_PRIV_TYPE_NONE,
952 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
953 "getregioncode"},
955 WLAN_SUBCMD_FWT_CLEANUP,
956 IW_PRIV_TYPE_NONE,
957 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
958 "fwt_cleanup"},
960 WLAN_SUBCMD_FWT_TIME,
961 IW_PRIV_TYPE_NONE,
962 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
963 "fwt_time"},
965 WLAN_SUBCMD_MESH_GET_TTL,
966 IW_PRIV_TYPE_NONE,
967 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
968 "mesh_get_ttl"},
970 WLAN_SETNONE_GETNONE,
971 IW_PRIV_TYPE_NONE,
972 IW_PRIV_TYPE_NONE,
973 ""},
975 WLAN_SUBCMD_FWT_RESET,
976 IW_PRIV_TYPE_NONE,
977 IW_PRIV_TYPE_NONE,
978 "fwt_reset"},
980 WLAN_SUBCMD_BT_RESET,
981 IW_PRIV_TYPE_NONE,
982 IW_PRIV_TYPE_NONE,
983 "bt_reset"},
985 WLAN_SET128CHAR_GET128CHAR,
986 IW_PRIV_TYPE_CHAR | 128,
987 IW_PRIV_TYPE_CHAR | 128,
988 ""},
989 /* BT Management */
991 WLAN_SUBCMD_BT_ADD,
992 IW_PRIV_TYPE_CHAR | 128,
993 IW_PRIV_TYPE_CHAR | 128,
994 "bt_add"},
996 WLAN_SUBCMD_BT_DEL,
997 IW_PRIV_TYPE_CHAR | 128,
998 IW_PRIV_TYPE_CHAR | 128,
999 "bt_del"},
1001 WLAN_SUBCMD_BT_LIST,
1002 IW_PRIV_TYPE_CHAR | 128,
1003 IW_PRIV_TYPE_CHAR | 128,
1004 "bt_list"},
1006 WLAN_SUBCMD_BT_SET_INVERT,
1007 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1008 IW_PRIV_TYPE_NONE,
1009 "bt_set_invert"},
1011 WLAN_SUBCMD_BT_GET_INVERT,
1012 IW_PRIV_TYPE_NONE,
1013 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1014 "bt_get_invert"},
1015 /* FWT Management */
1017 WLAN_SUBCMD_FWT_ADD,
1018 IW_PRIV_TYPE_CHAR | 128,
1019 IW_PRIV_TYPE_CHAR | 128,
1020 "fwt_add"},
1022 WLAN_SUBCMD_FWT_DEL,
1023 IW_PRIV_TYPE_CHAR | 128,
1024 IW_PRIV_TYPE_CHAR | 128,
1025 "fwt_del"},
1027 WLAN_SUBCMD_FWT_LOOKUP,
1028 IW_PRIV_TYPE_CHAR | 128,
1029 IW_PRIV_TYPE_CHAR | 128,
1030 "fwt_lookup"},
1032 WLAN_SUBCMD_FWT_LIST_NEIGHBOR,
1033 IW_PRIV_TYPE_CHAR | 128,
1034 IW_PRIV_TYPE_CHAR | 128,
1035 "fwt_list_neigh"},
1037 WLAN_SUBCMD_FWT_LIST,
1038 IW_PRIV_TYPE_CHAR | 128,
1039 IW_PRIV_TYPE_CHAR | 128,
1040 "fwt_list"},
1042 WLAN_SUBCMD_FWT_LIST_ROUTE,
1043 IW_PRIV_TYPE_CHAR | 128,
1044 IW_PRIV_TYPE_CHAR | 128,
1045 "fwt_list_route"},
1047 WLAN_SET_GET_SIXTEEN_INT,
1048 IW_PRIV_TYPE_INT | 16,
1049 IW_PRIV_TYPE_INT | 16,
1050 ""},
1052 WLAN_LED_GPIO_CTRL,
1053 IW_PRIV_TYPE_INT | 16,
1054 IW_PRIV_TYPE_INT | 16,
1055 "ledgpio"},
1058 static struct iw_statistics *wlan_get_wireless_stats(struct net_device *dev)
1060 enum {
1061 POOR = 30,
1062 FAIR = 60,
1063 GOOD = 80,
1064 VERY_GOOD = 90,
1065 EXCELLENT = 95,
1066 PERFECT = 100
1068 wlan_private *priv = dev->priv;
1069 wlan_adapter *adapter = priv->adapter;
1070 u32 rssi_qual;
1071 u32 tx_qual;
1072 u32 quality = 0;
1073 int stats_valid = 0;
1074 u8 rssi;
1075 u32 tx_retries;
1077 lbs_deb_enter(LBS_DEB_WEXT);
1079 priv->wstats.status = adapter->mode;
1081 /* If we're not associated, all quality values are meaningless */
1082 if (adapter->connect_status != libertas_connected)
1083 goto out;
1085 /* Quality by RSSI */
1086 priv->wstats.qual.level =
1087 CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_NOAVG],
1088 adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1090 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1091 priv->wstats.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1092 } else {
1093 priv->wstats.qual.noise =
1094 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1097 lbs_deb_wext("signal level %#x\n", priv->wstats.qual.level);
1098 lbs_deb_wext("noise %#x\n", priv->wstats.qual.noise);
1100 rssi = priv->wstats.qual.level - priv->wstats.qual.noise;
1101 if (rssi < 15)
1102 rssi_qual = rssi * POOR / 10;
1103 else if (rssi < 20)
1104 rssi_qual = (rssi - 15) * (FAIR - POOR) / 5 + POOR;
1105 else if (rssi < 30)
1106 rssi_qual = (rssi - 20) * (GOOD - FAIR) / 5 + FAIR;
1107 else if (rssi < 40)
1108 rssi_qual = (rssi - 30) * (VERY_GOOD - GOOD) /
1109 10 + GOOD;
1110 else
1111 rssi_qual = (rssi - 40) * (PERFECT - VERY_GOOD) /
1112 10 + VERY_GOOD;
1113 quality = rssi_qual;
1115 /* Quality by TX errors */
1116 priv->wstats.discard.retries = priv->stats.tx_errors;
1118 tx_retries = le16_to_cpu(adapter->logmsg.retry);
1120 if (tx_retries > 75)
1121 tx_qual = (90 - tx_retries) * POOR / 15;
1122 else if (tx_retries > 70)
1123 tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
1124 else if (tx_retries > 65)
1125 tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
1126 else if (tx_retries > 50)
1127 tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
1128 15 + GOOD;
1129 else
1130 tx_qual = (50 - tx_retries) *
1131 (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
1132 quality = min(quality, tx_qual);
1134 priv->wstats.discard.code = le16_to_cpu(adapter->logmsg.wepundecryptable);
1135 priv->wstats.discard.fragment = le16_to_cpu(adapter->logmsg.rxfrag);
1136 priv->wstats.discard.retries = tx_retries;
1137 priv->wstats.discard.misc = le16_to_cpu(adapter->logmsg.ackfailure);
1139 /* Calculate quality */
1140 priv->wstats.qual.qual = max(quality, (u32)100);
1141 priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1142 stats_valid = 1;
1144 /* update stats asynchronously for future calls */
1145 libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
1146 0, 0, NULL);
1147 libertas_prepare_and_send_command(priv, cmd_802_11_get_log, 0,
1148 0, 0, NULL);
1149 out:
1150 if (!stats_valid) {
1151 priv->wstats.miss.beacon = 0;
1152 priv->wstats.discard.retries = 0;
1153 priv->wstats.qual.qual = 0;
1154 priv->wstats.qual.level = 0;
1155 priv->wstats.qual.noise = 0;
1156 priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED;
1157 priv->wstats.qual.updated |= IW_QUAL_NOISE_INVALID |
1158 IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
1161 lbs_deb_leave(LBS_DEB_WEXT);
1162 return &priv->wstats;
1167 static int wlan_set_freq(struct net_device *dev, struct iw_request_info *info,
1168 struct iw_freq *fwrq, char *extra)
1170 int ret = -EINVAL;
1171 wlan_private *priv = dev->priv;
1172 wlan_adapter *adapter = priv->adapter;
1173 struct chan_freq_power *cfp;
1174 struct assoc_request * assoc_req;
1176 lbs_deb_enter(LBS_DEB_WEXT);
1178 mutex_lock(&adapter->lock);
1179 assoc_req = wlan_get_association_request(adapter);
1180 if (!assoc_req) {
1181 ret = -ENOMEM;
1182 goto out;
1185 /* If setting by frequency, convert to a channel */
1186 if (fwrq->e == 1) {
1187 long f = fwrq->m / 100000;
1189 cfp = find_cfp_by_band_and_freq(adapter, 0, f);
1190 if (!cfp) {
1191 lbs_deb_wext("invalid freq %ld\n", f);
1192 goto out;
1195 fwrq->e = 0;
1196 fwrq->m = (int) cfp->channel;
1199 /* Setting by channel number */
1200 if (fwrq->m > 1000 || fwrq->e > 0) {
1201 goto out;
1204 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, fwrq->m);
1205 if (!cfp) {
1206 goto out;
1209 assoc_req->channel = fwrq->m;
1210 ret = 0;
1212 out:
1213 if (ret == 0) {
1214 set_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags);
1215 wlan_postpone_association_work(priv);
1216 } else {
1217 wlan_cancel_association_work(priv);
1219 mutex_unlock(&adapter->lock);
1221 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1222 return ret;
1226 * @brief use index to get the data rate
1228 * @param index The index of data rate
1229 * @return data rate or 0
1231 u32 libertas_index_to_data_rate(u8 index)
1233 if (index >= sizeof(libertas_wlan_data_rates))
1234 index = 0;
1236 return libertas_wlan_data_rates[index];
1240 * @brief use rate to get the index
1242 * @param rate data rate
1243 * @return index or 0
1245 u8 libertas_data_rate_to_index(u32 rate)
1247 u8 *ptr;
1249 if (rate)
1250 if ((ptr = memchr(libertas_wlan_data_rates, (u8) rate,
1251 sizeof(libertas_wlan_data_rates))))
1252 return (ptr - libertas_wlan_data_rates);
1254 return 0;
1257 static int wlan_set_rate(struct net_device *dev, struct iw_request_info *info,
1258 struct iw_param *vwrq, char *extra)
1260 wlan_private *priv = dev->priv;
1261 wlan_adapter *adapter = priv->adapter;
1262 u32 data_rate;
1263 u16 action;
1264 int ret = 0;
1265 u8 rates[WLAN_SUPPORTED_RATES];
1266 u8 *rate;
1268 lbs_deb_enter(LBS_DEB_WEXT);
1270 lbs_deb_wext("vwrq->value %d\n", vwrq->value);
1272 if (vwrq->value == -1) {
1273 action = cmd_act_set_tx_auto; // Auto
1274 adapter->is_datarate_auto = 1;
1275 adapter->datarate = 0;
1276 } else {
1277 if (vwrq->value % 100000) {
1278 return -EINVAL;
1281 data_rate = vwrq->value / 500000;
1283 memset(rates, 0, sizeof(rates));
1284 get_active_data_rates(adapter, rates);
1285 rate = rates;
1286 while (*rate) {
1287 lbs_deb_wext("rate=0x%X, wanted data_rate 0x%X\n", *rate,
1288 data_rate);
1289 if ((*rate & 0x7f) == (data_rate & 0x7f))
1290 break;
1291 rate++;
1293 if (!*rate) {
1294 lbs_pr_alert("fixed data rate 0x%X out "
1295 "of range\n", data_rate);
1296 return -EINVAL;
1299 adapter->datarate = data_rate;
1300 action = cmd_act_set_tx_fix_rate;
1301 adapter->is_datarate_auto = 0;
1304 ret = libertas_prepare_and_send_command(priv, cmd_802_11_data_rate,
1305 action, cmd_option_waitforrsp, 0, NULL);
1307 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1308 return ret;
1311 static int wlan_get_rate(struct net_device *dev, struct iw_request_info *info,
1312 struct iw_param *vwrq, char *extra)
1314 wlan_private *priv = dev->priv;
1315 wlan_adapter *adapter = priv->adapter;
1317 lbs_deb_enter(LBS_DEB_WEXT);
1319 if (adapter->is_datarate_auto) {
1320 vwrq->fixed = 0;
1321 } else {
1322 vwrq->fixed = 1;
1325 vwrq->value = adapter->datarate * 500000;
1327 lbs_deb_leave(LBS_DEB_WEXT);
1328 return 0;
1331 static int wlan_set_mode(struct net_device *dev,
1332 struct iw_request_info *info, u32 * uwrq, char *extra)
1334 int ret = 0;
1335 wlan_private *priv = dev->priv;
1336 wlan_adapter *adapter = priv->adapter;
1337 struct assoc_request * assoc_req;
1339 lbs_deb_enter(LBS_DEB_WEXT);
1341 if ( (*uwrq != IW_MODE_ADHOC)
1342 && (*uwrq != IW_MODE_INFRA)
1343 && (*uwrq != IW_MODE_AUTO)) {
1344 lbs_deb_wext("Invalid mode: 0x%x\n", *uwrq);
1345 ret = -EINVAL;
1346 goto out;
1349 mutex_lock(&adapter->lock);
1350 assoc_req = wlan_get_association_request(adapter);
1351 if (!assoc_req) {
1352 ret = -ENOMEM;
1353 wlan_cancel_association_work(priv);
1354 } else {
1355 assoc_req->mode = *uwrq;
1356 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1357 wlan_postpone_association_work(priv);
1358 lbs_deb_wext("Switching to mode: 0x%x\n", *uwrq);
1360 mutex_unlock(&adapter->lock);
1362 out:
1363 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1364 return ret;
1369 * @brief Get Encryption key
1371 * @param dev A pointer to net_device structure
1372 * @param info A pointer to iw_request_info structure
1373 * @param vwrq A pointer to iw_param structure
1374 * @param extra A pointer to extra data buf
1375 * @return 0 --success, otherwise fail
1377 static int wlan_get_encode(struct net_device *dev,
1378 struct iw_request_info *info,
1379 struct iw_point *dwrq, u8 * extra)
1381 wlan_private *priv = dev->priv;
1382 wlan_adapter *adapter = priv->adapter;
1383 int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
1385 lbs_deb_enter(LBS_DEB_WEXT);
1387 lbs_deb_wext("flags 0x%x, index %d, length %d, wep_tx_keyidx %d\n",
1388 dwrq->flags, index, dwrq->length, adapter->wep_tx_keyidx);
1390 dwrq->flags = 0;
1392 /* Authentication method */
1393 switch (adapter->secinfo.auth_mode) {
1394 case IW_AUTH_ALG_OPEN_SYSTEM:
1395 dwrq->flags = IW_ENCODE_OPEN;
1396 break;
1398 case IW_AUTH_ALG_SHARED_KEY:
1399 case IW_AUTH_ALG_LEAP:
1400 dwrq->flags = IW_ENCODE_RESTRICTED;
1401 break;
1402 default:
1403 dwrq->flags = IW_ENCODE_DISABLED | IW_ENCODE_OPEN;
1404 break;
1407 if ( adapter->secinfo.wep_enabled
1408 || adapter->secinfo.WPAenabled
1409 || adapter->secinfo.WPA2enabled) {
1410 dwrq->flags &= ~IW_ENCODE_DISABLED;
1411 } else {
1412 dwrq->flags |= IW_ENCODE_DISABLED;
1415 memset(extra, 0, 16);
1417 mutex_lock(&adapter->lock);
1419 /* Default to returning current transmit key */
1420 if (index < 0)
1421 index = adapter->wep_tx_keyidx;
1423 if ((adapter->wep_keys[index].len) && adapter->secinfo.wep_enabled) {
1424 memcpy(extra, adapter->wep_keys[index].key,
1425 adapter->wep_keys[index].len);
1426 dwrq->length = adapter->wep_keys[index].len;
1428 dwrq->flags |= (index + 1);
1429 /* Return WEP enabled */
1430 dwrq->flags &= ~IW_ENCODE_DISABLED;
1431 } else if ((adapter->secinfo.WPAenabled)
1432 || (adapter->secinfo.WPA2enabled)) {
1433 /* return WPA enabled */
1434 dwrq->flags &= ~IW_ENCODE_DISABLED;
1435 } else {
1436 dwrq->flags |= IW_ENCODE_DISABLED;
1439 mutex_unlock(&adapter->lock);
1441 dwrq->flags |= IW_ENCODE_NOKEY;
1443 lbs_deb_wext("key: " MAC_FMT ", keylen %d\n",
1444 extra[0], extra[1], extra[2],
1445 extra[3], extra[4], extra[5], dwrq->length);
1447 lbs_deb_wext("return flags 0x%x\n", dwrq->flags);
1449 lbs_deb_leave(LBS_DEB_WEXT);
1450 return 0;
1454 * @brief Set Encryption key (internal)
1456 * @param priv A pointer to private card structure
1457 * @param key_material A pointer to key material
1458 * @param key_length length of key material
1459 * @param index key index to set
1460 * @param set_tx_key Force set TX key (1 = yes, 0 = no)
1461 * @return 0 --success, otherwise fail
1463 static int wlan_set_wep_key(struct assoc_request *assoc_req,
1464 const char *key_material,
1465 u16 key_length,
1466 u16 index,
1467 int set_tx_key)
1469 int ret = 0;
1470 struct WLAN_802_11_KEY *pkey;
1472 lbs_deb_enter(LBS_DEB_WEXT);
1474 /* Paranoid validation of key index */
1475 if (index > 3) {
1476 ret = -EINVAL;
1477 goto out;
1480 /* validate max key length */
1481 if (key_length > KEY_LEN_WEP_104) {
1482 ret = -EINVAL;
1483 goto out;
1486 pkey = &assoc_req->wep_keys[index];
1488 if (key_length > 0) {
1489 memset(pkey, 0, sizeof(struct WLAN_802_11_KEY));
1490 pkey->type = KEY_TYPE_ID_WEP;
1492 /* Standardize the key length */
1493 pkey->len = (key_length > KEY_LEN_WEP_40) ?
1494 KEY_LEN_WEP_104 : KEY_LEN_WEP_40;
1495 memcpy(pkey->key, key_material, key_length);
1498 if (set_tx_key) {
1499 /* Ensure the chosen key is valid */
1500 if (!pkey->len) {
1501 lbs_deb_wext("key not set, so cannot enable it\n");
1502 ret = -EINVAL;
1503 goto out;
1505 assoc_req->wep_tx_keyidx = index;
1508 assoc_req->secinfo.wep_enabled = 1;
1510 out:
1511 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1512 return ret;
1515 static int validate_key_index(u16 def_index, u16 raw_index,
1516 u16 *out_index, u16 *is_default)
1518 if (!out_index || !is_default)
1519 return -EINVAL;
1521 /* Verify index if present, otherwise use default TX key index */
1522 if (raw_index > 0) {
1523 if (raw_index > 4)
1524 return -EINVAL;
1525 *out_index = raw_index - 1;
1526 } else {
1527 *out_index = def_index;
1528 *is_default = 1;
1530 return 0;
1533 static void disable_wep(struct assoc_request *assoc_req)
1535 int i;
1537 lbs_deb_enter(LBS_DEB_WEXT);
1539 /* Set Open System auth mode */
1540 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1542 /* Clear WEP keys and mark WEP as disabled */
1543 assoc_req->secinfo.wep_enabled = 0;
1544 for (i = 0; i < 4; i++)
1545 assoc_req->wep_keys[i].len = 0;
1547 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1548 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1550 lbs_deb_leave(LBS_DEB_WEXT);
1553 static void disable_wpa(struct assoc_request *assoc_req)
1555 lbs_deb_enter(LBS_DEB_WEXT);
1557 memset(&assoc_req->wpa_mcast_key, 0, sizeof (struct WLAN_802_11_KEY));
1558 assoc_req->wpa_mcast_key.flags = KEY_INFO_WPA_MCAST;
1559 set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1561 memset(&assoc_req->wpa_unicast_key, 0, sizeof (struct WLAN_802_11_KEY));
1562 assoc_req->wpa_unicast_key.flags = KEY_INFO_WPA_UNICAST;
1563 set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1565 assoc_req->secinfo.WPAenabled = 0;
1566 assoc_req->secinfo.WPA2enabled = 0;
1567 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1569 lbs_deb_leave(LBS_DEB_WEXT);
1573 * @brief Set Encryption key
1575 * @param dev A pointer to net_device structure
1576 * @param info A pointer to iw_request_info structure
1577 * @param vwrq A pointer to iw_param structure
1578 * @param extra A pointer to extra data buf
1579 * @return 0 --success, otherwise fail
1581 static int wlan_set_encode(struct net_device *dev,
1582 struct iw_request_info *info,
1583 struct iw_point *dwrq, char *extra)
1585 int ret = 0;
1586 wlan_private *priv = dev->priv;
1587 wlan_adapter *adapter = priv->adapter;
1588 struct assoc_request * assoc_req;
1589 u16 is_default = 0, index = 0, set_tx_key = 0;
1591 lbs_deb_enter(LBS_DEB_WEXT);
1593 mutex_lock(&adapter->lock);
1594 assoc_req = wlan_get_association_request(adapter);
1595 if (!assoc_req) {
1596 ret = -ENOMEM;
1597 goto out;
1600 if (dwrq->flags & IW_ENCODE_DISABLED) {
1601 disable_wep (assoc_req);
1602 disable_wpa (assoc_req);
1603 goto out;
1606 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1607 (dwrq->flags & IW_ENCODE_INDEX),
1608 &index, &is_default);
1609 if (ret) {
1610 ret = -EINVAL;
1611 goto out;
1614 /* If WEP isn't enabled, or if there is no key data but a valid
1615 * index, set the TX key.
1617 if (!assoc_req->secinfo.wep_enabled || (dwrq->length == 0 && !is_default))
1618 set_tx_key = 1;
1620 ret = wlan_set_wep_key(assoc_req, extra, dwrq->length, index, set_tx_key);
1621 if (ret)
1622 goto out;
1624 if (dwrq->length)
1625 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1626 if (set_tx_key)
1627 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1629 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
1630 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1631 } else if (dwrq->flags & IW_ENCODE_OPEN) {
1632 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1635 out:
1636 if (ret == 0) {
1637 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1638 wlan_postpone_association_work(priv);
1639 } else {
1640 wlan_cancel_association_work(priv);
1642 mutex_unlock(&adapter->lock);
1644 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1645 return ret;
1649 * @brief Get Extended Encryption key (WPA/802.1x and WEP)
1651 * @param dev A pointer to net_device structure
1652 * @param info A pointer to iw_request_info structure
1653 * @param vwrq A pointer to iw_param structure
1654 * @param extra A pointer to extra data buf
1655 * @return 0 on success, otherwise failure
1657 static int wlan_get_encodeext(struct net_device *dev,
1658 struct iw_request_info *info,
1659 struct iw_point *dwrq,
1660 char *extra)
1662 int ret = -EINVAL;
1663 wlan_private *priv = dev->priv;
1664 wlan_adapter *adapter = priv->adapter;
1665 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1666 int index, max_key_len;
1668 lbs_deb_enter(LBS_DEB_WEXT);
1670 max_key_len = dwrq->length - sizeof(*ext);
1671 if (max_key_len < 0)
1672 goto out;
1674 index = dwrq->flags & IW_ENCODE_INDEX;
1675 if (index) {
1676 if (index < 1 || index > 4)
1677 goto out;
1678 index--;
1679 } else {
1680 index = adapter->wep_tx_keyidx;
1683 if (!ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY &&
1684 ext->alg != IW_ENCODE_ALG_WEP) {
1685 if (index != 0 || adapter->mode != IW_MODE_INFRA)
1686 goto out;
1689 dwrq->flags = index + 1;
1690 memset(ext, 0, sizeof(*ext));
1692 if ( !adapter->secinfo.wep_enabled
1693 && !adapter->secinfo.WPAenabled
1694 && !adapter->secinfo.WPA2enabled) {
1695 ext->alg = IW_ENCODE_ALG_NONE;
1696 ext->key_len = 0;
1697 dwrq->flags |= IW_ENCODE_DISABLED;
1698 } else {
1699 u8 *key = NULL;
1701 if ( adapter->secinfo.wep_enabled
1702 && !adapter->secinfo.WPAenabled
1703 && !adapter->secinfo.WPA2enabled) {
1704 /* WEP */
1705 ext->alg = IW_ENCODE_ALG_WEP;
1706 ext->key_len = adapter->wep_keys[index].len;
1707 key = &adapter->wep_keys[index].key[0];
1708 } else if ( !adapter->secinfo.wep_enabled
1709 && (adapter->secinfo.WPAenabled ||
1710 adapter->secinfo.WPA2enabled)) {
1711 /* WPA */
1712 struct WLAN_802_11_KEY * pkey = NULL;
1714 if ( adapter->wpa_mcast_key.len
1715 && (adapter->wpa_mcast_key.flags & KEY_INFO_WPA_ENABLED))
1716 pkey = &adapter->wpa_mcast_key;
1717 else if ( adapter->wpa_unicast_key.len
1718 && (adapter->wpa_unicast_key.flags & KEY_INFO_WPA_ENABLED))
1719 pkey = &adapter->wpa_unicast_key;
1721 if (pkey) {
1722 if (pkey->type == KEY_TYPE_ID_AES) {
1723 ext->alg = IW_ENCODE_ALG_CCMP;
1724 } else {
1725 ext->alg = IW_ENCODE_ALG_TKIP;
1727 ext->key_len = pkey->len;
1728 key = &pkey->key[0];
1729 } else {
1730 ext->alg = IW_ENCODE_ALG_TKIP;
1731 ext->key_len = 0;
1733 } else {
1734 goto out;
1737 if (ext->key_len > max_key_len) {
1738 ret = -E2BIG;
1739 goto out;
1742 if (ext->key_len)
1743 memcpy(ext->key, key, ext->key_len);
1744 else
1745 dwrq->flags |= IW_ENCODE_NOKEY;
1746 dwrq->flags |= IW_ENCODE_ENABLED;
1748 ret = 0;
1750 out:
1751 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1752 return ret;
1756 * @brief Set Encryption key Extended (WPA/802.1x and WEP)
1758 * @param dev A pointer to net_device structure
1759 * @param info A pointer to iw_request_info structure
1760 * @param vwrq A pointer to iw_param structure
1761 * @param extra A pointer to extra data buf
1762 * @return 0 --success, otherwise fail
1764 static int wlan_set_encodeext(struct net_device *dev,
1765 struct iw_request_info *info,
1766 struct iw_point *dwrq,
1767 char *extra)
1769 int ret = 0;
1770 wlan_private *priv = dev->priv;
1771 wlan_adapter *adapter = priv->adapter;
1772 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1773 int alg = ext->alg;
1774 struct assoc_request * assoc_req;
1776 lbs_deb_enter(LBS_DEB_WEXT);
1778 mutex_lock(&adapter->lock);
1779 assoc_req = wlan_get_association_request(adapter);
1780 if (!assoc_req) {
1781 ret = -ENOMEM;
1782 goto out;
1785 if ((alg == IW_ENCODE_ALG_NONE) || (dwrq->flags & IW_ENCODE_DISABLED)) {
1786 disable_wep (assoc_req);
1787 disable_wpa (assoc_req);
1788 } else if (alg == IW_ENCODE_ALG_WEP) {
1789 u16 is_default = 0, index, set_tx_key = 0;
1791 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1792 (dwrq->flags & IW_ENCODE_INDEX),
1793 &index, &is_default);
1794 if (ret)
1795 goto out;
1797 /* If WEP isn't enabled, or if there is no key data but a valid
1798 * index, or if the set-TX-key flag was passed, set the TX key.
1800 if ( !assoc_req->secinfo.wep_enabled
1801 || (dwrq->length == 0 && !is_default)
1802 || (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY))
1803 set_tx_key = 1;
1805 /* Copy key to driver */
1806 ret = wlan_set_wep_key (assoc_req, ext->key, ext->key_len, index,
1807 set_tx_key);
1808 if (ret)
1809 goto out;
1811 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
1812 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
1813 } else if (dwrq->flags & IW_ENCODE_OPEN) {
1814 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1817 /* Mark the various WEP bits as modified */
1818 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1819 if (dwrq->length)
1820 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1821 if (set_tx_key)
1822 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1823 } else if ((alg == IW_ENCODE_ALG_TKIP) || (alg == IW_ENCODE_ALG_CCMP)) {
1824 struct WLAN_802_11_KEY * pkey;
1826 /* validate key length */
1827 if (((alg == IW_ENCODE_ALG_TKIP)
1828 && (ext->key_len != KEY_LEN_WPA_TKIP))
1829 || ((alg == IW_ENCODE_ALG_CCMP)
1830 && (ext->key_len != KEY_LEN_WPA_AES))) {
1831 lbs_deb_wext("invalid size %d for key of alg"
1832 "type %d\n",
1833 ext->key_len,
1834 alg);
1835 ret = -EINVAL;
1836 goto out;
1839 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1840 pkey = &assoc_req->wpa_mcast_key;
1841 set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1842 } else {
1843 pkey = &assoc_req->wpa_unicast_key;
1844 set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1847 memset(pkey, 0, sizeof (struct WLAN_802_11_KEY));
1848 memcpy(pkey->key, ext->key, ext->key_len);
1849 pkey->len = ext->key_len;
1850 if (pkey->len)
1851 pkey->flags |= KEY_INFO_WPA_ENABLED;
1853 /* Do this after zeroing key structure */
1854 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1855 pkey->flags |= KEY_INFO_WPA_MCAST;
1856 } else {
1857 pkey->flags |= KEY_INFO_WPA_UNICAST;
1860 if (alg == IW_ENCODE_ALG_TKIP) {
1861 pkey->type = KEY_TYPE_ID_TKIP;
1862 } else if (alg == IW_ENCODE_ALG_CCMP) {
1863 pkey->type = KEY_TYPE_ID_AES;
1864 } else {
1865 ret = -EINVAL;
1866 goto out;
1869 /* If WPA isn't enabled yet, do that now */
1870 if ( assoc_req->secinfo.WPAenabled == 0
1871 && assoc_req->secinfo.WPA2enabled == 0) {
1872 assoc_req->secinfo.WPAenabled = 1;
1873 assoc_req->secinfo.WPA2enabled = 1;
1874 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1877 disable_wep (assoc_req);
1880 out:
1881 if (ret == 0) {
1882 wlan_postpone_association_work(priv);
1883 } else {
1884 wlan_cancel_association_work(priv);
1886 mutex_unlock(&adapter->lock);
1888 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1889 return ret;
1893 static int wlan_set_genie(struct net_device *dev,
1894 struct iw_request_info *info,
1895 struct iw_point *dwrq,
1896 char *extra)
1898 wlan_private *priv = dev->priv;
1899 wlan_adapter *adapter = priv->adapter;
1900 int ret = 0;
1901 struct assoc_request * assoc_req;
1903 lbs_deb_enter(LBS_DEB_WEXT);
1905 mutex_lock(&adapter->lock);
1906 assoc_req = wlan_get_association_request(adapter);
1907 if (!assoc_req) {
1908 ret = -ENOMEM;
1909 goto out;
1912 if (dwrq->length > MAX_WPA_IE_LEN ||
1913 (dwrq->length && extra == NULL)) {
1914 ret = -EINVAL;
1915 goto out;
1918 if (dwrq->length) {
1919 memcpy(&assoc_req->wpa_ie[0], extra, dwrq->length);
1920 assoc_req->wpa_ie_len = dwrq->length;
1921 } else {
1922 memset(&assoc_req->wpa_ie[0], 0, sizeof(adapter->wpa_ie));
1923 assoc_req->wpa_ie_len = 0;
1926 out:
1927 if (ret == 0) {
1928 set_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags);
1929 wlan_postpone_association_work(priv);
1930 } else {
1931 wlan_cancel_association_work(priv);
1933 mutex_unlock(&adapter->lock);
1935 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1936 return ret;
1939 static int wlan_get_genie(struct net_device *dev,
1940 struct iw_request_info *info,
1941 struct iw_point *dwrq,
1942 char *extra)
1944 int ret = 0;
1945 wlan_private *priv = dev->priv;
1946 wlan_adapter *adapter = priv->adapter;
1948 lbs_deb_enter(LBS_DEB_WEXT);
1950 if (adapter->wpa_ie_len == 0) {
1951 dwrq->length = 0;
1952 goto out;
1955 if (dwrq->length < adapter->wpa_ie_len) {
1956 ret = -E2BIG;
1957 goto out;
1960 dwrq->length = adapter->wpa_ie_len;
1961 memcpy(extra, &adapter->wpa_ie[0], adapter->wpa_ie_len);
1963 out:
1964 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1965 return ret;
1969 static int wlan_set_auth(struct net_device *dev,
1970 struct iw_request_info *info,
1971 struct iw_param *dwrq,
1972 char *extra)
1974 wlan_private *priv = dev->priv;
1975 wlan_adapter *adapter = priv->adapter;
1976 struct assoc_request * assoc_req;
1977 int ret = 0;
1978 int updated = 0;
1980 lbs_deb_enter(LBS_DEB_WEXT);
1982 mutex_lock(&adapter->lock);
1983 assoc_req = wlan_get_association_request(adapter);
1984 if (!assoc_req) {
1985 ret = -ENOMEM;
1986 goto out;
1989 switch (dwrq->flags & IW_AUTH_INDEX) {
1990 case IW_AUTH_TKIP_COUNTERMEASURES:
1991 case IW_AUTH_CIPHER_PAIRWISE:
1992 case IW_AUTH_CIPHER_GROUP:
1993 case IW_AUTH_KEY_MGMT:
1994 case IW_AUTH_DROP_UNENCRYPTED:
1996 * libertas does not use these parameters
1998 break;
2000 case IW_AUTH_WPA_VERSION:
2001 if (dwrq->value & IW_AUTH_WPA_VERSION_DISABLED) {
2002 assoc_req->secinfo.WPAenabled = 0;
2003 assoc_req->secinfo.WPA2enabled = 0;
2004 disable_wpa (assoc_req);
2006 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA) {
2007 assoc_req->secinfo.WPAenabled = 1;
2008 assoc_req->secinfo.wep_enabled = 0;
2009 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
2011 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA2) {
2012 assoc_req->secinfo.WPA2enabled = 1;
2013 assoc_req->secinfo.wep_enabled = 0;
2014 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
2016 updated = 1;
2017 break;
2019 case IW_AUTH_80211_AUTH_ALG:
2020 if (dwrq->value & IW_AUTH_ALG_SHARED_KEY) {
2021 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
2022 } else if (dwrq->value & IW_AUTH_ALG_OPEN_SYSTEM) {
2023 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
2024 } else if (dwrq->value & IW_AUTH_ALG_LEAP) {
2025 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_LEAP;
2026 } else {
2027 ret = -EINVAL;
2029 updated = 1;
2030 break;
2032 case IW_AUTH_WPA_ENABLED:
2033 if (dwrq->value) {
2034 if (!assoc_req->secinfo.WPAenabled &&
2035 !assoc_req->secinfo.WPA2enabled) {
2036 assoc_req->secinfo.WPAenabled = 1;
2037 assoc_req->secinfo.WPA2enabled = 1;
2038 assoc_req->secinfo.wep_enabled = 0;
2039 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
2041 } else {
2042 assoc_req->secinfo.WPAenabled = 0;
2043 assoc_req->secinfo.WPA2enabled = 0;
2044 disable_wpa (assoc_req);
2046 updated = 1;
2047 break;
2049 default:
2050 ret = -EOPNOTSUPP;
2051 break;
2054 out:
2055 if (ret == 0) {
2056 if (updated)
2057 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
2058 wlan_postpone_association_work(priv);
2059 } else if (ret != -EOPNOTSUPP) {
2060 wlan_cancel_association_work(priv);
2062 mutex_unlock(&adapter->lock);
2064 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2065 return ret;
2068 static int wlan_get_auth(struct net_device *dev,
2069 struct iw_request_info *info,
2070 struct iw_param *dwrq,
2071 char *extra)
2073 int ret = 0;
2074 wlan_private *priv = dev->priv;
2075 wlan_adapter *adapter = priv->adapter;
2077 lbs_deb_enter(LBS_DEB_WEXT);
2079 switch (dwrq->flags & IW_AUTH_INDEX) {
2080 case IW_AUTH_WPA_VERSION:
2081 dwrq->value = 0;
2082 if (adapter->secinfo.WPAenabled)
2083 dwrq->value |= IW_AUTH_WPA_VERSION_WPA;
2084 if (adapter->secinfo.WPA2enabled)
2085 dwrq->value |= IW_AUTH_WPA_VERSION_WPA2;
2086 if (!dwrq->value)
2087 dwrq->value |= IW_AUTH_WPA_VERSION_DISABLED;
2088 break;
2090 case IW_AUTH_80211_AUTH_ALG:
2091 dwrq->value = adapter->secinfo.auth_mode;
2092 break;
2094 case IW_AUTH_WPA_ENABLED:
2095 if (adapter->secinfo.WPAenabled && adapter->secinfo.WPA2enabled)
2096 dwrq->value = 1;
2097 break;
2099 default:
2100 ret = -EOPNOTSUPP;
2103 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2104 return ret;
2108 static int wlan_set_txpow(struct net_device *dev, struct iw_request_info *info,
2109 struct iw_param *vwrq, char *extra)
2111 int ret = 0;
2112 wlan_private *priv = dev->priv;
2113 wlan_adapter *adapter = priv->adapter;
2115 u16 dbm;
2117 lbs_deb_enter(LBS_DEB_WEXT);
2119 if (vwrq->disabled) {
2120 wlan_radio_ioctl(priv, RADIO_OFF);
2121 return 0;
2124 adapter->preamble = cmd_type_auto_preamble;
2126 wlan_radio_ioctl(priv, RADIO_ON);
2128 if ((vwrq->flags & IW_TXPOW_TYPE) == IW_TXPOW_MWATT) {
2129 dbm = (u16) mw_to_dbm(vwrq->value);
2130 } else
2131 dbm = (u16) vwrq->value;
2133 /* auto tx power control */
2135 if (vwrq->fixed == 0)
2136 dbm = 0xffff;
2138 lbs_deb_wext("txpower set %d dbm\n", dbm);
2140 ret = libertas_prepare_and_send_command(priv,
2141 cmd_802_11_rf_tx_power,
2142 cmd_act_tx_power_opt_set_low,
2143 cmd_option_waitforrsp, 0, (void *)&dbm);
2145 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2146 return ret;
2149 static int wlan_get_essid(struct net_device *dev, struct iw_request_info *info,
2150 struct iw_point *dwrq, char *extra)
2152 wlan_private *priv = dev->priv;
2153 wlan_adapter *adapter = priv->adapter;
2155 lbs_deb_enter(LBS_DEB_WEXT);
2158 * Note : if dwrq->flags != 0, we should get the relevant SSID from
2159 * the SSID list...
2163 * Get the current SSID
2165 if (adapter->connect_status == libertas_connected) {
2166 memcpy(extra, adapter->curbssparams.ssid.ssid,
2167 adapter->curbssparams.ssid.ssidlength);
2168 extra[adapter->curbssparams.ssid.ssidlength] = '\0';
2169 } else {
2170 memset(extra, 0, 32);
2171 extra[adapter->curbssparams.ssid.ssidlength] = '\0';
2174 * If none, we may want to get the one that was set
2177 /* To make the driver backward compatible with WPA supplicant v0.2.4 */
2178 if (dwrq->length == 32) /* check with WPA supplicant buffer size */
2179 dwrq->length = min_t(size_t, adapter->curbssparams.ssid.ssidlength,
2180 IW_ESSID_MAX_SIZE);
2181 else
2182 dwrq->length = adapter->curbssparams.ssid.ssidlength + 1;
2184 dwrq->flags = 1; /* active */
2186 lbs_deb_leave(LBS_DEB_WEXT);
2187 return 0;
2190 static int wlan_set_essid(struct net_device *dev, struct iw_request_info *info,
2191 struct iw_point *dwrq, char *extra)
2193 wlan_private *priv = dev->priv;
2194 wlan_adapter *adapter = priv->adapter;
2195 int ret = 0;
2196 struct WLAN_802_11_SSID ssid;
2197 struct assoc_request * assoc_req;
2198 int ssid_len = dwrq->length;
2200 lbs_deb_enter(LBS_DEB_WEXT);
2203 * WE-20 and earlier NULL pad the end of the SSID and increment
2204 * SSID length so it can be used like a string. WE-21 and later don't,
2205 * but some userspace tools aren't able to cope with the change.
2207 if ((ssid_len > 0) && (extra[ssid_len - 1] == '\0'))
2208 ssid_len--;
2210 /* Check the size of the string */
2211 if (ssid_len > IW_ESSID_MAX_SIZE) {
2212 ret = -E2BIG;
2213 goto out;
2216 memset(&ssid, 0, sizeof(struct WLAN_802_11_SSID));
2218 if (!dwrq->flags || !ssid_len) {
2219 /* "any" SSID requested; leave SSID blank */
2220 } else {
2221 /* Specific SSID requested */
2222 memcpy(&ssid.ssid, extra, ssid_len);
2223 ssid.ssidlength = ssid_len;
2226 lbs_deb_wext("requested new SSID '%s'\n",
2227 (ssid.ssidlength > 0) ? (char *)ssid.ssid : "any");
2229 out:
2230 mutex_lock(&adapter->lock);
2231 if (ret == 0) {
2232 /* Get or create the current association request */
2233 assoc_req = wlan_get_association_request(adapter);
2234 if (!assoc_req) {
2235 ret = -ENOMEM;
2236 } else {
2237 /* Copy the SSID to the association request */
2238 memcpy(&assoc_req->ssid, &ssid, sizeof(struct WLAN_802_11_SSID));
2239 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
2240 wlan_postpone_association_work(priv);
2244 /* Cancel the association request if there was an error */
2245 if (ret != 0) {
2246 wlan_cancel_association_work(priv);
2249 mutex_unlock(&adapter->lock);
2251 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2252 return ret;
2256 * @brief Connect to the AP or Ad-hoc Network with specific bssid
2258 * @param dev A pointer to net_device structure
2259 * @param info A pointer to iw_request_info structure
2260 * @param awrq A pointer to iw_param structure
2261 * @param extra A pointer to extra data buf
2262 * @return 0 --success, otherwise fail
2264 static int wlan_set_wap(struct net_device *dev, struct iw_request_info *info,
2265 struct sockaddr *awrq, char *extra)
2267 wlan_private *priv = dev->priv;
2268 wlan_adapter *adapter = priv->adapter;
2269 struct assoc_request * assoc_req;
2270 int ret = 0;
2272 lbs_deb_enter(LBS_DEB_WEXT);
2274 if (awrq->sa_family != ARPHRD_ETHER)
2275 return -EINVAL;
2277 lbs_deb_wext("ASSOC: WAP: sa_data " MAC_FMT "\n", MAC_ARG(awrq->sa_data));
2279 mutex_lock(&adapter->lock);
2281 /* Get or create the current association request */
2282 assoc_req = wlan_get_association_request(adapter);
2283 if (!assoc_req) {
2284 wlan_cancel_association_work(priv);
2285 ret = -ENOMEM;
2286 } else {
2287 /* Copy the BSSID to the association request */
2288 memcpy(&assoc_req->bssid, awrq->sa_data, ETH_ALEN);
2289 set_bit(ASSOC_FLAG_BSSID, &assoc_req->flags);
2290 wlan_postpone_association_work(priv);
2293 mutex_unlock(&adapter->lock);
2295 return ret;
2298 void libertas_get_fwversion(wlan_adapter * adapter, char *fwversion, int maxlen)
2300 union {
2301 u32 l;
2302 u8 c[4];
2303 } ver;
2304 char fwver[32];
2306 mutex_lock(&adapter->lock);
2307 ver.l = adapter->fwreleasenumber;
2308 mutex_unlock(&adapter->lock);
2310 if (ver.c[3] == 0)
2311 sprintf(fwver, "%u.%u.%u", ver.c[2], ver.c[1], ver.c[0]);
2312 else
2313 sprintf(fwver, "%u.%u.%u.p%u",
2314 ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
2316 snprintf(fwversion, maxlen, fwver);
2321 * iwconfig settable callbacks
2323 static const iw_handler wlan_handler[] = {
2324 (iw_handler) NULL, /* SIOCSIWCOMMIT */
2325 (iw_handler) wlan_get_name, /* SIOCGIWNAME */
2326 (iw_handler) NULL, /* SIOCSIWNWID */
2327 (iw_handler) NULL, /* SIOCGIWNWID */
2328 (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
2329 (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
2330 (iw_handler) wlan_set_mode, /* SIOCSIWMODE */
2331 (iw_handler) wlan_get_mode, /* SIOCGIWMODE */
2332 (iw_handler) NULL, /* SIOCSIWSENS */
2333 (iw_handler) NULL, /* SIOCGIWSENS */
2334 (iw_handler) NULL, /* SIOCSIWRANGE */
2335 (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
2336 (iw_handler) NULL, /* SIOCSIWPRIV */
2337 (iw_handler) NULL, /* SIOCGIWPRIV */
2338 (iw_handler) NULL, /* SIOCSIWSTATS */
2339 (iw_handler) NULL, /* SIOCGIWSTATS */
2340 iw_handler_set_spy, /* SIOCSIWSPY */
2341 iw_handler_get_spy, /* SIOCGIWSPY */
2342 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
2343 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
2344 (iw_handler) wlan_set_wap, /* SIOCSIWAP */
2345 (iw_handler) wlan_get_wap, /* SIOCGIWAP */
2346 (iw_handler) NULL, /* SIOCSIWMLME */
2347 (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
2348 (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2349 (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2350 (iw_handler) wlan_set_essid, /* SIOCSIWESSID */
2351 (iw_handler) wlan_get_essid, /* SIOCGIWESSID */
2352 (iw_handler) wlan_set_nick, /* SIOCSIWNICKN */
2353 (iw_handler) wlan_get_nick, /* SIOCGIWNICKN */
2354 (iw_handler) NULL, /* -- hole -- */
2355 (iw_handler) NULL, /* -- hole -- */
2356 (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
2357 (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
2358 (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
2359 (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
2360 (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
2361 (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
2362 (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
2363 (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
2364 (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
2365 (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
2366 (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
2367 (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
2368 (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
2369 (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
2370 (iw_handler) NULL, /* -- hole -- */
2371 (iw_handler) NULL, /* -- hole -- */
2372 (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
2373 (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
2374 (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
2375 (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
2376 (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2377 (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2378 (iw_handler) NULL, /* SIOCSIWPMKSA */
2381 static const iw_handler mesh_wlan_handler[] = {
2382 (iw_handler) NULL, /* SIOCSIWCOMMIT */
2383 (iw_handler) wlan_get_name, /* SIOCGIWNAME */
2384 (iw_handler) NULL, /* SIOCSIWNWID */
2385 (iw_handler) NULL, /* SIOCGIWNWID */
2386 (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
2387 (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
2388 (iw_handler) NULL, /* SIOCSIWMODE */
2389 (iw_handler) mesh_wlan_get_mode, /* SIOCGIWMODE */
2390 (iw_handler) NULL, /* SIOCSIWSENS */
2391 (iw_handler) NULL, /* SIOCGIWSENS */
2392 (iw_handler) NULL, /* SIOCSIWRANGE */
2393 (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
2394 (iw_handler) NULL, /* SIOCSIWPRIV */
2395 (iw_handler) NULL, /* SIOCGIWPRIV */
2396 (iw_handler) NULL, /* SIOCSIWSTATS */
2397 (iw_handler) NULL, /* SIOCGIWSTATS */
2398 iw_handler_set_spy, /* SIOCSIWSPY */
2399 iw_handler_get_spy, /* SIOCGIWSPY */
2400 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
2401 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
2402 (iw_handler) NULL, /* SIOCSIWAP */
2403 (iw_handler) NULL, /* SIOCGIWAP */
2404 (iw_handler) NULL, /* SIOCSIWMLME */
2405 (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
2406 (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2407 (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2408 (iw_handler) NULL, /* SIOCSIWESSID */
2409 (iw_handler) NULL, /* SIOCGIWESSID */
2410 (iw_handler) NULL, /* SIOCSIWNICKN */
2411 (iw_handler) mesh_get_nick, /* SIOCGIWNICKN */
2412 (iw_handler) NULL, /* -- hole -- */
2413 (iw_handler) NULL, /* -- hole -- */
2414 (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
2415 (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
2416 (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
2417 (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
2418 (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
2419 (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
2420 (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
2421 (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
2422 (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
2423 (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
2424 (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
2425 (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
2426 (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
2427 (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
2428 (iw_handler) NULL, /* -- hole -- */
2429 (iw_handler) NULL, /* -- hole -- */
2430 (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
2431 (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
2432 (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
2433 (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
2434 (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2435 (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2436 (iw_handler) NULL, /* SIOCSIWPMKSA */
2438 struct iw_handler_def libertas_handler_def = {
2439 .num_standard = sizeof(wlan_handler) / sizeof(iw_handler),
2440 .num_private = sizeof(wlan_private_handler) / sizeof(iw_handler),
2441 .num_private_args = sizeof(wlan_private_args) /
2442 sizeof(struct iw_priv_args),
2443 .standard = (iw_handler *) wlan_handler,
2444 .private = (iw_handler *) wlan_private_handler,
2445 .private_args = (struct iw_priv_args *)wlan_private_args,
2446 .get_wireless_stats = wlan_get_wireless_stats,
2449 struct iw_handler_def mesh_handler_def = {
2450 .num_standard = sizeof(mesh_wlan_handler) / sizeof(iw_handler),
2451 .num_private = sizeof(wlan_private_handler) / sizeof(iw_handler),
2452 .num_private_args = sizeof(wlan_private_args) /
2453 sizeof(struct iw_priv_args),
2454 .standard = (iw_handler *) mesh_wlan_handler,
2455 .private = (iw_handler *) wlan_private_handler,
2456 .private_args = (struct iw_priv_args *)wlan_private_args,
2457 .get_wireless_stats = wlan_get_wireless_stats,