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