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