brintos

brintos / linux-shallow public Read only

0
0
Text · 49.6 KiB · 6d8f1d1 Raw
1609 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * NXP Wireless LAN device driver: association and ad-hoc start/join4 *5 * Copyright 2011-2020 NXP6 */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 appends25 * the buffer as a pass through TLV type to the request.26 */27static int28mwifiex_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 return41	 *   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 pointer54		   param */55		*buffer += sizeof(ie_header);56		ret_len += sizeof(ie_header);57 58		/* Copy the generic IE buffer to the output buffer, advance59		   pointer */60		memcpy(*buffer, priv->gen_ie_buf, priv->gen_ie_buf_len);61 62		/* Increment the return size and the return buffer pointer63		   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 response82 *      - The TSF timestamp of our local MAC at the time we observed the83 *        beacon/probe response.84 *85 * The firmware uses the timestamp values to set an initial TSF value86 * in the MAC for the new association after a reassociation attempt.87 */88static int89mwifiex_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 taken132 * 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 for152			   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 a188 * target BSS and our adapter settings for use in an assoc/join command.189 */190static int191mwifiex_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 command220 * preparation routine.221 *222 * If the IE buffer has been setup by the application, this routine appends223 * the buffer as a WPS TLV type to the request.224 */225static int226mwifiex_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 return236	 * 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 appends267 * the buffer as a WAPI TLV type to the request.268 */269static int270mwifiex_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 return283	 *   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 pointer296		   param */297		*buffer += sizeof(ie_header);298		retLen += sizeof(ie_header);299 300		/* Copy the wapi IE buffer to the output buffer, advance301		   pointer */302		memcpy(*buffer, priv->wapi_ie, priv->wapi_ie_len);303 304		/* Increment the return size and the return buffer pointer305		   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	else338		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 address352 *      - Listen interval353 *      - Beacon interval354 *      - Capability information355 *356 * ...and the following TLVs, as required -357 *      - SSID TLV358 *      - PHY TLV359 *      - SS TLV360 *      - Rates TLV361 *      - Authentication TLV362 *      - Channel TLV363 *      - WPA/WPA2 IE364 *      - 11n TLV365 *      - Vendor specific TLV366 *      - WMM TLV367 *      - WAPI IE368 *      - Generic IE369 *      - TSF TLV370 *371 * Preparation also includes -372 *      - Setting command ID and proper size373 *      - Ensuring correct endian-ness374 */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_bssdesc437	    (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	else461		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 was492		   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 handler596 *597 * The response buffer for the association command has the following598 * memory layout.599 *600 * For cases where an association response was not received (indicated601 * 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 IEEE637 * 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 determine654 * 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	struct ieee80211_ht_operation *assoc_resp_ht_oper;667 668	if (!priv->attempted_bss_desc) {669		mwifiex_dbg(priv->adapter, ERROR,670			    "ASSOC_RESP: failed, association terminated by host\n");671		goto done;672	}673 674	if (adapter->host_mlme_enabled) {675		struct ieee80211_mgmt *hdr;676 677		hdr = (struct ieee80211_mgmt *)&resp->params;678		if (!memcmp(hdr->bssid,679			    priv->attempted_bss_desc->mac_address,680			    ETH_ALEN))681			assoc_rsp = (struct ieee_types_assoc_rsp *)682				&hdr->u.assoc_resp;683		else684			assoc_rsp =685				(struct ieee_types_assoc_rsp *)&resp->params;686	} else {687		assoc_rsp = (struct ieee_types_assoc_rsp *)&resp->params;688	}689 690	cap_info = le16_to_cpu(assoc_rsp->cap_info_bitmap);691	status_code = le16_to_cpu(assoc_rsp->status_code);692	aid = le16_to_cpu(assoc_rsp->a_id);693 694	if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))695		dev_err(priv->adapter->dev,696			"invalid AID value 0x%x; bits 15:14 not set\n",697			aid);698 699	aid &= ~(BIT(15) | BIT(14));700 701	priv->assoc_rsp_size = min(le16_to_cpu(resp->size) - S_DS_GEN,702				   sizeof(priv->assoc_rsp_buf));703 704	assoc_rsp->a_id = cpu_to_le16(aid);705	memcpy(priv->assoc_rsp_buf, &resp->params, priv->assoc_rsp_size);706 707	if (status_code) {708		priv->adapter->dbg.num_cmd_assoc_failure++;709		mwifiex_dbg(priv->adapter, ERROR,710			    "ASSOC_RESP: failed,\t"711			    "status code=%d err=%#x a_id=%#x\n",712			    status_code, cap_info,713			    le16_to_cpu(assoc_rsp->a_id));714 715		mwifiex_dbg(priv->adapter, ERROR, "assoc failure: reason %s\n",716			    assoc_failure_reason_to_str(cap_info));717		if (cap_info == CONNECT_ERR_ASSOC_ERR_TIMEOUT) {718			if (status_code == MWIFIEX_ASSOC_CMD_FAILURE_AUTH) {719				ret = WLAN_STATUS_AUTH_TIMEOUT;720				mwifiex_dbg(priv->adapter, ERROR,721					    "ASSOC_RESP: AUTH timeout\n");722			} else {723				ret = WLAN_STATUS_UNSPECIFIED_FAILURE;724				mwifiex_dbg(priv->adapter, ERROR,725					    "ASSOC_RESP: UNSPECIFIED failure\n");726			}727 728			if (priv->adapter->host_mlme_enabled)729				priv->assoc_rsp_size = 0;730		} else {731			ret = status_code;732		}733 734		goto done;735	}736 737	/* Send a Media Connected event, according to the Spec */738	priv->media_connected = true;739 740	priv->adapter->ps_state = PS_STATE_AWAKE;741	priv->adapter->pps_uapsd_mode = false;742	priv->adapter->tx_lock_flag = false;743 744	/* Set the attempted BSSID Index to current */745	bss_desc = priv->attempted_bss_desc;746 747	mwifiex_dbg(priv->adapter, INFO, "info: ASSOC_RESP: %s\n",748		    bss_desc->ssid.ssid);749 750	/* Make a copy of current BSSID descriptor */751	memcpy(&priv->curr_bss_params.bss_descriptor,752	       bss_desc, sizeof(struct mwifiex_bssdescriptor));753 754	/* Update curr_bss_params */755	priv->curr_bss_params.bss_descriptor.channel756		= bss_desc->phy_param_set.ds_param_set.current_chan;757 758	priv->curr_bss_params.band = (u8) bss_desc->bss_band;759 760	if (bss_desc->wmm_ie.vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC)761		priv->curr_bss_params.wmm_enabled = true;762	else763		priv->curr_bss_params.wmm_enabled = false;764 765	if ((priv->wmm_required || bss_desc->bcn_ht_cap) &&766	    priv->curr_bss_params.wmm_enabled)767		priv->wmm_enabled = true;768	else769		priv->wmm_enabled = false;770 771	priv->curr_bss_params.wmm_uapsd_enabled = false;772 773	if (priv->wmm_enabled)774		priv->curr_bss_params.wmm_uapsd_enabled775			= ((bss_desc->wmm_ie.qos_info_bitmap &776				IEEE80211_WMM_IE_AP_QOSINFO_UAPSD) ? 1 : 0);777 778	/* Store the bandwidth information from assoc response */779	ie_ptr = cfg80211_find_ie(WLAN_EID_HT_OPERATION, assoc_rsp->ie_buffer,780				  priv->assoc_rsp_size781				  - sizeof(struct ieee_types_assoc_rsp));782	if (ie_ptr) {783		assoc_resp_ht_oper = (struct ieee80211_ht_operation *)(ie_ptr784					+ sizeof(struct ieee_types_header));785		priv->assoc_resp_ht_param = assoc_resp_ht_oper->ht_param;786		priv->ht_param_present = true;787	} else {788		priv->ht_param_present = false;789	}790 791	mwifiex_dbg(priv->adapter, INFO,792		    "info: ASSOC_RESP: curr_pkt_filter is %#x\n",793		    priv->curr_pkt_filter);794	if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)795		priv->wpa_is_gtk_set = false;796 797	if (priv->wmm_enabled) {798		/* Don't re-enable carrier until we get the WMM_GET_STATUS799		   event */800		enable_data = false;801	} else {802		/* Since WMM is not enabled, setup the queues with the803		   defaults */804		mwifiex_wmm_setup_queue_priorities(priv, NULL);805		mwifiex_wmm_setup_ac_downgrade(priv);806	}807 808	if (enable_data)809		mwifiex_dbg(priv->adapter, INFO,810			    "info: post association, re-enabling data flow\n");811 812	/* Reset SNR/NF/RSSI values */813	priv->data_rssi_last = 0;814	priv->data_nf_last = 0;815	priv->data_rssi_avg = 0;816	priv->data_nf_avg = 0;817	priv->bcn_rssi_last = 0;818	priv->bcn_nf_last = 0;819	priv->bcn_rssi_avg = 0;820	priv->bcn_nf_avg = 0;821	priv->rxpd_rate = 0;822	priv->rxpd_htinfo = 0;823 824	mwifiex_save_curr_bcn(priv);825 826	priv->adapter->dbg.num_cmd_assoc_success++;827 828	mwifiex_dbg(priv->adapter, MSG, "assoc: associated with %pM\n",829		    priv->attempted_bss_desc->mac_address);830 831	/* Add the ra_list here for infra mode as there will be only 1 ra832	   always */833	mwifiex_ralist_add(priv,834			   priv->curr_bss_params.bss_descriptor.mac_address);835 836	if (!netif_carrier_ok(priv->netdev))837		netif_carrier_on(priv->netdev);838	mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);839 840	if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)841		priv->scan_block = true;842	else843		priv->port_open = true;844 845done:846	/* Need to indicate IOCTL complete */847	if (adapter->curr_cmd->wait_q_enabled) {848		if (ret)849			adapter->cmd_wait_q.status = -1;850		else851			adapter->cmd_wait_q.status = 0;852	}853 854	return ret;855}856 857/*858 * This function prepares command for ad-hoc start.859 *860 * Driver will fill up SSID, BSS mode, IBSS parameters, physical861 * parameters, probe delay, and capability information. Firmware862 * will fill up beacon period, basic rates and operational rates.863 *864 * In addition, the following TLVs are added -865 *      - Channel TLV866 *      - Vendor specific IE867 *      - WPA/WPA2 IE868 *      - HT Capabilities IE869 *      - HT Information IE870 *871 * Preparation also includes -872 *      - Setting command ID and proper size873 *      - Ensuring correct endian-ness874 */875int876mwifiex_cmd_802_11_ad_hoc_start(struct mwifiex_private *priv,877				struct host_cmd_ds_command *cmd,878				struct cfg80211_ssid *req_ssid)879{880	int rsn_ie_len = 0;881	struct mwifiex_adapter *adapter = priv->adapter;882	struct host_cmd_ds_802_11_ad_hoc_start *adhoc_start =883		&cmd->params.adhoc_start;884	struct mwifiex_bssdescriptor *bss_desc;885	u32 cmd_append_size = 0;886	u32 i;887	u16 tmp_cap;888	struct mwifiex_ie_types_chan_list_param_set *chan_tlv;889	u8 radio_type;890 891	struct mwifiex_ie_types_htcap *ht_cap;892	struct mwifiex_ie_types_htinfo *ht_info;893	u8 *pos = (u8 *) adhoc_start +894			sizeof(struct host_cmd_ds_802_11_ad_hoc_start);895 896	if (!adapter)897		return -1;898 899	cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_START);900 901	bss_desc = &priv->curr_bss_params.bss_descriptor;902	priv->attempted_bss_desc = bss_desc;903 904	/*905	 * Fill in the parameters for 2 data structures:906	 *   1. struct host_cmd_ds_802_11_ad_hoc_start command907	 *   2. bss_desc908	 * Driver will fill up SSID, bss_mode,IBSS param, Physical Param,909	 * probe delay, and Cap info.910	 * Firmware will fill up beacon period, Basic rates911	 * and operational rates.912	 */913 914	memset(adhoc_start->ssid, 0, IEEE80211_MAX_SSID_LEN);915 916	if (req_ssid->ssid_len > IEEE80211_MAX_SSID_LEN)917		req_ssid->ssid_len = IEEE80211_MAX_SSID_LEN;918	memcpy(adhoc_start->ssid, req_ssid->ssid, req_ssid->ssid_len);919 920	mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: SSID = %s\n",921		    adhoc_start->ssid);922 923	memset(bss_desc->ssid.ssid, 0, IEEE80211_MAX_SSID_LEN);924	memcpy(bss_desc->ssid.ssid, req_ssid->ssid, req_ssid->ssid_len);925 926	bss_desc->ssid.ssid_len = req_ssid->ssid_len;927 928	/* Set the BSS mode */929	adhoc_start->bss_mode = HostCmd_BSS_MODE_IBSS;930	bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;931	adhoc_start->beacon_period = cpu_to_le16(priv->beacon_period);932	bss_desc->beacon_period = priv->beacon_period;933 934	/* Set Physical param set */935/* Parameter IE Id */936#define DS_PARA_IE_ID   3937/* Parameter IE length */938#define DS_PARA_IE_LEN  1939 940	adhoc_start->phy_param_set.ds_param_set.element_id = DS_PARA_IE_ID;941	adhoc_start->phy_param_set.ds_param_set.len = DS_PARA_IE_LEN;942 943	if (!mwifiex_get_cfp(priv, adapter->adhoc_start_band,944			     (u16) priv->adhoc_channel, 0)) {945		struct mwifiex_chan_freq_power *cfp;946		cfp = mwifiex_get_cfp(priv, adapter->adhoc_start_band,947				      FIRST_VALID_CHANNEL, 0);948		if (cfp)949			priv->adhoc_channel = (u8) cfp->channel;950	}951 952	if (!priv->adhoc_channel) {953		mwifiex_dbg(adapter, ERROR,954			    "ADHOC_S_CMD: adhoc_channel cannot be 0\n");955		return -1;956	}957 958	mwifiex_dbg(adapter, INFO,959		    "info: ADHOC_S_CMD: creating ADHOC on channel %d\n",960		    priv->adhoc_channel);961 962	priv->curr_bss_params.bss_descriptor.channel = priv->adhoc_channel;963	priv->curr_bss_params.band = adapter->adhoc_start_band;964 965	bss_desc->channel = priv->adhoc_channel;966	adhoc_start->phy_param_set.ds_param_set.current_chan =967		priv->adhoc_channel;968 969	memcpy(&bss_desc->phy_param_set, &adhoc_start->phy_param_set,970	       sizeof(union ieee_types_phy_param_set));971 972	/* Set IBSS param set */973/* IBSS parameter IE Id */974#define IBSS_PARA_IE_ID   6975/* IBSS parameter IE length */976#define IBSS_PARA_IE_LEN  2977 978	adhoc_start->ss_param_set.ibss_param_set.element_id = IBSS_PARA_IE_ID;979	adhoc_start->ss_param_set.ibss_param_set.len = IBSS_PARA_IE_LEN;980	adhoc_start->ss_param_set.ibss_param_set.atim_window981					= cpu_to_le16(priv->atim_window);982	memcpy(&bss_desc->ss_param_set, &adhoc_start->ss_param_set,983	       sizeof(union ieee_types_ss_param_set));984 985	/* Set Capability info */986	bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_IBSS;987	tmp_cap = WLAN_CAPABILITY_IBSS;988 989	/* Set up privacy in bss_desc */990	if (priv->sec_info.encryption_mode) {991		/* Ad-Hoc capability privacy on */992		mwifiex_dbg(adapter, INFO,993			    "info: ADHOC_S_CMD: wep_status set privacy to WEP\n");994		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;995		tmp_cap |= WLAN_CAPABILITY_PRIVACY;996	} else {997		mwifiex_dbg(adapter, INFO,998			    "info: ADHOC_S_CMD: wep_status NOT set,\t"999			    "setting privacy to ACCEPT ALL\n");1000		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;1001	}1002 1003	memset(adhoc_start->data_rate, 0, sizeof(adhoc_start->data_rate));1004	mwifiex_get_active_data_rates(priv, adhoc_start->data_rate);1005	if ((adapter->adhoc_start_band & BAND_G) &&1006	    (priv->curr_pkt_filter & HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON)) {1007		if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,1008				     HostCmd_ACT_GEN_SET, 0,1009				     &priv->curr_pkt_filter, false)) {1010			mwifiex_dbg(adapter, ERROR,1011				    "ADHOC_S_CMD: G Protection config failed\n");1012			return -1;1013		}1014	}1015	/* Find the last non zero */1016	for (i = 0; i < sizeof(adhoc_start->data_rate); i++)1017		if (!adhoc_start->data_rate[i])1018			break;1019 1020	priv->curr_bss_params.num_of_rates = i;1021 1022	/* Copy the ad-hoc creating rates into Current BSS rate structure */1023	memcpy(&priv->curr_bss_params.data_rates,1024	       &adhoc_start->data_rate, priv->curr_bss_params.num_of_rates);1025 1026	mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: rates=%4ph\n",1027		    adhoc_start->data_rate);1028 1029	mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: AD-HOC Start command is ready\n");1030 1031	if (IS_SUPPORT_MULTI_BANDS(adapter)) {1032		/* Append a channel TLV */1033		chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;1034		chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);1035		chan_tlv->header.len =1036			cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));1037 1038		memset(chan_tlv->chan_scan_param, 0x00,1039		       sizeof(struct mwifiex_chan_scan_param_set));1040		chan_tlv->chan_scan_param[0].chan_number =1041			(u8) priv->curr_bss_params.bss_descriptor.channel;1042 1043		mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: TLV Chan = %d\n",1044			    chan_tlv->chan_scan_param[0].chan_number);1045 1046		chan_tlv->chan_scan_param[0].radio_type1047		       = mwifiex_band_to_radio_type(priv->curr_bss_params.band);1048		if (adapter->adhoc_start_band & BAND_GN ||1049		    adapter->adhoc_start_band & BAND_AN) {1050			if (adapter->sec_chan_offset ==1051					    IEEE80211_HT_PARAM_CHA_SEC_ABOVE)1052				chan_tlv->chan_scan_param[0].radio_type |=1053					(IEEE80211_HT_PARAM_CHA_SEC_ABOVE << 4);1054			else if (adapter->sec_chan_offset ==1055					    IEEE80211_HT_PARAM_CHA_SEC_BELOW)1056				chan_tlv->chan_scan_param[0].radio_type |=1057					(IEEE80211_HT_PARAM_CHA_SEC_BELOW << 4);1058		}1059		mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: TLV Band = %d\n",1060			    chan_tlv->chan_scan_param[0].radio_type);1061		pos += sizeof(chan_tlv->header) +1062			sizeof(struct mwifiex_chan_scan_param_set);1063		cmd_append_size +=1064			sizeof(chan_tlv->header) +1065			sizeof(struct mwifiex_chan_scan_param_set);1066	}1067 1068	/* Append vendor specific IE TLV */1069	cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,1070				MWIFIEX_VSIE_MASK_ADHOC, &pos);1071 1072	if (priv->sec_info.wpa_enabled) {1073		rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);1074		if (rsn_ie_len == -1)1075			return -1;1076		cmd_append_size += rsn_ie_len;1077	}1078 1079	if (adapter->adhoc_11n_enabled) {1080		/* Fill HT CAPABILITY */1081		ht_cap = (struct mwifiex_ie_types_htcap *) pos;1082		memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));1083		ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);1084		ht_cap->header.len =1085		       cpu_to_le16(sizeof(struct ieee80211_ht_cap));1086		radio_type = mwifiex_band_to_radio_type(1087					priv->adapter->config_bands);1088		mwifiex_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);1089 1090		if (adapter->sec_chan_offset ==1091					IEEE80211_HT_PARAM_CHA_SEC_NONE) {1092			u16 tmp_ht_cap;1093 1094			tmp_ht_cap = le16_to_cpu(ht_cap->ht_cap.cap_info);1095			tmp_ht_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;1096			tmp_ht_cap &= ~IEEE80211_HT_CAP_SGI_40;1097			ht_cap->ht_cap.cap_info = cpu_to_le16(tmp_ht_cap);1098		}1099 1100		pos += sizeof(struct mwifiex_ie_types_htcap);1101		cmd_append_size += sizeof(struct mwifiex_ie_types_htcap);1102 1103		/* Fill HT INFORMATION */1104		ht_info = (struct mwifiex_ie_types_htinfo *) pos;1105		memset(ht_info, 0, sizeof(struct mwifiex_ie_types_htinfo));1106		ht_info->header.type = cpu_to_le16(WLAN_EID_HT_OPERATION);1107		ht_info->header.len =1108			cpu_to_le16(sizeof(struct ieee80211_ht_operation));1109 1110		ht_info->ht_oper.primary_chan =1111			(u8) priv->curr_bss_params.bss_descriptor.channel;1112		if (adapter->sec_chan_offset) {1113			ht_info->ht_oper.ht_param = adapter->sec_chan_offset;1114			ht_info->ht_oper.ht_param |=1115					IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;1116		}1117		ht_info->ht_oper.operation_mode =1118		     cpu_to_le16(IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);1119		ht_info->ht_oper.basic_set[0] = 0xff;1120		pos += sizeof(struct mwifiex_ie_types_htinfo);1121		cmd_append_size +=1122				sizeof(struct mwifiex_ie_types_htinfo);1123	}1124 1125	cmd->size =1126		cpu_to_le16((u16)(sizeof(struct host_cmd_ds_802_11_ad_hoc_start)1127				  + S_DS_GEN + cmd_append_size));1128 1129	if (adapter->adhoc_start_band == BAND_B)1130		tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;1131	else1132		tmp_cap |= WLAN_CAPABILITY_SHORT_SLOT_TIME;1133 1134	adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);1135 1136	return 0;1137}1138 1139/*1140 * This function prepares command for ad-hoc join.1141 *1142 * Most of the parameters are set up by copying from the target BSS descriptor1143 * from the scan response.1144 *1145 * In addition, the following TLVs are added -1146 *      - Channel TLV1147 *      - Vendor specific IE1148 *      - WPA/WPA2 IE1149 *      - 11n IE1150 *1151 * Preparation also includes -1152 *      - Setting command ID and proper size1153 *      - Ensuring correct endian-ness1154 */1155int1156mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,1157			       struct host_cmd_ds_command *cmd,1158			       struct mwifiex_bssdescriptor *bss_desc)1159{1160	int rsn_ie_len = 0;1161	struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =1162		&cmd->params.adhoc_join;1163	struct mwifiex_ie_types_chan_list_param_set *chan_tlv;1164	u32 cmd_append_size = 0;1165	u16 tmp_cap;1166	u32 i, rates_size = 0;1167	u16 curr_pkt_filter;1168	u8 *pos =1169		(u8 *) adhoc_join +1170		sizeof(struct host_cmd_ds_802_11_ad_hoc_join);1171 1172/* Use G protection */1173#define USE_G_PROTECTION        0x021174	if (bss_desc->erp_flags & USE_G_PROTECTION) {1175		curr_pkt_filter =1176			priv->1177			curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;1178 1179		if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,1180				     HostCmd_ACT_GEN_SET, 0,1181				     &curr_pkt_filter, false)) {1182			mwifiex_dbg(priv->adapter, ERROR,1183				    "ADHOC_J_CMD: G Protection config failed\n");1184			return -1;1185		}1186	}1187 1188	priv->attempted_bss_desc = bss_desc;1189 1190	cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);1191 1192	adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;1193 1194	adhoc_join->bss_descriptor.beacon_period1195		= cpu_to_le16(bss_desc->beacon_period);1196 1197	memcpy(&adhoc_join->bss_descriptor.bssid,1198	       &bss_desc->mac_address, ETH_ALEN);1199 1200	memcpy(&adhoc_join->bss_descriptor.ssid,1201	       &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);1202 1203	memcpy(&adhoc_join->bss_descriptor.phy_param_set,1204	       &bss_desc->phy_param_set,1205	       sizeof(union ieee_types_phy_param_set));1206 1207	memcpy(&adhoc_join->bss_descriptor.ss_param_set,1208	       &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));1209 1210	tmp_cap = bss_desc->cap_info_bitmap;1211 1212	tmp_cap &= CAPINFO_MASK;1213 1214	mwifiex_dbg(priv->adapter, INFO,1215		    "info: ADHOC_J_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",1216		    tmp_cap, CAPINFO_MASK);1217 1218	/* Information on BSSID descriptor passed to FW */1219	mwifiex_dbg(priv->adapter, INFO,1220		    "info: ADHOC_J_CMD: BSSID=%pM, SSID='%s'\n",1221		    adhoc_join->bss_descriptor.bssid,1222		    adhoc_join->bss_descriptor.ssid);1223 1224	for (i = 0; i < MWIFIEX_SUPPORTED_RATES &&1225		    bss_desc->supported_rates[i]; i++)1226		;1227	rates_size = i;1228 1229	/* Copy Data Rates from the Rates recorded in scan response */1230	memset(adhoc_join->bss_descriptor.data_rates, 0,1231	       sizeof(adhoc_join->bss_descriptor.data_rates));1232	memcpy(adhoc_join->bss_descriptor.data_rates,1233	       bss_desc->supported_rates, rates_size);1234 1235	/* Copy the adhoc join rates into Current BSS state structure */1236	priv->curr_bss_params.num_of_rates = rates_size;1237	memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,1238	       rates_size);1239 1240	/* Copy the channel information */1241	priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;1242	priv->curr_bss_params.band = (u8) bss_desc->bss_band;1243 1244	if (priv->sec_info.wep_enabled || priv->sec_info.wpa_enabled)1245		tmp_cap |= WLAN_CAPABILITY_PRIVACY;1246 1247	if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {1248		/* Append a channel TLV */1249		chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;1250		chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);1251		chan_tlv->header.len =1252			cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));1253 1254		memset(chan_tlv->chan_scan_param, 0x00,1255		       sizeof(struct mwifiex_chan_scan_param_set));1256		chan_tlv->chan_scan_param[0].chan_number =1257			(bss_desc->phy_param_set.ds_param_set.current_chan);1258		mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_J_CMD: TLV Chan=%d\n",1259			    chan_tlv->chan_scan_param[0].chan_number);1260 1261		chan_tlv->chan_scan_param[0].radio_type =1262			mwifiex_band_to_radio_type((u8) bss_desc->bss_band);1263 1264		mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_J_CMD: TLV Band=%d\n",1265			    chan_tlv->chan_scan_param[0].radio_type);1266		pos += sizeof(chan_tlv->header) +1267				sizeof(struct mwifiex_chan_scan_param_set);1268		cmd_append_size += sizeof(chan_tlv->header) +1269				sizeof(struct mwifiex_chan_scan_param_set);1270	}1271 1272	if (priv->sec_info.wpa_enabled)1273		rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);1274	if (rsn_ie_len == -1)1275		return -1;1276	cmd_append_size += rsn_ie_len;1277 1278	if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))1279		cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,1280			bss_desc, &pos);1281 1282	/* Append vendor specific IE TLV */1283	cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,1284			MWIFIEX_VSIE_MASK_ADHOC, &pos);1285 1286	cmd->size = cpu_to_le161287		((u16) (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)1288			+ S_DS_GEN + cmd_append_size));1289 1290	adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);1291 1292	return 0;1293}1294 1295/*1296 * This function handles the command response of ad-hoc start and1297 * ad-hoc join.1298 *1299 * The function generates a device-connected event to notify1300 * the applications, in case of successful ad-hoc start/join, and1301 * saves the beacon buffer.1302 */1303int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,1304			      struct host_cmd_ds_command *resp)1305{1306	int ret = 0;1307	struct mwifiex_adapter *adapter = priv->adapter;1308	struct host_cmd_ds_802_11_ad_hoc_start_result *start_result =1309				&resp->params.start_result;1310	struct host_cmd_ds_802_11_ad_hoc_join_result *join_result =1311				&resp->params.join_result;1312	struct mwifiex_bssdescriptor *bss_desc;1313	u16 cmd = le16_to_cpu(resp->command);1314	u8 result;1315 1316	if (!priv->attempted_bss_desc) {1317		mwifiex_dbg(priv->adapter, ERROR,1318			    "ADHOC_RESP: failed, association terminated by host\n");1319		goto done;1320	}1321 1322	if (cmd == HostCmd_CMD_802_11_AD_HOC_START)1323		result = start_result->result;1324	else1325		result = join_result->result;1326 1327	bss_desc = priv->attempted_bss_desc;1328 1329	/* Join result code 0 --> SUCCESS */1330	if (result) {1331		mwifiex_dbg(priv->adapter, ERROR, "ADHOC_RESP: failed\n");1332		if (priv->media_connected)1333			mwifiex_reset_connect_state(priv, result, true);1334 1335		memset(&priv->curr_bss_params.bss_descriptor,1336		       0x00, sizeof(struct mwifiex_bssdescriptor));1337 1338		ret = -1;1339		goto done;1340	}1341 1342	/* Send a Media Connected event, according to the Spec */1343	priv->media_connected = true;1344 1345	if (le16_to_cpu(resp->command) == HostCmd_CMD_802_11_AD_HOC_START) {1346		mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_S_RESP %s\n",1347			    bss_desc->ssid.ssid);1348 1349		/* Update the created network descriptor with the new BSSID */1350		memcpy(bss_desc->mac_address,1351		       start_result->bssid, ETH_ALEN);1352 1353		priv->adhoc_state = ADHOC_STARTED;1354	} else {1355		/*1356		 * Now the join cmd should be successful.1357		 * If BSSID has changed use SSID to compare instead of BSSID1358		 */1359		mwifiex_dbg(priv->adapter, INFO,1360			    "info: ADHOC_J_RESP %s\n",1361			    bss_desc->ssid.ssid);1362 1363		/*1364		 * Make a copy of current BSSID descriptor, only needed for1365		 * join since the current descriptor is already being used1366		 * for adhoc start1367		 */1368		memcpy(&priv->curr_bss_params.bss_descriptor,1369		       bss_desc, sizeof(struct mwifiex_bssdescriptor));1370 1371		priv->adhoc_state = ADHOC_JOINED;1372	}1373 1374	mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_RESP: channel = %d\n",1375		    priv->adhoc_channel);1376	mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_RESP: BSSID = %pM\n",1377		    priv->curr_bss_params.bss_descriptor.mac_address);1378 1379	if (!netif_carrier_ok(priv->netdev))1380		netif_carrier_on(priv->netdev);1381	mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);1382 1383	mwifiex_save_curr_bcn(priv);1384 1385done:1386	/* Need to indicate IOCTL complete */1387	if (adapter->curr_cmd->wait_q_enabled) {1388		if (ret)1389			adapter->cmd_wait_q.status = -1;1390		else1391			adapter->cmd_wait_q.status = 0;1392 1393	}1394 1395	return ret;1396}1397 1398/*1399 * This function associates to a specific BSS discovered in a scan.1400 *1401 * It clears any past association response stored for application1402 * retrieval and calls the command preparation routine to send the1403 * command to firmware.1404 */1405int mwifiex_associate(struct mwifiex_private *priv,1406		      struct mwifiex_bssdescriptor *bss_desc)1407{1408	/* Return error if the adapter is not STA role or table entry1409	 * is not marked as infra.1410	 */1411	if ((GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) ||1412	    (bss_desc->bss_mode != NL80211_IFTYPE_STATION))1413		return -1;1414 1415	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&1416	    !bss_desc->disable_11n && !bss_desc->disable_11ac &&1417	    priv->adapter->config_bands & BAND_AAC)1418		mwifiex_set_11ac_ba_params(priv);1419	else1420		mwifiex_set_ba_params(priv);1421 1422	/* Clear any past association response stored for application1423	   retrieval */1424	priv->assoc_rsp_size = 0;1425 1426	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_ASSOCIATE,1427				HostCmd_ACT_GEN_SET, 0, bss_desc, true);1428}1429 1430/*1431 * This function starts an ad-hoc network.1432 *1433 * It calls the command preparation routine to send the command to firmware.1434 */1435int1436mwifiex_adhoc_start(struct mwifiex_private *priv,1437		    struct cfg80211_ssid *adhoc_ssid)1438{1439	mwifiex_dbg(priv->adapter, INFO, "info: Adhoc Channel = %d\n",1440		    priv->adhoc_channel);1441	mwifiex_dbg(priv->adapter, INFO, "info: curr_bss_params.channel = %d\n",1442		    priv->curr_bss_params.bss_descriptor.channel);1443	mwifiex_dbg(priv->adapter, INFO, "info: curr_bss_params.band = %d\n",1444		    priv->curr_bss_params.band);1445 1446	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&1447	    priv->adapter->config_bands & BAND_AAC)1448		mwifiex_set_11ac_ba_params(priv);1449	else1450		mwifiex_set_ba_params(priv);1451 1452	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_START,1453				HostCmd_ACT_GEN_SET, 0, adhoc_ssid, true);1454}1455 1456/*1457 * This function joins an ad-hoc network found in a previous scan.1458 *1459 * It calls the command preparation routine to send the command to firmware,1460 * if already not connected to the requested SSID.1461 */1462int mwifiex_adhoc_join(struct mwifiex_private *priv,1463		       struct mwifiex_bssdescriptor *bss_desc)1464{1465	mwifiex_dbg(priv->adapter, INFO,1466		    "info: adhoc join: curr_bss ssid =%s\n",1467		    priv->curr_bss_params.bss_descriptor.ssid.ssid);1468	mwifiex_dbg(priv->adapter, INFO,1469		    "info: adhoc join: curr_bss ssid_len =%u\n",1470		    priv->curr_bss_params.bss_descriptor.ssid.ssid_len);1471	mwifiex_dbg(priv->adapter, INFO, "info: adhoc join: ssid =%s\n",1472		    bss_desc->ssid.ssid);1473	mwifiex_dbg(priv->adapter, INFO, "info: adhoc join: ssid_len =%u\n",1474		    bss_desc->ssid.ssid_len);1475 1476	/* Check if the requested SSID is already joined */1477	if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&1478	    cfg80211_ssid_eq(&bss_desc->ssid,1479			     &priv->curr_bss_params.bss_descriptor.ssid) &&1480	    (priv->curr_bss_params.bss_descriptor.bss_mode ==1481							NL80211_IFTYPE_ADHOC)) {1482		mwifiex_dbg(priv->adapter, INFO,1483			    "info: ADHOC_J_CMD: new ad-hoc SSID\t"1484			    "is the same as current; not attempting to re-join\n");1485		return -1;1486	}1487 1488	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&1489	    !bss_desc->disable_11n && !bss_desc->disable_11ac &&1490	    priv->adapter->config_bands & BAND_AAC)1491		mwifiex_set_11ac_ba_params(priv);1492	else1493		mwifiex_set_ba_params(priv);1494 1495	mwifiex_dbg(priv->adapter, INFO,1496		    "info: curr_bss_params.channel = %d\n",1497		    priv->curr_bss_params.bss_descriptor.channel);1498	mwifiex_dbg(priv->adapter, INFO,1499		    "info: curr_bss_params.band = %c\n",1500		    priv->curr_bss_params.band);1501 1502	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,1503				HostCmd_ACT_GEN_SET, 0, bss_desc, true);1504}1505 1506/*1507 * This function deauthenticates/disconnects from infra network by sending1508 * deauthentication request.1509 */1510static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv, u8 *mac)1511{1512	u8 mac_address[ETH_ALEN];1513	int ret;1514 1515	if (!mac || is_zero_ether_addr(mac))1516		memcpy(mac_address,1517		       priv->curr_bss_params.bss_descriptor.mac_address,1518		       ETH_ALEN);1519	else1520		memcpy(mac_address, mac, ETH_ALEN);1521 1522	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,1523			       HostCmd_ACT_GEN_SET, 0, mac_address, true);1524 1525	return ret;1526}1527 1528/*1529 * This function deauthenticates/disconnects from a BSS.1530 *1531 * In case of infra made, it sends deauthentication request, and1532 * in case of ad-hoc mode, a stop network request is sent to the firmware.1533 * In AP mode, a command to stop bss is sent to firmware.1534 */1535int mwifiex_deauthenticate(struct mwifiex_private *priv, u8 *mac)1536{1537	int ret = 0;1538 1539	if (!priv->media_connected)1540		return 0;1541 1542	if (priv->adapter->host_mlme_enabled) {1543		priv->auth_flag = 0;1544		priv->auth_alg = WLAN_AUTH_NONE;1545		priv->host_mlme_reg = false;1546		priv->mgmt_frame_mask = 0;1547		if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,1548				     HostCmd_ACT_GEN_SET, 0,1549				     &priv->mgmt_frame_mask, false)) {1550			mwifiex_dbg(priv->adapter, ERROR,1551				    "could not unregister mgmt frame rx\n");1552			return -1;1553		}1554	}1555 1556	switch (priv->bss_mode) {1557	case NL80211_IFTYPE_STATION:1558	case NL80211_IFTYPE_P2P_CLIENT:1559		ret = mwifiex_deauthenticate_infra(priv, mac);1560		if (ret)1561			cfg80211_disconnected(priv->netdev, 0, NULL, 0,1562					      true, GFP_KERNEL);1563		break;1564	case NL80211_IFTYPE_ADHOC:1565		return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_STOP,1566					HostCmd_ACT_GEN_SET, 0, NULL, true);1567	case NL80211_IFTYPE_AP:1568		return mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,1569					HostCmd_ACT_GEN_SET, 0, NULL, true);1570	default:1571		break;1572	}1573 1574	return ret;1575}1576 1577/* This function deauthenticates/disconnects from all BSS. */1578void mwifiex_deauthenticate_all(struct mwifiex_adapter *adapter)1579{1580	struct mwifiex_private *priv;1581	int i;1582 1583	for (i = 0; i < adapter->priv_num; i++) {1584		priv = adapter->priv[i];1585		mwifiex_deauthenticate(priv, NULL);1586	}1587}1588EXPORT_SYMBOL_GPL(mwifiex_deauthenticate_all);1589 1590/*1591 * This function converts band to radio type used in channel TLV.1592 */1593u81594mwifiex_band_to_radio_type(u8 band)1595{1596	switch (band) {1597	case BAND_A:1598	case BAND_AN:1599	case BAND_A | BAND_AN:1600	case BAND_A | BAND_AN | BAND_AAC:1601		return HostCmd_SCAN_RADIO_TYPE_A;1602	case BAND_B:1603	case BAND_G:1604	case BAND_B | BAND_G:1605	default:1606		return HostCmd_SCAN_RADIO_TYPE_BG;1607	}1608}1609