blob: 5b7e5f257d6245738772e53e99835e7c0ea4f21e [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
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"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020#include "wext.h"
21#include "assoc.h"
22
23
24/**
Holger Schurigbf68dac2007-05-25 00:14:38 -040025 * the rates supported by the card
26 */
27static 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/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020033 * @brief Convert mw value to dbm value
34 *
35 * @param mw the value of mw
36 * @return the value of dbm
37 */
38static 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 */
86struct 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)
Holger Schurig9012b282007-05-25 11:27:16 -0400113 lbs_deb_wext("libertas_find_cfp_by_band_and_channel: can't find "
114 "cfp by band %d / channel %d\n", band, channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115
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 */
127static 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)
Holger Schurig9012b282007-05-25 11:27:16 -0400154 lbs_deb_wext("find_cfp_by_band_and_freql: can't find cfp by "
155 "band %d / freq %d\n", band, freq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156
157 return cfp;
158}
159
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200160
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 */
168int wlan_radio_ioctl(wlan_private * priv, u8 option)
169{
170 int ret = 0;
171 wlan_adapter *adapter = priv->adapter;
172
Holger Schurig9012b282007-05-25 11:27:16 -0400173 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200174
175 if (adapter->radioon != option) {
Holger Schurig9012b282007-05-25 11:27:16 -0400176 lbs_deb_wext("switching radio %s\n", option ? "on" : "off");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200177 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
Holger Schurig9012b282007-05-25 11:27:16 -0400185 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200186 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 */
197static 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 */
217static int get_active_data_rates(wlan_adapter * adapter,
218 u8* rates)
219{
220 int k = 0;
221
Holger Schurig9012b282007-05-25 11:27:16 -0400222 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200223
224 if (adapter->connect_status != libertas_connected) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400225 if (adapter->mode == IW_MODE_INFRA) {
Holger Schurig9012b282007-05-25 11:27:16 -0400226 lbs_deb_wext("infra\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200227 k = copyrates(rates, k, libertas_supported_rates,
228 sizeof(libertas_supported_rates));
229 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400230 lbs_deb_wext("Adhoc G\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200231 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
Holger Schurig9012b282007-05-25 11:27:16 -0400239 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", k);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240 return k;
241}
242
243static 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
Holger Schurig9012b282007-05-25 11:27:16 -0400251 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200252
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
Holger Schurig9012b282007-05-25 11:27:16 -0400269 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270 return 0;
271}
272
273static 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
Holger Schurig9012b282007-05-25 11:27:16 -0400280 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281
282 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0,
283 adapter->curbssparams.channel);
284
285 if (!cfp) {
286 if (adapter->curbssparams.channel)
Holger Schurig9012b282007-05-25 11:27:16 -0400287 lbs_deb_wext("invalid channel %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288 adapter->curbssparams.channel);
289 return -EINVAL;
290 }
291
292 fwrq->m = (long)cfp->freq * 100000;
293 fwrq->e = 1;
294
Holger Schurig9012b282007-05-25 11:27:16 -0400295 lbs_deb_wext("freq %u\n", fwrq->m);
296 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297 return 0;
298}
299
300static 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
Holger Schurig9012b282007-05-25 11:27:16 -0400306 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307
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
Holger Schurig9012b282007-05-25 11:27:16 -0400315 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316 return 0;
317}
318
319static 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
Holger Schurig9012b282007-05-25 11:27:16 -0400325 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326
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
Holger Schurig9012b282007-05-25 11:27:16 -0400340 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341 return 0;
342}
343
344static 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
Holger Schurig9012b282007-05-25 11:27:16 -0400350 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351
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
Holger Schurig9012b282007-05-25 11:27:16 -0400371 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372 return 0;
373}
374
Luis Carlos Cobo Rusf5e05b62007-05-25 23:08:34 -0400375static int mesh_get_nick(struct net_device *dev, struct iw_request_info *info,
376 struct iw_point *dwrq, char *extra)
377{
378 wlan_private *priv = dev->priv;
379 wlan_adapter *adapter = priv->adapter;
380
381 lbs_deb_enter(LBS_DEB_WEXT);
382
383 /* Use nickname to indicate that mesh is on */
384
385 if (adapter->connect_status == libertas_connected) {
386 strncpy(extra, "Mesh", 12);
387 extra[12] = '\0';
388 dwrq->length = strlen(extra) + 1;
389 }
390
391 else {
392 extra[0] = '\0';
393 dwrq->length = 1 ;
394 }
395
396 lbs_deb_leave(LBS_DEB_WEXT);
397 return 0;
398}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200399static int wlan_set_rts(struct net_device *dev, struct iw_request_info *info,
400 struct iw_param *vwrq, char *extra)
401{
402 int ret = 0;
403 wlan_private *priv = dev->priv;
404 wlan_adapter *adapter = priv->adapter;
David Woodhouse981f1872007-05-25 23:36:54 -0400405 u32 rthr = vwrq->value;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200406
Holger Schurig9012b282007-05-25 11:27:16 -0400407 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200408
409 if (vwrq->disabled) {
410 adapter->rtsthsd = rthr = MRVDRV_RTS_MAX_VALUE;
411 } else {
412 if (rthr < MRVDRV_RTS_MIN_VALUE || rthr > MRVDRV_RTS_MAX_VALUE)
413 return -EINVAL;
414 adapter->rtsthsd = rthr;
415 }
416
417 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
418 cmd_act_set, cmd_option_waitforrsp,
419 OID_802_11_RTS_THRESHOLD, &rthr);
420
Holger Schurig9012b282007-05-25 11:27:16 -0400421 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200422 return ret;
423}
424
425static int wlan_get_rts(struct net_device *dev, struct iw_request_info *info,
426 struct iw_param *vwrq, char *extra)
427{
428 int ret = 0;
429 wlan_private *priv = dev->priv;
430 wlan_adapter *adapter = priv->adapter;
431
Holger Schurig9012b282007-05-25 11:27:16 -0400432 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433
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);
Holger Schurig9012b282007-05-25 11:27:16 -0400438 if (ret)
439 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440
441 vwrq->value = adapter->rtsthsd;
442 vwrq->disabled = ((vwrq->value < MRVDRV_RTS_MIN_VALUE)
443 || (vwrq->value > MRVDRV_RTS_MAX_VALUE));
444 vwrq->fixed = 1;
445
Holger Schurig9012b282007-05-25 11:27:16 -0400446out:
447 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
448 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449}
450
451static int wlan_set_frag(struct net_device *dev, struct iw_request_info *info,
452 struct iw_param *vwrq, char *extra)
453{
454 int ret = 0;
David Woodhouse981f1872007-05-25 23:36:54 -0400455 u32 fthr = vwrq->value;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 wlan_private *priv = dev->priv;
457 wlan_adapter *adapter = priv->adapter;
458
Holger Schurig9012b282007-05-25 11:27:16 -0400459 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460
461 if (vwrq->disabled) {
462 adapter->fragthsd = fthr = MRVDRV_FRAG_MAX_VALUE;
463 } else {
464 if (fthr < MRVDRV_FRAG_MIN_VALUE
465 || fthr > MRVDRV_FRAG_MAX_VALUE)
466 return -EINVAL;
467 adapter->fragthsd = fthr;
468 }
469
470 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
471 cmd_act_set, cmd_option_waitforrsp,
472 OID_802_11_FRAGMENTATION_THRESHOLD, &fthr);
Holger Schurig9012b282007-05-25 11:27:16 -0400473
474 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200475 return ret;
476}
477
478static int wlan_get_frag(struct net_device *dev, struct iw_request_info *info,
479 struct iw_param *vwrq, char *extra)
480{
481 int ret = 0;
482 wlan_private *priv = dev->priv;
483 wlan_adapter *adapter = priv->adapter;
484
Holger Schurig9012b282007-05-25 11:27:16 -0400485 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486
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);
Holger Schurig9012b282007-05-25 11:27:16 -0400492 if (ret)
493 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494
495 vwrq->value = adapter->fragthsd;
496 vwrq->disabled = ((vwrq->value < MRVDRV_FRAG_MIN_VALUE)
497 || (vwrq->value > MRVDRV_FRAG_MAX_VALUE));
498 vwrq->fixed = 1;
499
Holger Schurig9012b282007-05-25 11:27:16 -0400500out:
501 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 return ret;
503}
504
505static int wlan_get_mode(struct net_device *dev,
506 struct iw_request_info *info, u32 * uwrq, char *extra)
507{
508 wlan_private *priv = dev->priv;
509 wlan_adapter *adapter = priv->adapter;
510
Holger Schurig9012b282007-05-25 11:27:16 -0400511 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512
Dan Williams0dc5a292007-05-10 22:58:02 -0400513 *uwrq = adapter->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514
Holger Schurig9012b282007-05-25 11:27:16 -0400515 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516 return 0;
517}
518
Luis Carlos Cobo Rusf5e05b62007-05-25 23:08:34 -0400519static int mesh_wlan_get_mode(struct net_device *dev,
520 struct iw_request_info *info, u32 * uwrq,
521 char *extra)
522{
523 lbs_deb_enter(LBS_DEB_WEXT);
524
525 *uwrq = IW_MODE_REPEAT ;
526
527 lbs_deb_leave(LBS_DEB_WEXT);
528 return 0;
529}
530
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531static int wlan_get_txpow(struct net_device *dev,
532 struct iw_request_info *info,
533 struct iw_param *vwrq, char *extra)
534{
535 int ret = 0;
536 wlan_private *priv = dev->priv;
537 wlan_adapter *adapter = priv->adapter;
538
Holger Schurig9012b282007-05-25 11:27:16 -0400539 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540
541 ret = libertas_prepare_and_send_command(priv,
542 cmd_802_11_rf_tx_power,
543 cmd_act_tx_power_opt_get,
544 cmd_option_waitforrsp, 0, NULL);
545
Holger Schurig9012b282007-05-25 11:27:16 -0400546 if (ret)
547 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548
Holger Schurig9012b282007-05-25 11:27:16 -0400549 lbs_deb_wext("tx power level %d dbm\n", adapter->txpowerlevel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200550 vwrq->value = adapter->txpowerlevel;
551 vwrq->fixed = 1;
552 if (adapter->radioon) {
553 vwrq->disabled = 0;
554 vwrq->flags = IW_TXPOW_DBM;
555 } else {
556 vwrq->disabled = 1;
557 }
558
Holger Schurig9012b282007-05-25 11:27:16 -0400559out:
560 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
561 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562}
563
564static int wlan_set_retry(struct net_device *dev, struct iw_request_info *info,
565 struct iw_param *vwrq, char *extra)
566{
567 int ret = 0;
568 wlan_private *priv = dev->priv;
569 wlan_adapter *adapter = priv->adapter;
570
Holger Schurig9012b282007-05-25 11:27:16 -0400571 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572
573 if (vwrq->flags == IW_RETRY_LIMIT) {
574 /* The MAC has a 4-bit Total_Tx_Count register
575 Total_Tx_Count = 1 + Tx_Retry_Count */
576#define TX_RETRY_MIN 0
577#define TX_RETRY_MAX 14
578 if (vwrq->value < TX_RETRY_MIN || vwrq->value > TX_RETRY_MAX)
579 return -EINVAL;
580
581 /* Adding 1 to convert retry count to try count */
582 adapter->txretrycount = vwrq->value + 1;
583
584 ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
585 cmd_act_set,
586 cmd_option_waitforrsp,
587 OID_802_11_TX_RETRYCOUNT, NULL);
588
Holger Schurig9012b282007-05-25 11:27:16 -0400589 if (ret)
590 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591 } else {
592 return -EOPNOTSUPP;
593 }
594
Holger Schurig9012b282007-05-25 11:27:16 -0400595out:
596 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
597 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598}
599
600static int wlan_get_retry(struct net_device *dev, struct iw_request_info *info,
601 struct iw_param *vwrq, char *extra)
602{
603 wlan_private *priv = dev->priv;
604 wlan_adapter *adapter = priv->adapter;
605 int ret = 0;
606
Holger Schurig9012b282007-05-25 11:27:16 -0400607 lbs_deb_enter(LBS_DEB_WEXT);
608
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 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);
Holger Schurig9012b282007-05-25 11:27:16 -0400614 if (ret)
615 goto out;
616
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617 vwrq->disabled = 0;
618 if (!vwrq->flags) {
619 vwrq->flags = IW_RETRY_LIMIT;
620 /* Subtract 1 to convert try count to retry count */
621 vwrq->value = adapter->txretrycount - 1;
622 }
623
Holger Schurig9012b282007-05-25 11:27:16 -0400624out:
625 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
626 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200627}
628
629static inline void sort_channels(struct iw_freq *freq, int num)
630{
631 int i, j;
632 struct iw_freq temp;
633
634 for (i = 0; i < num; i++)
635 for (j = i + 1; j < num; j++)
636 if (freq[i].i > freq[j].i) {
637 temp.i = freq[i].i;
638 temp.m = freq[i].m;
639
640 freq[i].i = freq[j].i;
641 freq[i].m = freq[j].m;
642
643 freq[j].i = temp.i;
644 freq[j].m = temp.m;
645 }
646}
647
648/* data rate listing
649 MULTI_BANDS:
650 abg a b b/g
651 Infra G(12) A(8) B(4) G(12)
652 Adhoc A+B(12) A(8) B(4) B(4)
653
654 non-MULTI_BANDS:
655 b b/g
656 Infra B(4) G(12)
657 Adhoc B(4) B(4)
658 */
659/**
660 * @brief Get Range Info
661 *
662 * @param dev A pointer to net_device structure
663 * @param info A pointer to iw_request_info structure
664 * @param vwrq A pointer to iw_param structure
665 * @param extra A pointer to extra data buf
666 * @return 0 --success, otherwise fail
667 */
668static int wlan_get_range(struct net_device *dev, struct iw_request_info *info,
669 struct iw_point *dwrq, char *extra)
670{
671 int i, j;
672 wlan_private *priv = dev->priv;
673 wlan_adapter *adapter = priv->adapter;
674 struct iw_range *range = (struct iw_range *)extra;
675 struct chan_freq_power *cfp;
676 u8 rates[WLAN_SUPPORTED_RATES];
677
678 u8 flag = 0;
679
Holger Schurig9012b282007-05-25 11:27:16 -0400680 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681
682 dwrq->length = sizeof(struct iw_range);
683 memset(range, 0, sizeof(struct iw_range));
684
685 range->min_nwid = 0;
686 range->max_nwid = 0;
687
688 memset(rates, 0, sizeof(rates));
689 range->num_bitrates = get_active_data_rates(adapter, rates);
690
691 for (i = 0; i < min_t(__u8, range->num_bitrates, IW_MAX_BITRATES) && rates[i];
692 i++) {
693 range->bitrate[i] = (rates[i] & 0x7f) * 500000;
694 }
695 range->num_bitrates = i;
Holger Schurig9012b282007-05-25 11:27:16 -0400696 lbs_deb_wext("IW_MAX_BITRATES %d, num_bitrates %d\n", IW_MAX_BITRATES,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 range->num_bitrates);
698
699 range->num_frequency = 0;
700 if (priv->adapter->enable11d &&
701 adapter->connect_status == libertas_connected) {
702 u8 chan_no;
703 u8 band;
704
705 struct parsed_region_chan_11d *parsed_region_chan =
706 &adapter->parsed_region_chan;
707
708 if (parsed_region_chan == NULL) {
Holger Schurig9012b282007-05-25 11:27:16 -0400709 lbs_deb_wext("11d: parsed_region_chan is NULL\n");
710 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711 }
712 band = parsed_region_chan->band;
Holger Schurig9012b282007-05-25 11:27:16 -0400713 lbs_deb_wext("band %d, nr_char %d\n", band,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200714 parsed_region_chan->nr_chan);
715
716 for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
717 && (i < parsed_region_chan->nr_chan); i++) {
718 chan_no = parsed_region_chan->chanpwr[i].chan;
Holger Schurig9012b282007-05-25 11:27:16 -0400719 lbs_deb_wext("chan_no %d\n", chan_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720 range->freq[range->num_frequency].i = (long)chan_no;
721 range->freq[range->num_frequency].m =
722 (long)libertas_chan_2_freq(chan_no, band) * 100000;
723 range->freq[range->num_frequency].e = 1;
724 range->num_frequency++;
725 }
726 flag = 1;
727 }
728 if (!flag) {
729 for (j = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
730 && (j < sizeof(adapter->region_channel)
731 / sizeof(adapter->region_channel[0])); j++) {
732 cfp = adapter->region_channel[j].CFP;
733 for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
734 && adapter->region_channel[j].valid
735 && cfp
736 && (i < adapter->region_channel[j].nrcfp); i++) {
737 range->freq[range->num_frequency].i =
738 (long)cfp->channel;
739 range->freq[range->num_frequency].m =
740 (long)cfp->freq * 100000;
741 range->freq[range->num_frequency].e = 1;
742 cfp++;
743 range->num_frequency++;
744 }
745 }
746 }
747
Holger Schurig9012b282007-05-25 11:27:16 -0400748 lbs_deb_wext("IW_MAX_FREQUENCIES %d, num_frequency %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749 IW_MAX_FREQUENCIES, range->num_frequency);
750
751 range->num_channels = range->num_frequency;
752
753 sort_channels(&range->freq[0], range->num_frequency);
754
755 /*
756 * Set an indication of the max TCP throughput in bit/s that we can
757 * expect using this interface
758 */
759 if (i > 2)
760 range->throughput = 5000 * 1000;
761 else
762 range->throughput = 1500 * 1000;
763
764 range->min_rts = MRVDRV_RTS_MIN_VALUE;
765 range->max_rts = MRVDRV_RTS_MAX_VALUE;
766 range->min_frag = MRVDRV_FRAG_MIN_VALUE;
767 range->max_frag = MRVDRV_FRAG_MAX_VALUE;
768
769 range->encoding_size[0] = 5;
770 range->encoding_size[1] = 13;
771 range->num_encoding_sizes = 2;
772 range->max_encoding_tokens = 4;
773
774 range->min_pmp = 1000000;
775 range->max_pmp = 120000000;
776 range->min_pmt = 1000;
777 range->max_pmt = 1000000;
778 range->pmp_flags = IW_POWER_PERIOD;
779 range->pmt_flags = IW_POWER_TIMEOUT;
780 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
781
782 /*
783 * Minimum version we recommend
784 */
785 range->we_version_source = 15;
786
787 /*
788 * Version we are compiled with
789 */
790 range->we_version_compiled = WIRELESS_EXT;
791
792 range->retry_capa = IW_RETRY_LIMIT;
793 range->retry_flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
794
795 range->min_retry = TX_RETRY_MIN;
796 range->max_retry = TX_RETRY_MAX;
797
798 /*
799 * Set the qual, level and noise range values
800 */
801 range->max_qual.qual = 100;
802 range->max_qual.level = 0;
803 range->max_qual.noise = 0;
804 range->max_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
805
806 range->avg_qual.qual = 70;
807 /* TODO: Find real 'good' to 'bad' threshold value for RSSI */
808 range->avg_qual.level = 0;
809 range->avg_qual.noise = 0;
810 range->avg_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
811
812 range->sensitivity = 0;
813
814 /*
815 * Setup the supported power level ranges
816 */
817 memset(range->txpower, 0, sizeof(range->txpower));
818 range->txpower[0] = 5;
819 range->txpower[1] = 7;
820 range->txpower[2] = 9;
821 range->txpower[3] = 11;
822 range->txpower[4] = 13;
823 range->txpower[5] = 15;
824 range->txpower[6] = 17;
825 range->txpower[7] = 19;
826
827 range->num_txpower = 8;
828 range->txpower_capa = IW_TXPOW_DBM;
829 range->txpower_capa |= IW_TXPOW_RANGE;
830
831 range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
832 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
833 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
834 range->event_capa[1] = IW_EVENT_CAPA_K_1;
835
836 if (adapter->fwcapinfo & FW_CAPINFO_WPA) {
837 range->enc_capa = IW_ENC_CAPA_WPA
838 | IW_ENC_CAPA_WPA2
839 | IW_ENC_CAPA_CIPHER_TKIP
840 | IW_ENC_CAPA_CIPHER_CCMP;
841 }
842
Holger Schurig9012b282007-05-25 11:27:16 -0400843out:
844 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845 return 0;
846}
847
848static int wlan_set_power(struct net_device *dev, struct iw_request_info *info,
849 struct iw_param *vwrq, char *extra)
850{
851 wlan_private *priv = dev->priv;
852 wlan_adapter *adapter = priv->adapter;
853
Holger Schurig9012b282007-05-25 11:27:16 -0400854 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200855
856 /* PS is currently supported only in Infrastructure mode
857 * Remove this check if it is to be supported in IBSS mode also
858 */
859
860 if (vwrq->disabled) {
861 adapter->psmode = wlan802_11powermodecam;
862 if (adapter->psstate != PS_STATE_FULL_POWER) {
863 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
864 }
865
866 return 0;
867 }
868
869 if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
Holger Schurig9012b282007-05-25 11:27:16 -0400870 lbs_deb_wext(
871 "setting power timeout is not supported\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872 return -EINVAL;
873 } else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
Holger Schurig9012b282007-05-25 11:27:16 -0400874 lbs_deb_wext("setting power period not supported\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200875 return -EINVAL;
876 }
877
878 if (adapter->psmode != wlan802_11powermodecam) {
879 return 0;
880 }
881
882 adapter->psmode = wlan802_11powermodemax_psp;
883
884 if (adapter->connect_status == libertas_connected) {
885 libertas_ps_sleep(priv, cmd_option_waitforrsp);
886 }
887
Holger Schurig9012b282007-05-25 11:27:16 -0400888 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200889 return 0;
890}
891
892static int wlan_get_power(struct net_device *dev, struct iw_request_info *info,
893 struct iw_param *vwrq, char *extra)
894{
895 wlan_private *priv = dev->priv;
896 wlan_adapter *adapter = priv->adapter;
897 int mode;
898
Holger Schurig9012b282007-05-25 11:27:16 -0400899 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200900
901 mode = adapter->psmode;
902
903 if ((vwrq->disabled = (mode == wlan802_11powermodecam))
Holger Schurig9012b282007-05-25 11:27:16 -0400904 || adapter->connect_status == libertas_disconnected)
905 {
906 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907 }
908
909 vwrq->value = 0;
910
Holger Schurig9012b282007-05-25 11:27:16 -0400911out:
912 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200913 return 0;
914}
915
916/*
917 * iwpriv settable callbacks
918 */
919
920static const iw_handler wlan_private_handler[] = {
921 NULL, /* SIOCIWFIRSTPRIV */
922};
923
924static const struct iw_priv_args wlan_private_args[] = {
925 /*
926 * { cmd, set_args, get_args, name }
927 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200928 /* Using iwpriv sub-command feature */
929 {
930 WLAN_SETONEINT_GETNONE, /* IOCTL: 24 */
931 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
932 IW_PRIV_TYPE_NONE,
933 ""},
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200934 {
935 WLANSETREGION,
936 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
937 IW_PRIV_TYPE_NONE,
938 "setregioncode"},
939 {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940 WLAN_SUBCMD_MESH_SET_TTL,
941 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
942 IW_PRIV_TYPE_NONE,
943 "mesh_set_ttl"},
944 {
945 WLAN_SETNONE_GETONEINT,
946 IW_PRIV_TYPE_NONE,
947 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
948 ""},
949 {
950 WLANGETREGION,
951 IW_PRIV_TYPE_NONE,
952 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
953 "getregioncode"},
954 {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955 WLAN_SUBCMD_FWT_CLEANUP,
956 IW_PRIV_TYPE_NONE,
957 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
958 "fwt_cleanup"},
959 {
960 WLAN_SUBCMD_FWT_TIME,
961 IW_PRIV_TYPE_NONE,
962 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
963 "fwt_time"},
964 {
965 WLAN_SUBCMD_MESH_GET_TTL,
966 IW_PRIV_TYPE_NONE,
967 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
968 "mesh_get_ttl"},
969 {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970 WLAN_SETNONE_GETNONE,
971 IW_PRIV_TYPE_NONE,
972 IW_PRIV_TYPE_NONE,
973 ""},
974 {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200975 WLAN_SUBCMD_FWT_RESET,
976 IW_PRIV_TYPE_NONE,
977 IW_PRIV_TYPE_NONE,
978 "fwt_reset"},
979 {
980 WLAN_SUBCMD_BT_RESET,
981 IW_PRIV_TYPE_NONE,
982 IW_PRIV_TYPE_NONE,
983 "bt_reset"},
984 {
985 WLAN_SET128CHAR_GET128CHAR,
986 IW_PRIV_TYPE_CHAR | 128,
987 IW_PRIV_TYPE_CHAR | 128,
988 ""},
989 /* BT Management */
990 {
991 WLAN_SUBCMD_BT_ADD,
992 IW_PRIV_TYPE_CHAR | 128,
993 IW_PRIV_TYPE_CHAR | 128,
994 "bt_add"},
995 {
996 WLAN_SUBCMD_BT_DEL,
997 IW_PRIV_TYPE_CHAR | 128,
998 IW_PRIV_TYPE_CHAR | 128,
999 "bt_del"},
1000 {
1001 WLAN_SUBCMD_BT_LIST,
1002 IW_PRIV_TYPE_CHAR | 128,
1003 IW_PRIV_TYPE_CHAR | 128,
1004 "bt_list"},
Luis Carlos Cobo90e8eaf2007-05-25 13:53:26 -04001005 {
1006 WLAN_SUBCMD_BT_SET_INVERT,
1007 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1008 IW_PRIV_TYPE_NONE,
1009 "bt_set_invert"},
1010 {
1011 WLAN_SUBCMD_BT_GET_INVERT,
1012 IW_PRIV_TYPE_NONE,
1013 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1014 "bt_get_invert"},
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015 /* FWT Management */
1016 {
1017 WLAN_SUBCMD_FWT_ADD,
1018 IW_PRIV_TYPE_CHAR | 128,
1019 IW_PRIV_TYPE_CHAR | 128,
1020 "fwt_add"},
1021 {
1022 WLAN_SUBCMD_FWT_DEL,
1023 IW_PRIV_TYPE_CHAR | 128,
1024 IW_PRIV_TYPE_CHAR | 128,
1025 "fwt_del"},
1026 {
1027 WLAN_SUBCMD_FWT_LOOKUP,
1028 IW_PRIV_TYPE_CHAR | 128,
1029 IW_PRIV_TYPE_CHAR | 128,
1030 "fwt_lookup"},
1031 {
1032 WLAN_SUBCMD_FWT_LIST_NEIGHBOR,
1033 IW_PRIV_TYPE_CHAR | 128,
1034 IW_PRIV_TYPE_CHAR | 128,
1035 "fwt_list_neigh"},
1036 {
1037 WLAN_SUBCMD_FWT_LIST,
1038 IW_PRIV_TYPE_CHAR | 128,
1039 IW_PRIV_TYPE_CHAR | 128,
1040 "fwt_list"},
1041 {
1042 WLAN_SUBCMD_FWT_LIST_ROUTE,
1043 IW_PRIV_TYPE_CHAR | 128,
1044 IW_PRIV_TYPE_CHAR | 128,
1045 "fwt_list_route"},
1046 {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 WLAN_SET_GET_SIXTEEN_INT,
1048 IW_PRIV_TYPE_INT | 16,
1049 IW_PRIV_TYPE_INT | 16,
1050 ""},
1051 {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001052 WLAN_LED_GPIO_CTRL,
1053 IW_PRIV_TYPE_INT | 16,
1054 IW_PRIV_TYPE_INT | 16,
1055 "ledgpio"},
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056};
1057
1058static struct iw_statistics *wlan_get_wireless_stats(struct net_device *dev)
1059{
1060 enum {
1061 POOR = 30,
1062 FAIR = 60,
1063 GOOD = 80,
1064 VERY_GOOD = 90,
1065 EXCELLENT = 95,
1066 PERFECT = 100
1067 };
1068 wlan_private *priv = dev->priv;
1069 wlan_adapter *adapter = priv->adapter;
1070 u32 rssi_qual;
1071 u32 tx_qual;
1072 u32 quality = 0;
1073 int stats_valid = 0;
1074 u8 rssi;
1075 u32 tx_retries;
1076
Holger Schurig9012b282007-05-25 11:27:16 -04001077 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001078
Dan Williams0dc5a292007-05-10 22:58:02 -04001079 priv->wstats.status = adapter->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080
1081 /* If we're not associated, all quality values are meaningless */
1082 if (adapter->connect_status != libertas_connected)
1083 goto out;
1084
1085 /* Quality by RSSI */
1086 priv->wstats.qual.level =
1087 CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_NOAVG],
1088 adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1089
1090 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1091 priv->wstats.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1092 } else {
1093 priv->wstats.qual.noise =
1094 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1095 }
1096
Holger Schurig9012b282007-05-25 11:27:16 -04001097 lbs_deb_wext("signal level %#x\n", priv->wstats.qual.level);
1098 lbs_deb_wext("noise %#x\n", priv->wstats.qual.noise);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099
1100 rssi = priv->wstats.qual.level - priv->wstats.qual.noise;
1101 if (rssi < 15)
1102 rssi_qual = rssi * POOR / 10;
1103 else if (rssi < 20)
1104 rssi_qual = (rssi - 15) * (FAIR - POOR) / 5 + POOR;
1105 else if (rssi < 30)
1106 rssi_qual = (rssi - 20) * (GOOD - FAIR) / 5 + FAIR;
1107 else if (rssi < 40)
1108 rssi_qual = (rssi - 30) * (VERY_GOOD - GOOD) /
1109 10 + GOOD;
1110 else
1111 rssi_qual = (rssi - 40) * (PERFECT - VERY_GOOD) /
1112 10 + VERY_GOOD;
1113 quality = rssi_qual;
1114
1115 /* Quality by TX errors */
1116 priv->wstats.discard.retries = priv->stats.tx_errors;
1117
David Woodhouse981f1872007-05-25 23:36:54 -04001118 tx_retries = le16_to_cpu(adapter->logmsg.retry);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001119
1120 if (tx_retries > 75)
1121 tx_qual = (90 - tx_retries) * POOR / 15;
1122 else if (tx_retries > 70)
1123 tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
1124 else if (tx_retries > 65)
1125 tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
1126 else if (tx_retries > 50)
1127 tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
1128 15 + GOOD;
1129 else
1130 tx_qual = (50 - tx_retries) *
1131 (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
1132 quality = min(quality, tx_qual);
1133
David Woodhouse981f1872007-05-25 23:36:54 -04001134 priv->wstats.discard.code = le16_to_cpu(adapter->logmsg.wepundecryptable);
1135 priv->wstats.discard.fragment = le16_to_cpu(adapter->logmsg.rxfrag);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136 priv->wstats.discard.retries = tx_retries;
David Woodhouse981f1872007-05-25 23:36:54 -04001137 priv->wstats.discard.misc = le16_to_cpu(adapter->logmsg.ackfailure);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138
1139 /* Calculate quality */
1140 priv->wstats.qual.qual = max(quality, (u32)100);
1141 priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1142 stats_valid = 1;
1143
1144 /* update stats asynchronously for future calls */
1145 libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
1146 0, 0, NULL);
1147 libertas_prepare_and_send_command(priv, cmd_802_11_get_log, 0,
1148 0, 0, NULL);
1149out:
1150 if (!stats_valid) {
1151 priv->wstats.miss.beacon = 0;
1152 priv->wstats.discard.retries = 0;
1153 priv->wstats.qual.qual = 0;
1154 priv->wstats.qual.level = 0;
1155 priv->wstats.qual.noise = 0;
1156 priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED;
1157 priv->wstats.qual.updated |= IW_QUAL_NOISE_INVALID |
1158 IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
1159 }
1160
Holger Schurig9012b282007-05-25 11:27:16 -04001161 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001162 return &priv->wstats;
1163
1164
1165}
1166
1167static int wlan_set_freq(struct net_device *dev, struct iw_request_info *info,
1168 struct iw_freq *fwrq, char *extra)
1169{
Dan Williamsef9a2642007-05-25 16:46:33 -04001170 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 wlan_private *priv = dev->priv;
1172 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001173 struct chan_freq_power *cfp;
Dan Williamsef9a2642007-05-25 16:46:33 -04001174 struct assoc_request * assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175
Holger Schurig9012b282007-05-25 11:27:16 -04001176 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177
Dan Williamsef9a2642007-05-25 16:46:33 -04001178 mutex_lock(&adapter->lock);
1179 assoc_req = wlan_get_association_request(adapter);
1180 if (!assoc_req) {
1181 ret = -ENOMEM;
1182 goto out;
1183 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001184
Dan Williamsef9a2642007-05-25 16:46:33 -04001185 /* If setting by frequency, convert to a channel */
1186 if (fwrq->e == 1) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001187 long f = fwrq->m / 100000;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188
1189 cfp = find_cfp_by_band_and_freq(adapter, 0, f);
1190 if (!cfp) {
Holger Schurig9012b282007-05-25 11:27:16 -04001191 lbs_deb_wext("invalid freq %ld\n", f);
Dan Williamsef9a2642007-05-25 16:46:33 -04001192 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193 }
1194
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001195 fwrq->e = 0;
Dan Williamsef9a2642007-05-25 16:46:33 -04001196 fwrq->m = (int) cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001197 }
1198
Dan Williamsef9a2642007-05-25 16:46:33 -04001199 /* Setting by channel number */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001200 if (fwrq->m > 1000 || fwrq->e > 0) {
Dan Williamsef9a2642007-05-25 16:46:33 -04001201 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001202 }
1203
Dan Williamsef9a2642007-05-25 16:46:33 -04001204 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, fwrq->m);
1205 if (!cfp) {
1206 goto out;
1207 }
1208
1209 assoc_req->channel = fwrq->m;
1210 ret = 0;
1211
Holger Schurig9012b282007-05-25 11:27:16 -04001212out:
Dan Williamsef9a2642007-05-25 16:46:33 -04001213 if (ret == 0) {
1214 set_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags);
1215 wlan_postpone_association_work(priv);
1216 } else {
1217 wlan_cancel_association_work(priv);
1218 }
1219 mutex_unlock(&adapter->lock);
1220
1221 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1222 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001223}
1224
1225/**
1226 * @brief use index to get the data rate
1227 *
1228 * @param index The index of data rate
1229 * @return data rate or 0
1230 */
1231u32 libertas_index_to_data_rate(u8 index)
1232{
1233 if (index >= sizeof(libertas_wlan_data_rates))
1234 index = 0;
1235
1236 return libertas_wlan_data_rates[index];
1237}
1238
1239/**
1240 * @brief use rate to get the index
1241 *
1242 * @param rate data rate
1243 * @return index or 0
1244 */
1245u8 libertas_data_rate_to_index(u32 rate)
1246{
1247 u8 *ptr;
1248
1249 if (rate)
1250 if ((ptr = memchr(libertas_wlan_data_rates, (u8) rate,
1251 sizeof(libertas_wlan_data_rates))))
1252 return (ptr - libertas_wlan_data_rates);
1253
1254 return 0;
1255}
1256
1257static int wlan_set_rate(struct net_device *dev, struct iw_request_info *info,
1258 struct iw_param *vwrq, char *extra)
1259{
1260 wlan_private *priv = dev->priv;
1261 wlan_adapter *adapter = priv->adapter;
1262 u32 data_rate;
1263 u16 action;
1264 int ret = 0;
1265 u8 rates[WLAN_SUPPORTED_RATES];
1266 u8 *rate;
1267
Holger Schurig9012b282007-05-25 11:27:16 -04001268 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001269
Holger Schurig9012b282007-05-25 11:27:16 -04001270 lbs_deb_wext("vwrq->value %d\n", vwrq->value);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001271
1272 if (vwrq->value == -1) {
1273 action = cmd_act_set_tx_auto; // Auto
1274 adapter->is_datarate_auto = 1;
1275 adapter->datarate = 0;
1276 } else {
1277 if (vwrq->value % 100000) {
1278 return -EINVAL;
1279 }
1280
1281 data_rate = vwrq->value / 500000;
1282
1283 memset(rates, 0, sizeof(rates));
1284 get_active_data_rates(adapter, rates);
1285 rate = rates;
1286 while (*rate) {
Holger Schurig9012b282007-05-25 11:27:16 -04001287 lbs_deb_wext("rate=0x%X, wanted data_rate 0x%X\n", *rate,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288 data_rate);
1289 if ((*rate & 0x7f) == (data_rate & 0x7f))
1290 break;
1291 rate++;
1292 }
1293 if (!*rate) {
Holger Schurig9012b282007-05-25 11:27:16 -04001294 lbs_pr_alert("fixed data rate 0x%X out "
1295 "of range\n", data_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296 return -EINVAL;
1297 }
1298
1299 adapter->datarate = data_rate;
1300 action = cmd_act_set_tx_fix_rate;
1301 adapter->is_datarate_auto = 0;
1302 }
1303
1304 ret = libertas_prepare_and_send_command(priv, cmd_802_11_data_rate,
1305 action, cmd_option_waitforrsp, 0, NULL);
1306
Holger Schurig9012b282007-05-25 11:27:16 -04001307 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001308 return ret;
1309}
1310
1311static int wlan_get_rate(struct net_device *dev, struct iw_request_info *info,
1312 struct iw_param *vwrq, char *extra)
1313{
1314 wlan_private *priv = dev->priv;
1315 wlan_adapter *adapter = priv->adapter;
1316
Holger Schurig9012b282007-05-25 11:27:16 -04001317 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318
1319 if (adapter->is_datarate_auto) {
1320 vwrq->fixed = 0;
1321 } else {
1322 vwrq->fixed = 1;
1323 }
1324
1325 vwrq->value = adapter->datarate * 500000;
1326
Holger Schurig9012b282007-05-25 11:27:16 -04001327 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001328 return 0;
1329}
1330
1331static int wlan_set_mode(struct net_device *dev,
1332 struct iw_request_info *info, u32 * uwrq, char *extra)
1333{
1334 int ret = 0;
1335 wlan_private *priv = dev->priv;
1336 wlan_adapter *adapter = priv->adapter;
1337 struct assoc_request * assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001338
Holger Schurig9012b282007-05-25 11:27:16 -04001339 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001340
Dan Williams0dc5a292007-05-10 22:58:02 -04001341 if ( (*uwrq != IW_MODE_ADHOC)
1342 && (*uwrq != IW_MODE_INFRA)
1343 && (*uwrq != IW_MODE_AUTO)) {
Holger Schurig9012b282007-05-25 11:27:16 -04001344 lbs_deb_wext("Invalid mode: 0x%x\n", *uwrq);
Dan Williams0dc5a292007-05-10 22:58:02 -04001345 ret = -EINVAL;
1346 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 }
1348
1349 mutex_lock(&adapter->lock);
1350 assoc_req = wlan_get_association_request(adapter);
1351 if (!assoc_req) {
1352 ret = -ENOMEM;
Dan Williams0dc5a292007-05-10 22:58:02 -04001353 wlan_cancel_association_work(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001354 } else {
Dan Williams0dc5a292007-05-10 22:58:02 -04001355 assoc_req->mode = *uwrq;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001356 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1357 wlan_postpone_association_work(priv);
Holger Schurig9012b282007-05-25 11:27:16 -04001358 lbs_deb_wext("Switching to mode: 0x%x\n", *uwrq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001359 }
1360 mutex_unlock(&adapter->lock);
1361
Dan Williams0dc5a292007-05-10 22:58:02 -04001362out:
Holger Schurig9012b282007-05-25 11:27:16 -04001363 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001364 return ret;
1365}
1366
1367
1368/**
1369 * @brief Get Encryption key
1370 *
1371 * @param dev A pointer to net_device structure
1372 * @param info A pointer to iw_request_info structure
1373 * @param vwrq A pointer to iw_param structure
1374 * @param extra A pointer to extra data buf
1375 * @return 0 --success, otherwise fail
1376 */
1377static int wlan_get_encode(struct net_device *dev,
1378 struct iw_request_info *info,
1379 struct iw_point *dwrq, u8 * extra)
1380{
1381 wlan_private *priv = dev->priv;
1382 wlan_adapter *adapter = priv->adapter;
1383 int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
1384
Holger Schurig9012b282007-05-25 11:27:16 -04001385 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001386
Holger Schurig9012b282007-05-25 11:27:16 -04001387 lbs_deb_wext("flags 0x%x, index %d, length %d, wep_tx_keyidx %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001388 dwrq->flags, index, dwrq->length, adapter->wep_tx_keyidx);
1389
1390 dwrq->flags = 0;
1391
1392 /* Authentication method */
Dan Williams6affe782007-05-10 22:56:42 -04001393 switch (adapter->secinfo.auth_mode) {
1394 case IW_AUTH_ALG_OPEN_SYSTEM:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001395 dwrq->flags = IW_ENCODE_OPEN;
1396 break;
1397
Dan Williams6affe782007-05-10 22:56:42 -04001398 case IW_AUTH_ALG_SHARED_KEY:
1399 case IW_AUTH_ALG_LEAP:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 dwrq->flags = IW_ENCODE_RESTRICTED;
1401 break;
1402 default:
1403 dwrq->flags = IW_ENCODE_DISABLED | IW_ENCODE_OPEN;
1404 break;
1405 }
1406
Dan Williams889c05b2007-05-10 22:57:23 -04001407 if ( adapter->secinfo.wep_enabled
1408 || adapter->secinfo.WPAenabled
1409 || adapter->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 dwrq->flags &= ~IW_ENCODE_DISABLED;
1411 } else {
1412 dwrq->flags |= IW_ENCODE_DISABLED;
1413 }
1414
1415 memset(extra, 0, 16);
1416
1417 mutex_lock(&adapter->lock);
1418
1419 /* Default to returning current transmit key */
1420 if (index < 0)
1421 index = adapter->wep_tx_keyidx;
1422
Dan Williams889c05b2007-05-10 22:57:23 -04001423 if ((adapter->wep_keys[index].len) && adapter->secinfo.wep_enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424 memcpy(extra, adapter->wep_keys[index].key,
1425 adapter->wep_keys[index].len);
1426 dwrq->length = adapter->wep_keys[index].len;
1427
1428 dwrq->flags |= (index + 1);
1429 /* Return WEP enabled */
1430 dwrq->flags &= ~IW_ENCODE_DISABLED;
1431 } else if ((adapter->secinfo.WPAenabled)
1432 || (adapter->secinfo.WPA2enabled)) {
1433 /* return WPA enabled */
1434 dwrq->flags &= ~IW_ENCODE_DISABLED;
1435 } else {
1436 dwrq->flags |= IW_ENCODE_DISABLED;
1437 }
1438
1439 mutex_unlock(&adapter->lock);
1440
1441 dwrq->flags |= IW_ENCODE_NOKEY;
1442
Holger Schurig9012b282007-05-25 11:27:16 -04001443 lbs_deb_wext("key: " MAC_FMT ", keylen %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001444 extra[0], extra[1], extra[2],
1445 extra[3], extra[4], extra[5], dwrq->length);
1446
Holger Schurig9012b282007-05-25 11:27:16 -04001447 lbs_deb_wext("return flags 0x%x\n", dwrq->flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001448
Holger Schurig9012b282007-05-25 11:27:16 -04001449 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450 return 0;
1451}
1452
1453/**
1454 * @brief Set Encryption key (internal)
1455 *
1456 * @param priv A pointer to private card structure
1457 * @param key_material A pointer to key material
1458 * @param key_length length of key material
1459 * @param index key index to set
1460 * @param set_tx_key Force set TX key (1 = yes, 0 = no)
1461 * @return 0 --success, otherwise fail
1462 */
1463static int wlan_set_wep_key(struct assoc_request *assoc_req,
1464 const char *key_material,
1465 u16 key_length,
1466 u16 index,
1467 int set_tx_key)
1468{
Holger Schurig9012b282007-05-25 11:27:16 -04001469 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470 struct WLAN_802_11_KEY *pkey;
1471
Holger Schurig9012b282007-05-25 11:27:16 -04001472 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001473
1474 /* Paranoid validation of key index */
1475 if (index > 3) {
Holger Schurig9012b282007-05-25 11:27:16 -04001476 ret = -EINVAL;
1477 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478 }
1479
1480 /* validate max key length */
1481 if (key_length > KEY_LEN_WEP_104) {
Holger Schurig9012b282007-05-25 11:27:16 -04001482 ret = -EINVAL;
1483 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001484 }
1485
1486 pkey = &assoc_req->wep_keys[index];
1487
1488 if (key_length > 0) {
1489 memset(pkey, 0, sizeof(struct WLAN_802_11_KEY));
1490 pkey->type = KEY_TYPE_ID_WEP;
1491
1492 /* Standardize the key length */
1493 pkey->len = (key_length > KEY_LEN_WEP_40) ?
1494 KEY_LEN_WEP_104 : KEY_LEN_WEP_40;
1495 memcpy(pkey->key, key_material, key_length);
1496 }
1497
1498 if (set_tx_key) {
1499 /* Ensure the chosen key is valid */
1500 if (!pkey->len) {
Holger Schurig9012b282007-05-25 11:27:16 -04001501 lbs_deb_wext("key not set, so cannot enable it\n");
1502 ret = -EINVAL;
1503 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001504 }
1505 assoc_req->wep_tx_keyidx = index;
1506 }
1507
Dan Williams889c05b2007-05-10 22:57:23 -04001508 assoc_req->secinfo.wep_enabled = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001509
Holger Schurig9012b282007-05-25 11:27:16 -04001510out:
1511 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1512 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001513}
1514
1515static int validate_key_index(u16 def_index, u16 raw_index,
1516 u16 *out_index, u16 *is_default)
1517{
1518 if (!out_index || !is_default)
1519 return -EINVAL;
1520
1521 /* Verify index if present, otherwise use default TX key index */
1522 if (raw_index > 0) {
1523 if (raw_index > 4)
1524 return -EINVAL;
1525 *out_index = raw_index - 1;
1526 } else {
1527 *out_index = def_index;
1528 *is_default = 1;
1529 }
1530 return 0;
1531}
1532
1533static void disable_wep(struct assoc_request *assoc_req)
1534{
1535 int i;
1536
Dan Williams90a42212007-05-25 23:01:24 -04001537 lbs_deb_enter(LBS_DEB_WEXT);
1538
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001539 /* Set Open System auth mode */
Dan Williams6affe782007-05-10 22:56:42 -04001540 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001541
1542 /* Clear WEP keys and mark WEP as disabled */
Dan Williams889c05b2007-05-10 22:57:23 -04001543 assoc_req->secinfo.wep_enabled = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001544 for (i = 0; i < 4; i++)
1545 assoc_req->wep_keys[i].len = 0;
1546
1547 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1548 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
Dan Williams90a42212007-05-25 23:01:24 -04001549
1550 lbs_deb_leave(LBS_DEB_WEXT);
1551}
1552
1553static void disable_wpa(struct assoc_request *assoc_req)
1554{
1555 lbs_deb_enter(LBS_DEB_WEXT);
1556
1557 memset(&assoc_req->wpa_mcast_key, 0, sizeof (struct WLAN_802_11_KEY));
1558 assoc_req->wpa_mcast_key.flags = KEY_INFO_WPA_MCAST;
1559 set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1560
1561 memset(&assoc_req->wpa_unicast_key, 0, sizeof (struct WLAN_802_11_KEY));
1562 assoc_req->wpa_unicast_key.flags = KEY_INFO_WPA_UNICAST;
1563 set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1564
1565 assoc_req->secinfo.WPAenabled = 0;
1566 assoc_req->secinfo.WPA2enabled = 0;
1567 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1568
1569 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001570}
1571
1572/**
1573 * @brief Set Encryption key
1574 *
1575 * @param dev A pointer to net_device structure
1576 * @param info A pointer to iw_request_info structure
1577 * @param vwrq A pointer to iw_param structure
1578 * @param extra A pointer to extra data buf
1579 * @return 0 --success, otherwise fail
1580 */
1581static int wlan_set_encode(struct net_device *dev,
1582 struct iw_request_info *info,
1583 struct iw_point *dwrq, char *extra)
1584{
1585 int ret = 0;
1586 wlan_private *priv = dev->priv;
1587 wlan_adapter *adapter = priv->adapter;
1588 struct assoc_request * assoc_req;
1589 u16 is_default = 0, index = 0, set_tx_key = 0;
1590
Holger Schurig9012b282007-05-25 11:27:16 -04001591 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001592
1593 mutex_lock(&adapter->lock);
1594 assoc_req = wlan_get_association_request(adapter);
1595 if (!assoc_req) {
1596 ret = -ENOMEM;
1597 goto out;
1598 }
1599
1600 if (dwrq->flags & IW_ENCODE_DISABLED) {
1601 disable_wep (assoc_req);
Dan Williams90a42212007-05-25 23:01:24 -04001602 disable_wpa (assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001603 goto out;
1604 }
1605
1606 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1607 (dwrq->flags & IW_ENCODE_INDEX),
1608 &index, &is_default);
1609 if (ret) {
1610 ret = -EINVAL;
1611 goto out;
1612 }
1613
1614 /* If WEP isn't enabled, or if there is no key data but a valid
1615 * index, set the TX key.
1616 */
Dan Williams889c05b2007-05-10 22:57:23 -04001617 if (!assoc_req->secinfo.wep_enabled || (dwrq->length == 0 && !is_default))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001618 set_tx_key = 1;
1619
1620 ret = wlan_set_wep_key(assoc_req, extra, dwrq->length, index, set_tx_key);
1621 if (ret)
1622 goto out;
1623
1624 if (dwrq->length)
1625 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1626 if (set_tx_key)
1627 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
1628
1629 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
Dan Williams6affe782007-05-10 22:56:42 -04001630 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001631 } else if (dwrq->flags & IW_ENCODE_OPEN) {
Dan Williams6affe782007-05-10 22:56:42 -04001632 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001633 }
1634
1635out:
1636 if (ret == 0) {
1637 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1638 wlan_postpone_association_work(priv);
1639 } else {
1640 wlan_cancel_association_work(priv);
1641 }
1642 mutex_unlock(&adapter->lock);
1643
Holger Schurig9012b282007-05-25 11:27:16 -04001644 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001645 return ret;
1646}
1647
1648/**
1649 * @brief Get Extended Encryption key (WPA/802.1x and WEP)
1650 *
1651 * @param dev A pointer to net_device structure
1652 * @param info A pointer to iw_request_info structure
1653 * @param vwrq A pointer to iw_param structure
1654 * @param extra A pointer to extra data buf
1655 * @return 0 on success, otherwise failure
1656 */
1657static int wlan_get_encodeext(struct net_device *dev,
1658 struct iw_request_info *info,
1659 struct iw_point *dwrq,
1660 char *extra)
1661{
1662 int ret = -EINVAL;
1663 wlan_private *priv = dev->priv;
1664 wlan_adapter *adapter = priv->adapter;
1665 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1666 int index, max_key_len;
1667
Holger Schurig9012b282007-05-25 11:27:16 -04001668 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001669
1670 max_key_len = dwrq->length - sizeof(*ext);
1671 if (max_key_len < 0)
1672 goto out;
1673
1674 index = dwrq->flags & IW_ENCODE_INDEX;
1675 if (index) {
1676 if (index < 1 || index > 4)
1677 goto out;
1678 index--;
1679 } else {
1680 index = adapter->wep_tx_keyidx;
1681 }
1682
1683 if (!ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY &&
1684 ext->alg != IW_ENCODE_ALG_WEP) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001685 if (index != 0 || adapter->mode != IW_MODE_INFRA)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001686 goto out;
1687 }
1688
1689 dwrq->flags = index + 1;
1690 memset(ext, 0, sizeof(*ext));
1691
Dan Williams889c05b2007-05-10 22:57:23 -04001692 if ( !adapter->secinfo.wep_enabled
1693 && !adapter->secinfo.WPAenabled
1694 && !adapter->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001695 ext->alg = IW_ENCODE_ALG_NONE;
1696 ext->key_len = 0;
1697 dwrq->flags |= IW_ENCODE_DISABLED;
1698 } else {
1699 u8 *key = NULL;
1700
Dan Williams889c05b2007-05-10 22:57:23 -04001701 if ( adapter->secinfo.wep_enabled
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001702 && !adapter->secinfo.WPAenabled
1703 && !adapter->secinfo.WPA2enabled) {
Dan Williams90a42212007-05-25 23:01:24 -04001704 /* WEP */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001705 ext->alg = IW_ENCODE_ALG_WEP;
1706 ext->key_len = adapter->wep_keys[index].len;
1707 key = &adapter->wep_keys[index].key[0];
Dan Williams889c05b2007-05-10 22:57:23 -04001708 } else if ( !adapter->secinfo.wep_enabled
1709 && (adapter->secinfo.WPAenabled ||
1710 adapter->secinfo.WPA2enabled)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001711 /* WPA */
Dan Williams90a42212007-05-25 23:01:24 -04001712 struct WLAN_802_11_KEY * pkey = NULL;
1713
1714 if ( adapter->wpa_mcast_key.len
1715 && (adapter->wpa_mcast_key.flags & KEY_INFO_WPA_ENABLED))
1716 pkey = &adapter->wpa_mcast_key;
1717 else if ( adapter->wpa_unicast_key.len
1718 && (adapter->wpa_unicast_key.flags & KEY_INFO_WPA_ENABLED))
1719 pkey = &adapter->wpa_unicast_key;
1720
1721 if (pkey) {
1722 if (pkey->type == KEY_TYPE_ID_AES) {
1723 ext->alg = IW_ENCODE_ALG_CCMP;
1724 } else {
1725 ext->alg = IW_ENCODE_ALG_TKIP;
1726 }
1727 ext->key_len = pkey->len;
1728 key = &pkey->key[0];
1729 } else {
1730 ext->alg = IW_ENCODE_ALG_TKIP;
1731 ext->key_len = 0;
1732 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001733 } else {
1734 goto out;
1735 }
1736
1737 if (ext->key_len > max_key_len) {
1738 ret = -E2BIG;
1739 goto out;
1740 }
1741
1742 if (ext->key_len)
1743 memcpy(ext->key, key, ext->key_len);
1744 else
1745 dwrq->flags |= IW_ENCODE_NOKEY;
1746 dwrq->flags |= IW_ENCODE_ENABLED;
1747 }
1748 ret = 0;
1749
1750out:
Holger Schurig9012b282007-05-25 11:27:16 -04001751 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001752 return ret;
1753}
1754
1755/**
1756 * @brief Set Encryption key Extended (WPA/802.1x and WEP)
1757 *
1758 * @param dev A pointer to net_device structure
1759 * @param info A pointer to iw_request_info structure
1760 * @param vwrq A pointer to iw_param structure
1761 * @param extra A pointer to extra data buf
1762 * @return 0 --success, otherwise fail
1763 */
1764static int wlan_set_encodeext(struct net_device *dev,
1765 struct iw_request_info *info,
1766 struct iw_point *dwrq,
1767 char *extra)
1768{
1769 int ret = 0;
1770 wlan_private *priv = dev->priv;
1771 wlan_adapter *adapter = priv->adapter;
1772 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1773 int alg = ext->alg;
1774 struct assoc_request * assoc_req;
1775
Holger Schurig9012b282007-05-25 11:27:16 -04001776 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001777
1778 mutex_lock(&adapter->lock);
1779 assoc_req = wlan_get_association_request(adapter);
1780 if (!assoc_req) {
1781 ret = -ENOMEM;
1782 goto out;
1783 }
1784
1785 if ((alg == IW_ENCODE_ALG_NONE) || (dwrq->flags & IW_ENCODE_DISABLED)) {
1786 disable_wep (assoc_req);
Dan Williams90a42212007-05-25 23:01:24 -04001787 disable_wpa (assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001788 } else if (alg == IW_ENCODE_ALG_WEP) {
1789 u16 is_default = 0, index, set_tx_key = 0;
1790
1791 ret = validate_key_index(assoc_req->wep_tx_keyidx,
1792 (dwrq->flags & IW_ENCODE_INDEX),
1793 &index, &is_default);
1794 if (ret)
1795 goto out;
1796
1797 /* If WEP isn't enabled, or if there is no key data but a valid
1798 * index, or if the set-TX-key flag was passed, set the TX key.
1799 */
Dan Williams889c05b2007-05-10 22:57:23 -04001800 if ( !assoc_req->secinfo.wep_enabled
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001801 || (dwrq->length == 0 && !is_default)
1802 || (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY))
1803 set_tx_key = 1;
1804
1805 /* Copy key to driver */
1806 ret = wlan_set_wep_key (assoc_req, ext->key, ext->key_len, index,
1807 set_tx_key);
1808 if (ret)
1809 goto out;
1810
1811 if (dwrq->flags & IW_ENCODE_RESTRICTED) {
Dan Williams6affe782007-05-10 22:56:42 -04001812 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001813 } else if (dwrq->flags & IW_ENCODE_OPEN) {
Dan Williams6affe782007-05-10 22:56:42 -04001814 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001815 }
1816
1817 /* Mark the various WEP bits as modified */
1818 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1819 if (dwrq->length)
1820 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1821 if (set_tx_key)
1822 set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823 } else if ((alg == IW_ENCODE_ALG_TKIP) || (alg == IW_ENCODE_ALG_CCMP)) {
1824 struct WLAN_802_11_KEY * pkey;
1825
1826 /* validate key length */
1827 if (((alg == IW_ENCODE_ALG_TKIP)
1828 && (ext->key_len != KEY_LEN_WPA_TKIP))
1829 || ((alg == IW_ENCODE_ALG_CCMP)
1830 && (ext->key_len != KEY_LEN_WPA_AES))) {
Holger Schurig9012b282007-05-25 11:27:16 -04001831 lbs_deb_wext("invalid size %d for key of alg"
1832 "type %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001833 ext->key_len,
1834 alg);
1835 ret = -EINVAL;
1836 goto out;
1837 }
1838
Dan Williams90a42212007-05-25 23:01:24 -04001839 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001840 pkey = &assoc_req->wpa_mcast_key;
Dan Williams90a42212007-05-25 23:01:24 -04001841 set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1842 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001843 pkey = &assoc_req->wpa_unicast_key;
Dan Williams90a42212007-05-25 23:01:24 -04001844 set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1845 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001846
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;
Dan Williams90a42212007-05-25 23:01:24 -04001850 if (pkey->len)
1851 pkey->flags |= KEY_INFO_WPA_ENABLED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001852
Dan Williams90a42212007-05-25 23:01:24 -04001853 /* Do this after zeroing key structure */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001854 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1855 pkey->flags |= KEY_INFO_WPA_MCAST;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001856 } else {
1857 pkey->flags |= KEY_INFO_WPA_UNICAST;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001858 }
1859
Dan Williams90a42212007-05-25 23:01:24 -04001860 if (alg == IW_ENCODE_ALG_TKIP) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001861 pkey->type = KEY_TYPE_ID_TKIP;
Dan Williams90a42212007-05-25 23:01:24 -04001862 } else if (alg == IW_ENCODE_ALG_CCMP) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001863 pkey->type = KEY_TYPE_ID_AES;
Dan Williams90a42212007-05-25 23:01:24 -04001864 } else {
1865 ret = -EINVAL;
1866 goto out;
1867 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001868
1869 /* If WPA isn't enabled yet, do that now */
1870 if ( assoc_req->secinfo.WPAenabled == 0
1871 && assoc_req->secinfo.WPA2enabled == 0) {
1872 assoc_req->secinfo.WPAenabled = 1;
1873 assoc_req->secinfo.WPA2enabled = 1;
1874 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
1875 }
1876
1877 disable_wep (assoc_req);
1878 }
1879
1880out:
1881 if (ret == 0) {
1882 wlan_postpone_association_work(priv);
1883 } else {
1884 wlan_cancel_association_work(priv);
1885 }
1886 mutex_unlock(&adapter->lock);
1887
Holger Schurig9012b282007-05-25 11:27:16 -04001888 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001889 return ret;
1890}
1891
1892
1893static int wlan_set_genie(struct net_device *dev,
1894 struct iw_request_info *info,
1895 struct iw_point *dwrq,
1896 char *extra)
1897{
1898 wlan_private *priv = dev->priv;
1899 wlan_adapter *adapter = priv->adapter;
1900 int ret = 0;
1901 struct assoc_request * assoc_req;
1902
Holger Schurig9012b282007-05-25 11:27:16 -04001903 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001904
1905 mutex_lock(&adapter->lock);
1906 assoc_req = wlan_get_association_request(adapter);
1907 if (!assoc_req) {
1908 ret = -ENOMEM;
1909 goto out;
1910 }
1911
1912 if (dwrq->length > MAX_WPA_IE_LEN ||
1913 (dwrq->length && extra == NULL)) {
1914 ret = -EINVAL;
1915 goto out;
1916 }
1917
1918 if (dwrq->length) {
1919 memcpy(&assoc_req->wpa_ie[0], extra, dwrq->length);
1920 assoc_req->wpa_ie_len = dwrq->length;
1921 } else {
1922 memset(&assoc_req->wpa_ie[0], 0, sizeof(adapter->wpa_ie));
1923 assoc_req->wpa_ie_len = 0;
1924 }
1925
1926out:
1927 if (ret == 0) {
1928 set_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags);
1929 wlan_postpone_association_work(priv);
1930 } else {
1931 wlan_cancel_association_work(priv);
1932 }
1933 mutex_unlock(&adapter->lock);
1934
Holger Schurig9012b282007-05-25 11:27:16 -04001935 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001936 return ret;
1937}
1938
1939static int wlan_get_genie(struct net_device *dev,
1940 struct iw_request_info *info,
1941 struct iw_point *dwrq,
1942 char *extra)
1943{
Holger Schurig9012b282007-05-25 11:27:16 -04001944 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001945 wlan_private *priv = dev->priv;
1946 wlan_adapter *adapter = priv->adapter;
1947
Holger Schurig9012b282007-05-25 11:27:16 -04001948 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001949
1950 if (adapter->wpa_ie_len == 0) {
1951 dwrq->length = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001952 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001953 }
1954
1955 if (dwrq->length < adapter->wpa_ie_len) {
Holger Schurig9012b282007-05-25 11:27:16 -04001956 ret = -E2BIG;
1957 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001958 }
1959
1960 dwrq->length = adapter->wpa_ie_len;
1961 memcpy(extra, &adapter->wpa_ie[0], adapter->wpa_ie_len);
1962
Holger Schurig9012b282007-05-25 11:27:16 -04001963out:
1964 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1965 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001966}
1967
1968
1969static int wlan_set_auth(struct net_device *dev,
1970 struct iw_request_info *info,
1971 struct iw_param *dwrq,
1972 char *extra)
1973{
1974 wlan_private *priv = dev->priv;
1975 wlan_adapter *adapter = priv->adapter;
1976 struct assoc_request * assoc_req;
1977 int ret = 0;
1978 int updated = 0;
1979
Holger Schurig9012b282007-05-25 11:27:16 -04001980 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001981
1982 mutex_lock(&adapter->lock);
1983 assoc_req = wlan_get_association_request(adapter);
1984 if (!assoc_req) {
1985 ret = -ENOMEM;
1986 goto out;
1987 }
1988
1989 switch (dwrq->flags & IW_AUTH_INDEX) {
1990 case IW_AUTH_TKIP_COUNTERMEASURES:
1991 case IW_AUTH_CIPHER_PAIRWISE:
1992 case IW_AUTH_CIPHER_GROUP:
1993 case IW_AUTH_KEY_MGMT:
Dan Williams90a42212007-05-25 23:01:24 -04001994 case IW_AUTH_DROP_UNENCRYPTED:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001995 /*
1996 * libertas does not use these parameters
1997 */
1998 break;
1999
2000 case IW_AUTH_WPA_VERSION:
2001 if (dwrq->value & IW_AUTH_WPA_VERSION_DISABLED) {
2002 assoc_req->secinfo.WPAenabled = 0;
2003 assoc_req->secinfo.WPA2enabled = 0;
Dan Williams90a42212007-05-25 23:01:24 -04002004 disable_wpa (assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002005 }
2006 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA) {
2007 assoc_req->secinfo.WPAenabled = 1;
Dan Williams889c05b2007-05-10 22:57:23 -04002008 assoc_req->secinfo.wep_enabled = 0;
Dan Williams6affe782007-05-10 22:56:42 -04002009 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002010 }
2011 if (dwrq->value & IW_AUTH_WPA_VERSION_WPA2) {
2012 assoc_req->secinfo.WPA2enabled = 1;
Dan Williams889c05b2007-05-10 22:57:23 -04002013 assoc_req->secinfo.wep_enabled = 0;
Dan Williams6affe782007-05-10 22:56:42 -04002014 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002015 }
2016 updated = 1;
2017 break;
2018
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002019 case IW_AUTH_80211_AUTH_ALG:
2020 if (dwrq->value & IW_AUTH_ALG_SHARED_KEY) {
Dan Williams6affe782007-05-10 22:56:42 -04002021 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002022 } else if (dwrq->value & IW_AUTH_ALG_OPEN_SYSTEM) {
Dan Williams6affe782007-05-10 22:56:42 -04002023 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002024 } else if (dwrq->value & IW_AUTH_ALG_LEAP) {
Dan Williams6affe782007-05-10 22:56:42 -04002025 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_LEAP;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002026 } else {
2027 ret = -EINVAL;
2028 }
2029 updated = 1;
2030 break;
2031
2032 case IW_AUTH_WPA_ENABLED:
2033 if (dwrq->value) {
2034 if (!assoc_req->secinfo.WPAenabled &&
2035 !assoc_req->secinfo.WPA2enabled) {
2036 assoc_req->secinfo.WPAenabled = 1;
2037 assoc_req->secinfo.WPA2enabled = 1;
Dan Williams889c05b2007-05-10 22:57:23 -04002038 assoc_req->secinfo.wep_enabled = 0;
Dan Williams6affe782007-05-10 22:56:42 -04002039 assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002040 }
2041 } else {
2042 assoc_req->secinfo.WPAenabled = 0;
2043 assoc_req->secinfo.WPA2enabled = 0;
Dan Williams90a42212007-05-25 23:01:24 -04002044 disable_wpa (assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002045 }
2046 updated = 1;
2047 break;
2048
2049 default:
2050 ret = -EOPNOTSUPP;
2051 break;
2052 }
2053
2054out:
2055 if (ret == 0) {
2056 if (updated)
2057 set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
2058 wlan_postpone_association_work(priv);
2059 } else if (ret != -EOPNOTSUPP) {
2060 wlan_cancel_association_work(priv);
2061 }
2062 mutex_unlock(&adapter->lock);
2063
Holger Schurig9012b282007-05-25 11:27:16 -04002064 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002065 return ret;
2066}
2067
2068static int wlan_get_auth(struct net_device *dev,
2069 struct iw_request_info *info,
2070 struct iw_param *dwrq,
2071 char *extra)
2072{
Holger Schurig9012b282007-05-25 11:27:16 -04002073 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002074 wlan_private *priv = dev->priv;
2075 wlan_adapter *adapter = priv->adapter;
2076
Holger Schurig9012b282007-05-25 11:27:16 -04002077 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002078
2079 switch (dwrq->flags & IW_AUTH_INDEX) {
2080 case IW_AUTH_WPA_VERSION:
2081 dwrq->value = 0;
2082 if (adapter->secinfo.WPAenabled)
2083 dwrq->value |= IW_AUTH_WPA_VERSION_WPA;
2084 if (adapter->secinfo.WPA2enabled)
2085 dwrq->value |= IW_AUTH_WPA_VERSION_WPA2;
2086 if (!dwrq->value)
2087 dwrq->value |= IW_AUTH_WPA_VERSION_DISABLED;
2088 break;
2089
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002090 case IW_AUTH_80211_AUTH_ALG:
Dan Williams6affe782007-05-10 22:56:42 -04002091 dwrq->value = adapter->secinfo.auth_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002092 break;
2093
2094 case IW_AUTH_WPA_ENABLED:
2095 if (adapter->secinfo.WPAenabled && adapter->secinfo.WPA2enabled)
2096 dwrq->value = 1;
2097 break;
2098
2099 default:
Holger Schurig9012b282007-05-25 11:27:16 -04002100 ret = -EOPNOTSUPP;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002101 }
2102
Holger Schurig9012b282007-05-25 11:27:16 -04002103 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
2104 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002105}
2106
2107
2108static int wlan_set_txpow(struct net_device *dev, struct iw_request_info *info,
2109 struct iw_param *vwrq, char *extra)
2110{
2111 int ret = 0;
2112 wlan_private *priv = dev->priv;
2113 wlan_adapter *adapter = priv->adapter;
2114
2115 u16 dbm;
2116
Holger Schurig9012b282007-05-25 11:27:16 -04002117 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002118
2119 if (vwrq->disabled) {
2120 wlan_radio_ioctl(priv, RADIO_OFF);
2121 return 0;
2122 }
2123
2124 adapter->preamble = cmd_type_auto_preamble;
2125
2126 wlan_radio_ioctl(priv, RADIO_ON);
2127
2128 if ((vwrq->flags & IW_TXPOW_TYPE) == IW_TXPOW_MWATT) {
2129 dbm = (u16) mw_to_dbm(vwrq->value);
2130 } else
2131 dbm = (u16) vwrq->value;
2132
2133 /* auto tx power control */
2134
2135 if (vwrq->fixed == 0)
2136 dbm = 0xffff;
2137
Holger Schurig9012b282007-05-25 11:27:16 -04002138 lbs_deb_wext("txpower set %d dbm\n", dbm);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002139
2140 ret = libertas_prepare_and_send_command(priv,
2141 cmd_802_11_rf_tx_power,
2142 cmd_act_tx_power_opt_set_low,
2143 cmd_option_waitforrsp, 0, (void *)&dbm);
2144
Holger Schurig9012b282007-05-25 11:27:16 -04002145 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002146 return ret;
2147}
2148
2149static int wlan_get_essid(struct net_device *dev, struct iw_request_info *info,
2150 struct iw_point *dwrq, char *extra)
2151{
2152 wlan_private *priv = dev->priv;
2153 wlan_adapter *adapter = priv->adapter;
2154
Holger Schurig9012b282007-05-25 11:27:16 -04002155 lbs_deb_enter(LBS_DEB_WEXT);
2156
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002157 /*
2158 * Note : if dwrq->flags != 0, we should get the relevant SSID from
2159 * the SSID list...
2160 */
2161
2162 /*
2163 * Get the current SSID
2164 */
2165 if (adapter->connect_status == libertas_connected) {
2166 memcpy(extra, adapter->curbssparams.ssid.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';
2172 }
2173 /*
2174 * If none, we may want to get the one that was set
2175 */
2176
2177 /* To make the driver backward compatible with WPA supplicant v0.2.4 */
2178 if (dwrq->length == 32) /* check with WPA supplicant buffer size */
2179 dwrq->length = min_t(size_t, adapter->curbssparams.ssid.ssidlength,
2180 IW_ESSID_MAX_SIZE);
2181 else
2182 dwrq->length = adapter->curbssparams.ssid.ssidlength + 1;
2183
2184 dwrq->flags = 1; /* active */
2185
Holger Schurig9012b282007-05-25 11:27:16 -04002186 lbs_deb_leave(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002187 return 0;
2188}
2189
2190static int wlan_set_essid(struct net_device *dev, struct iw_request_info *info,
2191 struct iw_point *dwrq, char *extra)
2192{
2193 wlan_private *priv = dev->priv;
2194 wlan_adapter *adapter = priv->adapter;
2195 int ret = 0;
2196 struct WLAN_802_11_SSID ssid;
2197 struct assoc_request * assoc_req;
2198 int ssid_len = dwrq->length;
2199
Holger Schurig9012b282007-05-25 11:27:16 -04002200 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002201
2202 /*
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.
2206 */
2207 if ((ssid_len > 0) && (extra[ssid_len - 1] == '\0'))
2208 ssid_len--;
2209
2210 /* Check the size of the string */
2211 if (ssid_len > IW_ESSID_MAX_SIZE) {
2212 ret = -E2BIG;
2213 goto out;
2214 }
2215
2216 memset(&ssid, 0, sizeof(struct WLAN_802_11_SSID));
2217
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;
2224 }
2225
Holger Schurig9012b282007-05-25 11:27:16 -04002226 lbs_deb_wext("requested new SSID '%s'\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002227 (ssid.ssidlength > 0) ? (char *)ssid.ssid : "any");
2228
2229out:
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);
2241 }
2242 }
2243
2244 /* Cancel the association request if there was an error */
2245 if (ret != 0) {
2246 wlan_cancel_association_work(priv);
2247 }
2248
2249 mutex_unlock(&adapter->lock);
2250
Holger Schurig9012b282007-05-25 11:27:16 -04002251 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002252 return ret;
2253}
2254
2255/**
2256 * @brief Connect to the AP or Ad-hoc Network with specific bssid
2257 *
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
2263 */
2264static int wlan_set_wap(struct net_device *dev, struct iw_request_info *info,
2265 struct sockaddr *awrq, char *extra)
2266{
2267 wlan_private *priv = dev->priv;
2268 wlan_adapter *adapter = priv->adapter;
2269 struct assoc_request * assoc_req;
2270 int ret = 0;
2271
Holger Schurig9012b282007-05-25 11:27:16 -04002272 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002273
2274 if (awrq->sa_family != ARPHRD_ETHER)
2275 return -EINVAL;
2276
Holger Schurig9012b282007-05-25 11:27:16 -04002277 lbs_deb_wext("ASSOC: WAP: sa_data " MAC_FMT "\n", MAC_ARG(awrq->sa_data));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002278
2279 mutex_lock(&adapter->lock);
2280
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);
2291 }
2292
2293 mutex_unlock(&adapter->lock);
2294
2295 return ret;
2296}
2297
2298void libertas_get_fwversion(wlan_adapter * adapter, char *fwversion, int maxlen)
2299{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002300 char fwver[32];
2301
2302 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002303
David Woodhousee5b3d472007-05-25 23:40:21 -04002304 if (adapter->fwreleasenumber[3] == 0)
2305 sprintf(fwver, "%u.%u.%u",
2306 adapter->fwreleasenumber[2],
2307 adapter->fwreleasenumber[1],
2308 adapter->fwreleasenumber[0]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002309 else
2310 sprintf(fwver, "%u.%u.%u.p%u",
David Woodhousee5b3d472007-05-25 23:40:21 -04002311 adapter->fwreleasenumber[2],
2312 adapter->fwreleasenumber[1],
2313 adapter->fwreleasenumber[0],
2314 adapter->fwreleasenumber[3]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002315
David Woodhousee5b3d472007-05-25 23:40:21 -04002316 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002317 snprintf(fwversion, maxlen, fwver);
2318}
2319
2320
2321/*
2322 * iwconfig settable callbacks
2323 */
2324static const iw_handler wlan_handler[] = {
2325 (iw_handler) NULL, /* SIOCSIWCOMMIT */
2326 (iw_handler) wlan_get_name, /* SIOCGIWNAME */
2327 (iw_handler) NULL, /* SIOCSIWNWID */
2328 (iw_handler) NULL, /* SIOCGIWNWID */
2329 (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
2330 (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
2331 (iw_handler) wlan_set_mode, /* SIOCSIWMODE */
2332 (iw_handler) wlan_get_mode, /* SIOCGIWMODE */
2333 (iw_handler) NULL, /* SIOCSIWSENS */
2334 (iw_handler) NULL, /* SIOCGIWSENS */
2335 (iw_handler) NULL, /* SIOCSIWRANGE */
2336 (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
2337 (iw_handler) NULL, /* SIOCSIWPRIV */
2338 (iw_handler) NULL, /* SIOCGIWPRIV */
2339 (iw_handler) NULL, /* SIOCSIWSTATS */
2340 (iw_handler) NULL, /* SIOCGIWSTATS */
2341 iw_handler_set_spy, /* SIOCSIWSPY */
2342 iw_handler_get_spy, /* SIOCGIWSPY */
2343 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
2344 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
2345 (iw_handler) wlan_set_wap, /* SIOCSIWAP */
2346 (iw_handler) wlan_get_wap, /* SIOCGIWAP */
2347 (iw_handler) NULL, /* SIOCSIWMLME */
2348 (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
2349 (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2350 (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2351 (iw_handler) wlan_set_essid, /* SIOCSIWESSID */
2352 (iw_handler) wlan_get_essid, /* SIOCGIWESSID */
2353 (iw_handler) wlan_set_nick, /* SIOCSIWNICKN */
2354 (iw_handler) wlan_get_nick, /* SIOCGIWNICKN */
2355 (iw_handler) NULL, /* -- hole -- */
2356 (iw_handler) NULL, /* -- hole -- */
2357 (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
2358 (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
2359 (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
2360 (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
2361 (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
2362 (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
2363 (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
2364 (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
2365 (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
2366 (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
2367 (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
2368 (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
2369 (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
2370 (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
2371 (iw_handler) NULL, /* -- hole -- */
2372 (iw_handler) NULL, /* -- hole -- */
2373 (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
2374 (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
2375 (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
2376 (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
2377 (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2378 (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2379 (iw_handler) NULL, /* SIOCSIWPMKSA */
2380};
2381
Luis Carlos Cobo Rusf5e05b62007-05-25 23:08:34 -04002382static const iw_handler mesh_wlan_handler[] = {
2383 (iw_handler) NULL, /* SIOCSIWCOMMIT */
2384 (iw_handler) wlan_get_name, /* SIOCGIWNAME */
2385 (iw_handler) NULL, /* SIOCSIWNWID */
2386 (iw_handler) NULL, /* SIOCGIWNWID */
2387 (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
2388 (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
2389 (iw_handler) NULL, /* SIOCSIWMODE */
2390 (iw_handler) mesh_wlan_get_mode, /* SIOCGIWMODE */
2391 (iw_handler) NULL, /* SIOCSIWSENS */
2392 (iw_handler) NULL, /* SIOCGIWSENS */
2393 (iw_handler) NULL, /* SIOCSIWRANGE */
2394 (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
2395 (iw_handler) NULL, /* SIOCSIWPRIV */
2396 (iw_handler) NULL, /* SIOCGIWPRIV */
2397 (iw_handler) NULL, /* SIOCSIWSTATS */
2398 (iw_handler) NULL, /* SIOCGIWSTATS */
2399 iw_handler_set_spy, /* SIOCSIWSPY */
2400 iw_handler_get_spy, /* SIOCGIWSPY */
2401 iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
2402 iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
2403 (iw_handler) NULL, /* SIOCSIWAP */
2404 (iw_handler) NULL, /* SIOCGIWAP */
2405 (iw_handler) NULL, /* SIOCSIWMLME */
2406 (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
2407 (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
2408 (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
2409 (iw_handler) NULL, /* SIOCSIWESSID */
2410 (iw_handler) NULL, /* SIOCGIWESSID */
2411 (iw_handler) NULL, /* SIOCSIWNICKN */
2412 (iw_handler) mesh_get_nick, /* SIOCGIWNICKN */
2413 (iw_handler) NULL, /* -- hole -- */
2414 (iw_handler) NULL, /* -- hole -- */
2415 (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
2416 (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
2417 (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
2418 (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
2419 (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
2420 (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
2421 (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
2422 (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
2423 (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
2424 (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
2425 (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
2426 (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
2427 (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
2428 (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
2429 (iw_handler) NULL, /* -- hole -- */
2430 (iw_handler) NULL, /* -- hole -- */
2431 (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
2432 (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
2433 (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
2434 (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
2435 (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
2436 (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
2437 (iw_handler) NULL, /* SIOCSIWPMKSA */
2438};
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002439struct iw_handler_def libertas_handler_def = {
2440 .num_standard = sizeof(wlan_handler) / sizeof(iw_handler),
2441 .num_private = sizeof(wlan_private_handler) / sizeof(iw_handler),
2442 .num_private_args = sizeof(wlan_private_args) /
2443 sizeof(struct iw_priv_args),
2444 .standard = (iw_handler *) wlan_handler,
2445 .private = (iw_handler *) wlan_private_handler,
2446 .private_args = (struct iw_priv_args *)wlan_private_args,
2447 .get_wireless_stats = wlan_get_wireless_stats,
2448};
Luis Carlos Cobo Rusf5e05b62007-05-25 23:08:34 -04002449
2450struct iw_handler_def mesh_handler_def = {
2451 .num_standard = sizeof(mesh_wlan_handler) / sizeof(iw_handler),
2452 .num_private = sizeof(wlan_private_handler) / sizeof(iw_handler),
2453 .num_private_args = sizeof(wlan_private_args) /
2454 sizeof(struct iw_priv_args),
2455 .standard = (iw_handler *) mesh_wlan_handler,
2456 .private = (iw_handler *) wlan_private_handler,
2457 .private_args = (struct iw_priv_args *)wlan_private_args,
2458 .get_wireless_stats = wlan_get_wireless_stats,
2459};