xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * NXP Wireless LAN device driver: functions for station ioctl
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright 2011-2020 NXP
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This software file (the "File") is distributed by NXP
7*4882a593Smuzhiyun  * under the terms of the GNU General Public License Version 2, June 1991
8*4882a593Smuzhiyun  * (the "License").  You may use, redistribute and/or modify this File in
9*4882a593Smuzhiyun  * accordance with the terms and conditions of the License, a copy of which
10*4882a593Smuzhiyun  * is available by writing to the Free Software Foundation, Inc.,
11*4882a593Smuzhiyun  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12*4882a593Smuzhiyun  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16*4882a593Smuzhiyun  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17*4882a593Smuzhiyun  * this warranty disclaimer.
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "decl.h"
21*4882a593Smuzhiyun #include "ioctl.h"
22*4882a593Smuzhiyun #include "util.h"
23*4882a593Smuzhiyun #include "fw.h"
24*4882a593Smuzhiyun #include "main.h"
25*4882a593Smuzhiyun #include "wmm.h"
26*4882a593Smuzhiyun #include "11n.h"
27*4882a593Smuzhiyun #include "cfg80211.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static int disconnect_on_suspend;
30*4882a593Smuzhiyun module_param(disconnect_on_suspend, int, 0644);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * Copies the multicast address list from device to driver.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * This function does not validate the destination memory for
36*4882a593Smuzhiyun  * size, and the calling function must ensure enough memory is
37*4882a593Smuzhiyun  * available.
38*4882a593Smuzhiyun  */
mwifiex_copy_mcast_addr(struct mwifiex_multicast_list * mlist,struct net_device * dev)39*4882a593Smuzhiyun int mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
40*4882a593Smuzhiyun 			    struct net_device *dev)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	int i = 0;
43*4882a593Smuzhiyun 	struct netdev_hw_addr *ha;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	netdev_for_each_mc_addr(ha, dev)
46*4882a593Smuzhiyun 		memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	return i;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun  * Wait queue completion handler.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * This function waits on a cmd wait queue. It also cancels the pending
55*4882a593Smuzhiyun  * request after waking up, in case of errors.
56*4882a593Smuzhiyun  */
mwifiex_wait_queue_complete(struct mwifiex_adapter * adapter,struct cmd_ctrl_node * cmd_queued)57*4882a593Smuzhiyun int mwifiex_wait_queue_complete(struct mwifiex_adapter *adapter,
58*4882a593Smuzhiyun 				struct cmd_ctrl_node *cmd_queued)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	int status;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/* Wait for completion */
63*4882a593Smuzhiyun 	status = wait_event_interruptible_timeout(adapter->cmd_wait_q.wait,
64*4882a593Smuzhiyun 						  *(cmd_queued->condition),
65*4882a593Smuzhiyun 						  (12 * HZ));
66*4882a593Smuzhiyun 	if (status <= 0) {
67*4882a593Smuzhiyun 		if (status == 0)
68*4882a593Smuzhiyun 			status = -ETIMEDOUT;
69*4882a593Smuzhiyun 		mwifiex_dbg(adapter, ERROR, "cmd_wait_q terminated: %d\n",
70*4882a593Smuzhiyun 			    status);
71*4882a593Smuzhiyun 		mwifiex_cancel_all_pending_cmd(adapter);
72*4882a593Smuzhiyun 		return status;
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	status = adapter->cmd_wait_q.status;
76*4882a593Smuzhiyun 	adapter->cmd_wait_q.status = 0;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	return status;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * This function prepares the correct firmware command and
83*4882a593Smuzhiyun  * issues it to set the multicast list.
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * This function can be used to enable promiscuous mode, or enable all
86*4882a593Smuzhiyun  * multicast packets, or to enable selective multicast.
87*4882a593Smuzhiyun  */
mwifiex_request_set_multicast_list(struct mwifiex_private * priv,struct mwifiex_multicast_list * mcast_list)88*4882a593Smuzhiyun int mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
89*4882a593Smuzhiyun 				struct mwifiex_multicast_list *mcast_list)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	int ret = 0;
92*4882a593Smuzhiyun 	u16 old_pkt_filter;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	old_pkt_filter = priv->curr_pkt_filter;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
97*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, INFO,
98*4882a593Smuzhiyun 			    "info: Enable Promiscuous mode\n");
99*4882a593Smuzhiyun 		priv->curr_pkt_filter |= HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
100*4882a593Smuzhiyun 		priv->curr_pkt_filter &=
101*4882a593Smuzhiyun 			~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
102*4882a593Smuzhiyun 	} else {
103*4882a593Smuzhiyun 		/* Multicast */
104*4882a593Smuzhiyun 		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
105*4882a593Smuzhiyun 		if (mcast_list->mode == MWIFIEX_ALL_MULTI_MODE) {
106*4882a593Smuzhiyun 			mwifiex_dbg(priv->adapter, INFO,
107*4882a593Smuzhiyun 				    "info: Enabling All Multicast!\n");
108*4882a593Smuzhiyun 			priv->curr_pkt_filter |=
109*4882a593Smuzhiyun 				HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
110*4882a593Smuzhiyun 		} else {
111*4882a593Smuzhiyun 			priv->curr_pkt_filter &=
112*4882a593Smuzhiyun 				~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
113*4882a593Smuzhiyun 			mwifiex_dbg(priv->adapter, INFO,
114*4882a593Smuzhiyun 				    "info: Set multicast list=%d\n",
115*4882a593Smuzhiyun 				    mcast_list->num_multicast_addr);
116*4882a593Smuzhiyun 			/* Send multicast addresses to firmware */
117*4882a593Smuzhiyun 			ret = mwifiex_send_cmd(priv,
118*4882a593Smuzhiyun 					       HostCmd_CMD_MAC_MULTICAST_ADR,
119*4882a593Smuzhiyun 					       HostCmd_ACT_GEN_SET, 0,
120*4882a593Smuzhiyun 					       mcast_list, false);
121*4882a593Smuzhiyun 		}
122*4882a593Smuzhiyun 	}
123*4882a593Smuzhiyun 	mwifiex_dbg(priv->adapter, INFO,
124*4882a593Smuzhiyun 		    "info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
125*4882a593Smuzhiyun 		    old_pkt_filter, priv->curr_pkt_filter);
126*4882a593Smuzhiyun 	if (old_pkt_filter != priv->curr_pkt_filter) {
127*4882a593Smuzhiyun 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
128*4882a593Smuzhiyun 				       HostCmd_ACT_GEN_SET,
129*4882a593Smuzhiyun 				       0, &priv->curr_pkt_filter, false);
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	return ret;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /*
136*4882a593Smuzhiyun  * This function fills bss descriptor structure using provided
137*4882a593Smuzhiyun  * information.
138*4882a593Smuzhiyun  * beacon_ie buffer is allocated in this function. It is caller's
139*4882a593Smuzhiyun  * responsibility to free the memory.
140*4882a593Smuzhiyun  */
mwifiex_fill_new_bss_desc(struct mwifiex_private * priv,struct cfg80211_bss * bss,struct mwifiex_bssdescriptor * bss_desc)141*4882a593Smuzhiyun int mwifiex_fill_new_bss_desc(struct mwifiex_private *priv,
142*4882a593Smuzhiyun 			      struct cfg80211_bss *bss,
143*4882a593Smuzhiyun 			      struct mwifiex_bssdescriptor *bss_desc)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	u8 *beacon_ie;
146*4882a593Smuzhiyun 	size_t beacon_ie_len;
147*4882a593Smuzhiyun 	struct mwifiex_bss_priv *bss_priv = (void *)bss->priv;
148*4882a593Smuzhiyun 	const struct cfg80211_bss_ies *ies;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	rcu_read_lock();
151*4882a593Smuzhiyun 	ies = rcu_dereference(bss->ies);
152*4882a593Smuzhiyun 	beacon_ie = kmemdup(ies->data, ies->len, GFP_ATOMIC);
153*4882a593Smuzhiyun 	beacon_ie_len = ies->len;
154*4882a593Smuzhiyun 	bss_desc->timestamp = ies->tsf;
155*4882a593Smuzhiyun 	rcu_read_unlock();
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (!beacon_ie) {
158*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, ERROR,
159*4882a593Smuzhiyun 			    " failed to alloc beacon_ie\n");
160*4882a593Smuzhiyun 		return -ENOMEM;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	memcpy(bss_desc->mac_address, bss->bssid, ETH_ALEN);
164*4882a593Smuzhiyun 	bss_desc->rssi = bss->signal;
165*4882a593Smuzhiyun 	/* The caller of this function will free beacon_ie */
166*4882a593Smuzhiyun 	bss_desc->beacon_buf = beacon_ie;
167*4882a593Smuzhiyun 	bss_desc->beacon_buf_size = beacon_ie_len;
168*4882a593Smuzhiyun 	bss_desc->beacon_period = bss->beacon_interval;
169*4882a593Smuzhiyun 	bss_desc->cap_info_bitmap = bss->capability;
170*4882a593Smuzhiyun 	bss_desc->bss_band = bss_priv->band;
171*4882a593Smuzhiyun 	bss_desc->fw_tsf = bss_priv->fw_tsf;
172*4882a593Smuzhiyun 	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_PRIVACY) {
173*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, INFO,
174*4882a593Smuzhiyun 			    "info: InterpretIE: AP WEP enabled\n");
175*4882a593Smuzhiyun 		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
176*4882a593Smuzhiyun 	} else {
177*4882a593Smuzhiyun 		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
178*4882a593Smuzhiyun 	}
179*4882a593Smuzhiyun 	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_IBSS)
180*4882a593Smuzhiyun 		bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
181*4882a593Smuzhiyun 	else
182*4882a593Smuzhiyun 		bss_desc->bss_mode = NL80211_IFTYPE_STATION;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	/* Disable 11ac by default. Enable it only where there
185*4882a593Smuzhiyun 	 * exist VHT_CAP IE in AP beacon
186*4882a593Smuzhiyun 	 */
187*4882a593Smuzhiyun 	bss_desc->disable_11ac = true;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_SPECTRUM_MGMT)
190*4882a593Smuzhiyun 		bss_desc->sensed_11h = true;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	return mwifiex_update_bss_desc_with_ie(priv->adapter, bss_desc);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
mwifiex_dnld_txpwr_table(struct mwifiex_private * priv)195*4882a593Smuzhiyun void mwifiex_dnld_txpwr_table(struct mwifiex_private *priv)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	if (priv->adapter->dt_node) {
198*4882a593Smuzhiyun 		char txpwr[] = {"marvell,00_txpwrlimit"};
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		memcpy(&txpwr[8], priv->adapter->country_code, 2);
201*4882a593Smuzhiyun 		mwifiex_dnld_dt_cfgdata(priv, priv->adapter->dt_node, txpwr);
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
mwifiex_process_country_ie(struct mwifiex_private * priv,struct cfg80211_bss * bss)205*4882a593Smuzhiyun static int mwifiex_process_country_ie(struct mwifiex_private *priv,
206*4882a593Smuzhiyun 				      struct cfg80211_bss *bss)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	const u8 *country_ie;
209*4882a593Smuzhiyun 	u8 country_ie_len;
210*4882a593Smuzhiyun 	struct mwifiex_802_11d_domain_reg *domain_info =
211*4882a593Smuzhiyun 					&priv->adapter->domain_reg;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	rcu_read_lock();
214*4882a593Smuzhiyun 	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
215*4882a593Smuzhiyun 	if (!country_ie) {
216*4882a593Smuzhiyun 		rcu_read_unlock();
217*4882a593Smuzhiyun 		return 0;
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	country_ie_len = country_ie[1];
221*4882a593Smuzhiyun 	if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) {
222*4882a593Smuzhiyun 		rcu_read_unlock();
223*4882a593Smuzhiyun 		return 0;
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (!strncmp(priv->adapter->country_code, &country_ie[2], 2)) {
227*4882a593Smuzhiyun 		rcu_read_unlock();
228*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, INFO,
229*4882a593Smuzhiyun 			    "11D: skip setting domain info in FW\n");
230*4882a593Smuzhiyun 		return 0;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	if (country_ie_len >
234*4882a593Smuzhiyun 	    (IEEE80211_COUNTRY_STRING_LEN + MWIFIEX_MAX_TRIPLET_802_11D)) {
235*4882a593Smuzhiyun 		rcu_read_unlock();
236*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, ERROR,
237*4882a593Smuzhiyun 			    "11D: country_ie_len overflow!, deauth AP\n");
238*4882a593Smuzhiyun 		return -EINVAL;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	memcpy(priv->adapter->country_code, &country_ie[2], 2);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	domain_info->country_code[0] = country_ie[2];
244*4882a593Smuzhiyun 	domain_info->country_code[1] = country_ie[3];
245*4882a593Smuzhiyun 	domain_info->country_code[2] = ' ';
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	country_ie_len -= IEEE80211_COUNTRY_STRING_LEN;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	domain_info->no_of_triplet =
250*4882a593Smuzhiyun 		country_ie_len / sizeof(struct ieee80211_country_ie_triplet);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	memcpy((u8 *)domain_info->triplet,
253*4882a593Smuzhiyun 	       &country_ie[2] + IEEE80211_COUNTRY_STRING_LEN, country_ie_len);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	rcu_read_unlock();
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
258*4882a593Smuzhiyun 			     HostCmd_ACT_GEN_SET, 0, NULL, false)) {
259*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, ERROR,
260*4882a593Smuzhiyun 			    "11D: setting domain info in FW fail\n");
261*4882a593Smuzhiyun 		return -1;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	mwifiex_dnld_txpwr_table(priv);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	return 0;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun  * In Ad-Hoc mode, the IBSS is created if not found in scan list.
271*4882a593Smuzhiyun  * In both Ad-Hoc and infra mode, an deauthentication is performed
272*4882a593Smuzhiyun  * first.
273*4882a593Smuzhiyun  */
mwifiex_bss_start(struct mwifiex_private * priv,struct cfg80211_bss * bss,struct cfg80211_ssid * req_ssid)274*4882a593Smuzhiyun int mwifiex_bss_start(struct mwifiex_private *priv, struct cfg80211_bss *bss,
275*4882a593Smuzhiyun 		      struct cfg80211_ssid *req_ssid)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	int ret;
278*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
279*4882a593Smuzhiyun 	struct mwifiex_bssdescriptor *bss_desc = NULL;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	priv->scan_block = false;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	if (bss) {
284*4882a593Smuzhiyun 		if (adapter->region_code == 0x00 &&
285*4882a593Smuzhiyun 		    mwifiex_process_country_ie(priv, bss))
286*4882a593Smuzhiyun 			return -EINVAL;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		/* Allocate and fill new bss descriptor */
289*4882a593Smuzhiyun 		bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
290*4882a593Smuzhiyun 				   GFP_KERNEL);
291*4882a593Smuzhiyun 		if (!bss_desc)
292*4882a593Smuzhiyun 			return -ENOMEM;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
295*4882a593Smuzhiyun 		if (ret)
296*4882a593Smuzhiyun 			goto done;
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
300*4882a593Smuzhiyun 	    priv->bss_mode == NL80211_IFTYPE_P2P_CLIENT) {
301*4882a593Smuzhiyun 		u8 config_bands;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		if (!bss_desc)
304*4882a593Smuzhiyun 			return -1;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		if (mwifiex_band_to_radio_type(bss_desc->bss_band) ==
307*4882a593Smuzhiyun 						HostCmd_SCAN_RADIO_TYPE_BG) {
308*4882a593Smuzhiyun 			config_bands = BAND_B | BAND_G | BAND_GN;
309*4882a593Smuzhiyun 		} else {
310*4882a593Smuzhiyun 			config_bands = BAND_A | BAND_AN;
311*4882a593Smuzhiyun 			if (adapter->fw_bands & BAND_AAC)
312*4882a593Smuzhiyun 				config_bands |= BAND_AAC;
313*4882a593Smuzhiyun 		}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 		if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands))
316*4882a593Smuzhiyun 			adapter->config_bands = config_bands;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 		ret = mwifiex_check_network_compatibility(priv, bss_desc);
319*4882a593Smuzhiyun 		if (ret)
320*4882a593Smuzhiyun 			goto done;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		if (mwifiex_11h_get_csa_closed_channel(priv) ==
323*4882a593Smuzhiyun 							(u8)bss_desc->channel) {
324*4882a593Smuzhiyun 			mwifiex_dbg(adapter, ERROR,
325*4882a593Smuzhiyun 				    "Attempt to reconnect on csa closed chan(%d)\n",
326*4882a593Smuzhiyun 				    bss_desc->channel);
327*4882a593Smuzhiyun 			ret = -1;
328*4882a593Smuzhiyun 			goto done;
329*4882a593Smuzhiyun 		}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 		mwifiex_dbg(adapter, INFO,
332*4882a593Smuzhiyun 			    "info: SSID found in scan list ...\t"
333*4882a593Smuzhiyun 			    "associating...\n");
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 		mwifiex_stop_net_dev_queue(priv->netdev, adapter);
336*4882a593Smuzhiyun 		if (netif_carrier_ok(priv->netdev))
337*4882a593Smuzhiyun 			netif_carrier_off(priv->netdev);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 		/* Clear any past association response stored for
340*4882a593Smuzhiyun 		 * application retrieval */
341*4882a593Smuzhiyun 		priv->assoc_rsp_size = 0;
342*4882a593Smuzhiyun 		ret = mwifiex_associate(priv, bss_desc);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 		/* If auth type is auto and association fails using open mode,
345*4882a593Smuzhiyun 		 * try to connect using shared mode */
346*4882a593Smuzhiyun 		if (ret == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
347*4882a593Smuzhiyun 		    priv->sec_info.is_authtype_auto &&
348*4882a593Smuzhiyun 		    priv->sec_info.wep_enabled) {
349*4882a593Smuzhiyun 			priv->sec_info.authentication_mode =
350*4882a593Smuzhiyun 						NL80211_AUTHTYPE_SHARED_KEY;
351*4882a593Smuzhiyun 			ret = mwifiex_associate(priv, bss_desc);
352*4882a593Smuzhiyun 		}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 		if (bss)
355*4882a593Smuzhiyun 			cfg80211_put_bss(priv->adapter->wiphy, bss);
356*4882a593Smuzhiyun 	} else {
357*4882a593Smuzhiyun 		/* Adhoc mode */
358*4882a593Smuzhiyun 		/* If the requested SSID matches current SSID, return */
359*4882a593Smuzhiyun 		if (bss_desc && bss_desc->ssid.ssid_len &&
360*4882a593Smuzhiyun 		    (!mwifiex_ssid_cmp(&priv->curr_bss_params.bss_descriptor.
361*4882a593Smuzhiyun 				       ssid, &bss_desc->ssid))) {
362*4882a593Smuzhiyun 			ret = 0;
363*4882a593Smuzhiyun 			goto done;
364*4882a593Smuzhiyun 		}
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 		priv->adhoc_is_link_sensed = false;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 		ret = mwifiex_check_network_compatibility(priv, bss_desc);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		mwifiex_stop_net_dev_queue(priv->netdev, adapter);
371*4882a593Smuzhiyun 		if (netif_carrier_ok(priv->netdev))
372*4882a593Smuzhiyun 			netif_carrier_off(priv->netdev);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 		if (!ret) {
375*4882a593Smuzhiyun 			mwifiex_dbg(adapter, INFO,
376*4882a593Smuzhiyun 				    "info: network found in scan\t"
377*4882a593Smuzhiyun 				    " list. Joining...\n");
378*4882a593Smuzhiyun 			ret = mwifiex_adhoc_join(priv, bss_desc);
379*4882a593Smuzhiyun 			if (bss)
380*4882a593Smuzhiyun 				cfg80211_put_bss(priv->adapter->wiphy, bss);
381*4882a593Smuzhiyun 		} else {
382*4882a593Smuzhiyun 			mwifiex_dbg(adapter, INFO,
383*4882a593Smuzhiyun 				    "info: Network not found in\t"
384*4882a593Smuzhiyun 				    "the list, creating adhoc with ssid = %s\n",
385*4882a593Smuzhiyun 				    req_ssid->ssid);
386*4882a593Smuzhiyun 			ret = mwifiex_adhoc_start(priv, req_ssid);
387*4882a593Smuzhiyun 		}
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun done:
391*4882a593Smuzhiyun 	/* beacon_ie buffer was allocated in function
392*4882a593Smuzhiyun 	 * mwifiex_fill_new_bss_desc(). Free it now.
393*4882a593Smuzhiyun 	 */
394*4882a593Smuzhiyun 	if (bss_desc)
395*4882a593Smuzhiyun 		kfree(bss_desc->beacon_buf);
396*4882a593Smuzhiyun 	kfree(bss_desc);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	if (ret < 0)
399*4882a593Smuzhiyun 		priv->attempted_bss_desc = NULL;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return ret;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun  * IOCTL request handler to set host sleep configuration.
406*4882a593Smuzhiyun  *
407*4882a593Smuzhiyun  * This function prepares the correct firmware command and
408*4882a593Smuzhiyun  * issues it.
409*4882a593Smuzhiyun  */
mwifiex_set_hs_params(struct mwifiex_private * priv,u16 action,int cmd_type,struct mwifiex_ds_hs_cfg * hs_cfg)410*4882a593Smuzhiyun int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
411*4882a593Smuzhiyun 			  int cmd_type, struct mwifiex_ds_hs_cfg *hs_cfg)
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
415*4882a593Smuzhiyun 	int status = 0;
416*4882a593Smuzhiyun 	u32 prev_cond = 0;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	if (!hs_cfg)
419*4882a593Smuzhiyun 		return -ENOMEM;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	switch (action) {
422*4882a593Smuzhiyun 	case HostCmd_ACT_GEN_SET:
423*4882a593Smuzhiyun 		if (adapter->pps_uapsd_mode) {
424*4882a593Smuzhiyun 			mwifiex_dbg(adapter, INFO,
425*4882a593Smuzhiyun 				    "info: Host Sleep IOCTL\t"
426*4882a593Smuzhiyun 				    "is blocked in UAPSD/PPS mode\n");
427*4882a593Smuzhiyun 			status = -1;
428*4882a593Smuzhiyun 			break;
429*4882a593Smuzhiyun 		}
430*4882a593Smuzhiyun 		if (hs_cfg->is_invoke_hostcmd) {
431*4882a593Smuzhiyun 			if (hs_cfg->conditions == HS_CFG_CANCEL) {
432*4882a593Smuzhiyun 				if (!test_bit(MWIFIEX_IS_HS_CONFIGURED,
433*4882a593Smuzhiyun 					      &adapter->work_flags))
434*4882a593Smuzhiyun 					/* Already cancelled */
435*4882a593Smuzhiyun 					break;
436*4882a593Smuzhiyun 				/* Save previous condition */
437*4882a593Smuzhiyun 				prev_cond = le32_to_cpu(adapter->hs_cfg
438*4882a593Smuzhiyun 							.conditions);
439*4882a593Smuzhiyun 				adapter->hs_cfg.conditions =
440*4882a593Smuzhiyun 						cpu_to_le32(hs_cfg->conditions);
441*4882a593Smuzhiyun 			} else if (hs_cfg->conditions) {
442*4882a593Smuzhiyun 				adapter->hs_cfg.conditions =
443*4882a593Smuzhiyun 						cpu_to_le32(hs_cfg->conditions);
444*4882a593Smuzhiyun 				adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
445*4882a593Smuzhiyun 				if (hs_cfg->gap)
446*4882a593Smuzhiyun 					adapter->hs_cfg.gap = (u8)hs_cfg->gap;
447*4882a593Smuzhiyun 			} else if (adapter->hs_cfg.conditions ==
448*4882a593Smuzhiyun 				   cpu_to_le32(HS_CFG_CANCEL)) {
449*4882a593Smuzhiyun 				/* Return failure if no parameters for HS
450*4882a593Smuzhiyun 				   enable */
451*4882a593Smuzhiyun 				status = -1;
452*4882a593Smuzhiyun 				break;
453*4882a593Smuzhiyun 			}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 			status = mwifiex_send_cmd(priv,
456*4882a593Smuzhiyun 						  HostCmd_CMD_802_11_HS_CFG_ENH,
457*4882a593Smuzhiyun 						  HostCmd_ACT_GEN_SET, 0,
458*4882a593Smuzhiyun 						  &adapter->hs_cfg,
459*4882a593Smuzhiyun 						  cmd_type == MWIFIEX_SYNC_CMD);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 			if (hs_cfg->conditions == HS_CFG_CANCEL)
462*4882a593Smuzhiyun 				/* Restore previous condition */
463*4882a593Smuzhiyun 				adapter->hs_cfg.conditions =
464*4882a593Smuzhiyun 						cpu_to_le32(prev_cond);
465*4882a593Smuzhiyun 		} else {
466*4882a593Smuzhiyun 			adapter->hs_cfg.conditions =
467*4882a593Smuzhiyun 						cpu_to_le32(hs_cfg->conditions);
468*4882a593Smuzhiyun 			adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
469*4882a593Smuzhiyun 			adapter->hs_cfg.gap = (u8)hs_cfg->gap;
470*4882a593Smuzhiyun 		}
471*4882a593Smuzhiyun 		break;
472*4882a593Smuzhiyun 	case HostCmd_ACT_GEN_GET:
473*4882a593Smuzhiyun 		hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
474*4882a593Smuzhiyun 		hs_cfg->gpio = adapter->hs_cfg.gpio;
475*4882a593Smuzhiyun 		hs_cfg->gap = adapter->hs_cfg.gap;
476*4882a593Smuzhiyun 		break;
477*4882a593Smuzhiyun 	default:
478*4882a593Smuzhiyun 		status = -1;
479*4882a593Smuzhiyun 		break;
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	return status;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun /*
486*4882a593Smuzhiyun  * Sends IOCTL request to cancel the existing Host Sleep configuration.
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
489*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
490*4882a593Smuzhiyun  */
mwifiex_cancel_hs(struct mwifiex_private * priv,int cmd_type)491*4882a593Smuzhiyun int mwifiex_cancel_hs(struct mwifiex_private *priv, int cmd_type)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	struct mwifiex_ds_hs_cfg hscfg;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	hscfg.conditions = HS_CFG_CANCEL;
496*4882a593Smuzhiyun 	hscfg.is_invoke_hostcmd = true;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	return mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
499*4882a593Smuzhiyun 				    cmd_type, &hscfg);
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun  * Sends IOCTL request to cancel the existing Host Sleep configuration.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
507*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
508*4882a593Smuzhiyun  */
mwifiex_enable_hs(struct mwifiex_adapter * adapter)509*4882a593Smuzhiyun int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	struct mwifiex_ds_hs_cfg hscfg;
512*4882a593Smuzhiyun 	struct mwifiex_private *priv;
513*4882a593Smuzhiyun 	int i;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	if (disconnect_on_suspend) {
516*4882a593Smuzhiyun 		for (i = 0; i < adapter->priv_num; i++) {
517*4882a593Smuzhiyun 			priv = adapter->priv[i];
518*4882a593Smuzhiyun 			if (priv)
519*4882a593Smuzhiyun 				mwifiex_deauthenticate(priv, NULL);
520*4882a593Smuzhiyun 		}
521*4882a593Smuzhiyun 	}
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	if (priv && priv->sched_scanning) {
526*4882a593Smuzhiyun #ifdef CONFIG_PM
527*4882a593Smuzhiyun 		if (priv->wdev.wiphy->wowlan_config &&
528*4882a593Smuzhiyun 		    !priv->wdev.wiphy->wowlan_config->nd_config) {
529*4882a593Smuzhiyun #endif
530*4882a593Smuzhiyun 			mwifiex_dbg(adapter, CMD, "aborting bgscan!\n");
531*4882a593Smuzhiyun 			mwifiex_stop_bg_scan(priv);
532*4882a593Smuzhiyun 			cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
533*4882a593Smuzhiyun #ifdef CONFIG_PM
534*4882a593Smuzhiyun 		}
535*4882a593Smuzhiyun #endif
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	if (adapter->hs_activated) {
539*4882a593Smuzhiyun 		mwifiex_dbg(adapter, CMD,
540*4882a593Smuzhiyun 			    "cmd: HS Already activated\n");
541*4882a593Smuzhiyun 		return true;
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	adapter->hs_activate_wait_q_woken = false;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	memset(&hscfg, 0, sizeof(hscfg));
547*4882a593Smuzhiyun 	hscfg.is_invoke_hostcmd = true;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	set_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
550*4882a593Smuzhiyun 	mwifiex_cancel_all_pending_cmd(adapter);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
553*4882a593Smuzhiyun 						   MWIFIEX_BSS_ROLE_STA),
554*4882a593Smuzhiyun 				  HostCmd_ACT_GEN_SET, MWIFIEX_SYNC_CMD,
555*4882a593Smuzhiyun 				  &hscfg)) {
556*4882a593Smuzhiyun 		mwifiex_dbg(adapter, ERROR,
557*4882a593Smuzhiyun 			    "IOCTL request HS enable failed\n");
558*4882a593Smuzhiyun 		return false;
559*4882a593Smuzhiyun 	}
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	if (wait_event_interruptible_timeout(adapter->hs_activate_wait_q,
562*4882a593Smuzhiyun 					     adapter->hs_activate_wait_q_woken,
563*4882a593Smuzhiyun 					     (10 * HZ)) <= 0) {
564*4882a593Smuzhiyun 		mwifiex_dbg(adapter, ERROR,
565*4882a593Smuzhiyun 			    "hs_activate_wait_q terminated\n");
566*4882a593Smuzhiyun 		return false;
567*4882a593Smuzhiyun 	}
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	return true;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mwifiex_enable_hs);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun /*
574*4882a593Smuzhiyun  * IOCTL request handler to get BSS information.
575*4882a593Smuzhiyun  *
576*4882a593Smuzhiyun  * This function collates the information from different driver structures
577*4882a593Smuzhiyun  * to send to the user.
578*4882a593Smuzhiyun  */
mwifiex_get_bss_info(struct mwifiex_private * priv,struct mwifiex_bss_info * info)579*4882a593Smuzhiyun int mwifiex_get_bss_info(struct mwifiex_private *priv,
580*4882a593Smuzhiyun 			 struct mwifiex_bss_info *info)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
583*4882a593Smuzhiyun 	struct mwifiex_bssdescriptor *bss_desc;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	if (!info)
586*4882a593Smuzhiyun 		return -1;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	bss_desc = &priv->curr_bss_params.bss_descriptor;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	info->bss_mode = priv->bss_mode;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	memcpy(&info->ssid, &bss_desc->ssid, sizeof(struct cfg80211_ssid));
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	info->bss_chan = bss_desc->channel;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	memcpy(info->country_code, adapter->country_code,
599*4882a593Smuzhiyun 	       IEEE80211_COUNTRY_STRING_LEN);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	info->media_connected = priv->media_connected;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	info->max_power_level = priv->max_tx_power_level;
604*4882a593Smuzhiyun 	info->min_power_level = priv->min_tx_power_level;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	info->adhoc_state = priv->adhoc_state;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	info->bcn_nf_last = priv->bcn_nf_last;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	if (priv->sec_info.wep_enabled)
611*4882a593Smuzhiyun 		info->wep_status = true;
612*4882a593Smuzhiyun 	else
613*4882a593Smuzhiyun 		info->wep_status = false;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	info->is_hs_configured = test_bit(MWIFIEX_IS_HS_CONFIGURED,
616*4882a593Smuzhiyun 					  &adapter->work_flags);
617*4882a593Smuzhiyun 	info->is_deep_sleep = adapter->is_deep_sleep;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	return 0;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun /*
623*4882a593Smuzhiyun  * The function disables auto deep sleep mode.
624*4882a593Smuzhiyun  */
mwifiex_disable_auto_ds(struct mwifiex_private * priv)625*4882a593Smuzhiyun int mwifiex_disable_auto_ds(struct mwifiex_private *priv)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct mwifiex_ds_auto_ds auto_ds = {
628*4882a593Smuzhiyun 		.auto_ds = DEEP_SLEEP_OFF,
629*4882a593Smuzhiyun 	};
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
632*4882a593Smuzhiyun 				DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds, true);
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mwifiex_disable_auto_ds);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun /*
637*4882a593Smuzhiyun  * Sends IOCTL request to get the data rate.
638*4882a593Smuzhiyun  *
639*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
640*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
641*4882a593Smuzhiyun  */
mwifiex_drv_get_data_rate(struct mwifiex_private * priv,u32 * rate)642*4882a593Smuzhiyun int mwifiex_drv_get_data_rate(struct mwifiex_private *priv, u32 *rate)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	int ret;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
647*4882a593Smuzhiyun 			       HostCmd_ACT_GEN_GET, 0, NULL, true);
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	if (!ret) {
650*4882a593Smuzhiyun 		if (priv->is_data_rate_auto)
651*4882a593Smuzhiyun 			*rate = mwifiex_index_to_data_rate(priv, priv->tx_rate,
652*4882a593Smuzhiyun 							   priv->tx_htinfo);
653*4882a593Smuzhiyun 		else
654*4882a593Smuzhiyun 			*rate = priv->data_rate;
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	return ret;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun /*
661*4882a593Smuzhiyun  * IOCTL request handler to set tx power configuration.
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  * This function prepares the correct firmware command and
664*4882a593Smuzhiyun  * issues it.
665*4882a593Smuzhiyun  *
666*4882a593Smuzhiyun  * For non-auto power mode, all the following power groups are set -
667*4882a593Smuzhiyun  *      - Modulation class HR/DSSS
668*4882a593Smuzhiyun  *      - Modulation class OFDM
669*4882a593Smuzhiyun  *      - Modulation class HTBW20
670*4882a593Smuzhiyun  *      - Modulation class HTBW40
671*4882a593Smuzhiyun  */
mwifiex_set_tx_power(struct mwifiex_private * priv,struct mwifiex_power_cfg * power_cfg)672*4882a593Smuzhiyun int mwifiex_set_tx_power(struct mwifiex_private *priv,
673*4882a593Smuzhiyun 			 struct mwifiex_power_cfg *power_cfg)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	int ret;
676*4882a593Smuzhiyun 	struct host_cmd_ds_txpwr_cfg *txp_cfg;
677*4882a593Smuzhiyun 	struct mwifiex_types_power_group *pg_tlv;
678*4882a593Smuzhiyun 	struct mwifiex_power_group *pg;
679*4882a593Smuzhiyun 	u8 *buf;
680*4882a593Smuzhiyun 	u16 dbm = 0;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	if (!power_cfg->is_power_auto) {
683*4882a593Smuzhiyun 		dbm = (u16) power_cfg->power_level;
684*4882a593Smuzhiyun 		if ((dbm < priv->min_tx_power_level) ||
685*4882a593Smuzhiyun 		    (dbm > priv->max_tx_power_level)) {
686*4882a593Smuzhiyun 			mwifiex_dbg(priv->adapter, ERROR,
687*4882a593Smuzhiyun 				    "txpower value %d dBm\t"
688*4882a593Smuzhiyun 				    "is out of range (%d dBm-%d dBm)\n",
689*4882a593Smuzhiyun 				    dbm, priv->min_tx_power_level,
690*4882a593Smuzhiyun 				    priv->max_tx_power_level);
691*4882a593Smuzhiyun 			return -1;
692*4882a593Smuzhiyun 		}
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun 	buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
695*4882a593Smuzhiyun 	if (!buf)
696*4882a593Smuzhiyun 		return -ENOMEM;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
699*4882a593Smuzhiyun 	txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
700*4882a593Smuzhiyun 	if (!power_cfg->is_power_auto) {
701*4882a593Smuzhiyun 		u16 dbm_min = power_cfg->is_power_fixed ?
702*4882a593Smuzhiyun 			      dbm : priv->min_tx_power_level;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 		txp_cfg->mode = cpu_to_le32(1);
705*4882a593Smuzhiyun 		pg_tlv = (struct mwifiex_types_power_group *)
706*4882a593Smuzhiyun 			 (buf + sizeof(struct host_cmd_ds_txpwr_cfg));
707*4882a593Smuzhiyun 		pg_tlv->type = cpu_to_le16(TLV_TYPE_POWER_GROUP);
708*4882a593Smuzhiyun 		pg_tlv->length =
709*4882a593Smuzhiyun 			cpu_to_le16(4 * sizeof(struct mwifiex_power_group));
710*4882a593Smuzhiyun 		pg = (struct mwifiex_power_group *)
711*4882a593Smuzhiyun 		     (buf + sizeof(struct host_cmd_ds_txpwr_cfg)
712*4882a593Smuzhiyun 		      + sizeof(struct mwifiex_types_power_group));
713*4882a593Smuzhiyun 		/* Power group for modulation class HR/DSSS */
714*4882a593Smuzhiyun 		pg->first_rate_code = 0x00;
715*4882a593Smuzhiyun 		pg->last_rate_code = 0x03;
716*4882a593Smuzhiyun 		pg->modulation_class = MOD_CLASS_HR_DSSS;
717*4882a593Smuzhiyun 		pg->power_step = 0;
718*4882a593Smuzhiyun 		pg->power_min = (s8) dbm_min;
719*4882a593Smuzhiyun 		pg->power_max = (s8) dbm;
720*4882a593Smuzhiyun 		pg++;
721*4882a593Smuzhiyun 		/* Power group for modulation class OFDM */
722*4882a593Smuzhiyun 		pg->first_rate_code = 0x00;
723*4882a593Smuzhiyun 		pg->last_rate_code = 0x07;
724*4882a593Smuzhiyun 		pg->modulation_class = MOD_CLASS_OFDM;
725*4882a593Smuzhiyun 		pg->power_step = 0;
726*4882a593Smuzhiyun 		pg->power_min = (s8) dbm_min;
727*4882a593Smuzhiyun 		pg->power_max = (s8) dbm;
728*4882a593Smuzhiyun 		pg++;
729*4882a593Smuzhiyun 		/* Power group for modulation class HTBW20 */
730*4882a593Smuzhiyun 		pg->first_rate_code = 0x00;
731*4882a593Smuzhiyun 		pg->last_rate_code = 0x20;
732*4882a593Smuzhiyun 		pg->modulation_class = MOD_CLASS_HT;
733*4882a593Smuzhiyun 		pg->power_step = 0;
734*4882a593Smuzhiyun 		pg->power_min = (s8) dbm_min;
735*4882a593Smuzhiyun 		pg->power_max = (s8) dbm;
736*4882a593Smuzhiyun 		pg->ht_bandwidth = HT_BW_20;
737*4882a593Smuzhiyun 		pg++;
738*4882a593Smuzhiyun 		/* Power group for modulation class HTBW40 */
739*4882a593Smuzhiyun 		pg->first_rate_code = 0x00;
740*4882a593Smuzhiyun 		pg->last_rate_code = 0x20;
741*4882a593Smuzhiyun 		pg->modulation_class = MOD_CLASS_HT;
742*4882a593Smuzhiyun 		pg->power_step = 0;
743*4882a593Smuzhiyun 		pg->power_min = (s8) dbm_min;
744*4882a593Smuzhiyun 		pg->power_max = (s8) dbm;
745*4882a593Smuzhiyun 		pg->ht_bandwidth = HT_BW_40;
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_TXPWR_CFG,
748*4882a593Smuzhiyun 			       HostCmd_ACT_GEN_SET, 0, buf, true);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	kfree(buf);
751*4882a593Smuzhiyun 	return ret;
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun /*
755*4882a593Smuzhiyun  * IOCTL request handler to get power save mode.
756*4882a593Smuzhiyun  *
757*4882a593Smuzhiyun  * This function prepares the correct firmware command and
758*4882a593Smuzhiyun  * issues it.
759*4882a593Smuzhiyun  */
mwifiex_drv_set_power(struct mwifiex_private * priv,u32 * ps_mode)760*4882a593Smuzhiyun int mwifiex_drv_set_power(struct mwifiex_private *priv, u32 *ps_mode)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun 	int ret;
763*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
764*4882a593Smuzhiyun 	u16 sub_cmd;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	if (*ps_mode)
767*4882a593Smuzhiyun 		adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
768*4882a593Smuzhiyun 	else
769*4882a593Smuzhiyun 		adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
770*4882a593Smuzhiyun 	sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
771*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
772*4882a593Smuzhiyun 			       sub_cmd, BITMAP_STA_PS, NULL, true);
773*4882a593Smuzhiyun 	if ((!ret) && (sub_cmd == DIS_AUTO_PS))
774*4882a593Smuzhiyun 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
775*4882a593Smuzhiyun 				       GET_PS, 0, NULL, false);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	return ret;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun /*
781*4882a593Smuzhiyun  * IOCTL request handler to set/reset WPA IE.
782*4882a593Smuzhiyun  *
783*4882a593Smuzhiyun  * The supplied WPA IE is treated as a opaque buffer. Only the first field
784*4882a593Smuzhiyun  * is checked to determine WPA version. If buffer length is zero, the existing
785*4882a593Smuzhiyun  * WPA IE is reset.
786*4882a593Smuzhiyun  */
mwifiex_set_wpa_ie(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)787*4882a593Smuzhiyun static int mwifiex_set_wpa_ie(struct mwifiex_private *priv,
788*4882a593Smuzhiyun 			      u8 *ie_data_ptr, u16 ie_len)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun 	if (ie_len) {
791*4882a593Smuzhiyun 		if (ie_len > sizeof(priv->wpa_ie)) {
792*4882a593Smuzhiyun 			mwifiex_dbg(priv->adapter, ERROR,
793*4882a593Smuzhiyun 				    "failed to copy WPA IE, too big\n");
794*4882a593Smuzhiyun 			return -1;
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 		memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
797*4882a593Smuzhiyun 		priv->wpa_ie_len = ie_len;
798*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, CMD,
799*4882a593Smuzhiyun 			    "cmd: Set Wpa_ie_len=%d IE=%#x\n",
800*4882a593Smuzhiyun 			    priv->wpa_ie_len, priv->wpa_ie[0]);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		if (priv->wpa_ie[0] == WLAN_EID_VENDOR_SPECIFIC) {
803*4882a593Smuzhiyun 			priv->sec_info.wpa_enabled = true;
804*4882a593Smuzhiyun 		} else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
805*4882a593Smuzhiyun 			priv->sec_info.wpa2_enabled = true;
806*4882a593Smuzhiyun 		} else {
807*4882a593Smuzhiyun 			priv->sec_info.wpa_enabled = false;
808*4882a593Smuzhiyun 			priv->sec_info.wpa2_enabled = false;
809*4882a593Smuzhiyun 		}
810*4882a593Smuzhiyun 	} else {
811*4882a593Smuzhiyun 		memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
812*4882a593Smuzhiyun 		priv->wpa_ie_len = 0;
813*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, INFO,
814*4882a593Smuzhiyun 			    "info: reset wpa_ie_len=%d IE=%#x\n",
815*4882a593Smuzhiyun 			    priv->wpa_ie_len, priv->wpa_ie[0]);
816*4882a593Smuzhiyun 		priv->sec_info.wpa_enabled = false;
817*4882a593Smuzhiyun 		priv->sec_info.wpa2_enabled = false;
818*4882a593Smuzhiyun 	}
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	return 0;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun /*
824*4882a593Smuzhiyun  * IOCTL request handler to set/reset WAPI IE.
825*4882a593Smuzhiyun  *
826*4882a593Smuzhiyun  * The supplied WAPI IE is treated as a opaque buffer. Only the first field
827*4882a593Smuzhiyun  * is checked to internally enable WAPI. If buffer length is zero, the existing
828*4882a593Smuzhiyun  * WAPI IE is reset.
829*4882a593Smuzhiyun  */
mwifiex_set_wapi_ie(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)830*4882a593Smuzhiyun static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
831*4882a593Smuzhiyun 			       u8 *ie_data_ptr, u16 ie_len)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	if (ie_len) {
834*4882a593Smuzhiyun 		if (ie_len > sizeof(priv->wapi_ie)) {
835*4882a593Smuzhiyun 			mwifiex_dbg(priv->adapter, ERROR,
836*4882a593Smuzhiyun 				    "info: failed to copy WAPI IE, too big\n");
837*4882a593Smuzhiyun 			return -1;
838*4882a593Smuzhiyun 		}
839*4882a593Smuzhiyun 		memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
840*4882a593Smuzhiyun 		priv->wapi_ie_len = ie_len;
841*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, CMD,
842*4882a593Smuzhiyun 			    "cmd: Set wapi_ie_len=%d IE=%#x\n",
843*4882a593Smuzhiyun 			    priv->wapi_ie_len, priv->wapi_ie[0]);
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 		if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
846*4882a593Smuzhiyun 			priv->sec_info.wapi_enabled = true;
847*4882a593Smuzhiyun 	} else {
848*4882a593Smuzhiyun 		memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
849*4882a593Smuzhiyun 		priv->wapi_ie_len = ie_len;
850*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, INFO,
851*4882a593Smuzhiyun 			    "info: Reset wapi_ie_len=%d IE=%#x\n",
852*4882a593Smuzhiyun 			    priv->wapi_ie_len, priv->wapi_ie[0]);
853*4882a593Smuzhiyun 		priv->sec_info.wapi_enabled = false;
854*4882a593Smuzhiyun 	}
855*4882a593Smuzhiyun 	return 0;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun /*
859*4882a593Smuzhiyun  * IOCTL request handler to set/reset WPS IE.
860*4882a593Smuzhiyun  *
861*4882a593Smuzhiyun  * The supplied WPS IE is treated as a opaque buffer. Only the first field
862*4882a593Smuzhiyun  * is checked to internally enable WPS. If buffer length is zero, the existing
863*4882a593Smuzhiyun  * WPS IE is reset.
864*4882a593Smuzhiyun  */
mwifiex_set_wps_ie(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)865*4882a593Smuzhiyun static int mwifiex_set_wps_ie(struct mwifiex_private *priv,
866*4882a593Smuzhiyun 			       u8 *ie_data_ptr, u16 ie_len)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun 	if (ie_len) {
869*4882a593Smuzhiyun 		if (ie_len > MWIFIEX_MAX_VSIE_LEN) {
870*4882a593Smuzhiyun 			mwifiex_dbg(priv->adapter, ERROR,
871*4882a593Smuzhiyun 				    "info: failed to copy WPS IE, too big\n");
872*4882a593Smuzhiyun 			return -1;
873*4882a593Smuzhiyun 		}
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 		priv->wps_ie = kzalloc(MWIFIEX_MAX_VSIE_LEN, GFP_KERNEL);
876*4882a593Smuzhiyun 		if (!priv->wps_ie)
877*4882a593Smuzhiyun 			return -ENOMEM;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 		memcpy(priv->wps_ie, ie_data_ptr, ie_len);
880*4882a593Smuzhiyun 		priv->wps_ie_len = ie_len;
881*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, CMD,
882*4882a593Smuzhiyun 			    "cmd: Set wps_ie_len=%d IE=%#x\n",
883*4882a593Smuzhiyun 			    priv->wps_ie_len, priv->wps_ie[0]);
884*4882a593Smuzhiyun 	} else {
885*4882a593Smuzhiyun 		kfree(priv->wps_ie);
886*4882a593Smuzhiyun 		priv->wps_ie_len = ie_len;
887*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, INFO,
888*4882a593Smuzhiyun 			    "info: Reset wps_ie_len=%d\n", priv->wps_ie_len);
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 	return 0;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun /*
894*4882a593Smuzhiyun  * IOCTL request handler to set WAPI key.
895*4882a593Smuzhiyun  *
896*4882a593Smuzhiyun  * This function prepares the correct firmware command and
897*4882a593Smuzhiyun  * issues it.
898*4882a593Smuzhiyun  */
mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)899*4882a593Smuzhiyun static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private *priv,
900*4882a593Smuzhiyun 			       struct mwifiex_ds_encrypt_key *encrypt_key)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
904*4882a593Smuzhiyun 				HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
905*4882a593Smuzhiyun 				encrypt_key, true);
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun /*
909*4882a593Smuzhiyun  * IOCTL request handler to set WEP network key.
910*4882a593Smuzhiyun  *
911*4882a593Smuzhiyun  * This function prepares the correct firmware command and
912*4882a593Smuzhiyun  * issues it, after validation checks.
913*4882a593Smuzhiyun  */
mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)914*4882a593Smuzhiyun static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private *priv,
915*4882a593Smuzhiyun 			      struct mwifiex_ds_encrypt_key *encrypt_key)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
918*4882a593Smuzhiyun 	int ret;
919*4882a593Smuzhiyun 	struct mwifiex_wep_key *wep_key;
920*4882a593Smuzhiyun 	int index;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
923*4882a593Smuzhiyun 		priv->wep_key_curr_index = 0;
924*4882a593Smuzhiyun 	wep_key = &priv->wep_key[priv->wep_key_curr_index];
925*4882a593Smuzhiyun 	index = encrypt_key->key_index;
926*4882a593Smuzhiyun 	if (encrypt_key->key_disable) {
927*4882a593Smuzhiyun 		priv->sec_info.wep_enabled = 0;
928*4882a593Smuzhiyun 	} else if (!encrypt_key->key_len) {
929*4882a593Smuzhiyun 		/* Copy the required key as the current key */
930*4882a593Smuzhiyun 		wep_key = &priv->wep_key[index];
931*4882a593Smuzhiyun 		if (!wep_key->key_length) {
932*4882a593Smuzhiyun 			mwifiex_dbg(adapter, ERROR,
933*4882a593Smuzhiyun 				    "key not set, so cannot enable it\n");
934*4882a593Smuzhiyun 			return -1;
935*4882a593Smuzhiyun 		}
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 		if (adapter->key_api_major_ver == KEY_API_VER_MAJOR_V2) {
938*4882a593Smuzhiyun 			memcpy(encrypt_key->key_material,
939*4882a593Smuzhiyun 			       wep_key->key_material, wep_key->key_length);
940*4882a593Smuzhiyun 			encrypt_key->key_len = wep_key->key_length;
941*4882a593Smuzhiyun 		}
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 		priv->wep_key_curr_index = (u16) index;
944*4882a593Smuzhiyun 		priv->sec_info.wep_enabled = 1;
945*4882a593Smuzhiyun 	} else {
946*4882a593Smuzhiyun 		wep_key = &priv->wep_key[index];
947*4882a593Smuzhiyun 		memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
948*4882a593Smuzhiyun 		/* Copy the key in the driver */
949*4882a593Smuzhiyun 		memcpy(wep_key->key_material,
950*4882a593Smuzhiyun 		       encrypt_key->key_material,
951*4882a593Smuzhiyun 		       encrypt_key->key_len);
952*4882a593Smuzhiyun 		wep_key->key_index = index;
953*4882a593Smuzhiyun 		wep_key->key_length = encrypt_key->key_len;
954*4882a593Smuzhiyun 		priv->sec_info.wep_enabled = 1;
955*4882a593Smuzhiyun 	}
956*4882a593Smuzhiyun 	if (wep_key->key_length) {
957*4882a593Smuzhiyun 		void *enc_key;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 		if (encrypt_key->key_disable) {
960*4882a593Smuzhiyun 			memset(&priv->wep_key[index], 0,
961*4882a593Smuzhiyun 			       sizeof(struct mwifiex_wep_key));
962*4882a593Smuzhiyun 			goto done;
963*4882a593Smuzhiyun 		}
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 		if (adapter->key_api_major_ver == KEY_API_VER_MAJOR_V2)
966*4882a593Smuzhiyun 			enc_key = encrypt_key;
967*4882a593Smuzhiyun 		else
968*4882a593Smuzhiyun 			enc_key = NULL;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 		/* Send request to firmware */
971*4882a593Smuzhiyun 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
972*4882a593Smuzhiyun 				       HostCmd_ACT_GEN_SET, 0, enc_key, false);
973*4882a593Smuzhiyun 		if (ret)
974*4882a593Smuzhiyun 			return ret;
975*4882a593Smuzhiyun 	}
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun done:
978*4882a593Smuzhiyun 	if (priv->sec_info.wep_enabled)
979*4882a593Smuzhiyun 		priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
980*4882a593Smuzhiyun 	else
981*4882a593Smuzhiyun 		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
984*4882a593Smuzhiyun 			       HostCmd_ACT_GEN_SET, 0,
985*4882a593Smuzhiyun 			       &priv->curr_pkt_filter, true);
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	return ret;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun /*
991*4882a593Smuzhiyun  * IOCTL request handler to set WPA key.
992*4882a593Smuzhiyun  *
993*4882a593Smuzhiyun  * This function prepares the correct firmware command and
994*4882a593Smuzhiyun  * issues it, after validation checks.
995*4882a593Smuzhiyun  *
996*4882a593Smuzhiyun  * Current driver only supports key length of up to 32 bytes.
997*4882a593Smuzhiyun  *
998*4882a593Smuzhiyun  * This function can also be used to disable a currently set key.
999*4882a593Smuzhiyun  */
mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)1000*4882a593Smuzhiyun static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private *priv,
1001*4882a593Smuzhiyun 			      struct mwifiex_ds_encrypt_key *encrypt_key)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun 	int ret;
1004*4882a593Smuzhiyun 	u8 remove_key = false;
1005*4882a593Smuzhiyun 	struct host_cmd_ds_802_11_key_material *ibss_key;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	/* Current driver only supports key length of up to 32 bytes */
1008*4882a593Smuzhiyun 	if (encrypt_key->key_len > WLAN_MAX_KEY_LEN) {
1009*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, ERROR,
1010*4882a593Smuzhiyun 			    "key length too long\n");
1011*4882a593Smuzhiyun 		return -1;
1012*4882a593Smuzhiyun 	}
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
1015*4882a593Smuzhiyun 		/*
1016*4882a593Smuzhiyun 		 * IBSS/WPA-None uses only one key (Group) for both receiving
1017*4882a593Smuzhiyun 		 * and sending unicast and multicast packets.
1018*4882a593Smuzhiyun 		 */
1019*4882a593Smuzhiyun 		/* Send the key as PTK to firmware */
1020*4882a593Smuzhiyun 		encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1021*4882a593Smuzhiyun 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1022*4882a593Smuzhiyun 				       HostCmd_ACT_GEN_SET,
1023*4882a593Smuzhiyun 				       KEY_INFO_ENABLED, encrypt_key, false);
1024*4882a593Smuzhiyun 		if (ret)
1025*4882a593Smuzhiyun 			return ret;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 		ibss_key = &priv->aes_key;
1028*4882a593Smuzhiyun 		memset(ibss_key, 0,
1029*4882a593Smuzhiyun 		       sizeof(struct host_cmd_ds_802_11_key_material));
1030*4882a593Smuzhiyun 		/* Copy the key in the driver */
1031*4882a593Smuzhiyun 		memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
1032*4882a593Smuzhiyun 		       encrypt_key->key_len);
1033*4882a593Smuzhiyun 		memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
1034*4882a593Smuzhiyun 		       sizeof(ibss_key->key_param_set.key_len));
1035*4882a593Smuzhiyun 		ibss_key->key_param_set.key_type_id
1036*4882a593Smuzhiyun 			= cpu_to_le16(KEY_TYPE_ID_TKIP);
1037*4882a593Smuzhiyun 		ibss_key->key_param_set.key_info = cpu_to_le16(KEY_ENABLED);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 		/* Send the key as GTK to firmware */
1040*4882a593Smuzhiyun 		encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	if (!encrypt_key->key_index)
1044*4882a593Smuzhiyun 		encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	if (remove_key)
1047*4882a593Smuzhiyun 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1048*4882a593Smuzhiyun 				       HostCmd_ACT_GEN_SET,
1049*4882a593Smuzhiyun 				       !KEY_INFO_ENABLED, encrypt_key, true);
1050*4882a593Smuzhiyun 	else
1051*4882a593Smuzhiyun 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1052*4882a593Smuzhiyun 				       HostCmd_ACT_GEN_SET,
1053*4882a593Smuzhiyun 				       KEY_INFO_ENABLED, encrypt_key, true);
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	return ret;
1056*4882a593Smuzhiyun }
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun /*
1059*4882a593Smuzhiyun  * IOCTL request handler to set/get network keys.
1060*4882a593Smuzhiyun  *
1061*4882a593Smuzhiyun  * This is a generic key handling function which supports WEP, WPA
1062*4882a593Smuzhiyun  * and WAPI.
1063*4882a593Smuzhiyun  */
1064*4882a593Smuzhiyun static int
mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)1065*4882a593Smuzhiyun mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
1066*4882a593Smuzhiyun 			      struct mwifiex_ds_encrypt_key *encrypt_key)
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun 	int status;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	if (encrypt_key->is_wapi_key)
1071*4882a593Smuzhiyun 		status = mwifiex_sec_ioctl_set_wapi_key(priv, encrypt_key);
1072*4882a593Smuzhiyun 	else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
1073*4882a593Smuzhiyun 		status = mwifiex_sec_ioctl_set_wpa_key(priv, encrypt_key);
1074*4882a593Smuzhiyun 	else
1075*4882a593Smuzhiyun 		status = mwifiex_sec_ioctl_set_wep_key(priv, encrypt_key);
1076*4882a593Smuzhiyun 	return status;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun /*
1080*4882a593Smuzhiyun  * This function returns the driver version.
1081*4882a593Smuzhiyun  */
1082*4882a593Smuzhiyun int
mwifiex_drv_get_driver_version(struct mwifiex_adapter * adapter,char * version,int max_len)1083*4882a593Smuzhiyun mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
1084*4882a593Smuzhiyun 			       int max_len)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun 	union {
1087*4882a593Smuzhiyun 		__le32 l;
1088*4882a593Smuzhiyun 		u8 c[4];
1089*4882a593Smuzhiyun 	} ver;
1090*4882a593Smuzhiyun 	char fw_ver[32];
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	ver.l = cpu_to_le32(adapter->fw_release_number);
1093*4882a593Smuzhiyun 	sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	snprintf(version, max_len, driver_version, fw_ver);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	mwifiex_dbg(adapter, MSG, "info: MWIFIEX VERSION: %s\n", version);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	return 0;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun /*
1103*4882a593Smuzhiyun  * Sends IOCTL request to set encoding parameters.
1104*4882a593Smuzhiyun  *
1105*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1106*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1107*4882a593Smuzhiyun  */
mwifiex_set_encode(struct mwifiex_private * priv,struct key_params * kp,const u8 * key,int key_len,u8 key_index,const u8 * mac_addr,int disable)1108*4882a593Smuzhiyun int mwifiex_set_encode(struct mwifiex_private *priv, struct key_params *kp,
1109*4882a593Smuzhiyun 		       const u8 *key, int key_len, u8 key_index,
1110*4882a593Smuzhiyun 		       const u8 *mac_addr, int disable)
1111*4882a593Smuzhiyun {
1112*4882a593Smuzhiyun 	struct mwifiex_ds_encrypt_key encrypt_key;
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 	memset(&encrypt_key, 0, sizeof(encrypt_key));
1115*4882a593Smuzhiyun 	encrypt_key.key_len = key_len;
1116*4882a593Smuzhiyun 	encrypt_key.key_index = key_index;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	if (kp && kp->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1119*4882a593Smuzhiyun 		encrypt_key.is_igtk_key = true;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	if (!disable) {
1122*4882a593Smuzhiyun 		if (key_len)
1123*4882a593Smuzhiyun 			memcpy(encrypt_key.key_material, key, key_len);
1124*4882a593Smuzhiyun 		else
1125*4882a593Smuzhiyun 			encrypt_key.is_current_wep_key = true;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 		if (mac_addr)
1128*4882a593Smuzhiyun 			memcpy(encrypt_key.mac_addr, mac_addr, ETH_ALEN);
1129*4882a593Smuzhiyun 		if (kp && kp->seq && kp->seq_len) {
1130*4882a593Smuzhiyun 			memcpy(encrypt_key.pn, kp->seq, kp->seq_len);
1131*4882a593Smuzhiyun 			encrypt_key.pn_len = kp->seq_len;
1132*4882a593Smuzhiyun 			encrypt_key.is_rx_seq_valid = true;
1133*4882a593Smuzhiyun 		}
1134*4882a593Smuzhiyun 	} else {
1135*4882a593Smuzhiyun 		encrypt_key.key_disable = true;
1136*4882a593Smuzhiyun 		if (mac_addr)
1137*4882a593Smuzhiyun 			memcpy(encrypt_key.mac_addr, mac_addr, ETH_ALEN);
1138*4882a593Smuzhiyun 	}
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	return mwifiex_sec_ioctl_encrypt_key(priv, &encrypt_key);
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun /*
1144*4882a593Smuzhiyun  * Sends IOCTL request to get extended version.
1145*4882a593Smuzhiyun  *
1146*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1147*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1148*4882a593Smuzhiyun  */
1149*4882a593Smuzhiyun int
mwifiex_get_ver_ext(struct mwifiex_private * priv,u32 version_str_sel)1150*4882a593Smuzhiyun mwifiex_get_ver_ext(struct mwifiex_private *priv, u32 version_str_sel)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun 	struct mwifiex_ver_ext ver_ext;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	memset(&ver_ext, 0, sizeof(ver_ext));
1155*4882a593Smuzhiyun 	ver_ext.version_str_sel = version_str_sel;
1156*4882a593Smuzhiyun 	if (mwifiex_send_cmd(priv, HostCmd_CMD_VERSION_EXT,
1157*4882a593Smuzhiyun 			     HostCmd_ACT_GEN_GET, 0, &ver_ext, true))
1158*4882a593Smuzhiyun 		return -1;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	return 0;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun int
mwifiex_remain_on_chan_cfg(struct mwifiex_private * priv,u16 action,struct ieee80211_channel * chan,unsigned int duration)1164*4882a593Smuzhiyun mwifiex_remain_on_chan_cfg(struct mwifiex_private *priv, u16 action,
1165*4882a593Smuzhiyun 			   struct ieee80211_channel *chan,
1166*4882a593Smuzhiyun 			   unsigned int duration)
1167*4882a593Smuzhiyun {
1168*4882a593Smuzhiyun 	struct host_cmd_ds_remain_on_chan roc_cfg;
1169*4882a593Smuzhiyun 	u8 sc;
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	memset(&roc_cfg, 0, sizeof(roc_cfg));
1172*4882a593Smuzhiyun 	roc_cfg.action = cpu_to_le16(action);
1173*4882a593Smuzhiyun 	if (action == HostCmd_ACT_GEN_SET) {
1174*4882a593Smuzhiyun 		roc_cfg.band_cfg = chan->band;
1175*4882a593Smuzhiyun 		sc = mwifiex_chan_type_to_sec_chan_offset(NL80211_CHAN_NO_HT);
1176*4882a593Smuzhiyun 		roc_cfg.band_cfg |= (sc << 2);
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 		roc_cfg.channel =
1179*4882a593Smuzhiyun 			ieee80211_frequency_to_channel(chan->center_freq);
1180*4882a593Smuzhiyun 		roc_cfg.duration = cpu_to_le32(duration);
1181*4882a593Smuzhiyun 	}
1182*4882a593Smuzhiyun 	if (mwifiex_send_cmd(priv, HostCmd_CMD_REMAIN_ON_CHAN,
1183*4882a593Smuzhiyun 			     action, 0, &roc_cfg, true)) {
1184*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, ERROR,
1185*4882a593Smuzhiyun 			    "failed to remain on channel\n");
1186*4882a593Smuzhiyun 		return -1;
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	return roc_cfg.status;
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun /*
1193*4882a593Smuzhiyun  * Sends IOCTL request to get statistics information.
1194*4882a593Smuzhiyun  *
1195*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1196*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1197*4882a593Smuzhiyun  */
1198*4882a593Smuzhiyun int
mwifiex_get_stats_info(struct mwifiex_private * priv,struct mwifiex_ds_get_stats * log)1199*4882a593Smuzhiyun mwifiex_get_stats_info(struct mwifiex_private *priv,
1200*4882a593Smuzhiyun 		       struct mwifiex_ds_get_stats *log)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_GET_LOG,
1203*4882a593Smuzhiyun 				HostCmd_ACT_GEN_GET, 0, log, true);
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun /*
1207*4882a593Smuzhiyun  * IOCTL request handler to read/write register.
1208*4882a593Smuzhiyun  *
1209*4882a593Smuzhiyun  * This function prepares the correct firmware command and
1210*4882a593Smuzhiyun  * issues it.
1211*4882a593Smuzhiyun  *
1212*4882a593Smuzhiyun  * Access to the following registers are supported -
1213*4882a593Smuzhiyun  *      - MAC
1214*4882a593Smuzhiyun  *      - BBP
1215*4882a593Smuzhiyun  *      - RF
1216*4882a593Smuzhiyun  *      - PMIC
1217*4882a593Smuzhiyun  *      - CAU
1218*4882a593Smuzhiyun  */
mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private * priv,struct mwifiex_ds_reg_rw * reg_rw,u16 action)1219*4882a593Smuzhiyun static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
1220*4882a593Smuzhiyun 					struct mwifiex_ds_reg_rw *reg_rw,
1221*4882a593Smuzhiyun 					u16 action)
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun 	u16 cmd_no;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	switch (reg_rw->type) {
1226*4882a593Smuzhiyun 	case MWIFIEX_REG_MAC:
1227*4882a593Smuzhiyun 		cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
1228*4882a593Smuzhiyun 		break;
1229*4882a593Smuzhiyun 	case MWIFIEX_REG_BBP:
1230*4882a593Smuzhiyun 		cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
1231*4882a593Smuzhiyun 		break;
1232*4882a593Smuzhiyun 	case MWIFIEX_REG_RF:
1233*4882a593Smuzhiyun 		cmd_no = HostCmd_CMD_RF_REG_ACCESS;
1234*4882a593Smuzhiyun 		break;
1235*4882a593Smuzhiyun 	case MWIFIEX_REG_PMIC:
1236*4882a593Smuzhiyun 		cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
1237*4882a593Smuzhiyun 		break;
1238*4882a593Smuzhiyun 	case MWIFIEX_REG_CAU:
1239*4882a593Smuzhiyun 		cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
1240*4882a593Smuzhiyun 		break;
1241*4882a593Smuzhiyun 	default:
1242*4882a593Smuzhiyun 		return -1;
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	return mwifiex_send_cmd(priv, cmd_no, action, 0, reg_rw, true);
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun /*
1249*4882a593Smuzhiyun  * Sends IOCTL request to write to a register.
1250*4882a593Smuzhiyun  *
1251*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1252*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1253*4882a593Smuzhiyun  */
1254*4882a593Smuzhiyun int
mwifiex_reg_write(struct mwifiex_private * priv,u32 reg_type,u32 reg_offset,u32 reg_value)1255*4882a593Smuzhiyun mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
1256*4882a593Smuzhiyun 		  u32 reg_offset, u32 reg_value)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun 	struct mwifiex_ds_reg_rw reg_rw;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	reg_rw.type = reg_type;
1261*4882a593Smuzhiyun 	reg_rw.offset = reg_offset;
1262*4882a593Smuzhiyun 	reg_rw.value = reg_value;
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	return mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_SET);
1265*4882a593Smuzhiyun }
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun /*
1268*4882a593Smuzhiyun  * Sends IOCTL request to read from a register.
1269*4882a593Smuzhiyun  *
1270*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1271*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1272*4882a593Smuzhiyun  */
1273*4882a593Smuzhiyun int
mwifiex_reg_read(struct mwifiex_private * priv,u32 reg_type,u32 reg_offset,u32 * value)1274*4882a593Smuzhiyun mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
1275*4882a593Smuzhiyun 		 u32 reg_offset, u32 *value)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun 	int ret;
1278*4882a593Smuzhiyun 	struct mwifiex_ds_reg_rw reg_rw;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	reg_rw.type = reg_type;
1281*4882a593Smuzhiyun 	reg_rw.offset = reg_offset;
1282*4882a593Smuzhiyun 	ret = mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_GET);
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	if (ret)
1285*4882a593Smuzhiyun 		goto done;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	*value = reg_rw.value;
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun done:
1290*4882a593Smuzhiyun 	return ret;
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun /*
1294*4882a593Smuzhiyun  * Sends IOCTL request to read from EEPROM.
1295*4882a593Smuzhiyun  *
1296*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1297*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1298*4882a593Smuzhiyun  */
1299*4882a593Smuzhiyun int
mwifiex_eeprom_read(struct mwifiex_private * priv,u16 offset,u16 bytes,u8 * value)1300*4882a593Smuzhiyun mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
1301*4882a593Smuzhiyun 		    u8 *value)
1302*4882a593Smuzhiyun {
1303*4882a593Smuzhiyun 	int ret;
1304*4882a593Smuzhiyun 	struct mwifiex_ds_read_eeprom rd_eeprom;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	rd_eeprom.offset =  offset;
1307*4882a593Smuzhiyun 	rd_eeprom.byte_count = bytes;
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	/* Send request to firmware */
1310*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
1311*4882a593Smuzhiyun 			       HostCmd_ACT_GEN_GET, 0, &rd_eeprom, true);
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	if (!ret)
1314*4882a593Smuzhiyun 		memcpy(value, rd_eeprom.value, min((u16)MAX_EEPROM_DATA,
1315*4882a593Smuzhiyun 		       rd_eeprom.byte_count));
1316*4882a593Smuzhiyun 	return ret;
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun /*
1320*4882a593Smuzhiyun  * This function sets a generic IE. In addition to generic IE, it can
1321*4882a593Smuzhiyun  * also handle WPA, WPA2 and WAPI IEs.
1322*4882a593Smuzhiyun  */
1323*4882a593Smuzhiyun static int
mwifiex_set_gen_ie_helper(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)1324*4882a593Smuzhiyun mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
1325*4882a593Smuzhiyun 			  u16 ie_len)
1326*4882a593Smuzhiyun {
1327*4882a593Smuzhiyun 	struct ieee_types_vendor_header *pvendor_ie;
1328*4882a593Smuzhiyun 	const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
1329*4882a593Smuzhiyun 	const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
1330*4882a593Smuzhiyun 	u16 unparsed_len = ie_len, cur_ie_len;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	/* If the passed length is zero, reset the buffer */
1333*4882a593Smuzhiyun 	if (!ie_len) {
1334*4882a593Smuzhiyun 		priv->gen_ie_buf_len = 0;
1335*4882a593Smuzhiyun 		priv->wps.session_enable = false;
1336*4882a593Smuzhiyun 		return 0;
1337*4882a593Smuzhiyun 	} else if (!ie_data_ptr ||
1338*4882a593Smuzhiyun 		   ie_len <= sizeof(struct ieee_types_header)) {
1339*4882a593Smuzhiyun 		return -1;
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 	pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	while (pvendor_ie) {
1344*4882a593Smuzhiyun 		cur_ie_len = pvendor_ie->len + sizeof(struct ieee_types_header);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 		if (pvendor_ie->element_id == WLAN_EID_RSN) {
1347*4882a593Smuzhiyun 			/* IE is a WPA/WPA2 IE so call set_wpa function */
1348*4882a593Smuzhiyun 			mwifiex_set_wpa_ie(priv, (u8 *)pvendor_ie, cur_ie_len);
1349*4882a593Smuzhiyun 			priv->wps.session_enable = false;
1350*4882a593Smuzhiyun 			goto next_ie;
1351*4882a593Smuzhiyun 		}
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 		if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
1354*4882a593Smuzhiyun 			/* IE is a WAPI IE so call set_wapi function */
1355*4882a593Smuzhiyun 			mwifiex_set_wapi_ie(priv, (u8 *)pvendor_ie,
1356*4882a593Smuzhiyun 					    cur_ie_len);
1357*4882a593Smuzhiyun 			goto next_ie;
1358*4882a593Smuzhiyun 		}
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 		if (pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC) {
1361*4882a593Smuzhiyun 			/* Test to see if it is a WPA IE, if not, then
1362*4882a593Smuzhiyun 			 * it is a gen IE
1363*4882a593Smuzhiyun 			 */
1364*4882a593Smuzhiyun 			if (!memcmp(&pvendor_ie->oui, wpa_oui,
1365*4882a593Smuzhiyun 				    sizeof(wpa_oui))) {
1366*4882a593Smuzhiyun 				/* IE is a WPA/WPA2 IE so call set_wpa function
1367*4882a593Smuzhiyun 				 */
1368*4882a593Smuzhiyun 				mwifiex_set_wpa_ie(priv, (u8 *)pvendor_ie,
1369*4882a593Smuzhiyun 						   cur_ie_len);
1370*4882a593Smuzhiyun 				priv->wps.session_enable = false;
1371*4882a593Smuzhiyun 				goto next_ie;
1372*4882a593Smuzhiyun 			}
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 			if (!memcmp(&pvendor_ie->oui, wps_oui,
1375*4882a593Smuzhiyun 				    sizeof(wps_oui))) {
1376*4882a593Smuzhiyun 				/* Test to see if it is a WPS IE,
1377*4882a593Smuzhiyun 				 * if so, enable wps session flag
1378*4882a593Smuzhiyun 				 */
1379*4882a593Smuzhiyun 				priv->wps.session_enable = true;
1380*4882a593Smuzhiyun 				mwifiex_dbg(priv->adapter, MSG,
1381*4882a593Smuzhiyun 					    "WPS Session Enabled.\n");
1382*4882a593Smuzhiyun 				mwifiex_set_wps_ie(priv, (u8 *)pvendor_ie,
1383*4882a593Smuzhiyun 						   cur_ie_len);
1384*4882a593Smuzhiyun 				goto next_ie;
1385*4882a593Smuzhiyun 			}
1386*4882a593Smuzhiyun 		}
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 		/* Saved in gen_ie, such as P2P IE.etc.*/
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 		/* Verify that the passed length is not larger than the
1391*4882a593Smuzhiyun 		 * available space remaining in the buffer
1392*4882a593Smuzhiyun 		 */
1393*4882a593Smuzhiyun 		if (cur_ie_len <
1394*4882a593Smuzhiyun 		    (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {
1395*4882a593Smuzhiyun 			/* Append the passed data to the end
1396*4882a593Smuzhiyun 			 * of the genIeBuffer
1397*4882a593Smuzhiyun 			 */
1398*4882a593Smuzhiyun 			memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len,
1399*4882a593Smuzhiyun 			       (u8 *)pvendor_ie, cur_ie_len);
1400*4882a593Smuzhiyun 			/* Increment the stored buffer length by the
1401*4882a593Smuzhiyun 			 * size passed
1402*4882a593Smuzhiyun 			 */
1403*4882a593Smuzhiyun 			priv->gen_ie_buf_len += cur_ie_len;
1404*4882a593Smuzhiyun 		}
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun next_ie:
1407*4882a593Smuzhiyun 		unparsed_len -= cur_ie_len;
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 		if (unparsed_len <= sizeof(struct ieee_types_header))
1410*4882a593Smuzhiyun 			pvendor_ie = NULL;
1411*4882a593Smuzhiyun 		else
1412*4882a593Smuzhiyun 			pvendor_ie = (struct ieee_types_vendor_header *)
1413*4882a593Smuzhiyun 				(((u8 *)pvendor_ie) + cur_ie_len);
1414*4882a593Smuzhiyun 	}
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	return 0;
1417*4882a593Smuzhiyun }
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun /*
1420*4882a593Smuzhiyun  * IOCTL request handler to set/get generic IE.
1421*4882a593Smuzhiyun  *
1422*4882a593Smuzhiyun  * In addition to various generic IEs, this function can also be
1423*4882a593Smuzhiyun  * used to set the ARP filter.
1424*4882a593Smuzhiyun  */
mwifiex_misc_ioctl_gen_ie(struct mwifiex_private * priv,struct mwifiex_ds_misc_gen_ie * gen_ie,u16 action)1425*4882a593Smuzhiyun static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
1426*4882a593Smuzhiyun 				     struct mwifiex_ds_misc_gen_ie *gen_ie,
1427*4882a593Smuzhiyun 				     u16 action)
1428*4882a593Smuzhiyun {
1429*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	switch (gen_ie->type) {
1432*4882a593Smuzhiyun 	case MWIFIEX_IE_TYPE_GEN_IE:
1433*4882a593Smuzhiyun 		if (action == HostCmd_ACT_GEN_GET) {
1434*4882a593Smuzhiyun 			gen_ie->len = priv->wpa_ie_len;
1435*4882a593Smuzhiyun 			memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
1436*4882a593Smuzhiyun 		} else {
1437*4882a593Smuzhiyun 			mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
1438*4882a593Smuzhiyun 						  (u16) gen_ie->len);
1439*4882a593Smuzhiyun 		}
1440*4882a593Smuzhiyun 		break;
1441*4882a593Smuzhiyun 	case MWIFIEX_IE_TYPE_ARP_FILTER:
1442*4882a593Smuzhiyun 		memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
1443*4882a593Smuzhiyun 		if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
1444*4882a593Smuzhiyun 			adapter->arp_filter_size = 0;
1445*4882a593Smuzhiyun 			mwifiex_dbg(adapter, ERROR,
1446*4882a593Smuzhiyun 				    "invalid ARP filter size\n");
1447*4882a593Smuzhiyun 			return -1;
1448*4882a593Smuzhiyun 		} else {
1449*4882a593Smuzhiyun 			memcpy(adapter->arp_filter, gen_ie->ie_data,
1450*4882a593Smuzhiyun 			       gen_ie->len);
1451*4882a593Smuzhiyun 			adapter->arp_filter_size = gen_ie->len;
1452*4882a593Smuzhiyun 		}
1453*4882a593Smuzhiyun 		break;
1454*4882a593Smuzhiyun 	default:
1455*4882a593Smuzhiyun 		mwifiex_dbg(adapter, ERROR, "invalid IE type\n");
1456*4882a593Smuzhiyun 		return -1;
1457*4882a593Smuzhiyun 	}
1458*4882a593Smuzhiyun 	return 0;
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun /*
1462*4882a593Smuzhiyun  * Sends IOCTL request to set a generic IE.
1463*4882a593Smuzhiyun  *
1464*4882a593Smuzhiyun  * This function allocates the IOCTL request buffer, fills it
1465*4882a593Smuzhiyun  * with requisite parameters and calls the IOCTL handler.
1466*4882a593Smuzhiyun  */
1467*4882a593Smuzhiyun int
mwifiex_set_gen_ie(struct mwifiex_private * priv,const u8 * ie,int ie_len)1468*4882a593Smuzhiyun mwifiex_set_gen_ie(struct mwifiex_private *priv, const u8 *ie, int ie_len)
1469*4882a593Smuzhiyun {
1470*4882a593Smuzhiyun 	struct mwifiex_ds_misc_gen_ie gen_ie;
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	if (ie_len > IEEE_MAX_IE_SIZE)
1473*4882a593Smuzhiyun 		return -EFAULT;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
1476*4882a593Smuzhiyun 	gen_ie.len = ie_len;
1477*4882a593Smuzhiyun 	memcpy(gen_ie.ie_data, ie, ie_len);
1478*4882a593Smuzhiyun 	if (mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET))
1479*4882a593Smuzhiyun 		return -EFAULT;
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 	return 0;
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun /* This function get Host Sleep wake up reason.
1485*4882a593Smuzhiyun  *
1486*4882a593Smuzhiyun  */
mwifiex_get_wakeup_reason(struct mwifiex_private * priv,u16 action,int cmd_type,struct mwifiex_ds_wakeup_reason * wakeup_reason)1487*4882a593Smuzhiyun int mwifiex_get_wakeup_reason(struct mwifiex_private *priv, u16 action,
1488*4882a593Smuzhiyun 			      int cmd_type,
1489*4882a593Smuzhiyun 			      struct mwifiex_ds_wakeup_reason *wakeup_reason)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun 	int status = 0;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	status = mwifiex_send_cmd(priv, HostCmd_CMD_HS_WAKEUP_REASON,
1494*4882a593Smuzhiyun 				  HostCmd_ACT_GEN_GET, 0, wakeup_reason,
1495*4882a593Smuzhiyun 				  cmd_type == MWIFIEX_SYNC_CMD);
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	return status;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun 
mwifiex_get_chan_info(struct mwifiex_private * priv,struct mwifiex_channel_band * channel_band)1500*4882a593Smuzhiyun int mwifiex_get_chan_info(struct mwifiex_private *priv,
1501*4882a593Smuzhiyun 			  struct mwifiex_channel_band *channel_band)
1502*4882a593Smuzhiyun {
1503*4882a593Smuzhiyun 	int status = 0;
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 	status = mwifiex_send_cmd(priv, HostCmd_CMD_STA_CONFIGURE,
1506*4882a593Smuzhiyun 				  HostCmd_ACT_GEN_GET, 0, channel_band,
1507*4882a593Smuzhiyun 				  MWIFIEX_SYNC_CMD);
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun 	return status;
1510*4882a593Smuzhiyun }
1511