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