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