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