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