xref: /linux/drivers/net/wireless/microchip/wilc1000/cfg80211.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6 
7 #include "cfg80211.h"
8 
9 #define GO_NEG_REQ			0x00
10 #define GO_NEG_RSP			0x01
11 #define GO_NEG_CONF			0x02
12 #define P2P_INV_REQ			0x03
13 #define P2P_INV_RSP			0x04
14 
15 #define WILC_INVALID_CHANNEL		0
16 
17 /* Operation at 2.4 GHz with channels 1-13 */
18 #define WILC_WLAN_OPERATING_CLASS_2_4GHZ		0x51
19 
20 static const struct ieee80211_txrx_stypes
21 	wilc_wfi_cfg80211_mgmt_types[NUM_NL80211_IFTYPES] = {
22 	[NL80211_IFTYPE_STATION] = {
23 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
24 			BIT(IEEE80211_STYPE_AUTH >> 4),
25 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
26 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
27 			BIT(IEEE80211_STYPE_AUTH >> 4)
28 	},
29 	[NL80211_IFTYPE_AP] = {
30 		.tx = 0xffff,
31 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
32 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
33 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
34 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
35 			BIT(IEEE80211_STYPE_AUTH >> 4) |
36 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
37 			BIT(IEEE80211_STYPE_ACTION >> 4)
38 	},
39 	[NL80211_IFTYPE_P2P_CLIENT] = {
40 		.tx = 0xffff,
41 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
42 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
43 			BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
44 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
45 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
46 			BIT(IEEE80211_STYPE_AUTH >> 4) |
47 			BIT(IEEE80211_STYPE_DEAUTH >> 4)
48 	}
49 };
50 
51 #ifdef CONFIG_PM
52 static const struct wiphy_wowlan_support wowlan_support = {
53 	.flags = WIPHY_WOWLAN_ANY
54 };
55 #endif
56 
57 struct wilc_p2p_mgmt_data {
58 	int size;
59 	u8 *buff;
60 };
61 
62 struct wilc_p2p_pub_act_frame {
63 	u8 category;
64 	u8 action;
65 	u8 oui[3];
66 	u8 oui_type;
67 	u8 oui_subtype;
68 	u8 dialog_token;
69 	u8 elem[];
70 } __packed;
71 
72 struct wilc_vendor_specific_ie {
73 	u8 tag_number;
74 	u8 tag_len;
75 	u8 oui[3];
76 	u8 oui_type;
77 	u8 attr[];
78 } __packed;
79 
80 struct wilc_attr_entry {
81 	u8  attr_type;
82 	__le16 attr_len;
83 	u8 val[];
84 } __packed;
85 
86 struct wilc_attr_oper_ch {
87 	u8 attr_type;
88 	__le16 attr_len;
89 	u8 country_code[IEEE80211_COUNTRY_STRING_LEN];
90 	u8 op_class;
91 	u8 op_channel;
92 } __packed;
93 
94 struct wilc_attr_ch_list {
95 	u8 attr_type;
96 	__le16 attr_len;
97 	u8 country_code[IEEE80211_COUNTRY_STRING_LEN];
98 	u8 elem[];
99 } __packed;
100 
101 struct wilc_ch_list_elem {
102 	u8 op_class;
103 	u8 no_of_channels;
104 	u8 ch_list[];
105 } __packed;
106 
107 static void cfg_scan_result(enum scan_event scan_event,
108 			    struct wilc_rcvd_net_info *info,
109 			    struct wilc_priv *priv)
110 {
111 	if (!priv->cfg_scanning)
112 		return;
113 
114 	if (scan_event == SCAN_EVENT_NETWORK_FOUND) {
115 		s32 freq;
116 		struct ieee80211_channel *channel;
117 		struct cfg80211_bss *bss;
118 		struct wiphy *wiphy = priv->dev->ieee80211_ptr->wiphy;
119 
120 		if (!wiphy || !info)
121 			return;
122 
123 		freq = ieee80211_channel_to_frequency((s32)info->ch,
124 						      NL80211_BAND_2GHZ);
125 		channel = ieee80211_get_channel(wiphy, freq);
126 		if (!channel)
127 			return;
128 
129 		bss = cfg80211_inform_bss_frame(wiphy, channel, info->mgmt,
130 						info->frame_len,
131 						(s32)info->rssi * 100,
132 						GFP_KERNEL);
133 		cfg80211_put_bss(wiphy, bss);
134 	} else if (scan_event == SCAN_EVENT_DONE) {
135 		mutex_lock(&priv->scan_req_lock);
136 
137 		if (priv->scan_req) {
138 			struct cfg80211_scan_info info = {
139 				.aborted = false,
140 			};
141 
142 			cfg80211_scan_done(priv->scan_req, &info);
143 			priv->cfg_scanning = false;
144 			priv->scan_req = NULL;
145 		}
146 		mutex_unlock(&priv->scan_req_lock);
147 	} else if (scan_event == SCAN_EVENT_ABORTED) {
148 		mutex_lock(&priv->scan_req_lock);
149 
150 		if (priv->scan_req) {
151 			struct cfg80211_scan_info info = {
152 				.aborted = false,
153 			};
154 
155 			cfg80211_scan_done(priv->scan_req, &info);
156 			priv->cfg_scanning = false;
157 			priv->scan_req = NULL;
158 		}
159 		mutex_unlock(&priv->scan_req_lock);
160 	}
161 }
162 
163 static void cfg_connect_result(enum conn_event conn_disconn_evt, u8 mac_status,
164 			       struct wilc_priv *priv)
165 {
166 	struct net_device *dev = priv->dev;
167 	struct wilc_vif *vif = netdev_priv(dev);
168 	struct wilc *wl = vif->wilc;
169 	struct host_if_drv *wfi_drv = priv->hif_drv;
170 	struct wilc_conn_info *conn_info = &wfi_drv->conn_info;
171 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
172 
173 	vif->connecting = false;
174 
175 	if (conn_disconn_evt == CONN_DISCONN_EVENT_CONN_RESP) {
176 		u16 connect_status = conn_info->status;
177 
178 		if (mac_status == WILC_MAC_STATUS_DISCONNECTED &&
179 		    connect_status == WLAN_STATUS_SUCCESS) {
180 			connect_status = WLAN_STATUS_UNSPECIFIED_FAILURE;
181 			wilc_wlan_set_bssid(priv->dev, NULL, WILC_STATION_MODE);
182 
183 			if (vif->iftype != WILC_CLIENT_MODE)
184 				wl->sta_ch = WILC_INVALID_CHANNEL;
185 
186 			netdev_err(dev, "Unspecified failure\n");
187 		}
188 
189 		if (connect_status == WLAN_STATUS_SUCCESS)
190 			memcpy(priv->associated_bss, conn_info->bssid,
191 			       ETH_ALEN);
192 
193 		cfg80211_ref_bss(wiphy, vif->bss);
194 		cfg80211_connect_bss(dev, conn_info->bssid, vif->bss,
195 				     conn_info->req_ies,
196 				     conn_info->req_ies_len,
197 				     conn_info->resp_ies,
198 				     conn_info->resp_ies_len,
199 				     connect_status, GFP_KERNEL,
200 				     NL80211_TIMEOUT_UNSPECIFIED);
201 
202 		vif->bss = NULL;
203 	} else if (conn_disconn_evt == CONN_DISCONN_EVENT_DISCONN_NOTIF) {
204 		u16 reason = 0;
205 
206 		eth_zero_addr(priv->associated_bss);
207 		wilc_wlan_set_bssid(priv->dev, NULL, WILC_STATION_MODE);
208 
209 		if (vif->iftype != WILC_CLIENT_MODE) {
210 			wl->sta_ch = WILC_INVALID_CHANNEL;
211 		} else {
212 			if (wfi_drv->ifc_up)
213 				reason = 3;
214 			else
215 				reason = 1;
216 		}
217 
218 		cfg80211_disconnected(dev, reason, NULL, 0, false, GFP_KERNEL);
219 	}
220 }
221 
222 struct wilc_vif *wilc_get_wl_to_vif(struct wilc *wl)
223 {
224 	struct wilc_vif *vif;
225 
226 	vif = list_first_or_null_rcu(&wl->vif_list, typeof(*vif), list);
227 	if (!vif)
228 		return ERR_PTR(-EINVAL);
229 
230 	return vif;
231 }
232 
233 static int set_channel(struct wiphy *wiphy,
234 		       struct cfg80211_chan_def *chandef)
235 {
236 	struct wilc *wl = wiphy_priv(wiphy);
237 	struct wilc_vif *vif;
238 	u32 channelnum;
239 	int result;
240 
241 	rcu_read_lock();
242 	vif = wilc_get_wl_to_vif(wl);
243 	if (IS_ERR(vif)) {
244 		rcu_read_unlock();
245 		return PTR_ERR(vif);
246 	}
247 
248 	channelnum = ieee80211_frequency_to_channel(chandef->chan->center_freq);
249 
250 	wl->op_ch = channelnum;
251 	result = wilc_set_mac_chnl_num(vif, channelnum);
252 	if (result)
253 		netdev_err(vif->ndev, "Error in setting channel\n");
254 
255 	rcu_read_unlock();
256 	return result;
257 }
258 
259 static int scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
260 {
261 	struct wilc_vif *vif = netdev_priv(request->wdev->netdev);
262 	struct wilc_priv *priv = &vif->priv;
263 	u32 i;
264 	int ret = 0;
265 	u8 scan_ch_list[WILC_MAX_NUM_SCANNED_CH];
266 	u8 scan_type;
267 
268 	if (request->n_channels > WILC_MAX_NUM_SCANNED_CH) {
269 		netdev_err(vif->ndev, "Requested scanned channels over\n");
270 		return -EINVAL;
271 	}
272 
273 	priv->scan_req = request;
274 	priv->cfg_scanning = true;
275 	for (i = 0; i < request->n_channels; i++) {
276 		u16 freq = request->channels[i]->center_freq;
277 
278 		scan_ch_list[i] = ieee80211_frequency_to_channel(freq);
279 	}
280 
281 	if (request->n_ssids)
282 		scan_type = WILC_FW_ACTIVE_SCAN;
283 	else
284 		scan_type = WILC_FW_PASSIVE_SCAN;
285 
286 	ret = wilc_scan(vif, WILC_FW_USER_SCAN, scan_type,
287 			scan_ch_list, cfg_scan_result, request);
288 
289 	if (ret) {
290 		priv->scan_req = NULL;
291 		priv->cfg_scanning = false;
292 	}
293 
294 	return ret;
295 }
296 
297 static int connect(struct wiphy *wiphy, struct net_device *dev,
298 		   struct cfg80211_connect_params *sme)
299 {
300 	struct wilc_vif *vif = netdev_priv(dev);
301 	struct wilc_priv *priv = &vif->priv;
302 	struct host_if_drv *wfi_drv = priv->hif_drv;
303 	int ret;
304 	u32 i;
305 	u8 security = WILC_FW_SEC_NO;
306 	enum mfptype mfp_type = WILC_FW_MFP_NONE;
307 	enum authtype auth_type = WILC_FW_AUTH_ANY;
308 	u32 cipher_group;
309 	struct cfg80211_bss *bss;
310 	void *join_params;
311 	u8 ch;
312 
313 	vif->connecting = true;
314 
315 	cipher_group = sme->crypto.cipher_group;
316 	if (cipher_group != 0) {
317 		if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2) {
318 			if (cipher_group == WLAN_CIPHER_SUITE_TKIP)
319 				security = WILC_FW_SEC_WPA2_TKIP;
320 			else
321 				security = WILC_FW_SEC_WPA2_AES;
322 		} else if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_1) {
323 			if (cipher_group == WLAN_CIPHER_SUITE_TKIP)
324 				security = WILC_FW_SEC_WPA_TKIP;
325 			else
326 				security = WILC_FW_SEC_WPA_AES;
327 		} else {
328 			ret = -ENOTSUPP;
329 			netdev_err(dev, "%s: Unsupported cipher\n",
330 				   __func__);
331 			goto out_error;
332 		}
333 	}
334 
335 	if ((sme->crypto.wpa_versions & NL80211_WPA_VERSION_1) ||
336 	    (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2)) {
337 		for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++) {
338 			u32 ciphers_pairwise = sme->crypto.ciphers_pairwise[i];
339 
340 			if (ciphers_pairwise == WLAN_CIPHER_SUITE_TKIP)
341 				security |= WILC_FW_TKIP;
342 			else
343 				security |= WILC_FW_AES;
344 		}
345 	}
346 
347 	switch (sme->auth_type) {
348 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
349 		auth_type = WILC_FW_AUTH_OPEN_SYSTEM;
350 		break;
351 
352 	case NL80211_AUTHTYPE_SAE:
353 		auth_type = WILC_FW_AUTH_SAE;
354 		if (sme->ssid_len) {
355 			memcpy(vif->auth.ssid.ssid, sme->ssid, sme->ssid_len);
356 			vif->auth.ssid.ssid_len = sme->ssid_len;
357 		}
358 		vif->auth.key_mgmt_suite = sme->crypto.akm_suites[0];
359 		ether_addr_copy(vif->auth.bssid, sme->bssid);
360 		break;
361 
362 	default:
363 		break;
364 	}
365 
366 	if (sme->crypto.n_akm_suites) {
367 		if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_8021X)
368 			auth_type = WILC_FW_AUTH_IEEE8021;
369 		else if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_PSK_SHA256)
370 			auth_type = WILC_FW_AUTH_OPEN_SYSTEM_SHA256;
371 		else if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_8021X_SHA256)
372 			auth_type = WILC_FW_AUTH_IEE8021X_SHA256;
373 	}
374 
375 	if (wfi_drv->usr_scan_req.scan_result) {
376 		netdev_err(vif->ndev, "%s: Scan in progress\n", __func__);
377 		ret = -EBUSY;
378 		goto out_error;
379 	}
380 
381 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid, sme->ssid,
382 			       sme->ssid_len, IEEE80211_BSS_TYPE_ANY,
383 			       IEEE80211_PRIVACY(sme->privacy));
384 	if (!bss) {
385 		ret = -EINVAL;
386 		goto out_error;
387 	}
388 
389 	if (ether_addr_equal_unaligned(vif->bssid, bss->bssid)) {
390 		ret = -EALREADY;
391 		goto out_put_bss;
392 	}
393 
394 	join_params = wilc_parse_join_bss_param(bss, &sme->crypto);
395 	if (!join_params) {
396 		netdev_err(dev, "%s: failed to construct join param\n",
397 			   __func__);
398 		ret = -EINVAL;
399 		goto out_put_bss;
400 	}
401 
402 	ch = ieee80211_frequency_to_channel(bss->channel->center_freq);
403 	vif->wilc->op_ch = ch;
404 	if (vif->iftype != WILC_CLIENT_MODE)
405 		vif->wilc->sta_ch = ch;
406 
407 	wilc_wlan_set_bssid(dev, bss->bssid, WILC_STATION_MODE);
408 
409 	wfi_drv->conn_info.security = security;
410 	wfi_drv->conn_info.auth_type = auth_type;
411 	wfi_drv->conn_info.conn_result = cfg_connect_result;
412 	wfi_drv->conn_info.priv = priv;
413 	wfi_drv->conn_info.param = join_params;
414 
415 	if (sme->mfp == NL80211_MFP_OPTIONAL)
416 		mfp_type = WILC_FW_MFP_OPTIONAL;
417 	else if (sme->mfp == NL80211_MFP_REQUIRED)
418 		mfp_type = WILC_FW_MFP_REQUIRED;
419 
420 	wfi_drv->conn_info.mfp_type = mfp_type;
421 
422 	ret = wilc_set_join_req(vif, bss->bssid, sme->ie, sme->ie_len);
423 	if (ret) {
424 		netdev_err(dev, "wilc_set_join_req(): Error\n");
425 		ret = -ENOENT;
426 		if (vif->iftype != WILC_CLIENT_MODE)
427 			vif->wilc->sta_ch = WILC_INVALID_CHANNEL;
428 		wilc_wlan_set_bssid(dev, NULL, WILC_STATION_MODE);
429 		wfi_drv->conn_info.conn_result = NULL;
430 		kfree(join_params);
431 		goto out_put_bss;
432 	}
433 	kfree(join_params);
434 	vif->bss = bss;
435 	cfg80211_put_bss(wiphy, bss);
436 	return 0;
437 
438 out_put_bss:
439 	cfg80211_put_bss(wiphy, bss);
440 
441 out_error:
442 	vif->connecting = false;
443 	return ret;
444 }
445 
446 static int disconnect(struct wiphy *wiphy, struct net_device *dev,
447 		      u16 reason_code)
448 {
449 	struct wilc_vif *vif = netdev_priv(dev);
450 	struct wilc_priv *priv = &vif->priv;
451 	struct wilc *wilc = vif->wilc;
452 	int ret;
453 
454 	vif->connecting = false;
455 
456 	if (!wilc)
457 		return -EIO;
458 
459 	if (wilc->close) {
460 		/* already disconnected done */
461 		cfg80211_disconnected(dev, 0, NULL, 0, true, GFP_KERNEL);
462 		return 0;
463 	}
464 
465 	if (vif->iftype != WILC_CLIENT_MODE)
466 		wilc->sta_ch = WILC_INVALID_CHANNEL;
467 	wilc_wlan_set_bssid(priv->dev, NULL, WILC_STATION_MODE);
468 
469 	priv->hif_drv->p2p_timeout = 0;
470 
471 	ret = wilc_disconnect(vif);
472 	if (ret != 0) {
473 		netdev_err(priv->dev, "Error in disconnecting\n");
474 		ret = -EINVAL;
475 	}
476 
477 	vif->bss = NULL;
478 
479 	return ret;
480 }
481 
482 static int wilc_wfi_cfg_allocate_wpa_entry(struct wilc_priv *priv, u8 idx)
483 {
484 	if (!priv->wilc_gtk[idx]) {
485 		priv->wilc_gtk[idx] = kzalloc(sizeof(*priv->wilc_gtk[idx]),
486 					      GFP_KERNEL);
487 		if (!priv->wilc_gtk[idx])
488 			return -ENOMEM;
489 	}
490 
491 	if (!priv->wilc_ptk[idx]) {
492 		priv->wilc_ptk[idx] = kzalloc(sizeof(*priv->wilc_ptk[idx]),
493 					      GFP_KERNEL);
494 		if (!priv->wilc_ptk[idx])
495 			return -ENOMEM;
496 	}
497 
498 	return 0;
499 }
500 
501 static int wilc_wfi_cfg_allocate_wpa_igtk_entry(struct wilc_priv *priv, u8 idx)
502 {
503 	idx -= 4;
504 	if (!priv->wilc_igtk[idx]) {
505 		priv->wilc_igtk[idx] = kzalloc(sizeof(*priv->wilc_igtk[idx]),
506 					       GFP_KERNEL);
507 		if (!priv->wilc_igtk[idx])
508 			return -ENOMEM;
509 	}
510 	return 0;
511 }
512 
513 static int wilc_wfi_cfg_copy_wpa_info(struct wilc_wfi_key *key_info,
514 				      struct key_params *params)
515 {
516 	kfree(key_info->key);
517 
518 	key_info->key = kmemdup(params->key, params->key_len, GFP_KERNEL);
519 	if (!key_info->key)
520 		return -ENOMEM;
521 
522 	kfree(key_info->seq);
523 
524 	if (params->seq_len > 0) {
525 		key_info->seq = kmemdup(params->seq, params->seq_len,
526 					GFP_KERNEL);
527 		if (!key_info->seq)
528 			return -ENOMEM;
529 	}
530 
531 	key_info->cipher = params->cipher;
532 	key_info->key_len = params->key_len;
533 	key_info->seq_len = params->seq_len;
534 
535 	return 0;
536 }
537 
538 static int add_key(struct wiphy *wiphy, struct net_device *netdev, int link_id,
539 		   u8 key_index, bool pairwise, const u8 *mac_addr,
540 		   struct key_params *params)
541 
542 {
543 	int ret = 0, keylen = params->key_len;
544 	const u8 *rx_mic = NULL;
545 	const u8 *tx_mic = NULL;
546 	u8 mode = WILC_FW_SEC_NO;
547 	u8 op_mode;
548 	struct wilc_vif *vif = netdev_priv(netdev);
549 	struct wilc_priv *priv = &vif->priv;
550 	struct wilc_wfi_key *key;
551 
552 	switch (params->cipher) {
553 	case WLAN_CIPHER_SUITE_TKIP:
554 	case WLAN_CIPHER_SUITE_CCMP:
555 		if (priv->wdev.iftype == NL80211_IFTYPE_AP ||
556 		    priv->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
557 			struct wilc_wfi_key *key;
558 
559 			ret = wilc_wfi_cfg_allocate_wpa_entry(priv, key_index);
560 			if (ret)
561 				return -ENOMEM;
562 
563 			if (params->key_len > 16 &&
564 			    params->cipher == WLAN_CIPHER_SUITE_TKIP) {
565 				tx_mic = params->key + 24;
566 				rx_mic = params->key + 16;
567 				keylen = params->key_len - 16;
568 			}
569 
570 			if (!pairwise) {
571 				if (params->cipher == WLAN_CIPHER_SUITE_TKIP)
572 					mode = WILC_FW_SEC_WPA_TKIP;
573 				else
574 					mode = WILC_FW_SEC_WPA2_AES;
575 
576 				priv->wilc_groupkey = mode;
577 
578 				key = priv->wilc_gtk[key_index];
579 			} else {
580 				if (params->cipher == WLAN_CIPHER_SUITE_TKIP)
581 					mode = WILC_FW_SEC_WPA_TKIP;
582 				else
583 					mode = priv->wilc_groupkey | WILC_FW_AES;
584 
585 				key = priv->wilc_ptk[key_index];
586 			}
587 			ret = wilc_wfi_cfg_copy_wpa_info(key, params);
588 			if (ret)
589 				return -ENOMEM;
590 
591 			op_mode = WILC_AP_MODE;
592 		} else {
593 			if (params->key_len > 16 &&
594 			    params->cipher == WLAN_CIPHER_SUITE_TKIP) {
595 				rx_mic = params->key + 24;
596 				tx_mic = params->key + 16;
597 				keylen = params->key_len - 16;
598 			}
599 
600 			op_mode = WILC_STATION_MODE;
601 		}
602 
603 		if (!pairwise)
604 			ret = wilc_add_rx_gtk(vif, params->key, keylen,
605 					      key_index, params->seq_len,
606 					      params->seq, rx_mic, tx_mic,
607 					      op_mode, mode);
608 		else
609 			ret = wilc_add_ptk(vif, params->key, keylen, mac_addr,
610 					   rx_mic, tx_mic, op_mode, mode,
611 					   key_index);
612 
613 		break;
614 	case WLAN_CIPHER_SUITE_AES_CMAC:
615 		ret = wilc_wfi_cfg_allocate_wpa_igtk_entry(priv, key_index);
616 		if (ret)
617 			return -ENOMEM;
618 
619 		key = priv->wilc_igtk[key_index - 4];
620 		ret = wilc_wfi_cfg_copy_wpa_info(key, params);
621 		if (ret)
622 			return -ENOMEM;
623 
624 		if (priv->wdev.iftype == NL80211_IFTYPE_AP ||
625 		    priv->wdev.iftype == NL80211_IFTYPE_P2P_GO)
626 			op_mode = WILC_AP_MODE;
627 		else
628 			op_mode = WILC_STATION_MODE;
629 
630 		ret = wilc_add_igtk(vif, params->key, keylen, params->seq,
631 				    params->seq_len, mac_addr, op_mode,
632 				    key_index);
633 		break;
634 
635 	default:
636 		netdev_err(netdev, "%s: Unsupported cipher\n", __func__);
637 		ret = -ENOTSUPP;
638 	}
639 
640 	return ret;
641 }
642 
643 static int del_key(struct wiphy *wiphy, struct net_device *netdev, int link_id,
644 		   u8 key_index,
645 		   bool pairwise,
646 		   const u8 *mac_addr)
647 {
648 	struct wilc_vif *vif = netdev_priv(netdev);
649 	struct wilc_priv *priv = &vif->priv;
650 
651 	if (!pairwise && (key_index == 4 || key_index == 5)) {
652 		key_index -= 4;
653 		if (priv->wilc_igtk[key_index]) {
654 			kfree(priv->wilc_igtk[key_index]->key);
655 			priv->wilc_igtk[key_index]->key = NULL;
656 			kfree(priv->wilc_igtk[key_index]->seq);
657 			priv->wilc_igtk[key_index]->seq = NULL;
658 			kfree(priv->wilc_igtk[key_index]);
659 			priv->wilc_igtk[key_index] = NULL;
660 		}
661 	} else {
662 		if (priv->wilc_gtk[key_index]) {
663 			kfree(priv->wilc_gtk[key_index]->key);
664 			priv->wilc_gtk[key_index]->key = NULL;
665 			kfree(priv->wilc_gtk[key_index]->seq);
666 			priv->wilc_gtk[key_index]->seq = NULL;
667 
668 			kfree(priv->wilc_gtk[key_index]);
669 			priv->wilc_gtk[key_index] = NULL;
670 		}
671 		if (priv->wilc_ptk[key_index]) {
672 			kfree(priv->wilc_ptk[key_index]->key);
673 			priv->wilc_ptk[key_index]->key = NULL;
674 			kfree(priv->wilc_ptk[key_index]->seq);
675 			priv->wilc_ptk[key_index]->seq = NULL;
676 			kfree(priv->wilc_ptk[key_index]);
677 			priv->wilc_ptk[key_index] = NULL;
678 		}
679 	}
680 
681 	return 0;
682 }
683 
684 static int get_key(struct wiphy *wiphy, struct net_device *netdev, int link_id,
685 		   u8 key_index, bool pairwise, const u8 *mac_addr,
686 		   void *cookie,
687 		   void (*callback)(void *cookie, struct key_params *))
688 {
689 	struct wilc_vif *vif = netdev_priv(netdev);
690 	struct wilc_priv *priv = &vif->priv;
691 	struct  key_params key_params;
692 
693 	if (!pairwise) {
694 		if (key_index == 4 || key_index == 5) {
695 			key_index -= 4;
696 			key_params.key = priv->wilc_igtk[key_index]->key;
697 			key_params.cipher = priv->wilc_igtk[key_index]->cipher;
698 			key_params.key_len = priv->wilc_igtk[key_index]->key_len;
699 			key_params.seq = priv->wilc_igtk[key_index]->seq;
700 			key_params.seq_len = priv->wilc_igtk[key_index]->seq_len;
701 		} else {
702 			key_params.key = priv->wilc_gtk[key_index]->key;
703 			key_params.cipher = priv->wilc_gtk[key_index]->cipher;
704 			key_params.key_len = priv->wilc_gtk[key_index]->key_len;
705 			key_params.seq = priv->wilc_gtk[key_index]->seq;
706 			key_params.seq_len = priv->wilc_gtk[key_index]->seq_len;
707 		}
708 	} else {
709 		key_params.key = priv->wilc_ptk[key_index]->key;
710 		key_params.cipher = priv->wilc_ptk[key_index]->cipher;
711 		key_params.key_len = priv->wilc_ptk[key_index]->key_len;
712 		key_params.seq = priv->wilc_ptk[key_index]->seq;
713 		key_params.seq_len = priv->wilc_ptk[key_index]->seq_len;
714 	}
715 
716 	callback(cookie, &key_params);
717 
718 	return 0;
719 }
720 
721 /* wiphy_new_nm() will WARNON if not present */
722 static int set_default_key(struct wiphy *wiphy, struct net_device *netdev,
723 			   int link_id, u8 key_index, bool unicast,
724 			   bool multicast)
725 {
726 	return 0;
727 }
728 
729 static int set_default_mgmt_key(struct wiphy *wiphy, struct net_device *netdev,
730 				int link_id, u8 key_index)
731 {
732 	struct wilc_vif *vif = netdev_priv(netdev);
733 
734 	return wilc_set_default_mgmt_key_index(vif, key_index);
735 }
736 
737 static int get_station(struct wiphy *wiphy, struct net_device *dev,
738 		       const u8 *mac, struct station_info *sinfo)
739 {
740 	struct wilc_vif *vif = netdev_priv(dev);
741 	struct wilc_priv *priv = &vif->priv;
742 	struct wilc *wilc = vif->wilc;
743 	u32 i = 0;
744 	u32 associatedsta = ~0;
745 	u32 inactive_time = 0;
746 
747 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
748 		for (i = 0; i < NUM_STA_ASSOCIATED; i++) {
749 			if (!(memcmp(mac,
750 				     priv->assoc_stainfo.sta_associated_bss[i],
751 				     ETH_ALEN))) {
752 				associatedsta = i;
753 				break;
754 			}
755 		}
756 
757 		if (associatedsta == ~0) {
758 			netdev_err(dev, "sta required is not associated\n");
759 			return -ENOENT;
760 		}
761 
762 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME);
763 
764 		wilc_get_inactive_time(vif, mac, &inactive_time);
765 		sinfo->inactive_time = 1000 * inactive_time;
766 	} else if (vif->iftype == WILC_STATION_MODE) {
767 		struct rf_info stats;
768 
769 		if (!wilc->initialized)
770 			return -EBUSY;
771 
772 		wilc_get_statistics(vif, &stats);
773 
774 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL) |
775 				 BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
776 				 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
777 				 BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
778 				 BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
779 
780 		sinfo->signal = stats.rssi;
781 		sinfo->rx_packets = stats.rx_cnt;
782 		sinfo->tx_packets = stats.tx_cnt + stats.tx_fail_cnt;
783 		sinfo->tx_failed = stats.tx_fail_cnt;
784 		sinfo->txrate.legacy = stats.link_speed * 10;
785 
786 		if (stats.link_speed > TCP_ACK_FILTER_LINK_SPEED_THRESH &&
787 		    stats.link_speed != DEFAULT_LINK_SPEED)
788 			wilc_enable_tcp_ack_filter(vif, true);
789 		else if (stats.link_speed != DEFAULT_LINK_SPEED)
790 			wilc_enable_tcp_ack_filter(vif, false);
791 	}
792 	return 0;
793 }
794 
795 static int change_bss(struct wiphy *wiphy, struct net_device *dev,
796 		      struct bss_parameters *params)
797 {
798 	return 0;
799 }
800 
801 static int set_wiphy_params(struct wiphy *wiphy, u32 changed)
802 {
803 	int ret = -EINVAL;
804 	struct cfg_param_attr cfg_param_val;
805 	struct wilc *wl = wiphy_priv(wiphy);
806 	struct wilc_vif *vif;
807 	struct wilc_priv *priv;
808 
809 	rcu_read_lock();
810 	vif = wilc_get_wl_to_vif(wl);
811 	if (IS_ERR(vif))
812 		goto out;
813 
814 	priv = &vif->priv;
815 	cfg_param_val.flag = 0;
816 
817 	if (changed & WIPHY_PARAM_RETRY_SHORT) {
818 		netdev_dbg(vif->ndev,
819 			   "Setting WIPHY_PARAM_RETRY_SHORT %d\n",
820 			   wiphy->retry_short);
821 		cfg_param_val.flag  |= WILC_CFG_PARAM_RETRY_SHORT;
822 		cfg_param_val.short_retry_limit = wiphy->retry_short;
823 	}
824 	if (changed & WIPHY_PARAM_RETRY_LONG) {
825 		netdev_dbg(vif->ndev,
826 			   "Setting WIPHY_PARAM_RETRY_LONG %d\n",
827 			   wiphy->retry_long);
828 		cfg_param_val.flag |= WILC_CFG_PARAM_RETRY_LONG;
829 		cfg_param_val.long_retry_limit = wiphy->retry_long;
830 	}
831 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
832 		if (wiphy->frag_threshold > 255 &&
833 		    wiphy->frag_threshold < 7937) {
834 			netdev_dbg(vif->ndev,
835 				   "Setting WIPHY_PARAM_FRAG_THRESHOLD %d\n",
836 				   wiphy->frag_threshold);
837 			cfg_param_val.flag |= WILC_CFG_PARAM_FRAG_THRESHOLD;
838 			cfg_param_val.frag_threshold = wiphy->frag_threshold;
839 		} else {
840 			netdev_err(vif->ndev,
841 				   "Fragmentation threshold out of range\n");
842 			goto out;
843 		}
844 	}
845 
846 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
847 		if (wiphy->rts_threshold > 255) {
848 			netdev_dbg(vif->ndev,
849 				   "Setting WIPHY_PARAM_RTS_THRESHOLD %d\n",
850 				   wiphy->rts_threshold);
851 			cfg_param_val.flag |= WILC_CFG_PARAM_RTS_THRESHOLD;
852 			cfg_param_val.rts_threshold = wiphy->rts_threshold;
853 		} else {
854 			netdev_err(vif->ndev, "RTS threshold out of range\n");
855 			goto out;
856 		}
857 	}
858 
859 	ret = wilc_hif_set_cfg(vif, &cfg_param_val);
860 	if (ret)
861 		netdev_err(priv->dev, "Error in setting WIPHY PARAMS\n");
862 
863 out:
864 	rcu_read_unlock();
865 	return ret;
866 }
867 
868 static int set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
869 		     struct cfg80211_pmksa *pmksa)
870 {
871 	struct wilc_vif *vif = netdev_priv(netdev);
872 	struct wilc_priv *priv = &vif->priv;
873 	u32 i;
874 	int ret = 0;
875 	u8 flag = 0;
876 
877 	for (i = 0; i < priv->pmkid_list.numpmkid; i++)	{
878 		if (!memcmp(pmksa->bssid, priv->pmkid_list.pmkidlist[i].bssid,
879 			    ETH_ALEN)) {
880 			flag = PMKID_FOUND;
881 			break;
882 		}
883 	}
884 	if (i < WILC_MAX_NUM_PMKIDS) {
885 		memcpy(priv->pmkid_list.pmkidlist[i].bssid, pmksa->bssid,
886 		       ETH_ALEN);
887 		memcpy(priv->pmkid_list.pmkidlist[i].pmkid, pmksa->pmkid,
888 		       WLAN_PMKID_LEN);
889 		if (!(flag == PMKID_FOUND))
890 			priv->pmkid_list.numpmkid++;
891 	} else {
892 		netdev_err(netdev, "Invalid PMKID index\n");
893 		ret = -EINVAL;
894 	}
895 
896 	if (!ret)
897 		ret = wilc_set_pmkid_info(vif, &priv->pmkid_list);
898 
899 	return ret;
900 }
901 
902 static int del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
903 		     struct cfg80211_pmksa *pmksa)
904 {
905 	u32 i;
906 	struct wilc_vif *vif = netdev_priv(netdev);
907 	struct wilc_priv *priv = &vif->priv;
908 
909 	for (i = 0; i < priv->pmkid_list.numpmkid; i++)	{
910 		if (!memcmp(pmksa->bssid, priv->pmkid_list.pmkidlist[i].bssid,
911 			    ETH_ALEN)) {
912 			memset(&priv->pmkid_list.pmkidlist[i], 0,
913 			       sizeof(struct wilc_pmkid));
914 			break;
915 		}
916 	}
917 
918 	if (i == priv->pmkid_list.numpmkid)
919 		return -EINVAL;
920 
921 	for (; i < (priv->pmkid_list.numpmkid - 1); i++) {
922 		memcpy(priv->pmkid_list.pmkidlist[i].bssid,
923 		       priv->pmkid_list.pmkidlist[i + 1].bssid,
924 		       ETH_ALEN);
925 		memcpy(priv->pmkid_list.pmkidlist[i].pmkid,
926 		       priv->pmkid_list.pmkidlist[i + 1].pmkid,
927 		       WLAN_PMKID_LEN);
928 	}
929 	priv->pmkid_list.numpmkid--;
930 
931 	return 0;
932 }
933 
934 static int flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
935 {
936 	struct wilc_vif *vif = netdev_priv(netdev);
937 
938 	memset(&vif->priv.pmkid_list, 0, sizeof(struct wilc_pmkid_attr));
939 
940 	return 0;
941 }
942 
943 static inline void wilc_wfi_cfg_parse_ch_attr(u8 *buf, u32 len, u8 sta_ch)
944 {
945 	struct wilc_attr_entry *e;
946 	struct wilc_attr_ch_list *ch_list;
947 	struct wilc_attr_oper_ch *op_ch;
948 	u32 index = 0;
949 	u8 ch_list_idx = 0;
950 	u8 op_ch_idx = 0;
951 
952 	if (sta_ch == WILC_INVALID_CHANNEL)
953 		return;
954 
955 	while (index + sizeof(*e) <= len) {
956 		u16 attr_size;
957 
958 		e = (struct wilc_attr_entry *)&buf[index];
959 		attr_size = le16_to_cpu(e->attr_len);
960 
961 		if (index + sizeof(*e) + attr_size > len)
962 			return;
963 
964 		if (e->attr_type == IEEE80211_P2P_ATTR_CHANNEL_LIST &&
965 		    attr_size >= (sizeof(struct wilc_attr_ch_list) - sizeof(*e)))
966 			ch_list_idx = index;
967 		else if (e->attr_type == IEEE80211_P2P_ATTR_OPER_CHANNEL &&
968 			 attr_size == (sizeof(struct wilc_attr_oper_ch) - sizeof(*e)))
969 			op_ch_idx = index;
970 
971 		if (ch_list_idx && op_ch_idx)
972 			break;
973 
974 		index += sizeof(*e) + attr_size;
975 	}
976 
977 	if (ch_list_idx) {
978 		u16 elem_size;
979 
980 		ch_list = (struct wilc_attr_ch_list *)&buf[ch_list_idx];
981 		/* the number of bytes following the final 'elem' member */
982 		elem_size = le16_to_cpu(ch_list->attr_len) -
983 			(sizeof(*ch_list) - sizeof(struct wilc_attr_entry));
984 		for (unsigned int i = 0; i < elem_size;) {
985 			struct wilc_ch_list_elem *e;
986 
987 			e = (struct wilc_ch_list_elem *)(ch_list->elem + i);
988 
989 			i += sizeof(*e);
990 			if (i > elem_size)
991 				break;
992 
993 			i += e->no_of_channels;
994 			if (i > elem_size)
995 				break;
996 
997 			if (e->op_class == WILC_WLAN_OPERATING_CLASS_2_4GHZ) {
998 				memset(e->ch_list, sta_ch, e->no_of_channels);
999 				break;
1000 			}
1001 		}
1002 	}
1003 
1004 	if (op_ch_idx) {
1005 		op_ch = (struct wilc_attr_oper_ch *)&buf[op_ch_idx];
1006 		op_ch->op_class = WILC_WLAN_OPERATING_CLASS_2_4GHZ;
1007 		op_ch->op_channel = sta_ch;
1008 	}
1009 }
1010 
1011 bool wilc_wfi_mgmt_frame_rx(struct wilc_vif *vif, u8 *buff, u32 size)
1012 {
1013 	struct wilc *wl = vif->wilc;
1014 	struct wilc_priv *priv = &vif->priv;
1015 	int freq;
1016 
1017 	freq = ieee80211_channel_to_frequency(wl->op_ch, NL80211_BAND_2GHZ);
1018 
1019 	return cfg80211_rx_mgmt(&priv->wdev, freq, 0, buff, size, 0);
1020 }
1021 
1022 void wilc_wfi_p2p_rx(struct wilc_vif *vif, u8 *buff, u32 size)
1023 {
1024 	struct wilc *wl = vif->wilc;
1025 	struct wilc_priv *priv = &vif->priv;
1026 	struct host_if_drv *wfi_drv = priv->hif_drv;
1027 	struct ieee80211_mgmt *mgmt;
1028 	struct wilc_vendor_specific_ie *p;
1029 	struct wilc_p2p_pub_act_frame *d;
1030 	int ie_offset = offsetof(struct ieee80211_mgmt, u) + sizeof(*d);
1031 	const u8 *vendor_ie;
1032 	u32 header, pkt_offset;
1033 	s32 freq;
1034 
1035 	header = get_unaligned_le32(buff - HOST_HDR_OFFSET);
1036 	pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
1037 
1038 	if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
1039 		bool ack = false;
1040 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)buff;
1041 
1042 		if (ieee80211_is_probe_resp(hdr->frame_control) ||
1043 		    pkt_offset & IS_MGMT_STATUS_SUCCES)
1044 			ack = true;
1045 
1046 		cfg80211_mgmt_tx_status(&priv->wdev, priv->tx_cookie, buff,
1047 					size, ack, GFP_KERNEL);
1048 		return;
1049 	}
1050 
1051 	freq = ieee80211_channel_to_frequency(wl->op_ch, NL80211_BAND_2GHZ);
1052 
1053 	mgmt = (struct ieee80211_mgmt *)buff;
1054 	if (!ieee80211_is_action(mgmt->frame_control))
1055 		goto out_rx_mgmt;
1056 
1057 	if (priv->cfg_scanning &&
1058 	    time_after_eq(jiffies, (unsigned long)wfi_drv->p2p_timeout)) {
1059 		netdev_dbg(vif->ndev, "Receiving action wrong ch\n");
1060 		return;
1061 	}
1062 
1063 	if (!ieee80211_is_public_action((struct ieee80211_hdr *)buff, size))
1064 		goto out_rx_mgmt;
1065 
1066 	d = (struct wilc_p2p_pub_act_frame *)(&mgmt->u.action);
1067 	if (d->oui_subtype != GO_NEG_REQ && d->oui_subtype != GO_NEG_RSP &&
1068 	    d->oui_subtype != P2P_INV_REQ && d->oui_subtype != P2P_INV_RSP)
1069 		goto out_rx_mgmt;
1070 
1071 	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1072 					    buff + ie_offset, size - ie_offset);
1073 	if (!vendor_ie)
1074 		goto out_rx_mgmt;
1075 
1076 	p = (struct wilc_vendor_specific_ie *)vendor_ie;
1077 	wilc_wfi_cfg_parse_ch_attr(p->attr, p->tag_len - 4, vif->wilc->sta_ch);
1078 
1079 out_rx_mgmt:
1080 	cfg80211_rx_mgmt(&priv->wdev, freq, 0, buff, size, 0);
1081 }
1082 
1083 static void wilc_wfi_mgmt_tx_complete(void *priv, int status)
1084 {
1085 	struct wilc_p2p_mgmt_data *pv_data = priv;
1086 
1087 	kfree(pv_data->buff);
1088 	kfree(pv_data);
1089 }
1090 
1091 static void wilc_wfi_remain_on_channel_expired(struct wilc_vif *vif, u64 cookie)
1092 {
1093 	struct wilc_priv *priv = &vif->priv;
1094 	struct wilc_wfi_p2p_listen_params *params = &priv->remain_on_ch_params;
1095 
1096 	if (cookie != params->listen_cookie)
1097 		return;
1098 
1099 	priv->p2p_listen_state = false;
1100 
1101 	cfg80211_remain_on_channel_expired(&priv->wdev, params->listen_cookie,
1102 					   params->listen_ch, GFP_KERNEL);
1103 }
1104 
1105 static int remain_on_channel(struct wiphy *wiphy,
1106 			     struct wireless_dev *wdev,
1107 			     struct ieee80211_channel *chan,
1108 			     unsigned int duration, u64 *cookie)
1109 {
1110 	int ret = 0;
1111 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1112 	struct wilc_priv *priv = &vif->priv;
1113 	u64 id;
1114 
1115 	if (wdev->iftype == NL80211_IFTYPE_AP) {
1116 		netdev_dbg(vif->ndev, "Required while in AP mode\n");
1117 		return ret;
1118 	}
1119 
1120 	id = ++priv->inc_roc_cookie;
1121 	if (id == 0)
1122 		id = ++priv->inc_roc_cookie;
1123 
1124 	ret = wilc_remain_on_channel(vif, id, chan->hw_value,
1125 				     wilc_wfi_remain_on_channel_expired);
1126 	if (ret)
1127 		return ret;
1128 
1129 	vif->wilc->op_ch = chan->hw_value;
1130 
1131 	priv->remain_on_ch_params.listen_ch = chan;
1132 	priv->remain_on_ch_params.listen_cookie = id;
1133 	*cookie = id;
1134 	priv->p2p_listen_state = true;
1135 	priv->remain_on_ch_params.listen_duration = duration;
1136 
1137 	cfg80211_ready_on_channel(wdev, *cookie, chan, duration, GFP_KERNEL);
1138 	mod_timer(&vif->hif_drv->remain_on_ch_timer,
1139 		  jiffies + msecs_to_jiffies(duration + 1000));
1140 
1141 	return ret;
1142 }
1143 
1144 static int cancel_remain_on_channel(struct wiphy *wiphy,
1145 				    struct wireless_dev *wdev,
1146 				    u64 cookie)
1147 {
1148 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1149 	struct wilc_priv *priv = &vif->priv;
1150 
1151 	if (cookie != priv->remain_on_ch_params.listen_cookie)
1152 		return -ENOENT;
1153 
1154 	return wilc_listen_state_expired(vif, cookie);
1155 }
1156 
1157 static int mgmt_tx(struct wiphy *wiphy,
1158 		   struct wireless_dev *wdev,
1159 		   struct cfg80211_mgmt_tx_params *params,
1160 		   u64 *cookie)
1161 {
1162 	struct ieee80211_channel *chan = params->chan;
1163 	unsigned int wait = params->wait;
1164 	const u8 *buf = params->buf;
1165 	size_t len = params->len;
1166 	const struct ieee80211_mgmt *mgmt;
1167 	struct wilc_p2p_mgmt_data *mgmt_tx;
1168 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1169 	struct wilc_priv *priv = &vif->priv;
1170 	struct host_if_drv *wfi_drv = priv->hif_drv;
1171 	struct wilc_vendor_specific_ie *p;
1172 	struct wilc_p2p_pub_act_frame *d;
1173 	int ie_offset = offsetof(struct ieee80211_mgmt, u) + sizeof(*d);
1174 	const u8 *vendor_ie;
1175 	int ret = 0;
1176 
1177 	*cookie = get_random_u32();
1178 	priv->tx_cookie = *cookie;
1179 	mgmt = (const struct ieee80211_mgmt *)buf;
1180 
1181 	if (!ieee80211_is_mgmt(mgmt->frame_control))
1182 		goto out;
1183 
1184 	mgmt_tx = kmalloc(sizeof(*mgmt_tx), GFP_KERNEL);
1185 	if (!mgmt_tx) {
1186 		ret = -ENOMEM;
1187 		goto out;
1188 	}
1189 
1190 	mgmt_tx->buff = kmemdup(buf, len, GFP_KERNEL);
1191 	if (!mgmt_tx->buff) {
1192 		ret = -ENOMEM;
1193 		kfree(mgmt_tx);
1194 		goto out;
1195 	}
1196 
1197 	mgmt_tx->size = len;
1198 
1199 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1200 		wilc_set_mac_chnl_num(vif, chan->hw_value);
1201 		vif->wilc->op_ch = chan->hw_value;
1202 		goto out_txq_add_pkt;
1203 	}
1204 
1205 	if (!ieee80211_is_public_action((struct ieee80211_hdr *)buf, len)) {
1206 		if (chan)
1207 			wilc_set_mac_chnl_num(vif, chan->hw_value);
1208 		else
1209 			wilc_set_mac_chnl_num(vif, vif->wilc->op_ch);
1210 
1211 		goto out_set_timeout;
1212 	}
1213 
1214 	d = (struct wilc_p2p_pub_act_frame *)(&mgmt->u.action);
1215 	if (d->oui_type != WLAN_OUI_TYPE_WFA_P2P ||
1216 	    d->oui_subtype != GO_NEG_CONF) {
1217 		wilc_set_mac_chnl_num(vif, chan->hw_value);
1218 		vif->wilc->op_ch = chan->hw_value;
1219 	}
1220 
1221 	if (d->oui_subtype != P2P_INV_REQ && d->oui_subtype != P2P_INV_RSP)
1222 		goto out_set_timeout;
1223 
1224 	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1225 					    mgmt_tx->buff + ie_offset,
1226 					    len - ie_offset);
1227 	if (!vendor_ie)
1228 		goto out_set_timeout;
1229 
1230 	p = (struct wilc_vendor_specific_ie *)vendor_ie;
1231 	wilc_wfi_cfg_parse_ch_attr(p->attr, p->tag_len - 4, vif->wilc->sta_ch);
1232 
1233 out_set_timeout:
1234 	wfi_drv->p2p_timeout = (jiffies + msecs_to_jiffies(wait));
1235 
1236 out_txq_add_pkt:
1237 
1238 	wilc_wlan_txq_add_mgmt_pkt(wdev->netdev, mgmt_tx,
1239 				   mgmt_tx->buff, mgmt_tx->size,
1240 				   wilc_wfi_mgmt_tx_complete);
1241 
1242 out:
1243 
1244 	return ret;
1245 }
1246 
1247 static int mgmt_tx_cancel_wait(struct wiphy *wiphy,
1248 			       struct wireless_dev *wdev,
1249 			       u64 cookie)
1250 {
1251 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1252 	struct wilc_priv *priv = &vif->priv;
1253 	struct host_if_drv *wfi_drv = priv->hif_drv;
1254 
1255 	wfi_drv->p2p_timeout = jiffies;
1256 
1257 	if (!priv->p2p_listen_state) {
1258 		struct wilc_wfi_p2p_listen_params *params;
1259 
1260 		params = &priv->remain_on_ch_params;
1261 
1262 		cfg80211_remain_on_channel_expired(wdev,
1263 						   params->listen_cookie,
1264 						   params->listen_ch,
1265 						   GFP_KERNEL);
1266 	}
1267 
1268 	return 0;
1269 }
1270 
1271 void wilc_update_mgmt_frame_registrations(struct wiphy *wiphy,
1272 					  struct wireless_dev *wdev,
1273 					  struct mgmt_frame_regs *upd)
1274 {
1275 	struct wilc *wl = wiphy_priv(wiphy);
1276 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1277 	u32 presp_bit = BIT(IEEE80211_STYPE_PROBE_REQ >> 4);
1278 	u32 action_bit = BIT(IEEE80211_STYPE_ACTION >> 4);
1279 	u32 pauth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
1280 
1281 	if (wl->initialized) {
1282 		bool prev = vif->mgmt_reg_stypes & presp_bit;
1283 		bool now = upd->interface_stypes & presp_bit;
1284 
1285 		if (now != prev)
1286 			wilc_frame_register(vif, IEEE80211_STYPE_PROBE_REQ, now);
1287 
1288 		prev = vif->mgmt_reg_stypes & action_bit;
1289 		now = upd->interface_stypes & action_bit;
1290 
1291 		if (now != prev)
1292 			wilc_frame_register(vif, IEEE80211_STYPE_ACTION, now);
1293 
1294 		prev = vif->mgmt_reg_stypes & pauth_bit;
1295 		now = upd->interface_stypes & pauth_bit;
1296 		if (now != prev)
1297 			wilc_frame_register(vif, IEEE80211_STYPE_AUTH, now);
1298 	}
1299 
1300 	vif->mgmt_reg_stypes =
1301 		upd->interface_stypes & (presp_bit | action_bit | pauth_bit);
1302 }
1303 
1304 static int external_auth(struct wiphy *wiphy, struct net_device *dev,
1305 			 struct cfg80211_external_auth_params *auth)
1306 {
1307 	struct wilc_vif *vif = netdev_priv(dev);
1308 
1309 	if (auth->status == WLAN_STATUS_SUCCESS)
1310 		wilc_set_external_auth_param(vif, auth);
1311 
1312 	return 0;
1313 }
1314 
1315 static int set_cqm_rssi_config(struct wiphy *wiphy, struct net_device *dev,
1316 			       s32 rssi_thold, u32 rssi_hyst)
1317 {
1318 	return 0;
1319 }
1320 
1321 static int dump_station(struct wiphy *wiphy, struct net_device *dev,
1322 			int idx, u8 *mac, struct station_info *sinfo)
1323 {
1324 	struct wilc_vif *vif = netdev_priv(dev);
1325 	int ret;
1326 
1327 	if (idx != 0)
1328 		return -ENOENT;
1329 
1330 	ret = wilc_get_rssi(vif, &sinfo->signal);
1331 	if (ret)
1332 		return ret;
1333 
1334 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1335 	memcpy(mac, vif->priv.associated_bss, ETH_ALEN);
1336 	return 0;
1337 }
1338 
1339 static int set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1340 			  bool enabled, int timeout)
1341 {
1342 	struct wilc_vif *vif = netdev_priv(dev);
1343 	struct wilc_priv *priv = &vif->priv;
1344 
1345 	if (!priv->hif_drv)
1346 		return -EIO;
1347 
1348 	wilc_set_power_mgmt(vif, enabled, timeout);
1349 
1350 	return 0;
1351 }
1352 
1353 static int change_virtual_intf(struct wiphy *wiphy, struct net_device *dev,
1354 			       enum nl80211_iftype type,
1355 			       struct vif_params *params)
1356 {
1357 	struct wilc *wl = wiphy_priv(wiphy);
1358 	struct wilc_vif *vif = netdev_priv(dev);
1359 	struct wilc_priv *priv = &vif->priv;
1360 
1361 	switch (type) {
1362 	case NL80211_IFTYPE_STATION:
1363 		vif->connecting = false;
1364 		dev->ieee80211_ptr->iftype = type;
1365 		priv->wdev.iftype = type;
1366 		vif->monitor_flag = 0;
1367 		if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE)
1368 			wilc_wfi_deinit_mon_interface(wl, true);
1369 		vif->iftype = WILC_STATION_MODE;
1370 
1371 		if (wl->initialized)
1372 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1373 						WILC_STATION_MODE, vif->idx);
1374 
1375 		memset(priv->assoc_stainfo.sta_associated_bss, 0,
1376 		       WILC_MAX_NUM_STA * ETH_ALEN);
1377 		break;
1378 
1379 	case NL80211_IFTYPE_P2P_CLIENT:
1380 		vif->connecting = false;
1381 		dev->ieee80211_ptr->iftype = type;
1382 		priv->wdev.iftype = type;
1383 		vif->monitor_flag = 0;
1384 		vif->iftype = WILC_CLIENT_MODE;
1385 
1386 		if (wl->initialized)
1387 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1388 						WILC_STATION_MODE, vif->idx);
1389 		break;
1390 
1391 	case NL80211_IFTYPE_AP:
1392 		dev->ieee80211_ptr->iftype = type;
1393 		priv->wdev.iftype = type;
1394 		vif->iftype = WILC_AP_MODE;
1395 
1396 		if (wl->initialized)
1397 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1398 						WILC_AP_MODE, vif->idx);
1399 		break;
1400 
1401 	case NL80211_IFTYPE_P2P_GO:
1402 		dev->ieee80211_ptr->iftype = type;
1403 		priv->wdev.iftype = type;
1404 		vif->iftype = WILC_GO_MODE;
1405 
1406 		if (wl->initialized)
1407 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1408 						WILC_AP_MODE, vif->idx);
1409 		break;
1410 
1411 	default:
1412 		netdev_err(dev, "Unknown interface type= %d\n", type);
1413 		return -EINVAL;
1414 	}
1415 
1416 	return 0;
1417 }
1418 
1419 static int start_ap(struct wiphy *wiphy, struct net_device *dev,
1420 		    struct cfg80211_ap_settings *settings)
1421 {
1422 	struct wilc_vif *vif = netdev_priv(dev);
1423 	int ret;
1424 
1425 	ret = set_channel(wiphy, &settings->chandef);
1426 	if (ret != 0)
1427 		netdev_err(dev, "Error in setting channel\n");
1428 
1429 	wilc_wlan_set_bssid(dev, dev->dev_addr, WILC_AP_MODE);
1430 
1431 	return wilc_add_beacon(vif, settings->beacon_interval,
1432 				   settings->dtim_period, &settings->beacon);
1433 }
1434 
1435 static int change_beacon(struct wiphy *wiphy, struct net_device *dev,
1436 			 struct cfg80211_ap_update *params)
1437 {
1438 	struct wilc_vif *vif = netdev_priv(dev);
1439 
1440 	return wilc_add_beacon(vif, 0, 0, &params->beacon);
1441 }
1442 
1443 static int stop_ap(struct wiphy *wiphy, struct net_device *dev,
1444 		   unsigned int link_id)
1445 {
1446 	int ret;
1447 	struct wilc_vif *vif = netdev_priv(dev);
1448 
1449 	wilc_wlan_set_bssid(dev, NULL, WILC_AP_MODE);
1450 
1451 	ret = wilc_del_beacon(vif);
1452 
1453 	if (ret)
1454 		netdev_err(dev, "Host delete beacon fail\n");
1455 
1456 	return ret;
1457 }
1458 
1459 static int add_station(struct wiphy *wiphy, struct net_device *dev,
1460 		       const u8 *mac, struct station_parameters *params)
1461 {
1462 	int ret = 0;
1463 	struct wilc_vif *vif = netdev_priv(dev);
1464 	struct wilc_priv *priv = &vif->priv;
1465 
1466 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
1467 		memcpy(priv->assoc_stainfo.sta_associated_bss[params->aid], mac,
1468 		       ETH_ALEN);
1469 
1470 		ret = wilc_add_station(vif, mac, params);
1471 		if (ret)
1472 			netdev_err(dev, "Host add station fail\n");
1473 	}
1474 
1475 	return ret;
1476 }
1477 
1478 static int del_station(struct wiphy *wiphy, struct net_device *dev,
1479 		       struct station_del_parameters *params)
1480 {
1481 	const u8 *mac = params->mac;
1482 	int ret = 0;
1483 	struct wilc_vif *vif = netdev_priv(dev);
1484 	struct wilc_priv *priv = &vif->priv;
1485 	struct sta_info *info;
1486 
1487 	if (!(vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE))
1488 		return ret;
1489 
1490 	info = &priv->assoc_stainfo;
1491 
1492 	if (!mac)
1493 		ret = wilc_del_allstation(vif, info->sta_associated_bss);
1494 
1495 	ret = wilc_del_station(vif, mac);
1496 	if (ret)
1497 		netdev_err(dev, "Host delete station fail\n");
1498 	return ret;
1499 }
1500 
1501 static int change_station(struct wiphy *wiphy, struct net_device *dev,
1502 			  const u8 *mac, struct station_parameters *params)
1503 {
1504 	int ret = 0;
1505 	struct wilc_vif *vif = netdev_priv(dev);
1506 
1507 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
1508 		ret = wilc_edit_station(vif, mac, params);
1509 		if (ret)
1510 			netdev_err(dev, "Host edit station fail\n");
1511 	}
1512 	return ret;
1513 }
1514 
1515 static struct wilc_vif *wilc_get_vif_from_type(struct wilc *wl, int type)
1516 {
1517 	struct wilc_vif *vif;
1518 
1519 	wilc_for_each_vif(wl, vif) {
1520 		if (vif->iftype == type)
1521 			return vif;
1522 	}
1523 
1524 	return NULL;
1525 }
1526 
1527 static struct wireless_dev *add_virtual_intf(struct wiphy *wiphy,
1528 					     const char *name,
1529 					     unsigned char name_assign_type,
1530 					     enum nl80211_iftype type,
1531 					     struct vif_params *params)
1532 {
1533 	struct wilc *wl = wiphy_priv(wiphy);
1534 	struct wilc_vif *vif;
1535 	struct wireless_dev *wdev;
1536 	int iftype;
1537 
1538 	if (type == NL80211_IFTYPE_MONITOR) {
1539 		struct net_device *ndev;
1540 
1541 		rcu_read_lock();
1542 		vif = wilc_get_vif_from_type(wl, WILC_AP_MODE);
1543 		if (!vif) {
1544 			vif = wilc_get_vif_from_type(wl, WILC_GO_MODE);
1545 			if (!vif) {
1546 				rcu_read_unlock();
1547 				goto validate_interface;
1548 			}
1549 		}
1550 
1551 		if (vif->monitor_flag) {
1552 			rcu_read_unlock();
1553 			goto validate_interface;
1554 		}
1555 
1556 		ndev = wilc_wfi_init_mon_interface(wl, name, vif->ndev);
1557 		if (ndev) {
1558 			vif->monitor_flag = 1;
1559 		} else {
1560 			rcu_read_unlock();
1561 			return ERR_PTR(-EINVAL);
1562 		}
1563 
1564 		wdev = &vif->priv.wdev;
1565 		rcu_read_unlock();
1566 		return wdev;
1567 	}
1568 
1569 validate_interface:
1570 	mutex_lock(&wl->vif_mutex);
1571 	if (wl->vif_num == WILC_NUM_CONCURRENT_IFC) {
1572 		pr_err("Reached maximum number of interface\n");
1573 		mutex_unlock(&wl->vif_mutex);
1574 		return ERR_PTR(-EINVAL);
1575 	}
1576 	mutex_unlock(&wl->vif_mutex);
1577 
1578 	switch (type) {
1579 	case NL80211_IFTYPE_STATION:
1580 		iftype = WILC_STATION_MODE;
1581 		break;
1582 	case NL80211_IFTYPE_AP:
1583 		iftype = WILC_AP_MODE;
1584 		break;
1585 	default:
1586 		return ERR_PTR(-EOPNOTSUPP);
1587 	}
1588 
1589 	vif = wilc_netdev_ifc_init(wl, name, iftype, type, true);
1590 	if (IS_ERR(vif))
1591 		return ERR_CAST(vif);
1592 
1593 	return &vif->priv.wdev;
1594 }
1595 
1596 static int del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
1597 {
1598 	struct wilc *wl = wiphy_priv(wiphy);
1599 	struct wilc_vif *vif;
1600 
1601 	if (wdev->iftype == NL80211_IFTYPE_AP ||
1602 	    wdev->iftype == NL80211_IFTYPE_P2P_GO)
1603 		wilc_wfi_deinit_mon_interface(wl, true);
1604 	vif = netdev_priv(wdev->netdev);
1605 	cfg80211_stop_iface(wiphy, wdev, GFP_KERNEL);
1606 	cfg80211_unregister_netdevice(vif->ndev);
1607 	vif->monitor_flag = 0;
1608 
1609 	mutex_lock(&wl->vif_mutex);
1610 	list_del_rcu(&vif->list);
1611 	wl->vif_num--;
1612 	mutex_unlock(&wl->vif_mutex);
1613 	synchronize_rcu();
1614 	return 0;
1615 }
1616 
1617 static int wilc_suspend(struct wiphy *wiphy, struct cfg80211_wowlan *wow)
1618 {
1619 	struct wilc *wl = wiphy_priv(wiphy);
1620 
1621 	if (!wow && wilc_wlan_get_num_conn_ifcs(wl))
1622 		wl->suspend_event = true;
1623 	else
1624 		wl->suspend_event = false;
1625 
1626 	return 0;
1627 }
1628 
1629 static int wilc_resume(struct wiphy *wiphy)
1630 {
1631 	return 0;
1632 }
1633 
1634 static void wilc_set_wakeup(struct wiphy *wiphy, bool enabled)
1635 {
1636 	struct wilc *wl = wiphy_priv(wiphy);
1637 	struct wilc_vif *vif;
1638 
1639 	rcu_read_lock();
1640 	vif = wilc_get_wl_to_vif(wl);
1641 	if (IS_ERR(vif)) {
1642 		rcu_read_unlock();
1643 		return;
1644 	}
1645 
1646 	netdev_info(vif->ndev, "cfg set wake up = %d\n", enabled);
1647 	wilc_set_wowlan_trigger(vif, enabled);
1648 	rcu_read_unlock();
1649 }
1650 
1651 static int set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1652 			enum nl80211_tx_power_setting type, int mbm)
1653 {
1654 	int ret;
1655 	s32 tx_power = MBM_TO_DBM(mbm);
1656 	struct wilc *wl = wiphy_priv(wiphy);
1657 	struct wilc_vif *vif;
1658 
1659 	if (!wl->initialized)
1660 		return -EIO;
1661 
1662 	rcu_read_lock();
1663 	vif = wilc_get_wl_to_vif(wl);
1664 	if (IS_ERR(vif)) {
1665 		rcu_read_unlock();
1666 		return -EINVAL;
1667 	}
1668 
1669 	netdev_info(vif->ndev, "Setting tx power %d\n", tx_power);
1670 	if (tx_power < 0)
1671 		tx_power = 0;
1672 	else if (tx_power > 18)
1673 		tx_power = 18;
1674 	ret = wilc_set_tx_power(vif, tx_power);
1675 	if (ret)
1676 		netdev_err(vif->ndev, "Failed to set tx power\n");
1677 	rcu_read_unlock();
1678 
1679 	return ret;
1680 }
1681 
1682 static int get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1683 			int *dbm)
1684 {
1685 	int ret;
1686 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1687 	struct wilc *wl = vif->wilc;
1688 
1689 	/* If firmware is not started, return. */
1690 	if (!wl->initialized)
1691 		return -EIO;
1692 
1693 	ret = wilc_get_tx_power(vif, (u8 *)dbm);
1694 	if (ret)
1695 		netdev_err(vif->ndev, "Failed to get tx power\n");
1696 
1697 	return ret;
1698 }
1699 
1700 static const struct cfg80211_ops wilc_cfg80211_ops = {
1701 	.set_monitor_channel = set_channel,
1702 	.scan = scan,
1703 	.connect = connect,
1704 	.disconnect = disconnect,
1705 	.add_key = add_key,
1706 	.del_key = del_key,
1707 	.get_key = get_key,
1708 	.set_default_key = set_default_key,
1709 	.set_default_mgmt_key = set_default_mgmt_key,
1710 	.add_virtual_intf = add_virtual_intf,
1711 	.del_virtual_intf = del_virtual_intf,
1712 	.change_virtual_intf = change_virtual_intf,
1713 
1714 	.start_ap = start_ap,
1715 	.change_beacon = change_beacon,
1716 	.stop_ap = stop_ap,
1717 	.add_station = add_station,
1718 	.del_station = del_station,
1719 	.change_station = change_station,
1720 	.get_station = get_station,
1721 	.dump_station = dump_station,
1722 	.change_bss = change_bss,
1723 	.set_wiphy_params = set_wiphy_params,
1724 
1725 	.external_auth = external_auth,
1726 	.set_pmksa = set_pmksa,
1727 	.del_pmksa = del_pmksa,
1728 	.flush_pmksa = flush_pmksa,
1729 	.remain_on_channel = remain_on_channel,
1730 	.cancel_remain_on_channel = cancel_remain_on_channel,
1731 	.mgmt_tx_cancel_wait = mgmt_tx_cancel_wait,
1732 	.mgmt_tx = mgmt_tx,
1733 	.update_mgmt_frame_registrations = wilc_update_mgmt_frame_registrations,
1734 	.set_power_mgmt = set_power_mgmt,
1735 	.set_cqm_rssi_config = set_cqm_rssi_config,
1736 
1737 	.suspend = wilc_suspend,
1738 	.resume = wilc_resume,
1739 	.set_wakeup = wilc_set_wakeup,
1740 	.set_tx_power = set_tx_power,
1741 	.get_tx_power = get_tx_power,
1742 
1743 };
1744 
1745 static void wlan_init_locks(struct wilc *wl)
1746 {
1747 	mutex_init(&wl->hif_cs);
1748 	mutex_init(&wl->rxq_cs);
1749 	mutex_init(&wl->cfg_cmd_lock);
1750 	mutex_init(&wl->vif_mutex);
1751 	mutex_init(&wl->deinit_lock);
1752 
1753 	spin_lock_init(&wl->txq_spinlock);
1754 	mutex_init(&wl->txq_add_to_head_cs);
1755 
1756 	init_completion(&wl->txq_event);
1757 	init_completion(&wl->cfg_event);
1758 	init_completion(&wl->sync_event);
1759 	init_completion(&wl->txq_thread_started);
1760 }
1761 
1762 void wlan_deinit_locks(struct wilc *wilc)
1763 {
1764 	mutex_destroy(&wilc->hif_cs);
1765 	mutex_destroy(&wilc->rxq_cs);
1766 	mutex_destroy(&wilc->cfg_cmd_lock);
1767 	mutex_destroy(&wilc->txq_add_to_head_cs);
1768 	mutex_destroy(&wilc->vif_mutex);
1769 	mutex_destroy(&wilc->deinit_lock);
1770 }
1771 
1772 int wilc_cfg80211_init(struct wilc **wilc, struct device *dev, int io_type,
1773 		       const struct wilc_hif_func *ops)
1774 {
1775 	struct wilc *wl;
1776 	struct wilc_vif *vif;
1777 	int ret, i;
1778 
1779 	wl = wilc_create_wiphy(dev);
1780 	if (!wl)
1781 		return -EINVAL;
1782 
1783 	wlan_init_locks(wl);
1784 
1785 	ret = wilc_wlan_cfg_init(wl);
1786 	if (ret)
1787 		goto free_wl;
1788 
1789 	*wilc = wl;
1790 	wl->io_type = io_type;
1791 	wl->hif_func = ops;
1792 
1793 	for (i = 0; i < NQUEUES; i++)
1794 		INIT_LIST_HEAD(&wl->txq[i].txq_head.list);
1795 
1796 	INIT_LIST_HEAD(&wl->rxq_head.list);
1797 	INIT_LIST_HEAD(&wl->vif_list);
1798 
1799 	wl->hif_workqueue = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
1800 						    wiphy_name(wl->wiphy));
1801 	if (!wl->hif_workqueue) {
1802 		ret = -ENOMEM;
1803 		goto free_cfg;
1804 	}
1805 	vif = wilc_netdev_ifc_init(wl, "wlan%d", WILC_STATION_MODE,
1806 				   NL80211_IFTYPE_STATION, false);
1807 	if (IS_ERR(vif)) {
1808 		ret = PTR_ERR(vif);
1809 		goto free_hq;
1810 	}
1811 
1812 	return 0;
1813 
1814 free_hq:
1815 	destroy_workqueue(wl->hif_workqueue);
1816 
1817 free_cfg:
1818 	wilc_wlan_cfg_deinit(wl);
1819 
1820 free_wl:
1821 	wlan_deinit_locks(wl);
1822 	wiphy_unregister(wl->wiphy);
1823 	wiphy_free(wl->wiphy);
1824 	return ret;
1825 }
1826 EXPORT_SYMBOL_GPL(wilc_cfg80211_init);
1827 
1828 struct wilc *wilc_create_wiphy(struct device *dev)
1829 {
1830 	struct wiphy *wiphy;
1831 	struct wilc *wl;
1832 	int ret;
1833 
1834 	wiphy = wiphy_new(&wilc_cfg80211_ops, sizeof(*wl));
1835 	if (!wiphy)
1836 		return NULL;
1837 
1838 	wl = wiphy_priv(wiphy);
1839 
1840 	memcpy(wl->bitrates, wilc_bitrates, sizeof(wilc_bitrates));
1841 	memcpy(wl->channels, wilc_2ghz_channels, sizeof(wilc_2ghz_channels));
1842 	wl->band.bitrates = wl->bitrates;
1843 	wl->band.n_bitrates = ARRAY_SIZE(wl->bitrates);
1844 	wl->band.channels = wl->channels;
1845 	wl->band.n_channels = ARRAY_SIZE(wilc_2ghz_channels);
1846 
1847 	wl->band.ht_cap.ht_supported = 1;
1848 	wl->band.ht_cap.cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
1849 	wl->band.ht_cap.mcs.rx_mask[0] = 0xff;
1850 	wl->band.ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K;
1851 	wl->band.ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
1852 
1853 	wiphy->bands[NL80211_BAND_2GHZ] = &wl->band;
1854 
1855 	wiphy->max_scan_ssids = WILC_MAX_NUM_PROBED_SSID;
1856 #ifdef CONFIG_PM
1857 	wiphy->wowlan = &wowlan_support;
1858 #endif
1859 	wiphy->max_num_pmkids = WILC_MAX_NUM_PMKIDS;
1860 	wiphy->max_scan_ie_len = 1000;
1861 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1862 	memcpy(wl->cipher_suites, wilc_cipher_suites,
1863 	       sizeof(wilc_cipher_suites));
1864 	wiphy->cipher_suites = wl->cipher_suites;
1865 	wiphy->n_cipher_suites = ARRAY_SIZE(wilc_cipher_suites);
1866 	wiphy->mgmt_stypes = wilc_wfi_cfg80211_mgmt_types;
1867 
1868 	wiphy->max_remain_on_channel_duration = 500;
1869 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1870 				BIT(NL80211_IFTYPE_AP) |
1871 				BIT(NL80211_IFTYPE_MONITOR) |
1872 				BIT(NL80211_IFTYPE_P2P_GO) |
1873 				BIT(NL80211_IFTYPE_P2P_CLIENT);
1874 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1875 	wiphy->features |= NL80211_FEATURE_SAE;
1876 	set_wiphy_dev(wiphy, dev);
1877 	wl->wiphy = wiphy;
1878 	ret = wiphy_register(wiphy);
1879 	if (ret) {
1880 		wiphy_free(wiphy);
1881 		return NULL;
1882 	}
1883 	return wl;
1884 }
1885 
1886 int wilc_init_host_int(struct net_device *net)
1887 {
1888 	int ret;
1889 	struct wilc_vif *vif = netdev_priv(net);
1890 	struct wilc_priv *priv = &vif->priv;
1891 
1892 	priv->p2p_listen_state = false;
1893 
1894 	mutex_init(&priv->scan_req_lock);
1895 	ret = wilc_init(net, &priv->hif_drv);
1896 	if (ret)
1897 		netdev_err(net, "Error while initializing hostinterface\n");
1898 
1899 	return ret;
1900 }
1901 
1902 void wilc_deinit_host_int(struct net_device *net)
1903 {
1904 	int ret;
1905 	struct wilc_vif *vif = netdev_priv(net);
1906 	struct wilc_priv *priv = &vif->priv;
1907 
1908 	priv->p2p_listen_state = false;
1909 
1910 	flush_workqueue(vif->wilc->hif_workqueue);
1911 	mutex_destroy(&priv->scan_req_lock);
1912 	ret = wilc_deinit(vif);
1913 
1914 	if (ret)
1915 		netdev_err(net, "Error while deinitializing host interface\n");
1916 }
1917 
1918