Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * NXP Wireless LAN device driver: association and ad-hoc start/join
   4 *
   5 * Copyright 2011-2020 NXP
   6 */
   7
   8#include "decl.h"
   9#include "ioctl.h"
  10#include "util.h"
  11#include "fw.h"
  12#include "main.h"
  13#include "wmm.h"
  14#include "11n.h"
  15#include "11ac.h"
  16
  17#define CAPINFO_MASK    (~(BIT(15) | BIT(14) | BIT(12) | BIT(11) | BIT(9)))
  18
  19/*
  20 * Append a generic IE as a pass through TLV to a TLV buffer.
  21 *
  22 * This function is called from the network join command preparation routine.
  23 *
  24 * If the IE buffer has been setup by the application, this routine appends
  25 * the buffer as a pass through TLV type to the request.
  26 */
  27static int
  28mwifiex_cmd_append_generic_ie(struct mwifiex_private *priv, u8 **buffer)
  29{
  30	int ret_len = 0;
  31	struct mwifiex_ie_types_header ie_header;
  32
  33	/* Null Checks */
  34	if (!buffer)
  35		return 0;
  36	if (!(*buffer))
  37		return 0;
  38
  39	/*
  40	 * If there is a generic ie buffer setup, append it to the return
  41	 *   parameter buffer pointer.
  42	 */
  43	if (priv->gen_ie_buf_len) {
  44		mwifiex_dbg(priv->adapter, INFO,
  45			    "info: %s: append generic ie len %d to %p\n",
  46			    __func__, priv->gen_ie_buf_len, *buffer);
  47
  48		/* Wrap the generic IE buffer with a pass through TLV type */
  49		ie_header.type = cpu_to_le16(TLV_TYPE_PASSTHROUGH);
  50		ie_header.len = cpu_to_le16(priv->gen_ie_buf_len);
  51		memcpy(*buffer, &ie_header, sizeof(ie_header));
  52
  53		/* Increment the return size and the return buffer pointer
  54		   param */
  55		*buffer += sizeof(ie_header);
  56		ret_len += sizeof(ie_header);
  57
  58		/* Copy the generic IE buffer to the output buffer, advance
  59		   pointer */
  60		memcpy(*buffer, priv->gen_ie_buf, priv->gen_ie_buf_len);
  61
  62		/* Increment the return size and the return buffer pointer
  63		   param */
  64		*buffer += priv->gen_ie_buf_len;
  65		ret_len += priv->gen_ie_buf_len;
  66
  67		/* Reset the generic IE buffer */
  68		priv->gen_ie_buf_len = 0;
  69	}
  70
  71	/* return the length appended to the buffer */
  72	return ret_len;
  73}
  74
  75/*
  76 * Append TSF tracking info from the scan table for the target AP.
  77 *
  78 * This function is called from the network join command preparation routine.
  79 *
  80 * The TSF table TSF sent to the firmware contains two TSF values:
  81 *      - The TSF of the target AP from its previous beacon/probe response
  82 *      - The TSF timestamp of our local MAC at the time we observed the
  83 *        beacon/probe response.
  84 *
  85 * The firmware uses the timestamp values to set an initial TSF value
  86 * in the MAC for the new association after a reassociation attempt.
  87 */
  88static int
  89mwifiex_cmd_append_tsf_tlv(struct mwifiex_private *priv, u8 **buffer,
  90			   struct mwifiex_bssdescriptor *bss_desc)
  91{
  92	struct mwifiex_ie_types_tsf_timestamp tsf_tlv;
  93	__le64 tsf_val;
  94
  95	/* Null Checks */
  96	if (buffer == NULL)
  97		return 0;
  98	if (*buffer == NULL)
  99		return 0;
 100
 101	memset(&tsf_tlv, 0x00, sizeof(struct mwifiex_ie_types_tsf_timestamp));
 102
 103	tsf_tlv.header.type = cpu_to_le16(TLV_TYPE_TSFTIMESTAMP);
 104	tsf_tlv.header.len = cpu_to_le16(2 * sizeof(tsf_val));
 105
 106	memcpy(*buffer, &tsf_tlv, sizeof(tsf_tlv.header));
 107	*buffer += sizeof(tsf_tlv.header);
 108
 109	/* TSF at the time when beacon/probe_response was received */
 110	tsf_val = cpu_to_le64(bss_desc->fw_tsf);
 111	memcpy(*buffer, &tsf_val, sizeof(tsf_val));
 112	*buffer += sizeof(tsf_val);
 113
 114	tsf_val = cpu_to_le64(bss_desc->timestamp);
 115
 116	mwifiex_dbg(priv->adapter, INFO,
 117		    "info: %s: TSF offset calc: %016llx - %016llx\n",
 118		    __func__, bss_desc->timestamp, bss_desc->fw_tsf);
 119
 120	memcpy(*buffer, &tsf_val, sizeof(tsf_val));
 121	*buffer += sizeof(tsf_val);
 122
 123	return sizeof(tsf_tlv.header) + (2 * sizeof(tsf_val));
 124}
 125
 126/*
 127 * This function finds out the common rates between rate1 and rate2.
 128 *
 129 * It will fill common rates in rate1 as output if found.
 130 *
 131 * NOTE: Setting the MSB of the basic rates needs to be taken
 132 * care of, either before or after calling this function.
 133 */
 134static int mwifiex_get_common_rates(struct mwifiex_private *priv, u8 *rate1,
 135				    u32 rate1_size, u8 *rate2, u32 rate2_size)
 136{
 137	int ret;
 138	u8 *ptr = rate1, *tmp;
 139	u32 i, j;
 140
 141	tmp = kmemdup(rate1, rate1_size, GFP_KERNEL);
 142	if (!tmp) {
 143		mwifiex_dbg(priv->adapter, ERROR, "failed to alloc tmp buf\n");
 144		return -ENOMEM;
 145	}
 146
 147	memset(rate1, 0, rate1_size);
 148
 149	for (i = 0; i < rate2_size && rate2[i]; i++) {
 150		for (j = 0; j < rate1_size && tmp[j]; j++) {
 151			/* Check common rate, excluding the bit for
 152			   basic rate */
 153			if ((rate2[i] & 0x7F) == (tmp[j] & 0x7F)) {
 154				*rate1++ = tmp[j];
 155				break;
 156			}
 157		}
 158	}
 159
 160	mwifiex_dbg(priv->adapter, INFO, "info: Tx data rate set to %#x\n",
 161		    priv->data_rate);
 162
 163	if (!priv->is_data_rate_auto) {
 164		while (*ptr) {
 165			if ((*ptr & 0x7f) == priv->data_rate) {
 166				ret = 0;
 167				goto done;
 168			}
 169			ptr++;
 170		}
 171		mwifiex_dbg(priv->adapter, ERROR,
 172			    "previously set fixed data rate %#x\t"
 173			    "is not compatible with the network\n",
 174			    priv->data_rate);
 175
 176		ret = -1;
 177		goto done;
 178	}
 179
 180	ret = 0;
 181done:
 182	kfree(tmp);
 183	return ret;
 184}
 185
 186/*
 187 * This function creates the intersection of the rates supported by a
 188 * target BSS and our adapter settings for use in an assoc/join command.
 189 */
 190static int
 191mwifiex_setup_rates_from_bssdesc(struct mwifiex_private *priv,
 192				 struct mwifiex_bssdescriptor *bss_desc,
 193				 u8 *out_rates, u32 *out_rates_size)
 194{
 195	u8 card_rates[MWIFIEX_SUPPORTED_RATES];
 196	u32 card_rates_size;
 197
 198	/* Copy AP supported rates */
 199	memcpy(out_rates, bss_desc->supported_rates, MWIFIEX_SUPPORTED_RATES);
 200	/* Get the STA supported rates */
 201	card_rates_size = mwifiex_get_active_data_rates(priv, card_rates);
 202	/* Get the common rates between AP and STA supported rates */
 203	if (mwifiex_get_common_rates(priv, out_rates, MWIFIEX_SUPPORTED_RATES,
 204				     card_rates, card_rates_size)) {
 205		*out_rates_size = 0;
 206		mwifiex_dbg(priv->adapter, ERROR,
 207			    "%s: cannot get common rates\n",
 208			    __func__);
 209		return -1;
 210	}
 211
 212	*out_rates_size =
 213		min_t(size_t, strlen(out_rates), MWIFIEX_SUPPORTED_RATES);
 214
 215	return 0;
 216}
 217
 218/*
 219 * This function appends a WPS IE. It is called from the network join command
 220 * preparation routine.
 221 *
 222 * If the IE buffer has been setup by the application, this routine appends
 223 * the buffer as a WPS TLV type to the request.
 224 */
 225static int
 226mwifiex_cmd_append_wps_ie(struct mwifiex_private *priv, u8 **buffer)
 227{
 228	int retLen = 0;
 229	struct mwifiex_ie_types_header ie_header;
 230
 231	if (!buffer || !*buffer)
 232		return 0;
 233
 234	/*
 235	 * If there is a wps ie buffer setup, append it to the return
 236	 * parameter buffer pointer.
 237	 */
 238	if (priv->wps_ie_len) {
 239		mwifiex_dbg(priv->adapter, CMD,
 240			    "cmd: append wps ie %d to %p\n",
 241			    priv->wps_ie_len, *buffer);
 242
 243		/* Wrap the generic IE buffer with a pass through TLV type */
 244		ie_header.type = cpu_to_le16(TLV_TYPE_PASSTHROUGH);
 245		ie_header.len = cpu_to_le16(priv->wps_ie_len);
 246		memcpy(*buffer, &ie_header, sizeof(ie_header));
 247		*buffer += sizeof(ie_header);
 248		retLen += sizeof(ie_header);
 249
 250		memcpy(*buffer, priv->wps_ie, priv->wps_ie_len);
 251		*buffer += priv->wps_ie_len;
 252		retLen += priv->wps_ie_len;
 253
 254	}
 255
 256	kfree(priv->wps_ie);
 257	priv->wps_ie_len = 0;
 258	return retLen;
 259}
 260
 261/*
 262 * This function appends a WAPI IE.
 263 *
 264 * This function is called from the network join command preparation routine.
 265 *
 266 * If the IE buffer has been setup by the application, this routine appends
 267 * the buffer as a WAPI TLV type to the request.
 268 */
 269static int
 270mwifiex_cmd_append_wapi_ie(struct mwifiex_private *priv, u8 **buffer)
 271{
 272	int retLen = 0;
 273	struct mwifiex_ie_types_header ie_header;
 274
 275	/* Null Checks */
 276	if (buffer == NULL)
 277		return 0;
 278	if (*buffer == NULL)
 279		return 0;
 280
 281	/*
 282	 * If there is a wapi ie buffer setup, append it to the return
 283	 *   parameter buffer pointer.
 284	 */
 285	if (priv->wapi_ie_len) {
 286		mwifiex_dbg(priv->adapter, CMD,
 287			    "cmd: append wapi ie %d to %p\n",
 288			    priv->wapi_ie_len, *buffer);
 289
 290		/* Wrap the generic IE buffer with a pass through TLV type */
 291		ie_header.type = cpu_to_le16(TLV_TYPE_WAPI_IE);
 292		ie_header.len = cpu_to_le16(priv->wapi_ie_len);
 293		memcpy(*buffer, &ie_header, sizeof(ie_header));
 294
 295		/* Increment the return size and the return buffer pointer
 296		   param */
 297		*buffer += sizeof(ie_header);
 298		retLen += sizeof(ie_header);
 299
 300		/* Copy the wapi IE buffer to the output buffer, advance
 301		   pointer */
 302		memcpy(*buffer, priv->wapi_ie, priv->wapi_ie_len);
 303
 304		/* Increment the return size and the return buffer pointer
 305		   param */
 306		*buffer += priv->wapi_ie_len;
 307		retLen += priv->wapi_ie_len;
 308
 309	}
 310	/* return the length appended to the buffer */
 311	return retLen;
 312}
 313
 314/*
 315 * This function appends rsn ie tlv for wpa/wpa2 security modes.
 316 * It is called from the network join command preparation routine.
 317 */
 318static int mwifiex_append_rsn_ie_wpa_wpa2(struct mwifiex_private *priv,
 319					  u8 **buffer)
 320{
 321	struct mwifiex_ie_types_rsn_param_set *rsn_ie_tlv;
 322	int rsn_ie_len;
 323
 324	if (!buffer || !(*buffer))
 325		return 0;
 326
 327	rsn_ie_tlv = (struct mwifiex_ie_types_rsn_param_set *) (*buffer);
 328	rsn_ie_tlv->header.type = cpu_to_le16((u16) priv->wpa_ie[0]);
 329	rsn_ie_tlv->header.type = cpu_to_le16(
 330				 le16_to_cpu(rsn_ie_tlv->header.type) & 0x00FF);
 331	rsn_ie_tlv->header.len = cpu_to_le16((u16) priv->wpa_ie[1]);
 332	rsn_ie_tlv->header.len = cpu_to_le16(le16_to_cpu(rsn_ie_tlv->header.len)
 333							 & 0x00FF);
 334	if (le16_to_cpu(rsn_ie_tlv->header.len) <= (sizeof(priv->wpa_ie) - 2))
 335		memcpy(rsn_ie_tlv->rsn_ie, &priv->wpa_ie[2],
 336		       le16_to_cpu(rsn_ie_tlv->header.len));
 337	else
 338		return -1;
 339
 340	rsn_ie_len = sizeof(rsn_ie_tlv->header) +
 341					le16_to_cpu(rsn_ie_tlv->header.len);
 342	*buffer += rsn_ie_len;
 343
 344	return rsn_ie_len;
 345}
 346
 347/*
 348 * This function prepares command for association.
 349 *
 350 * This sets the following parameters -
 351 *      - Peer MAC address
 352 *      - Listen interval
 353 *      - Beacon interval
 354 *      - Capability information
 355 *
 356 * ...and the following TLVs, as required -
 357 *      - SSID TLV
 358 *      - PHY TLV
 359 *      - SS TLV
 360 *      - Rates TLV
 361 *      - Authentication TLV
 362 *      - Channel TLV
 363 *      - WPA/WPA2 IE
 364 *      - 11n TLV
 365 *      - Vendor specific TLV
 366 *      - WMM TLV
 367 *      - WAPI IE
 368 *      - Generic IE
 369 *      - TSF TLV
 370 *
 371 * Preparation also includes -
 372 *      - Setting command ID and proper size
 373 *      - Ensuring correct endian-ness
 374 */
 375int mwifiex_cmd_802_11_associate(struct mwifiex_private *priv,
 376				 struct host_cmd_ds_command *cmd,
 377				 struct mwifiex_bssdescriptor *bss_desc)
 378{
 379	struct host_cmd_ds_802_11_associate *assoc = &cmd->params.associate;
 380	struct mwifiex_ie_types_ssid_param_set *ssid_tlv;
 381	struct mwifiex_ie_types_phy_param_set *phy_tlv;
 382	struct mwifiex_ie_types_ss_param_set *ss_tlv;
 383	struct mwifiex_ie_types_rates_param_set *rates_tlv;
 384	struct mwifiex_ie_types_auth_type *auth_tlv;
 385	struct mwifiex_ie_types_sae_pwe_mode *sae_pwe_tlv;
 386	struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
 387	struct mwifiex_ie_types_host_mlme *host_mlme_tlv;
 388	u8 rates[MWIFIEX_SUPPORTED_RATES];
 389	u32 rates_size;
 390	u16 tmp_cap;
 391	u8 *pos;
 392	int rsn_ie_len = 0;
 393
 394	pos = (u8 *) assoc;
 395
 396	cmd->command = cpu_to_le16(HostCmd_CMD_802_11_ASSOCIATE);
 397
 398	/* Save so we know which BSS Desc to use in the response handler */
 399	priv->attempted_bss_desc = bss_desc;
 400
 401	memcpy(assoc->peer_sta_addr,
 402	       bss_desc->mac_address, sizeof(assoc->peer_sta_addr));
 403	pos += sizeof(assoc->peer_sta_addr);
 404
 405	/* Set the listen interval */
 406	assoc->listen_interval = cpu_to_le16(priv->listen_interval);
 407	/* Set the beacon period */
 408	assoc->beacon_period = cpu_to_le16(bss_desc->beacon_period);
 409
 410	pos += sizeof(assoc->cap_info_bitmap);
 411	pos += sizeof(assoc->listen_interval);
 412	pos += sizeof(assoc->beacon_period);
 413	pos += sizeof(assoc->dtim_period);
 414
 415	ssid_tlv = (struct mwifiex_ie_types_ssid_param_set *) pos;
 416	ssid_tlv->header.type = cpu_to_le16(WLAN_EID_SSID);
 417	ssid_tlv->header.len = cpu_to_le16((u16) bss_desc->ssid.ssid_len);
 418	memcpy(ssid_tlv->ssid, bss_desc->ssid.ssid,
 419	       le16_to_cpu(ssid_tlv->header.len));
 420	pos += sizeof(ssid_tlv->header) + le16_to_cpu(ssid_tlv->header.len);
 421
 422	phy_tlv = (struct mwifiex_ie_types_phy_param_set *) pos;
 423	phy_tlv->header.type = cpu_to_le16(WLAN_EID_DS_PARAMS);
 424	phy_tlv->header.len = cpu_to_le16(sizeof(phy_tlv->fh_ds.ds_param_set));
 425	memcpy(&phy_tlv->fh_ds.ds_param_set,
 426	       &bss_desc->phy_param_set.ds_param_set.current_chan,
 427	       sizeof(phy_tlv->fh_ds.ds_param_set));
 428	pos += sizeof(phy_tlv->header) + le16_to_cpu(phy_tlv->header.len);
 429
 430	ss_tlv = (struct mwifiex_ie_types_ss_param_set *) pos;
 431	ss_tlv->header.type = cpu_to_le16(WLAN_EID_CF_PARAMS);
 432	ss_tlv->header.len = cpu_to_le16(sizeof(ss_tlv->cf_ibss.cf_param_set));
 433	pos += sizeof(ss_tlv->header) + le16_to_cpu(ss_tlv->header.len);
 434
 435	/* Get the common rates supported between the driver and the BSS Desc */
 436	if (mwifiex_setup_rates_from_bssdesc
 437	    (priv, bss_desc, rates, &rates_size))
 438		return -1;
 439
 440	/* Save the data rates into Current BSS state structure */
 441	priv->curr_bss_params.num_of_rates = rates_size;
 442	memcpy(&priv->curr_bss_params.data_rates, rates, rates_size);
 443
 444	/* Setup the Rates TLV in the association command */
 445	rates_tlv = (struct mwifiex_ie_types_rates_param_set *) pos;
 446	rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
 447	rates_tlv->header.len = cpu_to_le16((u16) rates_size);
 448	memcpy(rates_tlv->rates, rates, rates_size);
 449	pos += sizeof(rates_tlv->header) + rates_size;
 450	mwifiex_dbg(priv->adapter, INFO, "info: ASSOC_CMD: rates size = %d\n",
 451		    rates_size);
 452
 453	/* Add the Authentication type to be used for Auth frames */
 454	auth_tlv = (struct mwifiex_ie_types_auth_type *) pos;
 455	auth_tlv->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
 456	auth_tlv->header.len = cpu_to_le16(sizeof(auth_tlv->auth_type));
 457	if (priv->sec_info.wep_enabled)
 458		auth_tlv->auth_type = cpu_to_le16(
 459				(u16) priv->sec_info.authentication_mode);
 460	else
 461		auth_tlv->auth_type = cpu_to_le16(NL80211_AUTHTYPE_OPEN_SYSTEM);
 462
 463	pos += sizeof(auth_tlv->header) + le16_to_cpu(auth_tlv->header.len);
 464
 465	if (priv->sec_info.authentication_mode == WLAN_AUTH_SAE) {
 466		auth_tlv->auth_type = cpu_to_le16(MWIFIEX_AUTHTYPE_SAE);
 467		if (bss_desc->bcn_rsnx_ie &&
 468		    bss_desc->bcn_rsnx_ie->ieee_hdr.len &&
 469		    (bss_desc->bcn_rsnx_ie->data[0] &
 470		    WLAN_RSNX_CAPA_SAE_H2E)) {
 471			sae_pwe_tlv =
 472				(struct mwifiex_ie_types_sae_pwe_mode *)pos;
 473			sae_pwe_tlv->header.type =
 474				cpu_to_le16(TLV_TYPE_SAE_PWE_MODE);
 475			sae_pwe_tlv->header.len =
 476				cpu_to_le16(sizeof(sae_pwe_tlv->pwe[0]));
 477			sae_pwe_tlv->pwe[0] = bss_desc->bcn_rsnx_ie->data[0];
 478			pos += sizeof(sae_pwe_tlv->header) +
 479				sizeof(sae_pwe_tlv->pwe[0]);
 480		}
 481	}
 482
 483	if (IS_SUPPORT_MULTI_BANDS(priv->adapter) &&
 484	    !(ISSUPP_11NENABLED(priv->adapter->fw_cap_info) &&
 485	    (!bss_desc->disable_11n) &&
 486	    (priv->adapter->config_bands & BAND_GN ||
 487	     priv->adapter->config_bands & BAND_AN) &&
 488	    (bss_desc->bcn_ht_cap)
 489	    )
 490		) {
 491		/* Append a channel TLV for the channel the attempted AP was
 492		   found on */
 493		chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
 494		chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
 495		chan_tlv->header.len =
 496			cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
 497
 498		memset(chan_tlv->chan_scan_param, 0x00,
 499		       sizeof(struct mwifiex_chan_scan_param_set));
 500		chan_tlv->chan_scan_param[0].chan_number =
 501			(bss_desc->phy_param_set.ds_param_set.current_chan);
 502		mwifiex_dbg(priv->adapter, INFO, "info: Assoc: TLV Chan = %d\n",
 503			    chan_tlv->chan_scan_param[0].chan_number);
 504
 505		chan_tlv->chan_scan_param[0].radio_type =
 506			mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
 507
 508		mwifiex_dbg(priv->adapter, INFO, "info: Assoc: TLV Band = %d\n",
 509			    chan_tlv->chan_scan_param[0].radio_type);
 510		pos += sizeof(chan_tlv->header) +
 511			sizeof(struct mwifiex_chan_scan_param_set);
 512	}
 513
 514	if (priv->adapter->host_mlme_enabled) {
 515		host_mlme_tlv = (struct mwifiex_ie_types_host_mlme *)pos;
 516		host_mlme_tlv->header.type = cpu_to_le16(TLV_TYPE_HOST_MLME);
 517		host_mlme_tlv->header.len =
 518			cpu_to_le16(sizeof(host_mlme_tlv->host_mlme));
 519		host_mlme_tlv->host_mlme = 1;
 520		pos += sizeof(host_mlme_tlv->header) +
 521			sizeof(host_mlme_tlv->host_mlme);
 522	}
 523
 524	if (!priv->wps.session_enable) {
 525		if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
 526			rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
 527
 528		if (rsn_ie_len == -1)
 529			return -1;
 530	}
 531
 532	if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info) &&
 533	    (!bss_desc->disable_11n) &&
 534	    (priv->adapter->config_bands & BAND_GN ||
 535	     priv->adapter->config_bands & BAND_AN))
 536		mwifiex_cmd_append_11n_tlv(priv, bss_desc, &pos);
 537
 538	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
 539	    !bss_desc->disable_11n && !bss_desc->disable_11ac &&
 540	    priv->adapter->config_bands & BAND_AAC)
 541		mwifiex_cmd_append_11ac_tlv(priv, bss_desc, &pos);
 542
 543	/* Append vendor specific IE TLV */
 544	mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_ASSOC, &pos);
 545
 546	mwifiex_wmm_process_association_req(priv, &pos, &bss_desc->wmm_ie,
 547					    bss_desc->bcn_ht_cap);
 548	if (priv->sec_info.wapi_enabled && priv->wapi_ie_len)
 549		mwifiex_cmd_append_wapi_ie(priv, &pos);
 550
 551	if (priv->wps.session_enable && priv->wps_ie_len)
 552		mwifiex_cmd_append_wps_ie(priv, &pos);
 553
 554	mwifiex_cmd_append_generic_ie(priv, &pos);
 555
 556	mwifiex_cmd_append_tsf_tlv(priv, &pos, bss_desc);
 557
 558	mwifiex_11h_process_join(priv, &pos, bss_desc);
 559
 560	cmd->size = cpu_to_le16((u16) (pos - (u8 *) assoc) + S_DS_GEN);
 561
 562	/* Set the Capability info at last */
 563	tmp_cap = bss_desc->cap_info_bitmap;
 564
 565	if (priv->adapter->config_bands == BAND_B)
 566		tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
 567
 568	tmp_cap &= CAPINFO_MASK;
 569	mwifiex_dbg(priv->adapter, INFO,
 570		    "info: ASSOC_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
 571		    tmp_cap, CAPINFO_MASK);
 572	assoc->cap_info_bitmap = cpu_to_le16(tmp_cap);
 573
 574	return 0;
 575}
 576
 577static const char *assoc_failure_reason_to_str(u16 cap_info)
 578{
 579	switch (cap_info) {
 580	case CONNECT_ERR_AUTH_ERR_STA_FAILURE:
 581		return "CONNECT_ERR_AUTH_ERR_STA_FAILURE";
 582	case CONNECT_ERR_AUTH_MSG_UNHANDLED:
 583		return "CONNECT_ERR_AUTH_MSG_UNHANDLED";
 584	case CONNECT_ERR_ASSOC_ERR_TIMEOUT:
 585		return "CONNECT_ERR_ASSOC_ERR_TIMEOUT";
 586	case CONNECT_ERR_ASSOC_ERR_AUTH_REFUSED:
 587		return "CONNECT_ERR_ASSOC_ERR_AUTH_REFUSED";
 588	case CONNECT_ERR_STA_FAILURE:
 589		return "CONNECT_ERR_STA_FAILURE";
 590	}
 591
 592	return "Unknown connect failure";
 593}
 594/*
 595 * Association firmware command response handler
 596 *
 597 * The response buffer for the association command has the following
 598 * memory layout.
 599 *
 600 * For cases where an association response was not received (indicated
 601 * by the CapInfo and AId field):
 602 *
 603 *     .------------------------------------------------------------.
 604 *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
 605 *     .------------------------------------------------------------.
 606 *     |  cap_info/Error Return(t_u16):                             |
 607 *     |           0xFFFF(-1): Internal error                       |
 608 *     |           0xFFFE(-2): Authentication unhandled message     |
 609 *     |           0xFFFD(-3): Authentication refused               |
 610 *     |           0xFFFC(-4): Timeout waiting for AP response      |
 611 *     .------------------------------------------------------------.
 612 *     |  status_code(t_u16):                                       |
 613 *     |        If cap_info is -1:                                  |
 614 *     |           An internal firmware failure prevented the       |
 615 *     |           command from being processed.  The status_code   |
 616 *     |           will be set to 1.                                |
 617 *     |                                                            |
 618 *     |        If cap_info is -2:                                  |
 619 *     |           An authentication frame was received but was     |
 620 *     |           not handled by the firmware.  IEEE Status        |
 621 *     |           code for the failure is returned.                |
 622 *     |                                                            |
 623 *     |        If cap_info is -3:                                  |
 624 *     |           An authentication frame was received and the     |
 625 *     |           status_code is the IEEE Status reported in the   |
 626 *     |           response.                                        |
 627 *     |                                                            |
 628 *     |        If cap_info is -4:                                  |
 629 *     |           (1) Association response timeout                 |
 630 *     |           (2) Authentication response timeout              |
 631 *     .------------------------------------------------------------.
 632 *     |  a_id(t_u16): 0xFFFF                                       |
 633 *     .------------------------------------------------------------.
 634 *
 635 *
 636 * For cases where an association response was received, the IEEE
 637 * standard association response frame is returned:
 638 *
 639 *     .------------------------------------------------------------.
 640 *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
 641 *     .------------------------------------------------------------.
 642 *     |  cap_info(t_u16): IEEE Capability                          |
 643 *     .------------------------------------------------------------.
 644 *     |  status_code(t_u16): IEEE Status Code                      |
 645 *     .------------------------------------------------------------.
 646 *     |  a_id(t_u16): IEEE Association ID                          |
 647 *     .------------------------------------------------------------.
 648 *     |  IEEE IEs(variable): Any received IEs comprising the       |
 649 *     |                      remaining portion of a received       |
 650 *     |                      association response frame.           |
 651 *     .------------------------------------------------------------.
 652 *
 653 * For simplistic handling, the status_code field can be used to determine
 654 * an association success (0) or failure (non-zero).
 655 */
 656int mwifiex_ret_802_11_associate(struct mwifiex_private *priv,
 657			     struct host_cmd_ds_command *resp)
 658{
 659	struct mwifiex_adapter *adapter = priv->adapter;
 660	int ret = 0;
 661	struct ieee_types_assoc_rsp *assoc_rsp;
 662	struct mwifiex_bssdescriptor *bss_desc;
 663	bool enable_data = true;
 664	u16 cap_info, status_code, aid;
 665	const u8 *ie_ptr;
 666
 667	if (!priv->attempted_bss_desc) {
 668		mwifiex_dbg(priv->adapter, ERROR,
 669			    "ASSOC_RESP: failed, association terminated by host\n");
 670		goto done;
 671	}
 672
 673	if (adapter->host_mlme_enabled) {
 674		struct ieee80211_mgmt *hdr;
 675
 676		hdr = (struct ieee80211_mgmt *)&resp->params;
 677		if (!memcmp(hdr->bssid,
 678			    priv->attempted_bss_desc->mac_address,
 679			    ETH_ALEN))
 680			assoc_rsp = (struct ieee_types_assoc_rsp *)
 681				&hdr->u.assoc_resp;
 682		else
 683			assoc_rsp =
 684				(struct ieee_types_assoc_rsp *)&resp->params;
 685	} else {
 686		assoc_rsp = (struct ieee_types_assoc_rsp *)&resp->params;
 687	}
 688
 689	cap_info = le16_to_cpu(assoc_rsp->cap_info_bitmap);
 690	status_code = le16_to_cpu(assoc_rsp->status_code);
 691	aid = le16_to_cpu(assoc_rsp->a_id);
 692
 693	if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
 694		dev_err(priv->adapter->dev,
 695			"invalid AID value 0x%x; bits 15:14 not set\n",
 696			aid);
 697
 698	aid &= ~(BIT(15) | BIT(14));
 699
 700	priv->assoc_rsp_size = min(le16_to_cpu(resp->size) - S_DS_GEN,
 701				   sizeof(priv->assoc_rsp_buf));
 702
 703	assoc_rsp->a_id = cpu_to_le16(aid);
 704	memcpy(priv->assoc_rsp_buf, &resp->params, priv->assoc_rsp_size);
 705
 706	if (status_code) {
 707		priv->adapter->dbg.num_cmd_assoc_failure++;
 708		mwifiex_dbg(priv->adapter, ERROR,
 709			    "ASSOC_RESP: failed,\t"
 710			    "status code=%d err=%#x a_id=%#x\n",
 711			    status_code, cap_info,
 712			    le16_to_cpu(assoc_rsp->a_id));
 713
 714		mwifiex_dbg(priv->adapter, ERROR, "assoc failure: reason %s\n",
 715			    assoc_failure_reason_to_str(cap_info));
 716		if (cap_info == CONNECT_ERR_ASSOC_ERR_TIMEOUT) {
 717			if (status_code == MWIFIEX_ASSOC_CMD_FAILURE_AUTH) {
 718				ret = WLAN_STATUS_AUTH_TIMEOUT;
 719				mwifiex_dbg(priv->adapter, ERROR,
 720					    "ASSOC_RESP: AUTH timeout\n");
 721			} else {
 722				ret = WLAN_STATUS_UNSPECIFIED_FAILURE;
 723				mwifiex_dbg(priv->adapter, ERROR,
 724					    "ASSOC_RESP: UNSPECIFIED failure\n");
 725			}
 726
 727			if (priv->adapter->host_mlme_enabled)
 728				priv->assoc_rsp_size = 0;
 729		} else {
 730			ret = status_code;
 731		}
 732
 733		goto done;
 734	}
 735
 736	/* Send a Media Connected event, according to the Spec */
 737	priv->media_connected = true;
 738
 739	priv->adapter->ps_state = PS_STATE_AWAKE;
 740	priv->adapter->pps_uapsd_mode = false;
 741	priv->adapter->tx_lock_flag = false;
 742
 743	/* Set the attempted BSSID Index to current */
 744	bss_desc = priv->attempted_bss_desc;
 745
 746	mwifiex_dbg(priv->adapter, INFO, "info: ASSOC_RESP: %s\n",
 747		    bss_desc->ssid.ssid);
 748
 749	/* Make a copy of current BSSID descriptor */
 750	memcpy(&priv->curr_bss_params.bss_descriptor,
 751	       bss_desc, sizeof(struct mwifiex_bssdescriptor));
 752
 753	/* Update curr_bss_params */
 754	priv->curr_bss_params.bss_descriptor.channel
 755		= bss_desc->phy_param_set.ds_param_set.current_chan;
 756
 757	priv->curr_bss_params.band = (u8) bss_desc->bss_band;
 758
 759	if (bss_desc->wmm_ie.vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC)
 760		priv->curr_bss_params.wmm_enabled = true;
 761	else
 762		priv->curr_bss_params.wmm_enabled = false;
 763
 764	if ((priv->wmm_required || bss_desc->bcn_ht_cap) &&
 765	    priv->curr_bss_params.wmm_enabled)
 766		priv->wmm_enabled = true;
 767	else
 768		priv->wmm_enabled = false;
 769
 770	priv->curr_bss_params.wmm_uapsd_enabled = false;
 771
 772	if (priv->wmm_enabled)
 773		priv->curr_bss_params.wmm_uapsd_enabled
 774			= ((bss_desc->wmm_ie.qos_info_bitmap &
 775				IEEE80211_WMM_IE_AP_QOSINFO_UAPSD) ? 1 : 0);
 776
 777	/* Store the bandwidth information from assoc response */
 778	ie_ptr = cfg80211_find_ie(WLAN_EID_HT_OPERATION, assoc_rsp->ie_buffer,
 779				  priv->assoc_rsp_size
 780				  - sizeof(struct ieee_types_assoc_rsp));
 781
 782	priv->ht_param_present = ie_ptr ? true : false;
 783
 784	mwifiex_dbg(priv->adapter, INFO,
 785		    "info: ASSOC_RESP: curr_pkt_filter is %#x\n",
 786		    priv->curr_pkt_filter);
 787	if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
 788		priv->wpa_is_gtk_set = false;
 789
 790	if (priv->wmm_enabled) {
 791		/* Don't re-enable carrier until we get the WMM_GET_STATUS
 792		   event */
 793		enable_data = false;
 794	} else {
 795		/* Since WMM is not enabled, setup the queues with the
 796		   defaults */
 797		mwifiex_wmm_setup_queue_priorities(priv, NULL);
 798		mwifiex_wmm_setup_ac_downgrade(priv);
 799	}
 800
 801	if (enable_data)
 802		mwifiex_dbg(priv->adapter, INFO,
 803			    "info: post association, re-enabling data flow\n");
 804
 805	/* Reset SNR/NF/RSSI values */
 806	priv->data_rssi_last = 0;
 807	priv->data_nf_last = 0;
 808	priv->data_rssi_avg = 0;
 809	priv->data_nf_avg = 0;
 810	priv->bcn_rssi_last = 0;
 811	priv->bcn_nf_last = 0;
 812	priv->bcn_rssi_avg = 0;
 813	priv->bcn_nf_avg = 0;
 814	priv->rxpd_rate = 0;
 815	priv->rxpd_htinfo = 0;
 816
 817	mwifiex_save_curr_bcn(priv);
 818
 819	priv->adapter->dbg.num_cmd_assoc_success++;
 820
 821	mwifiex_dbg(priv->adapter, MSG, "assoc: associated with %pM\n",
 822		    priv->attempted_bss_desc->mac_address);
 823
 824	/* Add the ra_list here for infra mode as there will be only 1 ra
 825	   always */
 826	mwifiex_ralist_add(priv,
 827			   priv->curr_bss_params.bss_descriptor.mac_address);
 828
 829	if (!netif_carrier_ok(priv->netdev))
 830		netif_carrier_on(priv->netdev);
 831	mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
 832
 833	if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
 834		priv->scan_block = true;
 835	else
 836		priv->port_open = true;
 837
 838done:
 839	/* Need to indicate IOCTL complete */
 840	if (adapter->curr_cmd->wait_q_enabled) {
 841		if (ret)
 842			adapter->cmd_wait_q.status = -1;
 843		else
 844			adapter->cmd_wait_q.status = 0;
 845	}
 846
 847	return ret;
 848}
 849
 850/*
 851 * This function prepares command for ad-hoc start.
 852 *
 853 * Driver will fill up SSID, BSS mode, IBSS parameters, physical
 854 * parameters, probe delay, and capability information. Firmware
 855 * will fill up beacon period, basic rates and operational rates.
 856 *
 857 * In addition, the following TLVs are added -
 858 *      - Channel TLV
 859 *      - Vendor specific IE
 860 *      - WPA/WPA2 IE
 861 *      - HT Capabilities IE
 862 *      - HT Information IE
 863 *
 864 * Preparation also includes -
 865 *      - Setting command ID and proper size
 866 *      - Ensuring correct endian-ness
 867 */
 868int
 869mwifiex_cmd_802_11_ad_hoc_start(struct mwifiex_private *priv,
 870				struct host_cmd_ds_command *cmd,
 871				struct cfg80211_ssid *req_ssid)
 872{
 873	int rsn_ie_len = 0;
 874	struct mwifiex_adapter *adapter = priv->adapter;
 875	struct host_cmd_ds_802_11_ad_hoc_start *adhoc_start =
 876		&cmd->params.adhoc_start;
 877	struct mwifiex_bssdescriptor *bss_desc;
 878	u32 cmd_append_size = 0;
 879	u32 i;
 880	u16 tmp_cap;
 881	struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
 882	u8 radio_type;
 883
 884	struct mwifiex_ie_types_htcap *ht_cap;
 885	struct mwifiex_ie_types_htinfo *ht_info;
 886	u8 *pos = (u8 *) adhoc_start +
 887			sizeof(struct host_cmd_ds_802_11_ad_hoc_start);
 888
 889	if (!adapter)
 890		return -1;
 891
 892	cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_START);
 893
 894	bss_desc = &priv->curr_bss_params.bss_descriptor;
 895	priv->attempted_bss_desc = bss_desc;
 896
 897	/*
 898	 * Fill in the parameters for 2 data structures:
 899	 *   1. struct host_cmd_ds_802_11_ad_hoc_start command
 900	 *   2. bss_desc
 901	 * Driver will fill up SSID, bss_mode,IBSS param, Physical Param,
 902	 * probe delay, and Cap info.
 903	 * Firmware will fill up beacon period, Basic rates
 904	 * and operational rates.
 905	 */
 906
 907	memset(adhoc_start->ssid, 0, IEEE80211_MAX_SSID_LEN);
 908
 909	if (req_ssid->ssid_len > IEEE80211_MAX_SSID_LEN)
 910		req_ssid->ssid_len = IEEE80211_MAX_SSID_LEN;
 911	memcpy(adhoc_start->ssid, req_ssid->ssid, req_ssid->ssid_len);
 912
 913	mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: SSID = %s\n",
 914		    adhoc_start->ssid);
 915
 916	memset(bss_desc->ssid.ssid, 0, IEEE80211_MAX_SSID_LEN);
 917	memcpy(bss_desc->ssid.ssid, req_ssid->ssid, req_ssid->ssid_len);
 918
 919	bss_desc->ssid.ssid_len = req_ssid->ssid_len;
 920
 921	/* Set the BSS mode */
 922	adhoc_start->bss_mode = HostCmd_BSS_MODE_IBSS;
 923	bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
 924	adhoc_start->beacon_period = cpu_to_le16(priv->beacon_period);
 925	bss_desc->beacon_period = priv->beacon_period;
 926
 927	/* Set Physical param set */
 928/* Parameter IE Id */
 929#define DS_PARA_IE_ID   3
 930/* Parameter IE length */
 931#define DS_PARA_IE_LEN  1
 932
 933	adhoc_start->phy_param_set.ds_param_set.element_id = DS_PARA_IE_ID;
 934	adhoc_start->phy_param_set.ds_param_set.len = DS_PARA_IE_LEN;
 935
 936	if (!mwifiex_get_cfp(priv, adapter->adhoc_start_band,
 937			     (u16) priv->adhoc_channel, 0)) {
 938		struct mwifiex_chan_freq_power *cfp;
 939		cfp = mwifiex_get_cfp(priv, adapter->adhoc_start_band,
 940				      FIRST_VALID_CHANNEL, 0);
 941		if (cfp)
 942			priv->adhoc_channel = (u8) cfp->channel;
 943	}
 944
 945	if (!priv->adhoc_channel) {
 946		mwifiex_dbg(adapter, ERROR,
 947			    "ADHOC_S_CMD: adhoc_channel cannot be 0\n");
 948		return -1;
 949	}
 950
 951	mwifiex_dbg(adapter, INFO,
 952		    "info: ADHOC_S_CMD: creating ADHOC on channel %d\n",
 953		    priv->adhoc_channel);
 954
 955	priv->curr_bss_params.bss_descriptor.channel = priv->adhoc_channel;
 956	priv->curr_bss_params.band = adapter->adhoc_start_band;
 957
 958	bss_desc->channel = priv->adhoc_channel;
 959	adhoc_start->phy_param_set.ds_param_set.current_chan =
 960		priv->adhoc_channel;
 961
 962	memcpy(&bss_desc->phy_param_set, &adhoc_start->phy_param_set,
 963	       sizeof(union ieee_types_phy_param_set));
 964
 965	/* Set IBSS param set */
 966/* IBSS parameter IE Id */
 967#define IBSS_PARA_IE_ID   6
 968/* IBSS parameter IE length */
 969#define IBSS_PARA_IE_LEN  2
 970
 971	adhoc_start->ss_param_set.ibss_param_set.element_id = IBSS_PARA_IE_ID;
 972	adhoc_start->ss_param_set.ibss_param_set.len = IBSS_PARA_IE_LEN;
 973	adhoc_start->ss_param_set.ibss_param_set.atim_window
 974					= cpu_to_le16(priv->atim_window);
 975	memcpy(&bss_desc->ss_param_set, &adhoc_start->ss_param_set,
 976	       sizeof(union ieee_types_ss_param_set));
 977
 978	/* Set Capability info */
 979	bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_IBSS;
 980	tmp_cap = WLAN_CAPABILITY_IBSS;
 981
 982	/* Set up privacy in bss_desc */
 983	if (priv->sec_info.encryption_mode) {
 984		/* Ad-Hoc capability privacy on */
 985		mwifiex_dbg(adapter, INFO,
 986			    "info: ADHOC_S_CMD: wep_status set privacy to WEP\n");
 987		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
 988		tmp_cap |= WLAN_CAPABILITY_PRIVACY;
 989	} else {
 990		mwifiex_dbg(adapter, INFO,
 991			    "info: ADHOC_S_CMD: wep_status NOT set,\t"
 992			    "setting privacy to ACCEPT ALL\n");
 993		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
 994	}
 995
 996	memset(adhoc_start->data_rate, 0, sizeof(adhoc_start->data_rate));
 997	mwifiex_get_active_data_rates(priv, adhoc_start->data_rate);
 998	if ((adapter->adhoc_start_band & BAND_G) &&
 999	    (priv->curr_pkt_filter & HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON)) {
1000		if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1001				     HostCmd_ACT_GEN_SET, 0,
1002				     &priv->curr_pkt_filter, false)) {
1003			mwifiex_dbg(adapter, ERROR,
1004				    "ADHOC_S_CMD: G Protection config failed\n");
1005			return -1;
1006		}
1007	}
1008	/* Find the last non zero */
1009	for (i = 0; i < sizeof(adhoc_start->data_rate); i++)
1010		if (!adhoc_start->data_rate[i])
1011			break;
1012
1013	priv->curr_bss_params.num_of_rates = i;
1014
1015	/* Copy the ad-hoc creating rates into Current BSS rate structure */
1016	memcpy(&priv->curr_bss_params.data_rates,
1017	       &adhoc_start->data_rate, priv->curr_bss_params.num_of_rates);
1018
1019	mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: rates=%4ph\n",
1020		    adhoc_start->data_rate);
1021
1022	mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: AD-HOC Start command is ready\n");
1023
1024	if (IS_SUPPORT_MULTI_BANDS(adapter)) {
1025		/* Append a channel TLV */
1026		chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1027		chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1028		chan_tlv->header.len =
1029			cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1030
1031		memset(chan_tlv->chan_scan_param, 0x00,
1032		       sizeof(struct mwifiex_chan_scan_param_set));
1033		chan_tlv->chan_scan_param[0].chan_number =
1034			(u8) priv->curr_bss_params.bss_descriptor.channel;
1035
1036		mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: TLV Chan = %d\n",
1037			    chan_tlv->chan_scan_param[0].chan_number);
1038
1039		chan_tlv->chan_scan_param[0].radio_type
1040		       = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1041		if (adapter->adhoc_start_band & BAND_GN ||
1042		    adapter->adhoc_start_band & BAND_AN) {
1043			if (adapter->sec_chan_offset ==
1044					    IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
1045				chan_tlv->chan_scan_param[0].radio_type |=
1046					(IEEE80211_HT_PARAM_CHA_SEC_ABOVE << 4);
1047			else if (adapter->sec_chan_offset ==
1048					    IEEE80211_HT_PARAM_CHA_SEC_BELOW)
1049				chan_tlv->chan_scan_param[0].radio_type |=
1050					(IEEE80211_HT_PARAM_CHA_SEC_BELOW << 4);
1051		}
1052		mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: TLV Band = %d\n",
1053			    chan_tlv->chan_scan_param[0].radio_type);
1054		pos += sizeof(chan_tlv->header) +
1055			sizeof(struct mwifiex_chan_scan_param_set);
1056		cmd_append_size +=
1057			sizeof(chan_tlv->header) +
1058			sizeof(struct mwifiex_chan_scan_param_set);
1059	}
1060
1061	/* Append vendor specific IE TLV */
1062	cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1063				MWIFIEX_VSIE_MASK_ADHOC, &pos);
1064
1065	if (priv->sec_info.wpa_enabled) {
1066		rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1067		if (rsn_ie_len == -1)
1068			return -1;
1069		cmd_append_size += rsn_ie_len;
1070	}
1071
1072	if (adapter->adhoc_11n_enabled) {
1073		/* Fill HT CAPABILITY */
1074		ht_cap = (struct mwifiex_ie_types_htcap *) pos;
1075		memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));
1076		ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
1077		ht_cap->header.len =
1078		       cpu_to_le16(sizeof(struct ieee80211_ht_cap));
1079		radio_type = mwifiex_band_to_radio_type(
1080					priv->adapter->config_bands);
1081		mwifiex_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);
1082
1083		if (adapter->sec_chan_offset ==
1084					IEEE80211_HT_PARAM_CHA_SEC_NONE) {
1085			u16 tmp_ht_cap;
1086
1087			tmp_ht_cap = le16_to_cpu(ht_cap->ht_cap.cap_info);
1088			tmp_ht_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1089			tmp_ht_cap &= ~IEEE80211_HT_CAP_SGI_40;
1090			ht_cap->ht_cap.cap_info = cpu_to_le16(tmp_ht_cap);
1091		}
1092
1093		pos += sizeof(struct mwifiex_ie_types_htcap);
1094		cmd_append_size += sizeof(struct mwifiex_ie_types_htcap);
1095
1096		/* Fill HT INFORMATION */
1097		ht_info = (struct mwifiex_ie_types_htinfo *) pos;
1098		memset(ht_info, 0, sizeof(struct mwifiex_ie_types_htinfo));
1099		ht_info->header.type = cpu_to_le16(WLAN_EID_HT_OPERATION);
1100		ht_info->header.len =
1101			cpu_to_le16(sizeof(struct ieee80211_ht_operation));
1102
1103		ht_info->ht_oper.primary_chan =
1104			(u8) priv->curr_bss_params.bss_descriptor.channel;
1105		if (adapter->sec_chan_offset) {
1106			ht_info->ht_oper.ht_param = adapter->sec_chan_offset;
1107			ht_info->ht_oper.ht_param |=
1108					IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
1109		}
1110		ht_info->ht_oper.operation_mode =
1111		     cpu_to_le16(IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1112		ht_info->ht_oper.basic_set[0] = 0xff;
1113		pos += sizeof(struct mwifiex_ie_types_htinfo);
1114		cmd_append_size +=
1115				sizeof(struct mwifiex_ie_types_htinfo);
1116	}
1117
1118	cmd->size =
1119		cpu_to_le16((u16)(sizeof(struct host_cmd_ds_802_11_ad_hoc_start)
1120				  + S_DS_GEN + cmd_append_size));
1121
1122	if (adapter->adhoc_start_band == BAND_B)
1123		tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
1124	else
1125		tmp_cap |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1126
1127	adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);
1128
1129	return 0;
1130}
1131
1132/*
1133 * This function prepares command for ad-hoc join.
1134 *
1135 * Most of the parameters are set up by copying from the target BSS descriptor
1136 * from the scan response.
1137 *
1138 * In addition, the following TLVs are added -
1139 *      - Channel TLV
1140 *      - Vendor specific IE
1141 *      - WPA/WPA2 IE
1142 *      - 11n IE
1143 *
1144 * Preparation also includes -
1145 *      - Setting command ID and proper size
1146 *      - Ensuring correct endian-ness
1147 */
1148int
1149mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,
1150			       struct host_cmd_ds_command *cmd,
1151			       struct mwifiex_bssdescriptor *bss_desc)
1152{
1153	int rsn_ie_len = 0;
1154	struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =
1155		&cmd->params.adhoc_join;
1156	struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
1157	u32 cmd_append_size = 0;
1158	u16 tmp_cap;
1159	u32 i, rates_size = 0;
1160	u16 curr_pkt_filter;
1161	u8 *pos =
1162		(u8 *) adhoc_join +
1163		sizeof(struct host_cmd_ds_802_11_ad_hoc_join);
1164
1165/* Use G protection */
1166#define USE_G_PROTECTION        0x02
1167	if (bss_desc->erp_flags & USE_G_PROTECTION) {
1168		curr_pkt_filter =
1169			priv->
1170			curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;
1171
1172		if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1173				     HostCmd_ACT_GEN_SET, 0,
1174				     &curr_pkt_filter, false)) {
1175			mwifiex_dbg(priv->adapter, ERROR,
1176				    "ADHOC_J_CMD: G Protection config failed\n");
1177			return -1;
1178		}
1179	}
1180
1181	priv->attempted_bss_desc = bss_desc;
1182
1183	cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);
1184
1185	adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;
1186
1187	adhoc_join->bss_descriptor.beacon_period
1188		= cpu_to_le16(bss_desc->beacon_period);
1189
1190	memcpy(&adhoc_join->bss_descriptor.bssid,
1191	       &bss_desc->mac_address, ETH_ALEN);
1192
1193	memcpy(&adhoc_join->bss_descriptor.ssid,
1194	       &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);
1195
1196	memcpy(&adhoc_join->bss_descriptor.phy_param_set,
1197	       &bss_desc->phy_param_set,
1198	       sizeof(union ieee_types_phy_param_set));
1199
1200	memcpy(&adhoc_join->bss_descriptor.ss_param_set,
1201	       &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));
1202
1203	tmp_cap = bss_desc->cap_info_bitmap;
1204
1205	tmp_cap &= CAPINFO_MASK;
1206
1207	mwifiex_dbg(priv->adapter, INFO,
1208		    "info: ADHOC_J_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
1209		    tmp_cap, CAPINFO_MASK);
1210
1211	/* Information on BSSID descriptor passed to FW */
1212	mwifiex_dbg(priv->adapter, INFO,
1213		    "info: ADHOC_J_CMD: BSSID=%pM, SSID='%s'\n",
1214		    adhoc_join->bss_descriptor.bssid,
1215		    adhoc_join->bss_descriptor.ssid);
1216
1217	for (i = 0; i < MWIFIEX_SUPPORTED_RATES &&
1218		    bss_desc->supported_rates[i]; i++)
1219		;
1220	rates_size = i;
1221
1222	/* Copy Data Rates from the Rates recorded in scan response */
1223	memset(adhoc_join->bss_descriptor.data_rates, 0,
1224	       sizeof(adhoc_join->bss_descriptor.data_rates));
1225	memcpy(adhoc_join->bss_descriptor.data_rates,
1226	       bss_desc->supported_rates, rates_size);
1227
1228	/* Copy the adhoc join rates into Current BSS state structure */
1229	priv->curr_bss_params.num_of_rates = rates_size;
1230	memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,
1231	       rates_size);
1232
1233	/* Copy the channel information */
1234	priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;
1235	priv->curr_bss_params.band = (u8) bss_desc->bss_band;
1236
1237	if (priv->sec_info.wep_enabled || priv->sec_info.wpa_enabled)
1238		tmp_cap |= WLAN_CAPABILITY_PRIVACY;
1239
1240	if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {
1241		/* Append a channel TLV */
1242		chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1243		chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1244		chan_tlv->header.len =
1245			cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1246
1247		memset(chan_tlv->chan_scan_param, 0x00,
1248		       sizeof(struct mwifiex_chan_scan_param_set));
1249		chan_tlv->chan_scan_param[0].chan_number =
1250			(bss_desc->phy_param_set.ds_param_set.current_chan);
1251		mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_J_CMD: TLV Chan=%d\n",
1252			    chan_tlv->chan_scan_param[0].chan_number);
1253
1254		chan_tlv->chan_scan_param[0].radio_type =
1255			mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
1256
1257		mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_J_CMD: TLV Band=%d\n",
1258			    chan_tlv->chan_scan_param[0].radio_type);
1259		pos += sizeof(chan_tlv->header) +
1260				sizeof(struct mwifiex_chan_scan_param_set);
1261		cmd_append_size += sizeof(chan_tlv->header) +
1262				sizeof(struct mwifiex_chan_scan_param_set);
1263	}
1264
1265	if (priv->sec_info.wpa_enabled)
1266		rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1267	if (rsn_ie_len == -1)
1268		return -1;
1269	cmd_append_size += rsn_ie_len;
1270
1271	if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
1272		cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,
1273			bss_desc, &pos);
1274
1275	/* Append vendor specific IE TLV */
1276	cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1277			MWIFIEX_VSIE_MASK_ADHOC, &pos);
1278
1279	cmd->size = cpu_to_le16
1280		((u16) (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)
1281			+ S_DS_GEN + cmd_append_size));
1282
1283	adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);
1284
1285	return 0;
1286}
1287
1288/*
1289 * This function handles the command response of ad-hoc start and
1290 * ad-hoc join.
1291 *
1292 * The function generates a device-connected event to notify
1293 * the applications, in case of successful ad-hoc start/join, and
1294 * saves the beacon buffer.
1295 */
1296int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,
1297			      struct host_cmd_ds_command *resp)
1298{
1299	int ret = 0;
1300	struct mwifiex_adapter *adapter = priv->adapter;
1301	struct host_cmd_ds_802_11_ad_hoc_start_result *start_result =
1302				&resp->params.start_result;
1303	struct host_cmd_ds_802_11_ad_hoc_join_result *join_result =
1304				&resp->params.join_result;
1305	struct mwifiex_bssdescriptor *bss_desc;
1306	u16 cmd = le16_to_cpu(resp->command);
1307	u8 result;
1308
1309	if (!priv->attempted_bss_desc) {
1310		mwifiex_dbg(priv->adapter, ERROR,
1311			    "ADHOC_RESP: failed, association terminated by host\n");
1312		goto done;
1313	}
1314
1315	if (cmd == HostCmd_CMD_802_11_AD_HOC_START)
1316		result = start_result->result;
1317	else
1318		result = join_result->result;
1319
1320	bss_desc = priv->attempted_bss_desc;
1321
1322	/* Join result code 0 --> SUCCESS */
1323	if (result) {
1324		mwifiex_dbg(priv->adapter, ERROR, "ADHOC_RESP: failed\n");
1325		if (priv->media_connected)
1326			mwifiex_reset_connect_state(priv, result, true);
1327
1328		memset(&priv->curr_bss_params.bss_descriptor,
1329		       0x00, sizeof(struct mwifiex_bssdescriptor));
1330
1331		ret = -1;
1332		goto done;
1333	}
1334
1335	/* Send a Media Connected event, according to the Spec */
1336	priv->media_connected = true;
1337
1338	if (le16_to_cpu(resp->command) == HostCmd_CMD_802_11_AD_HOC_START) {
1339		mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_S_RESP %s\n",
1340			    bss_desc->ssid.ssid);
1341
1342		/* Update the created network descriptor with the new BSSID */
1343		memcpy(bss_desc->mac_address,
1344		       start_result->bssid, ETH_ALEN);
1345
1346		priv->adhoc_state = ADHOC_STARTED;
1347	} else {
1348		/*
1349		 * Now the join cmd should be successful.
1350		 * If BSSID has changed use SSID to compare instead of BSSID
1351		 */
1352		mwifiex_dbg(priv->adapter, INFO,
1353			    "info: ADHOC_J_RESP %s\n",
1354			    bss_desc->ssid.ssid);
1355
1356		/*
1357		 * Make a copy of current BSSID descriptor, only needed for
1358		 * join since the current descriptor is already being used
1359		 * for adhoc start
1360		 */
1361		memcpy(&priv->curr_bss_params.bss_descriptor,
1362		       bss_desc, sizeof(struct mwifiex_bssdescriptor));
1363
1364		priv->adhoc_state = ADHOC_JOINED;
1365	}
1366
1367	mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_RESP: channel = %d\n",
1368		    priv->adhoc_channel);
1369	mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_RESP: BSSID = %pM\n",
1370		    priv->curr_bss_params.bss_descriptor.mac_address);
1371
1372	if (!netif_carrier_ok(priv->netdev))
1373		netif_carrier_on(priv->netdev);
1374	mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
1375
1376	mwifiex_save_curr_bcn(priv);
1377
1378done:
1379	/* Need to indicate IOCTL complete */
1380	if (adapter->curr_cmd->wait_q_enabled) {
1381		if (ret)
1382			adapter->cmd_wait_q.status = -1;
1383		else
1384			adapter->cmd_wait_q.status = 0;
1385
1386	}
1387
1388	return ret;
1389}
1390
1391/*
1392 * This function associates to a specific BSS discovered in a scan.
1393 *
1394 * It clears any past association response stored for application
1395 * retrieval and calls the command preparation routine to send the
1396 * command to firmware.
1397 */
1398int mwifiex_associate(struct mwifiex_private *priv,
1399		      struct mwifiex_bssdescriptor *bss_desc)
1400{
1401	/* Return error if the adapter is not STA role or table entry
1402	 * is not marked as infra.
1403	 */
1404	if ((GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) ||
1405	    (bss_desc->bss_mode != NL80211_IFTYPE_STATION))
1406		return -1;
1407
1408	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
1409	    !bss_desc->disable_11n && !bss_desc->disable_11ac &&
1410	    priv->adapter->config_bands & BAND_AAC)
1411		mwifiex_set_11ac_ba_params(priv);
1412	else
1413		mwifiex_set_ba_params(priv);
1414
1415	/* Clear any past association response stored for application
1416	   retrieval */
1417	priv->assoc_rsp_size = 0;
1418
1419	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_ASSOCIATE,
1420				HostCmd_ACT_GEN_SET, 0, bss_desc, true);
1421}
1422
1423/*
1424 * This function starts an ad-hoc network.
1425 *
1426 * It calls the command preparation routine to send the command to firmware.
1427 */
1428int
1429mwifiex_adhoc_start(struct mwifiex_private *priv,
1430		    struct cfg80211_ssid *adhoc_ssid)
1431{
1432	mwifiex_dbg(priv->adapter, INFO, "info: Adhoc Channel = %d\n",
1433		    priv->adhoc_channel);
1434	mwifiex_dbg(priv->adapter, INFO, "info: curr_bss_params.channel = %d\n",
1435		    priv->curr_bss_params.bss_descriptor.channel);
1436	mwifiex_dbg(priv->adapter, INFO, "info: curr_bss_params.band = %d\n",
1437		    priv->curr_bss_params.band);
1438
1439	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
1440	    priv->adapter->config_bands & BAND_AAC)
1441		mwifiex_set_11ac_ba_params(priv);
1442	else
1443		mwifiex_set_ba_params(priv);
1444
1445	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_START,
1446				HostCmd_ACT_GEN_SET, 0, adhoc_ssid, true);
1447}
1448
1449/*
1450 * This function joins an ad-hoc network found in a previous scan.
1451 *
1452 * It calls the command preparation routine to send the command to firmware,
1453 * if already not connected to the requested SSID.
1454 */
1455int mwifiex_adhoc_join(struct mwifiex_private *priv,
1456		       struct mwifiex_bssdescriptor *bss_desc)
1457{
1458	mwifiex_dbg(priv->adapter, INFO,
1459		    "info: adhoc join: curr_bss ssid =%s\n",
1460		    priv->curr_bss_params.bss_descriptor.ssid.ssid);
1461	mwifiex_dbg(priv->adapter, INFO,
1462		    "info: adhoc join: curr_bss ssid_len =%u\n",
1463		    priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
1464	mwifiex_dbg(priv->adapter, INFO, "info: adhoc join: ssid =%s\n",
1465		    bss_desc->ssid.ssid);
1466	mwifiex_dbg(priv->adapter, INFO, "info: adhoc join: ssid_len =%u\n",
1467		    bss_desc->ssid.ssid_len);
1468
1469	/* Check if the requested SSID is already joined */
1470	if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&
1471	    cfg80211_ssid_eq(&bss_desc->ssid,
1472			     &priv->curr_bss_params.bss_descriptor.ssid) &&
1473	    (priv->curr_bss_params.bss_descriptor.bss_mode ==
1474							NL80211_IFTYPE_ADHOC)) {
1475		mwifiex_dbg(priv->adapter, INFO,
1476			    "info: ADHOC_J_CMD: new ad-hoc SSID\t"
1477			    "is the same as current; not attempting to re-join\n");
1478		return -1;
1479	}
1480
1481	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
1482	    !bss_desc->disable_11n && !bss_desc->disable_11ac &&
1483	    priv->adapter->config_bands & BAND_AAC)
1484		mwifiex_set_11ac_ba_params(priv);
1485	else
1486		mwifiex_set_ba_params(priv);
1487
1488	mwifiex_dbg(priv->adapter, INFO,
1489		    "info: curr_bss_params.channel = %d\n",
1490		    priv->curr_bss_params.bss_descriptor.channel);
1491	mwifiex_dbg(priv->adapter, INFO,
1492		    "info: curr_bss_params.band = %c\n",
1493		    priv->curr_bss_params.band);
1494
1495	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,
1496				HostCmd_ACT_GEN_SET, 0, bss_desc, true);
1497}
1498
1499/*
1500 * This function deauthenticates/disconnects from infra network by sending
1501 * deauthentication request.
1502 */
1503static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv, u8 *mac)
1504{
1505	u8 mac_address[ETH_ALEN];
1506	int ret;
1507
1508	if (!mac || is_zero_ether_addr(mac))
1509		memcpy(mac_address,
1510		       priv->curr_bss_params.bss_descriptor.mac_address,
1511		       ETH_ALEN);
1512	else
1513		memcpy(mac_address, mac, ETH_ALEN);
1514
1515	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
1516			       HostCmd_ACT_GEN_SET, 0, mac_address, true);
1517
1518	return ret;
1519}
1520
1521/*
1522 * This function deauthenticates/disconnects from a BSS.
1523 *
1524 * In case of infra made, it sends deauthentication request, and
1525 * in case of ad-hoc mode, a stop network request is sent to the firmware.
1526 * In AP mode, a command to stop bss is sent to firmware.
1527 */
1528int mwifiex_deauthenticate(struct mwifiex_private *priv, u8 *mac)
1529{
1530	int ret = 0;
1531
1532	if (!priv->media_connected)
1533		return 0;
1534
1535	if (priv->adapter->host_mlme_enabled) {
1536		priv->auth_flag = 0;
1537		priv->auth_alg = WLAN_AUTH_NONE;
1538		priv->host_mlme_reg = false;
1539		priv->mgmt_frame_mask = 0;
1540		if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
1541				     HostCmd_ACT_GEN_SET, 0,
1542				     &priv->mgmt_frame_mask, false)) {
1543			mwifiex_dbg(priv->adapter, ERROR,
1544				    "could not unregister mgmt frame rx\n");
1545			return -1;
1546		}
1547	}
1548
1549	switch (priv->bss_mode) {
1550	case NL80211_IFTYPE_STATION:
1551	case NL80211_IFTYPE_P2P_CLIENT:
1552		ret = mwifiex_deauthenticate_infra(priv, mac);
1553		if (ret)
1554			cfg80211_disconnected(priv->netdev, 0, NULL, 0,
1555					      true, GFP_KERNEL);
1556		break;
1557	case NL80211_IFTYPE_ADHOC:
1558		return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_STOP,
1559					HostCmd_ACT_GEN_SET, 0, NULL, true);
1560	case NL80211_IFTYPE_AP:
1561		return mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
1562					HostCmd_ACT_GEN_SET, 0, NULL, true);
1563	default:
1564		break;
1565	}
1566
1567	return ret;
1568}
1569
1570/* This function deauthenticates/disconnects from all BSS. */
1571void mwifiex_deauthenticate_all(struct mwifiex_adapter *adapter)
1572{
1573	struct mwifiex_private *priv;
1574	int i;
1575
1576	for (i = 0; i < adapter->priv_num; i++) {
1577		priv = adapter->priv[i];
1578		mwifiex_deauthenticate(priv, NULL);
1579	}
1580}
1581EXPORT_SYMBOL_GPL(mwifiex_deauthenticate_all);
1582
1583/*
1584 * This function converts band to radio type used in channel TLV.
1585 */
1586u8
1587mwifiex_band_to_radio_type(u8 band)
1588{
1589	switch (band) {
1590	case BAND_A:
1591	case BAND_AN:
1592	case BAND_A | BAND_AN:
1593	case BAND_A | BAND_AN | BAND_AAC:
1594		return HostCmd_SCAN_RADIO_TYPE_A;
1595	case BAND_B:
1596	case BAND_G:
1597	case BAND_B | BAND_G:
1598	default:
1599		return HostCmd_SCAN_RADIO_TYPE_BG;
1600	}
1601}