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