join.c 24.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
  *  Functions implementing wlan infrastructure and adhoc join routines,
  *  IOCTL handlers as well as command preperation and response routines
  *  for sending adhoc start, adhoc join, and association commands
  *  to the firmware.
  */
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>
10
#include <linux/etherdevice.h>
11 12 13 14 15 16 17

#include <net/iw_handler.h>

#include "host.h"
#include "decl.h"
#include "join.h"
#include "dev.h"
18
#include "assoc.h"
19

20
/* Supported rates for ad-hoc B mode */
21
static u8 adhoc_rates_b[5] = { 0x02, 0x04, 0x0b, 0x16, 0x00 };
22 23


24
/**
25
 *  @brief This function finds common rates between rate1 and card rates.
26 27 28 29 30 31 32 33
 *
 * It will fill common rates in rate1 as output if found.
 *
 * NOTE: Setting the MSB of the basic rates need to be taken
 *   care, either before or after calling this function
 *
 *  @param adapter     A pointer to wlan_adapter structure
 *  @param rate1       the buffer which keeps input and output
34
 *  @param rate1_size  the size of rate1 buffer; new size of buffer on return
35 36 37
 *
 *  @return            0 or -1
 */
38
static int get_common_rates(wlan_adapter * adapter, u8 * rates, u16 *rates_size)
39
{
40 41 42
	u8 *card_rates = libertas_bg_rates;
	size_t num_card_rates = sizeof(libertas_bg_rates);
	int ret = 0, i, j;
43
	u8 tmp[30];
44
	size_t tmp_size = 0;
45

46 47 48 49 50
	/* For each rate in card_rates that exists in rate1, copy to tmp */
	for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
		for (j = 0; rates[j] && (j < *rates_size); j++) {
			if (rates[j] == card_rates[i])
				tmp[tmp_size++] = card_rates[i];
51 52 53
		}
	}

54 55 56 57
	lbs_dbg_hex("rate1 (AP) rates:", rates, *rates_size);
	lbs_dbg_hex("rate2 (Card) rates:", card_rates, num_card_rates);
	lbs_dbg_hex("Common rates:", tmp, tmp_size);
	lbs_deb_join("Tx datarate is currently 0x%X\n", adapter->cur_rate);
58

59 60 61
	if (!adapter->auto_rate) {
		for (i = 0; i < tmp_size; i++) {
			if (tmp[i] == adapter->cur_rate)
62 63
				goto done;
		}
64 65
		lbs_pr_alert("Previously set fixed data rate %#x isn't "
		       "compatible with the network.\n", adapter->cur_rate);
66 67 68 69
		ret = -1;
		goto done;
	}
	ret = 0;
70

71
done:
72 73 74
	memset(rates, 0, *rates_size);
	*rates_size = min_t(int, tmp_size, *rates_size);
	memcpy(rates, tmp, *rates_size);
75 76 77
	return ret;
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/**
 *  @brief Sets the MSB on basic rates as the firmware requires
 *
 * Scan through an array and set the MSB for basic data rates.
 *
 *  @param rates     buffer of data rates
 *  @param len       size of buffer
 */
static void libertas_set_basic_rate_flags(u8 * rates, size_t len)
{
	int i;

	for (i = 0; i < len; i++) {
		if (rates[i] == 0x02 || rates[i] == 0x04 ||
		    rates[i] == 0x0b || rates[i] == 0x16)
			rates[i] |= 0x80;
	}
}

/**
 *  @brief Unsets the MSB on basic rates
 *
 * Scan through an array and unset the MSB for basic data rates.
 *
 *  @param rates     buffer of data rates
 *  @param len       size of buffer
 */
void libertas_unset_basic_rate_flags(u8 * rates, size_t len)
{
	int i;

	for (i = 0; i < len; i++)
		rates[i] &= 0x7f;
}


115 116 117 118 119
int libertas_send_deauth(wlan_private * priv)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;

120
	if (adapter->mode == IW_MODE_INFRA &&
121
	    adapter->connect_status == LIBERTAS_CONNECTED)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		ret = libertas_send_deauthentication(priv);
	else
		ret = -ENOTSUPP;

	return ret;
}

/**
 *  @brief Associate to a specific BSS discovered in a scan
 *
 *  @param priv      A pointer to wlan_private structure
 *  @param pbssdesc  Pointer to the BSS descriptor to associate with.
 *
 *  @return          0-success, otherwise fail
 */
137
int wlan_associate(wlan_private * priv, struct assoc_request * assoc_req)
138 139 140 141
{
	wlan_adapter *adapter = priv->adapter;
	int ret;

142
	lbs_deb_enter(LBS_DEB_JOIN);
143

144 145
	ret = libertas_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
				    0, CMD_OPTION_WAITFORRSP,
146
				    0, assoc_req->bss.bssid);
147

148 149
	if (ret)
		goto done;
150 151

	/* set preamble to firmware */
152 153
	if (   (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
	    && (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
154
		adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
155
	else
156
		adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
157 158 159

	libertas_set_radio_control(priv);

160 161
	ret = libertas_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
				    0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
162

163 164
done:
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
165 166 167 168 169 170 171 172 173 174
	return ret;
}

/**
 *  @brief Start an Adhoc Network
 *
 *  @param priv         A pointer to wlan_private structure
 *  @param adhocssid    The ssid of the Adhoc Network
 *  @return             0--success, -1--fail
 */
175
int libertas_start_adhoc_network(wlan_private * priv, struct assoc_request * assoc_req)
176 177 178 179 180 181
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;

	adapter->adhoccreate = 1;

182
	if (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
183
		lbs_deb_join("AdhocStart: Short preamble\n");
184
		adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
185 186
	} else {
		lbs_deb_join("AdhocStart: Long preamble\n");
187
		adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
188 189 190 191
	}

	libertas_set_radio_control(priv);

192 193
	lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
	lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
194

195 196
	ret = libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
				    0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
197 198 199 200 201 202 203 204 205 206 207 208 209

	return ret;
}

/**
 *  @brief Join an adhoc network found in a previous scan
 *
 *  @param priv         A pointer to wlan_private structure
 *  @param pbssdesc     Pointer to a BSS descriptor found in a previous scan
 *                      to attempt to join
 *
 *  @return             0--success, -1--fail
 */
210
int libertas_join_adhoc_network(wlan_private * priv, struct assoc_request * assoc_req)
211 212
{
	wlan_adapter *adapter = priv->adapter;
213
	struct bss_descriptor * bss = &assoc_req->bss;
214 215
	int ret = 0;

216 217 218 219 220 221 222 223
	lbs_deb_join("%s: Current SSID '%s', ssid length %u\n",
	             __func__,
	             escape_essid(adapter->curbssparams.ssid,
	                          adapter->curbssparams.ssid_len),
	             adapter->curbssparams.ssid_len);
	lbs_deb_join("%s: requested ssid '%s', ssid length %u\n",
	             __func__, escape_essid(bss->ssid, bss->ssid_len),
	             bss->ssid_len);
224 225

	/* check if the requested SSID is already joined */
226
	if (adapter->curbssparams.ssid_len
227
	    && !libertas_ssid_cmp(adapter->curbssparams.ssid,
228 229
	                          adapter->curbssparams.ssid_len,
	                          bss->ssid, bss->ssid_len)
230
	    && (adapter->mode == IW_MODE_ADHOC)) {
231
		lbs_deb_join(
232 233 234 235 236
		       "ADHOC_J_CMD: New ad-hoc SSID is the same as current, "
		       "not attempting to re-join");
		return -1;
	}

237
	/* Use shortpreamble only when both creator and card supports
238
	   short preamble */
239 240
	if (   !(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
	    || !(adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
241
		lbs_deb_join("AdhocJoin: Long preamble\n");
242
		adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
243
	} else {
244
		lbs_deb_join("AdhocJoin: Short preamble\n");
245
		adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
246 247 248 249
	}

	libertas_set_radio_control(priv);

250 251
	lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
	lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
252 253 254

	adapter->adhoccreate = 0;

255 256
	ret = libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
				    0, CMD_OPTION_WAITFORRSP,
257
				    OID_802_11_SSID, assoc_req);
258 259 260 261 262 263

	return ret;
}

int libertas_stop_adhoc_network(wlan_private * priv)
{
264 265
	return libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
				     0, CMD_OPTION_WAITFORRSP, 0, NULL);
266 267 268 269 270 271 272 273 274 275
}

/**
 *  @brief Send Deauthentication Request
 *
 *  @param priv      A pointer to wlan_private structure
 *  @return          0--success, -1--fail
 */
int libertas_send_deauthentication(wlan_private * priv)
{
276 277
	return libertas_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
				     0, CMD_OPTION_WAITFORRSP, 0, NULL);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

/**
 *  @brief This function prepares command of authenticate.
 *
 *  @param priv      A pointer to wlan_private structure
 *  @param cmd       A pointer to cmd_ds_command structure
 *  @param pdata_buf Void cast of pointer to a BSSID to authenticate with
 *
 *  @return         0 or -1
 */
int libertas_cmd_80211_authenticate(wlan_private * priv,
				 struct cmd_ds_command *cmd,
				 void *pdata_buf)
{
	wlan_adapter *adapter = priv->adapter;
294 295
	struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
	int ret = -1;
296 297
	u8 *bssid = pdata_buf;

298 299
	lbs_deb_enter(LBS_DEB_JOIN);

300
	cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
	                        + S_DS_GEN);

	/* translate auth mode to 802.11 defined wire value */
	switch (adapter->secinfo.auth_mode) {
	case IW_AUTH_ALG_OPEN_SYSTEM:
		pauthenticate->authtype = 0x00;
		break;
	case IW_AUTH_ALG_SHARED_KEY:
		pauthenticate->authtype = 0x01;
		break;
	case IW_AUTH_ALG_LEAP:
		pauthenticate->authtype = 0x80;
		break;
	default:
316
		lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
317 318 319
		             adapter->secinfo.auth_mode);
		goto out;
	}
320 321 322

	memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);

323 324
	lbs_deb_join("AUTH_CMD: BSSID is : " MAC_FMT " auth=0x%X\n",
	             MAC_ARG(bssid), pauthenticate->authtype);
325
	ret = 0;
326

327
out:
328
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
329
	return ret;
330 331 332 333 334 335 336 337
}

int libertas_cmd_80211_deauthenticate(wlan_private * priv,
				   struct cmd_ds_command *cmd)
{
	wlan_adapter *adapter = priv->adapter;
	struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;

338
	lbs_deb_enter(LBS_DEB_JOIN);
339

340
	cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
341
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
342 343 344
			     S_DS_GEN);

	/* set AP MAC address */
345
	memmove(dauth->macaddr, adapter->curbssparams.bssid, ETH_ALEN);
346 347 348 349 350

	/* Reason code 3 = Station is leaving */
#define REASON_CODE_STA_LEAVING 3
	dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);

351
	lbs_deb_leave(LBS_DEB_JOIN);
352 353 354 355 356 357 358 359 360
	return 0;
}

int libertas_cmd_80211_associate(wlan_private * priv,
			      struct cmd_ds_command *cmd, void *pdata_buf)
{
	wlan_adapter *adapter = priv->adapter;
	struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
	int ret = 0;
361 362
	struct assoc_request * assoc_req = pdata_buf;
	struct bss_descriptor * bss = &assoc_req->bss;
363
	u8 *pos;
364
	u16 tmpcap, tmplen;
365 366 367 368 369 370
	struct mrvlietypes_ssidparamset *ssid;
	struct mrvlietypes_phyparamset *phy;
	struct mrvlietypes_ssparamset *ss;
	struct mrvlietypes_ratesparamset *rates;
	struct mrvlietypes_rsnparamset *rsn;

371
	lbs_deb_enter(LBS_DEB_JOIN);
372 373 374 375 376 377 378 379

	pos = (u8 *) passo;

	if (!adapter) {
		ret = -1;
		goto done;
	}

380
	cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
381

382
	memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
383 384 385
	pos += sizeof(passo->peerstaaddr);

	/* set the listen interval */
386
	passo->listeninterval = cpu_to_le16(adapter->listeninterval);
387

388
	pos += sizeof(passo->capability);
389 390 391 392 393 394
	pos += sizeof(passo->listeninterval);
	pos += sizeof(passo->bcnperiod);
	pos += sizeof(passo->dtimperiod);

	ssid = (struct mrvlietypes_ssidparamset *) pos;
	ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
395
	tmplen = bss->ssid_len;
396
	ssid->header.len = cpu_to_le16(tmplen);
397
	memcpy(ssid->ssid, bss->ssid, tmplen);
398
	pos += sizeof(ssid->header) + tmplen;
399 400 401

	phy = (struct mrvlietypes_phyparamset *) pos;
	phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
402 403
	tmplen = sizeof(phy->fh_ds.dsparamset);
	phy->header.len = cpu_to_le16(tmplen);
404
	memcpy(&phy->fh_ds.dsparamset,
405
	       &bss->phyparamset.dsparamset.currentchan,
406 407
	       tmplen);
	pos += sizeof(phy->header) + tmplen;
408 409 410

	ss = (struct mrvlietypes_ssparamset *) pos;
	ss->header.type = cpu_to_le16(TLV_TYPE_CF);
411 412 413
	tmplen = sizeof(ss->cf_ibss.cfparamset);
	ss->header.len = cpu_to_le16(tmplen);
	pos += sizeof(ss->header) + tmplen;
414 415 416

	rates = (struct mrvlietypes_ratesparamset *) pos;
	rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
417 418 419
	memcpy(&rates->rates, &bss->rates, MAX_RATES);
	tmplen = MAX_RATES;
	if (get_common_rates(adapter, rates->rates, &tmplen)) {
420 421 422
		ret = -1;
		goto done;
	}
423 424
	pos += sizeof(rates->header) + tmplen;
	rates->header.len = cpu_to_le16(tmplen);
425 426 427 428 429 430 431 432 433 434
	lbs_deb_join("ASSOC_CMD: num rates = %u\n", tmplen);

	/* Copy the infra. association rates into Current BSS state structure */
	memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
	memcpy(&adapter->curbssparams.rates, &rates->rates, tmplen);

	/* Set MSB on basic rates as the firmware requires, but _after_
	 * copying to current bss rates.
	 */
	libertas_set_basic_rate_flags(rates->rates, tmplen);
435

436
	if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
437
		rsn = (struct mrvlietypes_rsnparamset *) pos;
438 439 440 441 442
		/* WPA_IE or WPA2_IE */
		rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
		tmplen = (u16) assoc_req->wpa_ie[1];
		rsn->header.len = cpu_to_le16(tmplen);
		memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
443
		lbs_dbg_hex("ASSOC_CMD: RSN IE", (u8 *) rsn,
444 445
			sizeof(rsn->header) + tmplen);
		pos += sizeof(rsn->header) + tmplen;
446 447 448
	}

	/* update curbssparams */
449
	adapter->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
450

451
	if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
452 453 454 455 456 457
		ret = -1;
		goto done;
	}

	cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);

458 459 460 461 462 463
	/* set the capability info */
	tmpcap = (bss->capability & CAPINFO_MASK);
	if (bss->mode == IW_MODE_INFRA)
		tmpcap |= WLAN_CAPABILITY_ESS;
	passo->capability = cpu_to_le16(tmpcap);
	lbs_deb_join("ASSOC_CMD: capability=%4X CAPINFO_MASK=%4X\n",
464
		     tmpcap, CAPINFO_MASK);
465

466 467
done:
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
468 469 470 471
	return ret;
}

int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
472
				 struct cmd_ds_command *cmd, void *pdata_buf)
473 474 475 476 477
{
	wlan_adapter *adapter = priv->adapter;
	struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
	int ret = 0;
	int cmdappendsize = 0;
478
	struct assoc_request * assoc_req = pdata_buf;
479
	u16 tmpcap = 0;
480
	size_t ratesize = 0;
481

482
	lbs_deb_enter(LBS_DEB_JOIN);
483 484 485 486 487 488

	if (!adapter) {
		ret = -1;
		goto done;
	}

489
	cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
490 491 492 493 494 495 496 497 498 499 500 501 502

	/*
	 * Fill in the parameters for 2 data structures:
	 *   1. cmd_ds_802_11_ad_hoc_start command
	 *   2. adapter->scantable[i]
	 *
	 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
	 *   probe delay, and cap info.
	 *
	 * Firmware will fill up beacon period, DTIM, Basic rates
	 *   and operational rates.
	 */

503 504
	memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
	memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
505

506 507 508
	lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
	             escape_essid(assoc_req->ssid, assoc_req->ssid_len),
	             assoc_req->ssid_len);
509 510

	/* set the BSS type */
511
	adhs->bsstype = CMD_BSS_TYPE_IBSS;
512
	adapter->mode = IW_MODE_ADHOC;
513
	adhs->beaconperiod = cpu_to_le16(adapter->beaconperiod);
514 515 516 517 518 519 520 521

	/* set Physical param set */
#define DS_PARA_IE_ID   3
#define DS_PARA_IE_LEN  1

	adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
	adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;

522
	WARN_ON(!assoc_req->channel);
523

524
	lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
525
		     assoc_req->channel);
526

527
	adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
528 529 530 531 532 533 534

	/* set IBSS param set */
#define IBSS_PARA_IE_ID   6
#define IBSS_PARA_IE_LEN  2

	adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
	adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
535
	adhs->ssparamset.ibssparamset.atimwindow = cpu_to_le16(adapter->atimwindow);
536 537

	/* set capability info */
538
	tmpcap = WLAN_CAPABILITY_IBSS;
539
	if (assoc_req->secinfo.wep_enabled) {
540
		lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
541
		tmpcap |= WLAN_CAPABILITY_PRIVACY;
542
	} else {
543
		lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
544
	}
545 546 547
	adhs->capability = cpu_to_le16(tmpcap);

	/* probedelay */
548
	adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
549

550
	memset(adhs->rates, 0, sizeof(adhs->rates));
551
	if (adapter->adhoc_grate_enabled) {
552 553
		ratesize = min(sizeof(adhs->rates), sizeof(libertas_bg_rates));
		memcpy(adhs->rates, libertas_bg_rates, ratesize);
554
	} else {
555 556
		ratesize = min(sizeof(adhs->rates), sizeof(adhoc_rates_b));
		memcpy(adhs->rates, adhoc_rates_b, ratesize);
557 558 559
	}

	/* Copy the ad-hoc creating rates into Current BSS state structure */
560 561 562 563 564 565 566
	memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
	memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);

	/* Set MSB on basic rates as the firmware requires, but _after_
	 * copying to current bss rates.
	 */
	libertas_set_basic_rate_flags(adhs->rates, ratesize);
567

568
	lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
569
	       adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
570

571
	lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
572 573

	if (libertas_create_dnld_countryinfo_11d(priv)) {
574
		lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
575 576 577 578
		ret = -1;
		goto done;
	}

579 580
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
				S_DS_GEN + cmdappendsize);
581 582 583

	ret = 0;
done:
584
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
585 586 587 588 589 590
	return ret;
}

int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
				struct cmd_ds_command *cmd)
{
591
	cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
592 593 594 595 596 597 598 599 600
	cmd->size = cpu_to_le16(S_DS_GEN);

	return 0;
}

int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
				struct cmd_ds_command *cmd, void *pdata_buf)
{
	wlan_adapter *adapter = priv->adapter;
601
	struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
602 603
	struct assoc_request * assoc_req = pdata_buf;
	struct bss_descriptor *bss = &assoc_req->bss;
604 605
	int cmdappendsize = 0;
	int ret = 0;
606
	u16 ratesize = 0;
607

608
	lbs_deb_enter(LBS_DEB_JOIN);
609

610
	cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
611

612
	join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
613
	join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
614

615 616
	memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
	memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
617

618 619
	memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
	       sizeof(union ieeetypes_phyparamset));
620

621 622
	memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
	       sizeof(union IEEEtypes_ssparamset));
623

624
	join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
625
	lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
626
	       bss->capability, CAPINFO_MASK);
627 628

	/* information on BSSID descriptor passed to FW */
629 630
	lbs_deb_join(
	       "ADHOC_J_CMD: BSSID = " MAC_FMT ", SSID = '%s'\n",
631
	       MAC_ARG(join_cmd->bss.bssid), join_cmd->bss.ssid);
632 633

	/* failtimeout */
634
	join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
635 636

	/* probedelay */
637
	join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
638

639
	adapter->curbssparams.channel = bss->channel;
640

641 642 643 644 645
	/* Copy Data rates from the rates recorded in scan response */
	memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
	ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
	memcpy(join_cmd->bss.rates, bss->rates, ratesize);
	if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
646
		lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
647 648 649 650
		ret = -1;
		goto done;
	}

651 652 653
	/* Copy the ad-hoc creating rates into Current BSS state structure */
	memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
	memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
654

655 656
	/* Set MSB on basic rates as the firmware requires, but _after_
	 * copying to current bss rates.
657
	 */
658
	libertas_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
659

660
	join_cmd->bss.ssparamset.ibssparamset.atimwindow =
661
	    cpu_to_le16(bss->atimwindow);
662

663
	if (assoc_req->secinfo.wep_enabled) {
664 665 666
		u16 tmp = le16_to_cpu(join_cmd->bss.capability);
		tmp |= WLAN_CAPABILITY_PRIVACY;
		join_cmd->bss.capability = cpu_to_le16(tmp);
667 668
	}

669
	if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
670
		/* wake up first */
671
		__le32 Localpsmode;
672

673
		Localpsmode = cpu_to_le32(WLAN802_11POWERMODECAM);
674
		ret = libertas_prepare_and_send_command(priv,
675 676
					    CMD_802_11_PS_MODE,
					    CMD_ACT_SET,
677 678 679 680 681 682 683 684
					    0, 0, &Localpsmode);

		if (ret) {
			ret = -1;
			goto done;
		}
	}

685
	if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
686 687 688 689
		ret = -1;
		goto done;
	}

690 691
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
				S_DS_GEN + cmdappendsize);
692

693 694
done:
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
695 696 697 698 699 700 701 702 703 704
	return ret;
}

int libertas_ret_80211_associate(wlan_private * priv,
			      struct cmd_ds_command *resp)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;
	union iwreq_data wrqu;
	struct ieeetypes_assocrsp *passocrsp;
705
	struct bss_descriptor * bss;
706

707
	lbs_deb_enter(LBS_DEB_JOIN);
708

709 710 711 712 713 714 715
	if (!adapter->in_progress_assoc_req) {
		lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
		ret = -1;
		goto done;
	}
	bss = &adapter->in_progress_assoc_req->bss;

716 717
	passocrsp = (struct ieeetypes_assocrsp *) & resp->params;

718
	if (le16_to_cpu(passocrsp->statuscode)) {
719 720
		libertas_mac_event_disconnected(priv);

721
		lbs_deb_join("ASSOC_RESP: Association failed, status code = %d\n",
722
			     le16_to_cpu(passocrsp->statuscode));
723 724 725 726 727 728 729 730 731

		ret = -1;
		goto done;
	}

	lbs_dbg_hex("ASSOC_RESP:", (void *)&resp->params,
		le16_to_cpu(resp->size) - S_DS_GEN);

	/* Send a Media Connected event, according to the Spec */
732
	adapter->connect_status = LIBERTAS_CONNECTED;
733

734 735
	lbs_deb_join("ASSOC_RESP: assocated to '%s'\n",
	             escape_essid(bss->ssid, bss->ssid_len));
736

737
	/* Update current SSID and BSSID */
738 739
	memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
	adapter->curbssparams.ssid_len = bss->ssid_len;
740
	memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
741

742
	lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
743 744 745 746 747 748 749 750 751 752
	       adapter->currentpacketfilter);

	adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
	adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;

	memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
	memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
	adapter->nextSNRNF = 0;
	adapter->numSNRNF = 0;

753 754
	netif_carrier_on(priv->dev);
	netif_wake_queue(priv->dev);
755

756 757 758
	netif_carrier_on(priv->mesh_dev);
	netif_wake_queue(priv->mesh_dev);

759
	lbs_deb_join("ASSOC_RESP: Associated \n");
760 761 762

	memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
763
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
764

765 766
done:
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
767 768 769 770 771 772
	return ret;
}

int libertas_ret_80211_disassociate(wlan_private * priv,
				 struct cmd_ds_command *resp)
{
773
	lbs_deb_enter(LBS_DEB_JOIN);
774 775 776

	libertas_mac_event_disconnected(priv);

777
	lbs_deb_leave(LBS_DEB_JOIN);
778 779 780 781 782 783 784 785 786 787 788 789
	return 0;
}

int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
				 struct cmd_ds_command *resp)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;
	u16 command = le16_to_cpu(resp->command);
	u16 result = le16_to_cpu(resp->result);
	struct cmd_ds_802_11_ad_hoc_result *padhocresult;
	union iwreq_data wrqu;
790
	struct bss_descriptor *bss;
791

792
	lbs_deb_enter(LBS_DEB_JOIN);
793 794 795

	padhocresult = &resp->params.result;

796 797 798
	lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
	lbs_deb_join("ADHOC_RESP: command = %x\n", command);
	lbs_deb_join("ADHOC_RESP: result = %x\n", result);
799

800 801 802 803 804 805
	if (!adapter->in_progress_assoc_req) {
		lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
		ret = -1;
		goto done;
	}
	bss = &adapter->in_progress_assoc_req->bss;
806 807 808 809 810

	/*
	 * Join result code 0 --> SUCCESS
	 */
	if (result) {
811
		lbs_deb_join("ADHOC_RESP: failed\n");
812
		if (adapter->connect_status == LIBERTAS_CONNECTED) {
813 814
			libertas_mac_event_disconnected(priv);
		}
815 816
		ret = -1;
		goto done;
817 818 819 820 821 822
	}

	/*
	 * Now the join cmd should be successful
	 * If BSSID has changed use SSID to compare instead of BSSID
	 */
823 824
	lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
	             escape_essid(bss->ssid, bss->ssid_len));
825 826

	/* Send a Media Connected event, according to the Spec */
827
	adapter->connect_status = LIBERTAS_CONNECTED;
828

829
	if (command == CMD_RET_802_11_AD_HOC_START) {
830
		/* Update the created network descriptor with the new BSSID */
831
		memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
832 833 834
	}

	/* Set the BSSID from the joined/started descriptor */
835
	memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
836 837

	/* Set the new SSID to current SSID */
838 839
	memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
	adapter->curbssparams.ssid_len = bss->ssid_len;
840

841 842
	netif_carrier_on(priv->dev);
	netif_wake_queue(priv->dev);
843

844 845 846
	netif_carrier_on(priv->mesh_dev);
	netif_wake_queue(priv->mesh_dev);

847 848 849
	memset(&wrqu, 0, sizeof(wrqu));
	memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
850
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
851

852
	lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
853
	lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
854
	lbs_deb_join("ADHOC_RESP: BSSID = " MAC_FMT "\n",
855
	       MAC_ARG(padhocresult->bssid));
856

857 858
done:
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
859 860 861 862 863 864
	return ret;
}

int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
				struct cmd_ds_command *resp)
{
865
	lbs_deb_enter(LBS_DEB_JOIN);
866 867 868

	libertas_mac_event_disconnected(priv);

869
	lbs_deb_leave(LBS_DEB_JOIN);
870 871
	return 0;
}