blob: 5a466eced9796b1dc22619f4d7add7a91dd93ec0 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * Functions implementing wlan infrastructure and adhoc join routines,
3 * IOCTL handlers as well as command preperation and response routines
4 * for sending adhoc start, adhoc join, and association commands
5 * to the firmware.
6 */
7#include <linux/netdevice.h>
8#include <linux/if_arp.h>
9#include <linux/wireless.h>
10
11#include <net/iw_handler.h>
12
13#include "host.h"
14#include "decl.h"
15#include "join.h"
16#include "dev.h"
17
Dan Williams889c05b2007-05-10 22:57:23 -040018#define AD_HOC_CAP_PRIVACY_ON 1
19
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020/**
21 * @brief This function finds out the common rates between rate1 and rate2.
22 *
23 * It will fill common rates in rate1 as output if found.
24 *
25 * NOTE: Setting the MSB of the basic rates need to be taken
26 * care, either before or after calling this function
27 *
28 * @param adapter A pointer to wlan_adapter structure
29 * @param rate1 the buffer which keeps input and output
30 * @param rate1_size the size of rate1 buffer
31 * @param rate2 the buffer which keeps rate2
32 * @param rate2_size the size of rate2 buffer.
33 *
34 * @return 0 or -1
35 */
36static int get_common_rates(wlan_adapter * adapter, u8 * rate1,
37 int rate1_size, u8 * rate2, int rate2_size)
38{
39 u8 *ptr = rate1;
40 int ret = 0;
41 u8 tmp[30];
42 int i;
43
44 memset(&tmp, 0, sizeof(tmp));
45 memcpy(&tmp, rate1, min_t(size_t, rate1_size, sizeof(tmp)));
46 memset(rate1, 0, rate1_size);
47
48 /* Mask the top bit of the original values */
49 for (i = 0; tmp[i] && i < sizeof(tmp); i++)
50 tmp[i] &= 0x7F;
51
52 for (i = 0; rate2[i] && i < rate2_size; i++) {
53 /* Check for Card Rate in tmp, excluding the top bit */
54 if (strchr(tmp, rate2[i] & 0x7F)) {
55 /* values match, so copy the Card Rate to rate1 */
56 *rate1++ = rate2[i];
57 }
58 }
59
60 lbs_dbg_hex("rate1 (AP) rates:", tmp, sizeof(tmp));
61 lbs_dbg_hex("rate2 (Card) rates:", rate2, rate2_size);
62 lbs_dbg_hex("Common rates:", ptr, rate1_size);
63 lbs_pr_debug(1, "Tx datarate is set to 0x%X\n", adapter->datarate);
64
65 if (!adapter->is_datarate_auto) {
66 while (*ptr) {
67 if ((*ptr & 0x7f) == adapter->datarate) {
68 ret = 0;
69 goto done;
70 }
71 ptr++;
72 }
73 lbs_pr_alert( "Previously set fixed data rate %#x isn't "
74 "compatible with the network.\n", adapter->datarate);
75
76 ret = -1;
77 goto done;
78 }
79
80 ret = 0;
81done:
82 return ret;
83}
84
85int libertas_send_deauth(wlan_private * priv)
86{
87 wlan_adapter *adapter = priv->adapter;
88 int ret = 0;
89
90 if (adapter->inframode == wlan802_11infrastructure &&
91 adapter->connect_status == libertas_connected)
92 ret = libertas_send_deauthentication(priv);
93 else
94 ret = -ENOTSUPP;
95
96 return ret;
97}
98
99int libertas_do_adhocstop_ioctl(wlan_private * priv)
100{
101 wlan_adapter *adapter = priv->adapter;
102 int ret = 0;
103
104 if (adapter->inframode == wlan802_11ibss &&
105 adapter->connect_status == libertas_connected)
106 ret = libertas_stop_adhoc_network(priv);
107 else
108 ret = -ENOTSUPP;
109
110 return ret;
111}
112
113/**
114 * @brief Associate to a specific BSS discovered in a scan
115 *
116 * @param priv A pointer to wlan_private structure
117 * @param pbssdesc Pointer to the BSS descriptor to associate with.
118 *
119 * @return 0-success, otherwise fail
120 */
121int wlan_associate(wlan_private * priv, struct bss_descriptor * pbssdesc)
122{
123 wlan_adapter *adapter = priv->adapter;
124 int ret;
125
126 ENTER();
127
128 ret = libertas_prepare_and_send_command(priv, cmd_802_11_authenticate,
129 0, cmd_option_waitforrsp,
130 0, pbssdesc->macaddress);
131
132 if (ret) {
133 LEAVE();
134 return ret;
135 }
136
137 /* set preamble to firmware */
138 if (adapter->capinfo.shortpreamble && pbssdesc->cap.shortpreamble)
139 adapter->preamble = cmd_type_short_preamble;
140 else
141 adapter->preamble = cmd_type_long_preamble;
142
143 libertas_set_radio_control(priv);
144
145 ret = libertas_prepare_and_send_command(priv, cmd_802_11_associate,
146 0, cmd_option_waitforrsp, 0, pbssdesc);
147
148 LEAVE();
149 return ret;
150}
151
152/**
153 * @brief Start an Adhoc Network
154 *
155 * @param priv A pointer to wlan_private structure
156 * @param adhocssid The ssid of the Adhoc Network
157 * @return 0--success, -1--fail
158 */
159int libertas_start_adhoc_network(wlan_private * priv, struct WLAN_802_11_SSID *adhocssid)
160{
161 wlan_adapter *adapter = priv->adapter;
162 int ret = 0;
163
164 adapter->adhoccreate = 1;
165
166 if (!adapter->capinfo.shortpreamble) {
167 lbs_pr_debug(1, "AdhocStart: Long preamble\n");
168 adapter->preamble = cmd_type_long_preamble;
169 } else {
170 lbs_pr_debug(1, "AdhocStart: Short preamble\n");
171 adapter->preamble = cmd_type_short_preamble;
172 }
173
174 libertas_set_radio_control(priv);
175
176 lbs_pr_debug(1, "Adhoc channel = %d\n", adapter->adhocchannel);
177 lbs_pr_debug(1, "curbssparams.channel = %d\n",
178 adapter->curbssparams.channel);
179 lbs_pr_debug(1, "curbssparams.band = %d\n", adapter->curbssparams.band);
180
181 ret = libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_start,
182 0, cmd_option_waitforrsp, 0, adhocssid);
183
184 return ret;
185}
186
187/**
188 * @brief Join an adhoc network found in a previous scan
189 *
190 * @param priv A pointer to wlan_private structure
191 * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
192 * to attempt to join
193 *
194 * @return 0--success, -1--fail
195 */
196int libertas_join_adhoc_network(wlan_private * priv, struct bss_descriptor * pbssdesc)
197{
198 wlan_adapter *adapter = priv->adapter;
199 int ret = 0;
200
201 lbs_pr_debug(1, "libertas_join_adhoc_network: CurBss.ssid =%s\n",
202 adapter->curbssparams.ssid.ssid);
203 lbs_pr_debug(1, "libertas_join_adhoc_network: CurBss.ssid_len =%u\n",
204 adapter->curbssparams.ssid.ssidlength);
205 lbs_pr_debug(1, "libertas_join_adhoc_network: ssid =%s\n", pbssdesc->ssid.ssid);
206 lbs_pr_debug(1, "libertas_join_adhoc_network: ssid len =%u\n",
207 pbssdesc->ssid.ssidlength);
208
209 /* check if the requested SSID is already joined */
210 if (adapter->curbssparams.ssid.ssidlength
211 && !libertas_SSID_cmp(&pbssdesc->ssid, &adapter->curbssparams.ssid)
212 && (adapter->curbssparams.bssdescriptor.inframode ==
213 wlan802_11ibss)) {
214
215 lbs_pr_debug(1,
216 "ADHOC_J_CMD: New ad-hoc SSID is the same as current, "
217 "not attempting to re-join");
218
219 return -1;
220 }
221
222 /*Use shortpreamble only when both creator and card supports
223 short preamble */
224 if (!pbssdesc->cap.shortpreamble || !adapter->capinfo.shortpreamble) {
225 lbs_pr_debug(1, "AdhocJoin: Long preamble\n");
226 adapter->preamble = cmd_type_long_preamble;
227 } else {
228 lbs_pr_debug(1, "AdhocJoin: Short preamble\n");
229 adapter->preamble = cmd_type_short_preamble;
230 }
231
232 libertas_set_radio_control(priv);
233
234 lbs_pr_debug(1, "curbssparams.channel = %d\n",
235 adapter->curbssparams.channel);
236 lbs_pr_debug(1, "curbssparams.band = %c\n", adapter->curbssparams.band);
237
238 adapter->adhoccreate = 0;
239
240 ret = libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_join,
241 0, cmd_option_waitforrsp,
242 OID_802_11_SSID, pbssdesc);
243
244 return ret;
245}
246
247int libertas_stop_adhoc_network(wlan_private * priv)
248{
249 return libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_stop,
250 0, cmd_option_waitforrsp, 0, NULL);
251}
252
253/**
254 * @brief Send Deauthentication Request
255 *
256 * @param priv A pointer to wlan_private structure
257 * @return 0--success, -1--fail
258 */
259int libertas_send_deauthentication(wlan_private * priv)
260{
261 return libertas_prepare_and_send_command(priv, cmd_802_11_deauthenticate,
262 0, cmd_option_waitforrsp, 0, NULL);
263}
264
265/**
266 * @brief Set Idle Off
267 *
268 * @param priv A pointer to wlan_private structure
269 * @return 0 --success, otherwise fail
270 */
271int libertas_idle_off(wlan_private * priv)
272{
273 wlan_adapter *adapter = priv->adapter;
274 int ret = 0;
275 const u8 zeromac[] = { 0, 0, 0, 0, 0, 0 };
276 int i;
277
278 ENTER();
279
280 if (adapter->connect_status == libertas_disconnected) {
281 if (adapter->inframode == wlan802_11infrastructure) {
282 if (memcmp(adapter->previousbssid, zeromac,
283 sizeof(zeromac)) != 0) {
284
285 lbs_pr_debug(1, "Previous SSID = %s\n",
286 adapter->previousssid.ssid);
287 lbs_pr_debug(1, "Previous BSSID = "
288 "%02x:%02x:%02x:%02x:%02x:%02x:\n",
289 adapter->previousbssid[0],
290 adapter->previousbssid[1],
291 adapter->previousbssid[2],
292 adapter->previousbssid[3],
293 adapter->previousbssid[4],
294 adapter->previousbssid[5]);
295
296 i = libertas_find_SSID_in_list(adapter,
297 &adapter->previousssid,
298 adapter->previousbssid,
299 adapter->inframode);
300
301 if (i < 0) {
302 libertas_send_specific_BSSID_scan(priv,
303 adapter->
304 previousbssid,
305 1);
306 i = libertas_find_SSID_in_list(adapter,
307 &adapter->
308 previousssid,
309 adapter->
310 previousbssid,
311 adapter->
312 inframode);
313 }
314
315 if (i < 0) {
316 /* If the BSSID could not be found, try just the SSID */
317 i = libertas_find_SSID_in_list(adapter,
318 &adapter->
319 previousssid, NULL,
320 adapter->
321 inframode);
322 }
323
324 if (i < 0) {
325 libertas_send_specific_SSID_scan(priv,
326 &adapter->
327 previousssid,
328 1);
329 i = libertas_find_SSID_in_list(adapter,
330 &adapter->
331 previousssid, NULL,
332 adapter->
333 inframode);
334 }
335
336 if (i >= 0) {
337 ret =
338 wlan_associate(priv,
339 &adapter->
340 scantable[i]);
341 }
342 }
343 } else if (adapter->inframode == wlan802_11ibss) {
344 ret = libertas_prepare_and_send_command(priv,
345 cmd_802_11_ad_hoc_start,
346 0,
347 cmd_option_waitforrsp,
348 0, &adapter->previousssid);
349 }
350 }
351 /* else it is connected */
352
353 lbs_pr_debug(1, "\nwlanidle is off");
354 LEAVE();
355 return ret;
356}
357
358/**
359 * @brief Set Idle On
360 *
361 * @param priv A pointer to wlan_private structure
362 * @return 0 --success, otherwise fail
363 */
364int libertas_idle_on(wlan_private * priv)
365{
366 wlan_adapter *adapter = priv->adapter;
367 int ret = 0;
368
369 if (adapter->connect_status == libertas_connected) {
370 if (adapter->inframode == wlan802_11infrastructure) {
371 lbs_pr_debug(1, "Previous SSID = %s\n",
372 adapter->previousssid.ssid);
373 memmove(&adapter->previousssid,
374 &adapter->curbssparams.ssid,
375 sizeof(struct WLAN_802_11_SSID));
376 libertas_send_deauth(priv);
377
378 } else if (adapter->inframode == wlan802_11ibss) {
379 ret = libertas_stop_adhoc_network(priv);
380 }
381
382 }
383
384 lbs_pr_debug(1, "\nwlanidle is on");
385
386 return ret;
387}
388
389/**
390 * @brief This function prepares command of authenticate.
391 *
392 * @param priv A pointer to wlan_private structure
393 * @param cmd A pointer to cmd_ds_command structure
394 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
395 *
396 * @return 0 or -1
397 */
398int libertas_cmd_80211_authenticate(wlan_private * priv,
399 struct cmd_ds_command *cmd,
400 void *pdata_buf)
401{
402 wlan_adapter *adapter = priv->adapter;
Dan Williams6affe782007-05-10 22:56:42 -0400403 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
404 int ret = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200405 u8 *bssid = pdata_buf;
406
407 cmd->command = cpu_to_le16(cmd_802_11_authenticate);
Dan Williams6affe782007-05-10 22:56:42 -0400408 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
409 + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410
Dan Williams6affe782007-05-10 22:56:42 -0400411 /* translate auth mode to 802.11 defined wire value */
412 switch (adapter->secinfo.auth_mode) {
413 case IW_AUTH_ALG_OPEN_SYSTEM:
414 pauthenticate->authtype = 0x00;
415 break;
416 case IW_AUTH_ALG_SHARED_KEY:
417 pauthenticate->authtype = 0x01;
418 break;
419 case IW_AUTH_ALG_LEAP:
420 pauthenticate->authtype = 0x80;
421 break;
422 default:
423 lbs_pr_debug(1, "AUTH_CMD: invalid auth alg 0x%X\n",
424 adapter->secinfo.auth_mode);
425 goto out;
426 }
427
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
429
430 lbs_pr_debug(1, "AUTH_CMD: Bssid is : %x:%x:%x:%x:%x:%x\n",
431 bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
Dan Williams6affe782007-05-10 22:56:42 -0400432 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433
Dan Williams6affe782007-05-10 22:56:42 -0400434out:
435 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436}
437
438int libertas_cmd_80211_deauthenticate(wlan_private * priv,
439 struct cmd_ds_command *cmd)
440{
441 wlan_adapter *adapter = priv->adapter;
442 struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
443
444 ENTER();
445
446 cmd->command = cpu_to_le16(cmd_802_11_deauthenticate);
447 cmd->size =
448 cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
449 S_DS_GEN);
450
451 /* set AP MAC address */
452 memmove(dauth->macaddr, adapter->curbssparams.bssid,
453 ETH_ALEN);
454
455 /* Reason code 3 = Station is leaving */
456#define REASON_CODE_STA_LEAVING 3
457 dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
458
459 LEAVE();
460 return 0;
461}
462
463int libertas_cmd_80211_associate(wlan_private * priv,
464 struct cmd_ds_command *cmd, void *pdata_buf)
465{
466 wlan_adapter *adapter = priv->adapter;
467 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
468 int ret = 0;
469 struct bss_descriptor *pbssdesc;
470 u8 *card_rates;
471 u8 *pos;
472 int card_rates_size;
473 u16 tmpcap;
474 struct mrvlietypes_ssidparamset *ssid;
475 struct mrvlietypes_phyparamset *phy;
476 struct mrvlietypes_ssparamset *ss;
477 struct mrvlietypes_ratesparamset *rates;
478 struct mrvlietypes_rsnparamset *rsn;
479
480 ENTER();
481
482 pbssdesc = pdata_buf;
483 pos = (u8 *) passo;
484
485 if (!adapter) {
486 ret = -1;
487 goto done;
488 }
489
490 cmd->command = cpu_to_le16(cmd_802_11_associate);
491
492 /* Save so we know which BSS Desc to use in the response handler */
493 adapter->pattemptedbssdesc = pbssdesc;
494
495 memcpy(passo->peerstaaddr,
496 pbssdesc->macaddress, sizeof(passo->peerstaaddr));
497 pos += sizeof(passo->peerstaaddr);
498
499 /* set the listen interval */
500 passo->listeninterval = adapter->listeninterval;
501
502 pos += sizeof(passo->capinfo);
503 pos += sizeof(passo->listeninterval);
504 pos += sizeof(passo->bcnperiod);
505 pos += sizeof(passo->dtimperiod);
506
507 ssid = (struct mrvlietypes_ssidparamset *) pos;
508 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
509 ssid->header.len = pbssdesc->ssid.ssidlength;
510 memcpy(ssid->ssid, pbssdesc->ssid.ssid, ssid->header.len);
511 pos += sizeof(ssid->header) + ssid->header.len;
512 ssid->header.len = cpu_to_le16(ssid->header.len);
513
514 phy = (struct mrvlietypes_phyparamset *) pos;
515 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
516 phy->header.len = sizeof(phy->fh_ds.dsparamset);
517 memcpy(&phy->fh_ds.dsparamset,
518 &pbssdesc->phyparamset.dsparamset.currentchan,
519 sizeof(phy->fh_ds.dsparamset));
520 pos += sizeof(phy->header) + phy->header.len;
521 phy->header.len = cpu_to_le16(phy->header.len);
522
523 ss = (struct mrvlietypes_ssparamset *) pos;
524 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
525 ss->header.len = sizeof(ss->cf_ibss.cfparamset);
526 pos += sizeof(ss->header) + ss->header.len;
527 ss->header.len = cpu_to_le16(ss->header.len);
528
529 rates = (struct mrvlietypes_ratesparamset *) pos;
530 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
531
532 memcpy(&rates->rates, &pbssdesc->libertas_supported_rates, WLAN_SUPPORTED_RATES);
533
534 card_rates = libertas_supported_rates;
535 card_rates_size = sizeof(libertas_supported_rates);
536
537 if (get_common_rates(adapter, rates->rates, WLAN_SUPPORTED_RATES,
538 card_rates, card_rates_size)) {
539 ret = -1;
540 goto done;
541 }
542
543 rates->header.len = min_t(size_t, strlen(rates->rates), WLAN_SUPPORTED_RATES);
544 adapter->curbssparams.numofrates = rates->header.len;
545
546 pos += sizeof(rates->header) + rates->header.len;
547 rates->header.len = cpu_to_le16(rates->header.len);
548
549 if (adapter->secinfo.WPAenabled || adapter->secinfo.WPA2enabled) {
550 rsn = (struct mrvlietypes_rsnparamset *) pos;
551 rsn->header.type = (u16) adapter->wpa_ie[0]; /* WPA_IE or WPA2_IE */
552 rsn->header.type = cpu_to_le16(rsn->header.type);
553 rsn->header.len = (u16) adapter->wpa_ie[1];
554 memcpy(rsn->rsnie, &adapter->wpa_ie[2], rsn->header.len);
555 lbs_dbg_hex("ASSOC_CMD: RSN IE", (u8 *) rsn,
556 sizeof(rsn->header) + rsn->header.len);
557 pos += sizeof(rsn->header) + rsn->header.len;
558 rsn->header.len = cpu_to_le16(rsn->header.len);
559 }
560
561 /* update curbssparams */
562 adapter->curbssparams.channel =
563 (pbssdesc->phyparamset.dsparamset.currentchan);
564
565 /* Copy the infra. association rates into Current BSS state structure */
566 memcpy(&adapter->curbssparams.datarates, &rates->rates,
567 min_t(size_t, sizeof(adapter->curbssparams.datarates), rates->header.len));
568
569 lbs_pr_debug(1, "ASSOC_CMD: rates->header.len = %d\n", rates->header.len);
570
571 /* set IBSS field */
572 if (pbssdesc->inframode == wlan802_11infrastructure) {
573#define CAPINFO_ESS_MODE 1
574 passo->capinfo.ess = CAPINFO_ESS_MODE;
575 }
576
577 if (libertas_parse_dnld_countryinfo_11d(priv)) {
578 ret = -1;
579 goto done;
580 }
581
582 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
583
584 /* set the capability info at last */
585 memcpy(&tmpcap, &pbssdesc->cap, sizeof(passo->capinfo));
586 tmpcap &= CAPINFO_MASK;
587 lbs_pr_debug(1, "ASSOC_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
588 tmpcap, CAPINFO_MASK);
589 tmpcap = cpu_to_le16(tmpcap);
590 memcpy(&passo->capinfo, &tmpcap, sizeof(passo->capinfo));
591
592 done:
593 LEAVE();
594 return ret;
595}
596
597int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
598 struct cmd_ds_command *cmd, void *pssid)
599{
600 wlan_adapter *adapter = priv->adapter;
601 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
602 int ret = 0;
603 int cmdappendsize = 0;
604 int i;
605 u16 tmpcap;
606 struct bss_descriptor *pbssdesc;
607 struct WLAN_802_11_SSID *ssid = pssid;
608
609 ENTER();
610
611 if (!adapter) {
612 ret = -1;
613 goto done;
614 }
615
616 cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_start);
617
618 pbssdesc = &adapter->curbssparams.bssdescriptor;
619 adapter->pattemptedbssdesc = pbssdesc;
620
621 /*
622 * Fill in the parameters for 2 data structures:
623 * 1. cmd_ds_802_11_ad_hoc_start command
624 * 2. adapter->scantable[i]
625 *
626 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
627 * probe delay, and cap info.
628 *
629 * Firmware will fill up beacon period, DTIM, Basic rates
630 * and operational rates.
631 */
632
633 memset(adhs->SSID, 0, IW_ESSID_MAX_SIZE);
634
635 memcpy(adhs->SSID, ssid->ssid, ssid->ssidlength);
636
637 lbs_pr_debug(1, "ADHOC_S_CMD: SSID = %s\n", adhs->SSID);
638
639 memset(pbssdesc->ssid.ssid, 0, IW_ESSID_MAX_SIZE);
640 memcpy(pbssdesc->ssid.ssid, ssid->ssid, ssid->ssidlength);
641
642 pbssdesc->ssid.ssidlength = ssid->ssidlength;
643
644 /* set the BSS type */
645 adhs->bsstype = cmd_bss_type_ibss;
646 pbssdesc->inframode = wlan802_11ibss;
647 adhs->beaconperiod = adapter->beaconperiod;
648
649 /* set Physical param set */
650#define DS_PARA_IE_ID 3
651#define DS_PARA_IE_LEN 1
652
653 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
654 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
655
656 WARN_ON(!adapter->adhocchannel);
657
658 lbs_pr_debug(1, "ADHOC_S_CMD: Creating ADHOC on channel %d\n",
659 adapter->adhocchannel);
660
661 adapter->curbssparams.channel = adapter->adhocchannel;
662
663 pbssdesc->channel = adapter->adhocchannel;
664 adhs->phyparamset.dsparamset.currentchan = adapter->adhocchannel;
665
666 memcpy(&pbssdesc->phyparamset,
667 &adhs->phyparamset, sizeof(union ieeetypes_phyparamset));
668
669 /* set IBSS param set */
670#define IBSS_PARA_IE_ID 6
671#define IBSS_PARA_IE_LEN 2
672
673 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
674 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
675 adhs->ssparamset.ibssparamset.atimwindow = adapter->atimwindow;
676 memcpy(&pbssdesc->ssparamset,
677 &adhs->ssparamset, sizeof(union IEEEtypes_ssparamset));
678
679 /* set capability info */
680 adhs->cap.ess = 0;
681 adhs->cap.ibss = 1;
682 pbssdesc->cap.ibss = 1;
683
684 /* probedelay */
685 adhs->probedelay = cpu_to_le16(cmd_scan_probe_delay_time);
686
687 /* set up privacy in adapter->scantable[i] */
Dan Williams889c05b2007-05-10 22:57:23 -0400688 if (adapter->secinfo.wep_enabled) {
689 lbs_pr_debug(1, "ADHOC_S_CMD: WEP enabled, setting privacy on\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200690 pbssdesc->privacy = wlan802_11privfilter8021xWEP;
691 adhs->cap.privacy = AD_HOC_CAP_PRIVACY_ON;
692 } else {
Dan Williams889c05b2007-05-10 22:57:23 -0400693 lbs_pr_debug(1, "ADHOC_S_CMD: WEP disabled, setting privacy off\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 pbssdesc->privacy = wlan802_11privfilteracceptall;
695 }
696
697 memset(adhs->datarate, 0, sizeof(adhs->datarate));
698
699 if (adapter->adhoc_grate_enabled) {
700 memcpy(adhs->datarate, libertas_adhoc_rates_g,
701 min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_g)));
702 } else {
703 memcpy(adhs->datarate, libertas_adhoc_rates_b,
704 min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_b)));
705 }
706
707 /* Find the last non zero */
708 for (i = 0; i < sizeof(adhs->datarate) && adhs->datarate[i]; i++) ;
709
710 adapter->curbssparams.numofrates = i;
711
712 /* Copy the ad-hoc creating rates into Current BSS state structure */
713 memcpy(&adapter->curbssparams.datarates,
714 &adhs->datarate, adapter->curbssparams.numofrates);
715
716 lbs_pr_debug(1, "ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
717 adhs->datarate[0], adhs->datarate[1],
718 adhs->datarate[2], adhs->datarate[3]);
719
720 lbs_pr_debug(1, "ADHOC_S_CMD: AD HOC Start command is ready\n");
721
722 if (libertas_create_dnld_countryinfo_11d(priv)) {
723 lbs_pr_debug(1, "ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
724 ret = -1;
725 goto done;
726 }
727
728 cmd->size =
729 cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start)
730 + S_DS_GEN + cmdappendsize);
731
732 memcpy(&tmpcap, &adhs->cap, sizeof(u16));
733 tmpcap = cpu_to_le16(tmpcap);
734 memcpy(&adhs->cap, &tmpcap, sizeof(u16));
735
736 ret = 0;
737done:
738 LEAVE();
739 return ret;
740}
741
742int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
743 struct cmd_ds_command *cmd)
744{
745 cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_stop);
746 cmd->size = cpu_to_le16(S_DS_GEN);
747
748 return 0;
749}
750
751int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
752 struct cmd_ds_command *cmd, void *pdata_buf)
753{
754 wlan_adapter *adapter = priv->adapter;
755 struct cmd_ds_802_11_ad_hoc_join *padhocjoin = &cmd->params.adj;
756 struct bss_descriptor *pbssdesc = pdata_buf;
757 int cmdappendsize = 0;
758 int ret = 0;
759 u8 *card_rates;
760 int card_rates_size;
761 u16 tmpcap;
762 int i;
763
764 ENTER();
765
766 adapter->pattemptedbssdesc = pbssdesc;
767
768 cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_join);
769
770 padhocjoin->bssdescriptor.bsstype = cmd_bss_type_ibss;
771
772 padhocjoin->bssdescriptor.beaconperiod = pbssdesc->beaconperiod;
773
774 memcpy(&padhocjoin->bssdescriptor.BSSID,
775 &pbssdesc->macaddress, ETH_ALEN);
776
777 memcpy(&padhocjoin->bssdescriptor.SSID,
778 &pbssdesc->ssid.ssid, pbssdesc->ssid.ssidlength);
779
780 memcpy(&padhocjoin->bssdescriptor.phyparamset,
781 &pbssdesc->phyparamset, sizeof(union ieeetypes_phyparamset));
782
783 memcpy(&padhocjoin->bssdescriptor.ssparamset,
784 &pbssdesc->ssparamset, sizeof(union IEEEtypes_ssparamset));
785
786 memcpy(&tmpcap, &pbssdesc->cap, sizeof(struct ieeetypes_capinfo));
787 tmpcap &= CAPINFO_MASK;
788
789 lbs_pr_debug(1, "ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
790 tmpcap, CAPINFO_MASK);
791 memcpy(&padhocjoin->bssdescriptor.cap, &tmpcap,
792 sizeof(struct ieeetypes_capinfo));
793
794 /* information on BSSID descriptor passed to FW */
795 lbs_pr_debug(1,
796 "ADHOC_J_CMD: BSSID = %2x-%2x-%2x-%2x-%2x-%2x, SSID = %s\n",
797 padhocjoin->bssdescriptor.BSSID[0],
798 padhocjoin->bssdescriptor.BSSID[1],
799 padhocjoin->bssdescriptor.BSSID[2],
800 padhocjoin->bssdescriptor.BSSID[3],
801 padhocjoin->bssdescriptor.BSSID[4],
802 padhocjoin->bssdescriptor.BSSID[5],
803 padhocjoin->bssdescriptor.SSID);
804
805 lbs_pr_debug(1, "ADHOC_J_CMD: Data Rate = %x\n",
806 (u32) padhocjoin->bssdescriptor.datarates);
807
808 /* failtimeout */
809 padhocjoin->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
810
811 /* probedelay */
812 padhocjoin->probedelay =
813 cpu_to_le16(cmd_scan_probe_delay_time);
814
815 /* Copy Data rates from the rates recorded in scan response */
816 memset(padhocjoin->bssdescriptor.datarates, 0,
817 sizeof(padhocjoin->bssdescriptor.datarates));
818 memcpy(padhocjoin->bssdescriptor.datarates, pbssdesc->datarates,
819 min(sizeof(padhocjoin->bssdescriptor.datarates),
820 sizeof(pbssdesc->datarates)));
821
822 card_rates = libertas_supported_rates;
823 card_rates_size = sizeof(libertas_supported_rates);
824
825 adapter->curbssparams.channel = pbssdesc->channel;
826
827 if (get_common_rates(adapter, padhocjoin->bssdescriptor.datarates,
828 sizeof(padhocjoin->bssdescriptor.datarates),
829 card_rates, card_rates_size)) {
830 lbs_pr_debug(1, "ADHOC_J_CMD: get_common_rates returns error.\n");
831 ret = -1;
832 goto done;
833 }
834
835 /* Find the last non zero */
836 for (i = 0; i < sizeof(padhocjoin->bssdescriptor.datarates)
837 && padhocjoin->bssdescriptor.datarates[i]; i++) ;
838
839 adapter->curbssparams.numofrates = i;
840
841 /*
842 * Copy the adhoc joining rates to Current BSS State structure
843 */
844 memcpy(adapter->curbssparams.datarates,
845 padhocjoin->bssdescriptor.datarates,
846 adapter->curbssparams.numofrates);
847
848 padhocjoin->bssdescriptor.ssparamset.ibssparamset.atimwindow =
849 cpu_to_le16(pbssdesc->atimwindow);
850
Dan Williams889c05b2007-05-10 22:57:23 -0400851 if (adapter->secinfo.wep_enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200852 padhocjoin->bssdescriptor.cap.privacy = AD_HOC_CAP_PRIVACY_ON;
853 }
854
855 if (adapter->psmode == wlan802_11powermodemax_psp) {
856 /* wake up first */
857 enum WLAN_802_11_POWER_MODE Localpsmode;
858
859 Localpsmode = wlan802_11powermodecam;
860 ret = libertas_prepare_and_send_command(priv,
861 cmd_802_11_ps_mode,
862 cmd_act_set,
863 0, 0, &Localpsmode);
864
865 if (ret) {
866 ret = -1;
867 goto done;
868 }
869 }
870
871 if (libertas_parse_dnld_countryinfo_11d(priv)) {
872 ret = -1;
873 goto done;
874 }
875
876 cmd->size =
877 cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join)
878 + S_DS_GEN + cmdappendsize);
879
880 memcpy(&tmpcap, &padhocjoin->bssdescriptor.cap,
881 sizeof(struct ieeetypes_capinfo));
882 tmpcap = cpu_to_le16(tmpcap);
883
884 memcpy(&padhocjoin->bssdescriptor.cap,
885 &tmpcap, sizeof(struct ieeetypes_capinfo));
886
887 done:
888 LEAVE();
889 return ret;
890}
891
892int libertas_ret_80211_associate(wlan_private * priv,
893 struct cmd_ds_command *resp)
894{
895 wlan_adapter *adapter = priv->adapter;
896 int ret = 0;
897 union iwreq_data wrqu;
898 struct ieeetypes_assocrsp *passocrsp;
899 struct bss_descriptor *pbssdesc;
900
901 ENTER();
902
903 passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
904
905 if (passocrsp->statuscode) {
906
907 libertas_mac_event_disconnected(priv);
908
909 lbs_pr_debug(1,
910 "ASSOC_RESP: Association failed, status code = %d\n",
911 passocrsp->statuscode);
912
913 ret = -1;
914 goto done;
915 }
916
917 lbs_dbg_hex("ASSOC_RESP:", (void *)&resp->params,
918 le16_to_cpu(resp->size) - S_DS_GEN);
919
920 /* Send a Media Connected event, according to the Spec */
921 adapter->connect_status = libertas_connected;
922
923 /* Set the attempted BSSID Index to current */
924 pbssdesc = adapter->pattemptedbssdesc;
925
926 lbs_pr_debug(1, "ASSOC_RESP: %s\n", pbssdesc->ssid.ssid);
927
928 /* Set the new SSID to current SSID */
929 memcpy(&adapter->curbssparams.ssid,
930 &pbssdesc->ssid, sizeof(struct WLAN_802_11_SSID));
931
932 /* Set the new BSSID (AP's MAC address) to current BSSID */
933 memcpy(adapter->curbssparams.bssid,
934 pbssdesc->macaddress, ETH_ALEN);
935
936 /* Make a copy of current BSSID descriptor */
937 memcpy(&adapter->curbssparams.bssdescriptor,
938 pbssdesc, sizeof(struct bss_descriptor));
939
940 lbs_pr_debug(1, "ASSOC_RESP: currentpacketfilter is %x\n",
941 adapter->currentpacketfilter);
942
943 adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
944 adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
945
946 memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
947 memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
948 adapter->nextSNRNF = 0;
949 adapter->numSNRNF = 0;
950
951 netif_carrier_on(priv->wlan_dev.netdev);
952 netif_wake_queue(priv->wlan_dev.netdev);
953
954 lbs_pr_debug(1, "ASSOC_RESP: Associated \n");
955
956 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
957 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
958 wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
959
960 done:
961 LEAVE();
962 return ret;
963}
964
965int libertas_ret_80211_disassociate(wlan_private * priv,
966 struct cmd_ds_command *resp)
967{
968 ENTER();
969
970 libertas_mac_event_disconnected(priv);
971
972 LEAVE();
973 return 0;
974}
975
976int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
977 struct cmd_ds_command *resp)
978{
979 wlan_adapter *adapter = priv->adapter;
980 int ret = 0;
981 u16 command = le16_to_cpu(resp->command);
982 u16 result = le16_to_cpu(resp->result);
983 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
984 union iwreq_data wrqu;
985 struct bss_descriptor *pbssdesc;
986
987 ENTER();
988
989 padhocresult = &resp->params.result;
990
991 lbs_pr_debug(1, "ADHOC_S_RESP: size = %d\n", le16_to_cpu(resp->size));
992 lbs_pr_debug(1, "ADHOC_S_RESP: command = %x\n", command);
993 lbs_pr_debug(1, "ADHOC_S_RESP: result = %x\n", result);
994
995 pbssdesc = adapter->pattemptedbssdesc;
996
997 /*
998 * Join result code 0 --> SUCCESS
999 */
1000 if (result) {
1001 lbs_pr_debug(1, "ADHOC_RESP failed\n");
1002 if (adapter->connect_status == libertas_connected) {
1003 libertas_mac_event_disconnected(priv);
1004 }
1005
1006 memset(&adapter->curbssparams.bssdescriptor,
1007 0x00, sizeof(adapter->curbssparams.bssdescriptor));
1008
1009 LEAVE();
1010 return -1;
1011 }
1012
1013 /*
1014 * Now the join cmd should be successful
1015 * If BSSID has changed use SSID to compare instead of BSSID
1016 */
1017 lbs_pr_debug(1, "ADHOC_J_RESP %s\n", pbssdesc->ssid.ssid);
1018
1019 /* Send a Media Connected event, according to the Spec */
1020 adapter->connect_status = libertas_connected;
1021
1022 if (command == cmd_ret_802_11_ad_hoc_start) {
1023 /* Update the created network descriptor with the new BSSID */
1024 memcpy(pbssdesc->macaddress,
1025 padhocresult->BSSID, ETH_ALEN);
1026 } else {
1027
1028 /* Make a copy of current BSSID descriptor, only needed for join since
1029 * the current descriptor is already being used for adhoc start
1030 */
1031 memmove(&adapter->curbssparams.bssdescriptor,
1032 pbssdesc, sizeof(struct bss_descriptor));
1033 }
1034
1035 /* Set the BSSID from the joined/started descriptor */
1036 memcpy(&adapter->curbssparams.bssid,
1037 pbssdesc->macaddress, ETH_ALEN);
1038
1039 /* Set the new SSID to current SSID */
1040 memcpy(&adapter->curbssparams.ssid,
1041 &pbssdesc->ssid, sizeof(struct WLAN_802_11_SSID));
1042
1043 netif_carrier_on(priv->wlan_dev.netdev);
1044 netif_wake_queue(priv->wlan_dev.netdev);
1045
1046 memset(&wrqu, 0, sizeof(wrqu));
1047 memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
1048 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1049 wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
1050
1051 lbs_pr_debug(1, "ADHOC_RESP: - Joined/Started Ad Hoc\n");
1052 lbs_pr_debug(1, "ADHOC_RESP: channel = %d\n", adapter->adhocchannel);
1053 lbs_pr_debug(1, "ADHOC_RESP: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
1054 padhocresult->BSSID[0], padhocresult->BSSID[1],
1055 padhocresult->BSSID[2], padhocresult->BSSID[3],
1056 padhocresult->BSSID[4], padhocresult->BSSID[5]);
1057
1058 LEAVE();
1059 return ret;
1060}
1061
1062int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
1063 struct cmd_ds_command *resp)
1064{
1065 ENTER();
1066
1067 libertas_mac_event_disconnected(priv);
1068
1069 LEAVE();
1070 return 0;
1071}