xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/p2p.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2012 Broadcom Corporation
4  */
5 #include <linux/slab.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/unaligned.h>
10 #include <net/cfg80211.h>
11 
12 #include <brcmu_wifi.h>
13 #include <brcmu_utils.h>
14 #include <defs.h>
15 #include "core.h"
16 #include "debug.h"
17 #include "fwil.h"
18 #include "fwil_types.h"
19 #include "p2p.h"
20 #include "cfg80211.h"
21 #include "feature.h"
22 
23 /* parameters used for p2p escan */
24 #define P2PAPI_SCAN_NPROBES 1
25 #define P2PAPI_SCAN_DWELL_TIME_MS 80
26 #define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
27 #define P2PAPI_SCAN_HOME_TIME_MS 60
28 #define P2PAPI_SCAN_NPROBS_TIME_MS 30
29 #define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
30 #define WL_SCAN_CONNECT_DWELL_TIME_MS 200
31 #define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
32 
33 #define BRCMF_P2P_WILDCARD_SSID		"DIRECT-"
34 #define BRCMF_P2P_WILDCARD_SSID_LEN	(sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
35 
36 #define SOCIAL_CHAN_1		1
37 #define SOCIAL_CHAN_2		6
38 #define SOCIAL_CHAN_3		11
39 #define IS_P2P_SOCIAL_CHANNEL(channel) ((channel == SOCIAL_CHAN_1) || \
40 					 (channel == SOCIAL_CHAN_2) || \
41 					 (channel == SOCIAL_CHAN_3))
42 #define BRCMF_P2P_TEMP_CHAN	SOCIAL_CHAN_3
43 #define SOCIAL_CHAN_CNT		3
44 #define AF_PEER_SEARCH_CNT	2
45 
46 #define BRCMF_SCB_TIMEOUT_VALUE	20
47 
48 #define P2P_AF_CATEGORY		0x7f
49 #define P2P_OUI			"\x50\x6F\x9A"	/* P2P OUI */
50 #define P2P_OUI_LEN		3		/* P2P OUI length */
51 
52 /* Action Frame Constants */
53 #define DOT11_ACTION_HDR_LEN	2	/* action frame category + action */
54 #define DOT11_ACTION_CAT_OFF	0	/* category offset */
55 #define DOT11_ACTION_ACT_OFF	1	/* action offset */
56 
57 #define P2P_AF_DWELL_TIME		200
58 #define P2P_AF_MIN_DWELL_TIME		100
59 #define P2P_AF_MED_DWELL_TIME		400
60 #define P2P_AF_LONG_DWELL_TIME		1000
61 #define P2P_AF_TX_MAX_RETRY		5
62 #define P2P_AF_MAX_WAIT_TIME		msecs_to_jiffies(2000)
63 #define P2P_INVALID_CHANNEL		-1
64 #define P2P_CHANNEL_SYNC_RETRY		5
65 #define P2P_AF_FRM_SCAN_MAX_WAIT	msecs_to_jiffies(450)
66 #define P2P_DEFAULT_SLEEP_TIME_VSDB	200
67 #define P2P_AF_RETRY_DELAY_TIME		40
68 
69 /* WiFi P2P Public Action Frame OUI Subtypes */
70 #define P2P_PAF_GON_REQ		0	/* Group Owner Negotiation Req */
71 #define P2P_PAF_GON_RSP		1	/* Group Owner Negotiation Rsp */
72 #define P2P_PAF_GON_CONF	2	/* Group Owner Negotiation Confirm */
73 #define P2P_PAF_INVITE_REQ	3	/* P2P Invitation Request */
74 #define P2P_PAF_INVITE_RSP	4	/* P2P Invitation Response */
75 #define P2P_PAF_DEVDIS_REQ	5	/* Device Discoverability Request */
76 #define P2P_PAF_DEVDIS_RSP	6	/* Device Discoverability Response */
77 #define P2P_PAF_PROVDIS_REQ	7	/* Provision Discovery Request */
78 #define P2P_PAF_PROVDIS_RSP	8	/* Provision Discovery Response */
79 #define P2P_PAF_SUBTYPE_INVALID	255	/* Invalid Subtype */
80 
81 /* WiFi P2P Action Frame OUI Subtypes */
82 #define P2P_AF_NOTICE_OF_ABSENCE	0	/* Notice of Absence */
83 #define P2P_AF_PRESENCE_REQ		1	/* P2P Presence Request */
84 #define P2P_AF_PRESENCE_RSP		2	/* P2P Presence Response */
85 #define P2P_AF_GO_DISC_REQ		3	/* GO Discoverability Request */
86 
87 /* P2P Service Discovery related */
88 #define P2PSD_ACTION_CATEGORY		0x04	/* Public action frame */
89 #define P2PSD_ACTION_ID_GAS_IREQ	0x0a	/* GAS Initial Request AF */
90 #define P2PSD_ACTION_ID_GAS_IRESP	0x0b	/* GAS Initial Response AF */
91 #define P2PSD_ACTION_ID_GAS_CREQ	0x0c	/* GAS Comeback Request AF */
92 #define P2PSD_ACTION_ID_GAS_CRESP	0x0d	/* GAS Comeback Response AF */
93 
94 #define BRCMF_P2P_DISABLE_TIMEOUT	msecs_to_jiffies(500)
95 
96 /* Mask for retry counter of custom dwell time */
97 #define CUSTOM_RETRY_MASK 0xff000000
98 /**
99  * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
100  *
101  * @state: requested discovery state (see enum brcmf_p2p_disc_state).
102  * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
103  * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
104  */
105 struct brcmf_p2p_disc_st_le {
106 	u8 state;
107 	__le16 chspec;
108 	__le16 dwell;
109 };
110 
111 /**
112  * enum brcmf_p2p_disc_state - P2P discovery state values
113  *
114  * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
115  * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
116  * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
117  */
118 enum brcmf_p2p_disc_state {
119 	WL_P2P_DISC_ST_SCAN,
120 	WL_P2P_DISC_ST_LISTEN,
121 	WL_P2P_DISC_ST_SEARCH
122 };
123 
124 /**
125  * struct brcmf_p2p_scan_le - P2P specific scan request.
126  *
127  * @type: type of scan method requested (values: 'E' or 'S').
128  * @reserved: reserved (ignored).
129  * @eparams: parameters used for type 'E'.
130  * @sparams: parameters used for type 'S'.
131  */
132 struct brcmf_p2p_scan_le {
133 	u8 type;
134 	u8 reserved[3];
135 	union {
136 		struct brcmf_escan_params_le eparams;
137 		struct brcmf_scan_params_le sparams;
138 	};
139 };
140 
141 /**
142  * struct brcmf_p2p_pub_act_frame - WiFi P2P Public Action Frame
143  *
144  * @category: WLAN_CATEGORY_PUBLIC
145  * @action: WLAN_PUB_ACTION_VENDOR_SPECIFIC
146  * @oui: P2P_OUI
147  * @oui_type: OUI type - WLAN_OUI_TYPE_WFA_P2P
148  * @subtype: OUI subtype - P2P_TYPE_*
149  * @dialog_token: nonzero, identifies req/rsp transaction
150  * @elts: Variable length information elements.
151  */
152 struct brcmf_p2p_pub_act_frame {
153 	u8	category;
154 	u8	action;
155 	u8	oui[3];
156 	u8	oui_type;
157 	u8	subtype;
158 	u8	dialog_token;
159 	u8	elts[];
160 };
161 
162 /**
163  * struct brcmf_p2p_action_frame - WiFi P2P Action Frame
164  *
165  * @category: P2P_AF_CATEGORY
166  * @oui: OUI - P2P_OUI
167  * @type: OUI Type - WLAN_OUI_TYPE_WFA_P2P
168  * @subtype: OUI Subtype - P2P_AF_*
169  * @dialog_token: nonzero, identifies req/resp tranaction
170  * @elts: Variable length information elements.
171  */
172 struct brcmf_p2p_action_frame {
173 	u8	category;
174 	u8	oui[3];
175 	u8	type;
176 	u8	subtype;
177 	u8	dialog_token;
178 	u8	elts[];
179 };
180 
181 /**
182  * struct brcmf_p2psd_gas_pub_act_frame - Wi-Fi GAS Public Action Frame
183  *
184  * @category: 0x04 Public Action Frame
185  * @action: 0x6c Advertisement Protocol
186  * @dialog_token: nonzero, identifies req/rsp transaction
187  * @query_data: Query Data. SD gas ireq SD gas iresp
188  */
189 struct brcmf_p2psd_gas_pub_act_frame {
190 	u8	category;
191 	u8	action;
192 	u8	dialog_token;
193 	u8	query_data[];
194 };
195 
196 /**
197  * struct brcmf_config_af_params - Action Frame Parameters for tx.
198  *
199  * @mpc_onoff: To make sure to send successfully action frame, we have to
200  *             turn off mpc  0: off, 1: on,  (-1): do nothing
201  * @search_channel: 1: search peer's channel to send af
202  * @extra_listen: keep the dwell time to get af response frame.
203  */
204 struct brcmf_config_af_params {
205 	s32 mpc_onoff;
206 	bool search_channel;
207 	bool extra_listen;
208 };
209 
210 /**
211  * brcmf_p2p_is_pub_action() - true if p2p public type frame.
212  *
213  * @frame: action frame data.
214  * @frame_len: length of action frame data.
215  *
216  * Determine if action frame is p2p public action type
217  */
brcmf_p2p_is_pub_action(void * frame,u32 frame_len)218 static bool brcmf_p2p_is_pub_action(void *frame, u32 frame_len)
219 {
220 	struct brcmf_p2p_pub_act_frame *pact_frm;
221 
222 	if (frame == NULL)
223 		return false;
224 
225 	pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
226 	if (frame_len < sizeof(*pact_frm))
227 		return false;
228 
229 	if (pact_frm->category == WLAN_CATEGORY_PUBLIC &&
230 	    pact_frm->action == WLAN_PUB_ACTION_VENDOR_SPECIFIC &&
231 	    pact_frm->oui_type == WLAN_OUI_TYPE_WFA_P2P &&
232 	    get_unaligned_be24(pact_frm->oui) == WLAN_OUI_WFA)
233 		return true;
234 
235 	return false;
236 }
237 
238 /**
239  * brcmf_p2p_is_dpp_pub_action() - true if dpp public type frame.
240  *
241  * @frame: action frame data.
242  * @frame_len: length of action frame data.
243  *
244  * Determine if action frame is dpp public action type
245  */
brcmf_p2p_is_dpp_pub_action(void * frame,u32 frame_len)246 static bool brcmf_p2p_is_dpp_pub_action(void *frame, u32 frame_len)
247 {
248 	struct brcmf_p2p_pub_act_frame *pact_frm;
249 
250 	if (!frame)
251 		return false;
252 
253 	pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
254 	if (frame_len < sizeof(struct brcmf_p2p_pub_act_frame) - 1)
255 		return false;
256 
257 	if (pact_frm->category == WLAN_CATEGORY_PUBLIC &&
258 	    pact_frm->action == WLAN_PUB_ACTION_VENDOR_SPECIFIC &&
259 	    pact_frm->oui_type == WLAN_OUI_TYPE_WFA_DPP &&
260 	    get_unaligned_be24(pact_frm->oui) == WLAN_OUI_WFA)
261 		return true;
262 
263 	return false;
264 }
265 
266 /**
267  * brcmf_p2p_is_p2p_action() - true if p2p action type frame.
268  *
269  * @frame: action frame data.
270  * @frame_len: length of action frame data.
271  *
272  * Determine if action frame is p2p action type
273  */
brcmf_p2p_is_p2p_action(void * frame,u32 frame_len)274 static bool brcmf_p2p_is_p2p_action(void *frame, u32 frame_len)
275 {
276 	struct brcmf_p2p_action_frame *act_frm;
277 
278 	if (frame == NULL)
279 		return false;
280 
281 	act_frm = (struct brcmf_p2p_action_frame *)frame;
282 	if (frame_len < sizeof(*act_frm))
283 		return false;
284 
285 	if (act_frm->category == P2P_AF_CATEGORY &&
286 	    act_frm->type  == WLAN_OUI_TYPE_WFA_P2P &&
287 	    memcmp(act_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
288 		return true;
289 
290 	return false;
291 }
292 
293 /**
294  * brcmf_p2p_is_gas_action() - true if p2p gas action type frame.
295  *
296  * @frame: action frame data.
297  * @frame_len: length of action frame data.
298  *
299  * Determine if action frame is p2p gas action type
300  */
brcmf_p2p_is_gas_action(void * frame,u32 frame_len)301 static bool brcmf_p2p_is_gas_action(void *frame, u32 frame_len)
302 {
303 	struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
304 
305 	if (frame == NULL)
306 		return false;
307 
308 	sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
309 	if (frame_len < sizeof(*sd_act_frm))
310 		return false;
311 
312 	if (sd_act_frm->category != P2PSD_ACTION_CATEGORY)
313 		return false;
314 
315 	if (sd_act_frm->action == P2PSD_ACTION_ID_GAS_IREQ ||
316 	    sd_act_frm->action == P2PSD_ACTION_ID_GAS_IRESP ||
317 	    sd_act_frm->action == P2PSD_ACTION_ID_GAS_CREQ ||
318 	    sd_act_frm->action == P2PSD_ACTION_ID_GAS_CRESP)
319 		return true;
320 
321 	return false;
322 }
323 
324 /**
325  * brcmf_p2p_print_actframe() - debug print routine.
326  *
327  * @tx: Received or to be transmitted
328  * @frame: action frame data.
329  * @frame_len: length of action frame data.
330  *
331  * Print information about the p2p action frame
332  */
333 
334 #ifdef DEBUG
335 
brcmf_p2p_print_actframe(bool tx,void * frame,u32 frame_len)336 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
337 {
338 	struct brcmf_p2p_pub_act_frame *pact_frm;
339 	struct brcmf_p2p_action_frame *act_frm;
340 	struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
341 
342 	if (!frame || frame_len <= 2)
343 		return;
344 
345 	if (brcmf_p2p_is_pub_action(frame, frame_len)) {
346 		pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
347 		switch (pact_frm->subtype) {
348 		case P2P_PAF_GON_REQ:
349 			brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
350 				  (tx) ? "TX" : "RX");
351 			break;
352 		case P2P_PAF_GON_RSP:
353 			brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
354 				  (tx) ? "TX" : "RX");
355 			break;
356 		case P2P_PAF_GON_CONF:
357 			brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
358 				  (tx) ? "TX" : "RX");
359 			break;
360 		case P2P_PAF_INVITE_REQ:
361 			brcmf_dbg(TRACE, "%s P2P Invitation Request  Frame\n",
362 				  (tx) ? "TX" : "RX");
363 			break;
364 		case P2P_PAF_INVITE_RSP:
365 			brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
366 				  (tx) ? "TX" : "RX");
367 			break;
368 		case P2P_PAF_DEVDIS_REQ:
369 			brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
370 				  (tx) ? "TX" : "RX");
371 			break;
372 		case P2P_PAF_DEVDIS_RSP:
373 			brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
374 				  (tx) ? "TX" : "RX");
375 			break;
376 		case P2P_PAF_PROVDIS_REQ:
377 			brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
378 				  (tx) ? "TX" : "RX");
379 			break;
380 		case P2P_PAF_PROVDIS_RSP:
381 			brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
382 				  (tx) ? "TX" : "RX");
383 			break;
384 		default:
385 			brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
386 				  (tx) ? "TX" : "RX");
387 			break;
388 		}
389 	} else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
390 		act_frm = (struct brcmf_p2p_action_frame *)frame;
391 		switch (act_frm->subtype) {
392 		case P2P_AF_NOTICE_OF_ABSENCE:
393 			brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
394 				  (tx) ? "TX" : "RX");
395 			break;
396 		case P2P_AF_PRESENCE_REQ:
397 			brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
398 				  (tx) ? "TX" : "RX");
399 			break;
400 		case P2P_AF_PRESENCE_RSP:
401 			brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
402 				  (tx) ? "TX" : "RX");
403 			break;
404 		case P2P_AF_GO_DISC_REQ:
405 			brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
406 				  (tx) ? "TX" : "RX");
407 			break;
408 		default:
409 			brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
410 				  (tx) ? "TX" : "RX");
411 		}
412 
413 	} else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
414 		sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
415 		switch (sd_act_frm->action) {
416 		case P2PSD_ACTION_ID_GAS_IREQ:
417 			brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
418 				  (tx) ? "TX" : "RX");
419 			break;
420 		case P2PSD_ACTION_ID_GAS_IRESP:
421 			brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
422 				  (tx) ? "TX" : "RX");
423 			break;
424 		case P2PSD_ACTION_ID_GAS_CREQ:
425 			brcmf_dbg(TRACE, "%s P2P GAS Comeback Request\n",
426 				  (tx) ? "TX" : "RX");
427 			break;
428 		case P2PSD_ACTION_ID_GAS_CRESP:
429 			brcmf_dbg(TRACE, "%s P2P GAS Comeback Response\n",
430 				  (tx) ? "TX" : "RX");
431 			break;
432 		default:
433 			brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
434 				  (tx) ? "TX" : "RX");
435 			break;
436 		}
437 	}
438 }
439 
440 #else
441 
brcmf_p2p_print_actframe(bool tx,void * frame,u32 frame_len)442 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
443 {
444 }
445 
446 #endif
447 
448 
449 /**
450  * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
451  *
452  * @ifp: ifp to use for iovars (primary).
453  * @p2p_mac: mac address to configure for p2p_da_override
454  */
brcmf_p2p_set_firmware(struct brcmf_if * ifp,u8 * p2p_mac)455 static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
456 {
457 	struct brcmf_pub *drvr = ifp->drvr;
458 	s32 ret = 0;
459 
460 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
461 	brcmf_fil_iovar_int_set(ifp, "apsta", 1);
462 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
463 
464 	/* In case of COB type, firmware has default mac address
465 	 * After Initializing firmware, we have to set current mac address to
466 	 * firmware for P2P device address. This must be done with discovery
467 	 * disabled.
468 	 */
469 	brcmf_fil_iovar_int_set(ifp, "p2p_disc", 0);
470 
471 	ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
472 				       ETH_ALEN);
473 	if (ret)
474 		bphy_err(drvr, "failed to update device address ret %d\n", ret);
475 
476 	return ret;
477 }
478 
479 /**
480  * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
481  *
482  * @p2p: P2P specific data.
483  * @dev_addr: optional device address.
484  *
485  * P2P needs mac addresses for P2P device and interface. If no device
486  * address it specified, these are derived from a random ethernet
487  * address.
488  */
brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info * p2p,u8 * dev_addr)489 static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
490 {
491 	struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
492 	bool random_addr = false;
493 	bool local_admin = false;
494 
495 	if (!dev_addr || is_zero_ether_addr(dev_addr)) {
496 		/* If the primary interface address is already locally
497 		 * administered, create a new random address.
498 		 */
499 		if (pri_ifp->mac_addr[0] & 0x02) {
500 			random_addr = true;
501 		} else {
502 			dev_addr = pri_ifp->mac_addr;
503 			local_admin = true;
504 		}
505 	}
506 
507 	/* Generate the P2P Device Address obtaining a random ethernet
508 	 * address with the locally administered bit set.
509 	 */
510 	if (random_addr)
511 		eth_random_addr(p2p->dev_addr);
512 	else
513 		memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
514 
515 	if (local_admin)
516 		p2p->dev_addr[0] |= 0x02;
517 
518 	/* Generate the P2P Interface Address.  If the discovery and connection
519 	 * BSSCFGs need to simultaneously co-exist, then this address must be
520 	 * different from the P2P Device Address, but also locally administered.
521 	 */
522 	memcpy(p2p->conn_int_addr, p2p->dev_addr, ETH_ALEN);
523 	p2p->conn_int_addr[0] |= 0x02;
524 	p2p->conn_int_addr[4] ^= 0x80;
525 
526 	memcpy(p2p->conn2_int_addr, p2p->dev_addr, ETH_ALEN);
527 	p2p->conn2_int_addr[0] |= 0x02;
528 	p2p->conn2_int_addr[4] ^= 0x90;
529 }
530 
531 /**
532  * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
533  *
534  * @request: the scan request as received from cfg80211.
535  *
536  * returns true if one of the ssids in the request matches the
537  * P2P wildcard ssid; otherwise returns false.
538  */
brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request * request)539 static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
540 {
541 	struct cfg80211_ssid *ssids = request->ssids;
542 	int i;
543 
544 	for (i = 0; i < request->n_ssids; i++) {
545 		if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
546 			continue;
547 
548 		brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
549 		if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
550 			    BRCMF_P2P_WILDCARD_SSID_LEN))
551 			return true;
552 	}
553 	return false;
554 }
555 
556 /**
557  * brcmf_p2p_set_discover_state - set discover state in firmware.
558  *
559  * @ifp: low-level interface object.
560  * @state: discover state to set.
561  * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
562  * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
563  */
brcmf_p2p_set_discover_state(struct brcmf_if * ifp,u8 state,u16 chanspec,u16 listen_ms)564 static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
565 					u16 chanspec, u16 listen_ms)
566 {
567 	struct brcmf_p2p_disc_st_le discover_state;
568 	s32 ret = 0;
569 	brcmf_dbg(TRACE, "enter\n");
570 
571 	discover_state.state = state;
572 	discover_state.chspec = cpu_to_le16(chanspec);
573 	discover_state.dwell = cpu_to_le16(listen_ms);
574 	ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
575 					sizeof(discover_state));
576 	return ret;
577 }
578 
579 /**
580  * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
581  *
582  * @p2p: P2P specific data.
583  *
584  * Resets the discovery state and disables it in firmware.
585  */
brcmf_p2p_deinit_discovery(struct brcmf_p2p_info * p2p)586 static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
587 {
588 	struct brcmf_cfg80211_vif *vif;
589 
590 	brcmf_dbg(TRACE, "enter\n");
591 
592 	/* Set the discovery state to SCAN */
593 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
594 	(void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
595 
596 	/* Disable P2P discovery in the firmware */
597 	vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
598 	(void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
599 
600 	return 0;
601 }
602 
603 /**
604  * brcmf_p2p_enable_discovery() - initialize and configure discovery.
605  *
606  * @p2p: P2P specific data.
607  *
608  * Initializes the discovery device and configure the virtual interface.
609  */
brcmf_p2p_enable_discovery(struct brcmf_p2p_info * p2p)610 static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
611 {
612 	struct brcmf_pub *drvr = p2p->cfg->pub;
613 	struct brcmf_cfg80211_vif *vif;
614 	s32 ret = 0;
615 
616 	brcmf_dbg(TRACE, "enter\n");
617 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
618 	if (!vif) {
619 		bphy_err(drvr, "P2P config device not available\n");
620 		ret = -EPERM;
621 		goto exit;
622 	}
623 
624 	if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
625 		brcmf_dbg(INFO, "P2P config device already configured\n");
626 		goto exit;
627 	}
628 
629 	/* Re-initialize P2P Discovery in the firmware */
630 	vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
631 	ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
632 	if (ret < 0) {
633 		bphy_err(drvr, "set p2p_disc error\n");
634 		goto exit;
635 	}
636 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
637 	ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
638 	if (ret < 0) {
639 		bphy_err(drvr, "unable to set WL_P2P_DISC_ST_SCAN\n");
640 		goto exit;
641 	}
642 
643 	/*
644 	 * Set wsec to any non-zero value in the discovery bsscfg
645 	 * to ensure our P2P probe responses have the privacy bit
646 	 * set in the 802.11 WPA IE. Some peer devices may not
647 	 * initiate WPS with us if this bit is not set.
648 	 */
649 	ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
650 	if (ret < 0) {
651 		bphy_err(drvr, "wsec error %d\n", ret);
652 		goto exit;
653 	}
654 
655 	set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
656 exit:
657 	return ret;
658 }
659 
660 /**
661  * brcmf_p2p_escan() - initiate a P2P scan.
662  *
663  * @p2p: P2P specific data.
664  * @num_chans: number of channels to scan.
665  * @chanspecs: channel parameters for @num_chans channels.
666  * @search_state: P2P discover state to use.
667  * @bss_type: type of P2P bss.
668  */
brcmf_p2p_escan(struct brcmf_p2p_info * p2p,u32 num_chans,u16 chanspecs[],s32 search_state,enum p2p_bss_type bss_type)669 static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
670 			   u16 chanspecs[], s32 search_state,
671 			   enum p2p_bss_type bss_type)
672 {
673 	struct brcmf_pub *drvr = p2p->cfg->pub;
674 	s32 ret = 0;
675 	s32 memsize = offsetof(struct brcmf_p2p_scan_le,
676 			       eparams.params_le.channel_list);
677 	s32 nprobes;
678 	s32 active;
679 	u32 i;
680 	u8 *memblk;
681 	struct brcmf_cfg80211_vif *vif;
682 	struct brcmf_p2p_scan_le *p2p_params;
683 	struct brcmf_scan_params_le *sparams;
684 
685 	memsize += num_chans * sizeof(__le16);
686 	memblk = kzalloc(memsize, GFP_KERNEL);
687 	if (!memblk)
688 		return -ENOMEM;
689 
690 	vif = p2p->bss_idx[bss_type].vif;
691 	if (vif == NULL) {
692 		bphy_err(drvr, "no vif for bss type %d\n", bss_type);
693 		ret = -EINVAL;
694 		goto exit;
695 	}
696 	p2p_params = (struct brcmf_p2p_scan_le *)memblk;
697 	sparams = &p2p_params->eparams.params_le;
698 
699 	switch (search_state) {
700 	case WL_P2P_DISC_ST_SEARCH:
701 		/*
702 		 * If we in SEARCH STATE, we don't need to set SSID explictly
703 		 * because dongle use P2P WILDCARD internally by default, use
704 		 * null ssid, which it is already due to kzalloc.
705 		 */
706 		break;
707 	case WL_P2P_DISC_ST_SCAN:
708 		/*
709 		 * wpa_supplicant has p2p_find command with type social or
710 		 * progressive. For progressive, we need to set the ssid to
711 		 * P2P WILDCARD because we just do broadcast scan unless
712 		 * setting SSID.
713 		 */
714 		sparams->ssid_le.SSID_len =
715 				cpu_to_le32(BRCMF_P2P_WILDCARD_SSID_LEN);
716 		memcpy(sparams->ssid_le.SSID, BRCMF_P2P_WILDCARD_SSID,
717 		       BRCMF_P2P_WILDCARD_SSID_LEN);
718 		break;
719 	default:
720 		bphy_err(drvr, " invalid search state %d\n", search_state);
721 		ret = -EINVAL;
722 		goto exit;
723 	}
724 
725 	brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
726 
727 	/*
728 	 * set p2p scan parameters.
729 	 */
730 	p2p_params->type = 'E';
731 
732 	/* determine the scan engine parameters */
733 	sparams->bss_type = DOT11_BSSTYPE_ANY;
734 	sparams->scan_type = BRCMF_SCANTYPE_ACTIVE;
735 
736 	eth_broadcast_addr(sparams->bssid);
737 	sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
738 
739 	/*
740 	 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
741 	 * supported by the supplicant.
742 	 */
743 	if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
744 		active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
745 	else if (num_chans == AF_PEER_SEARCH_CNT)
746 		active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
747 	else if (brcmf_get_vif_state_any(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
748 		active = -1;
749 	else
750 		active = P2PAPI_SCAN_DWELL_TIME_MS;
751 
752 	/* Override scan params to find a peer for a connection */
753 	if (num_chans == 1) {
754 		active = WL_SCAN_CONNECT_DWELL_TIME_MS;
755 		/* WAR to sync with presence period of VSDB GO.
756 		 * send probe request more frequently
757 		 */
758 		nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
759 	} else {
760 		nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
761 	}
762 
763 	if (nprobes <= 0)
764 		nprobes = 1;
765 
766 	brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
767 	sparams->active_time = cpu_to_le32(active);
768 	sparams->nprobes = cpu_to_le32(nprobes);
769 	sparams->passive_time = cpu_to_le32(-1);
770 	sparams->channel_num = cpu_to_le32(num_chans &
771 					   BRCMF_SCAN_PARAMS_COUNT_MASK);
772 	for (i = 0; i < num_chans; i++)
773 		sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
774 
775 	/* set the escan specific parameters */
776 	p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
777 	p2p_params->eparams.action =  cpu_to_le16(WL_ESCAN_ACTION_START);
778 	p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
779 	/* perform p2p scan on primary device */
780 	ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
781 	if (!ret)
782 		set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
783 exit:
784 	kfree(memblk);
785 	return ret;
786 }
787 
788 /**
789  * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
790  *
791  * @cfg: driver private data for cfg80211 interface.
792  * @ifp: interface control.
793  * @request: scan request from cfg80211.
794  *
795  * Determines the P2P discovery state based to scan request parameters and
796  * validates the channels in the request.
797  */
brcmf_p2p_run_escan(struct brcmf_cfg80211_info * cfg,struct brcmf_if * ifp,struct cfg80211_scan_request * request)798 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
799 			       struct brcmf_if *ifp,
800 			       struct cfg80211_scan_request *request)
801 {
802 	struct brcmf_p2p_info *p2p = &cfg->p2p;
803 	struct brcmf_pub *drvr = cfg->pub;
804 	s32 err = 0;
805 	s32 search_state = WL_P2P_DISC_ST_SCAN;
806 	struct brcmf_cfg80211_vif *vif;
807 	struct net_device *dev = NULL;
808 	int i, num_nodfs = 0;
809 	u16 *chanspecs;
810 
811 	brcmf_dbg(TRACE, "enter\n");
812 
813 	if (!request) {
814 		err = -EINVAL;
815 		goto exit;
816 	}
817 
818 	if (request->n_channels) {
819 		chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
820 				    GFP_KERNEL);
821 		if (!chanspecs) {
822 			err = -ENOMEM;
823 			goto exit;
824 		}
825 		vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
826 		if (vif)
827 			dev = vif->wdev.netdev;
828 		if (request->n_channels == 3 &&
829 		    request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
830 		    request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
831 		    request->channels[2]->hw_value == SOCIAL_CHAN_3) {
832 			/* SOCIAL CHANNELS 1, 6, 11 */
833 			search_state = WL_P2P_DISC_ST_SEARCH;
834 			brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
835 		} else if (dev != NULL &&
836 			   vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
837 			/* If you are already a GO, then do SEARCH only */
838 			brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
839 			search_state = WL_P2P_DISC_ST_SEARCH;
840 		} else {
841 			brcmf_dbg(INFO, "P2P SCAN STATE START\n");
842 		}
843 
844 		/*
845 		 * no P2P scanning on passive or DFS channels.
846 		 */
847 		for (i = 0; i < request->n_channels; i++) {
848 			struct ieee80211_channel *chan = request->channels[i];
849 
850 			if (chan->flags & (IEEE80211_CHAN_RADAR |
851 					   IEEE80211_CHAN_NO_IR))
852 				continue;
853 
854 			chanspecs[i] = channel_to_chanspec(&p2p->cfg->d11inf,
855 							   chan);
856 			brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
857 				  num_nodfs, chan->hw_value, chanspecs[i]);
858 			num_nodfs++;
859 		}
860 		err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
861 				      P2PAPI_BSSCFG_DEVICE);
862 		kfree(chanspecs);
863 	}
864 exit:
865 	if (err)
866 		bphy_err(drvr, "error (%d)\n", err);
867 	return err;
868 }
869 
870 
871 /**
872  * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
873  *
874  * @ie: string of information elements.
875  * @ie_len: length of string.
876  *
877  * Scan ie for p2p ie and look for attribute 6 channel. If available determine
878  * channel and return it.
879  */
brcmf_p2p_find_listen_channel(const u8 * ie,u32 ie_len)880 static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
881 {
882 	u8 channel_ie[5];
883 	s32 listen_channel;
884 	s32 err;
885 
886 	err = cfg80211_get_p2p_attr(ie, ie_len,
887 				    IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
888 				    channel_ie, sizeof(channel_ie));
889 	if (err < 0)
890 		return err;
891 
892 	/* listen channel subel length format:     */
893 	/* 3(country) + 1(op. class) + 1(chan num) */
894 	listen_channel = (s32)channel_ie[3 + 1];
895 
896 	if (listen_channel == SOCIAL_CHAN_1 ||
897 	    listen_channel == SOCIAL_CHAN_2 ||
898 	    listen_channel == SOCIAL_CHAN_3) {
899 		brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
900 		return listen_channel;
901 	}
902 
903 	return -EPERM;
904 }
905 
906 
907 /**
908  * brcmf_p2p_scan_prep() - prepare scan based on request.
909  *
910  * @wiphy: wiphy device.
911  * @request: scan request from cfg80211.
912  * @vif: vif on which scan request is to be executed.
913  *
914  * Prepare the scan appropriately for type of scan requested. Overrides the
915  * escan .run() callback for peer-to-peer scanning.
916  */
brcmf_p2p_scan_prep(struct wiphy * wiphy,struct cfg80211_scan_request * request,struct brcmf_cfg80211_vif * vif)917 int brcmf_p2p_scan_prep(struct wiphy *wiphy,
918 			struct cfg80211_scan_request *request,
919 			struct brcmf_cfg80211_vif *vif)
920 {
921 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
922 	struct brcmf_p2p_info *p2p = &cfg->p2p;
923 	int err;
924 
925 	if (brcmf_p2p_scan_is_p2p_request(request)) {
926 		/* find my listen channel */
927 		err = brcmf_p2p_find_listen_channel(request->ie,
928 						    request->ie_len);
929 		if (err < 0)
930 			return err;
931 
932 		p2p->afx_hdl.my_listen_chan = err;
933 
934 		clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
935 		brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
936 
937 		err = brcmf_p2p_enable_discovery(p2p);
938 		if (err)
939 			return err;
940 
941 		/* override .run_escan() callback. */
942 		cfg->escan_info.run = brcmf_p2p_run_escan;
943 	}
944 	return 0;
945 }
946 
947 
948 /**
949  * brcmf_p2p_discover_listen() - set firmware to discover listen state.
950  *
951  * @p2p: p2p device.
952  * @channel: channel nr for discover listen.
953  * @duration: time in ms to stay on channel.
954  *
955  */
956 static s32
brcmf_p2p_discover_listen(struct brcmf_p2p_info * p2p,u16 channel,u32 duration)957 brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
958 {
959 	struct brcmf_pub *drvr = p2p->cfg->pub;
960 	struct brcmf_cfg80211_vif *vif;
961 	struct brcmu_chan ch;
962 	s32 err = 0;
963 
964 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
965 	if (!vif) {
966 		bphy_err(drvr, "Discovery is not set, so we have nothing to do\n");
967 		err = -EPERM;
968 		goto exit;
969 	}
970 
971 	if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
972 		bphy_err(drvr, "Previous LISTEN is not completed yet\n");
973 		/* WAR: prevent cookie mismatch in wpa_supplicant return OK */
974 		goto exit;
975 	}
976 
977 	ch.chnum = channel;
978 	ch.bw = BRCMU_CHAN_BW_20;
979 	p2p->cfg->d11inf.encchspec(&ch);
980 	err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
981 					   ch.chspec, (u16)duration);
982 	if (!err)
983 		set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
984 exit:
985 	return err;
986 }
987 
988 
989 /**
990  * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
991  *
992  * @wiphy: wiphy device.
993  * @wdev: wireless device.
994  * @channel: channel to stay on.
995  * @duration: time in ms to remain on channel.
996  * @cookie: cookie.
997  * @rx_addr: Address to match against the destination of received frames
998  */
brcmf_p2p_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct ieee80211_channel * channel,unsigned int duration,u64 cookie,const u8 * rx_addr)999 int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
1000 				struct ieee80211_channel *channel,
1001 				unsigned int duration, u64 cookie,
1002 				const u8 *rx_addr)
1003 {
1004 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1005 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1006 	s32 err;
1007 	u16 channel_nr;
1008 
1009 	channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
1010 	brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
1011 		  duration);
1012 
1013 	err = brcmf_p2p_enable_discovery(p2p);
1014 	if (err)
1015 		goto exit;
1016 	err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
1017 	if (err)
1018 		goto exit;
1019 
1020 	memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
1021 	p2p->remain_on_channel_cookie = cookie;
1022 	cfg80211_ready_on_channel(wdev, cookie, channel, duration, GFP_KERNEL);
1023 
1024 exit:
1025 	return err;
1026 }
1027 
1028 
1029 /**
1030  * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
1031  *
1032  * @ifp: interfac control.
1033  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1034  * @data: payload of message. Not used.
1035  *
1036  */
brcmf_p2p_notify_listen_complete(struct brcmf_if * ifp,const struct brcmf_event_msg * e,void * data)1037 int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
1038 				     const struct brcmf_event_msg *e,
1039 				     void *data)
1040 {
1041 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1042 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1043 
1044 	brcmf_dbg(TRACE, "Enter\n");
1045 	if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
1046 			       &p2p->status)) {
1047 		if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1048 				       &p2p->status)) {
1049 			clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1050 				  &p2p->status);
1051 			brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1052 			complete(&p2p->wait_next_af);
1053 		}
1054 
1055 		cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1056 						   p2p->remain_on_channel_cookie,
1057 						   &p2p->remain_on_channel,
1058 						   GFP_KERNEL);
1059 	}
1060 	return 0;
1061 }
1062 
1063 
1064 /**
1065  * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1066  *
1067  * @ifp: interfac control.
1068  *
1069  */
brcmf_p2p_cancel_remain_on_channel(struct brcmf_if * ifp)1070 void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1071 {
1072 	if (!ifp)
1073 		return;
1074 	brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1075 	brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1076 }
1077 
1078 
1079 /**
1080  * brcmf_p2p_act_frm_search() - search function for action frame.
1081  *
1082  * @p2p: p2p device.
1083  * @channel: channel on which action frame is to be trasmitted.
1084  *
1085  * search function to reach at common channel to send action frame. When
1086  * channel is 0 then all social channels will be used to send af
1087  */
brcmf_p2p_act_frm_search(struct brcmf_p2p_info * p2p,u16 channel)1088 static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1089 {
1090 	struct brcmf_pub *drvr = p2p->cfg->pub;
1091 	s32 err;
1092 	u32 channel_cnt;
1093 	u16 *default_chan_list;
1094 	u32 i;
1095 	struct brcmu_chan ch;
1096 
1097 	brcmf_dbg(TRACE, "Enter\n");
1098 
1099 	if (channel)
1100 		channel_cnt = AF_PEER_SEARCH_CNT;
1101 	else
1102 		channel_cnt = SOCIAL_CHAN_CNT;
1103 	default_chan_list = kcalloc(channel_cnt, sizeof(*default_chan_list),
1104 				    GFP_KERNEL);
1105 	if (default_chan_list == NULL) {
1106 		bphy_err(drvr, "channel list allocation failed\n");
1107 		err = -ENOMEM;
1108 		goto exit;
1109 	}
1110 	ch.bw = BRCMU_CHAN_BW_20;
1111 	if (channel) {
1112 		ch.chnum = channel;
1113 		p2p->cfg->d11inf.encchspec(&ch);
1114 		/* insert same channel to the chan_list */
1115 		for (i = 0; i < channel_cnt; i++)
1116 			default_chan_list[i] = ch.chspec;
1117 	} else {
1118 		ch.chnum = SOCIAL_CHAN_1;
1119 		p2p->cfg->d11inf.encchspec(&ch);
1120 		default_chan_list[0] = ch.chspec;
1121 		ch.chnum = SOCIAL_CHAN_2;
1122 		p2p->cfg->d11inf.encchspec(&ch);
1123 		default_chan_list[1] = ch.chspec;
1124 		ch.chnum = SOCIAL_CHAN_3;
1125 		p2p->cfg->d11inf.encchspec(&ch);
1126 		default_chan_list[2] = ch.chspec;
1127 	}
1128 	err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
1129 			      WL_P2P_DISC_ST_SEARCH, P2PAPI_BSSCFG_DEVICE);
1130 	kfree(default_chan_list);
1131 exit:
1132 	return err;
1133 }
1134 
1135 
1136 /**
1137  * brcmf_p2p_afx_handler() - afx worker thread.
1138  *
1139  * @work:
1140  *
1141  */
brcmf_p2p_afx_handler(struct work_struct * work)1142 static void brcmf_p2p_afx_handler(struct work_struct *work)
1143 {
1144 	struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
1145 	struct brcmf_p2p_info *p2p = container_of(afx_hdl,
1146 						  struct brcmf_p2p_info,
1147 						  afx_hdl);
1148 	struct brcmf_pub *drvr = p2p->cfg->pub;
1149 	s32 err;
1150 
1151 	if (!afx_hdl->is_active)
1152 		return;
1153 
1154 	if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1155 		/* 100ms ~ 300ms */
1156 		err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
1157 						100 * get_random_u32_inclusive(1, 3));
1158 	else
1159 		err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1160 
1161 	if (err) {
1162 		bphy_err(drvr, "ERROR occurred! value is (%d)\n", err);
1163 		if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1164 			     &p2p->status))
1165 			complete(&afx_hdl->act_frm_scan);
1166 	}
1167 }
1168 
1169 
1170 /**
1171  * brcmf_p2p_af_searching_channel() - search channel.
1172  *
1173  * @p2p: p2p device info struct.
1174  *
1175  */
brcmf_p2p_af_searching_channel(struct brcmf_p2p_info * p2p)1176 static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1177 {
1178 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1179 	struct brcmf_cfg80211_vif *pri_vif;
1180 	s32 retry;
1181 
1182 	brcmf_dbg(TRACE, "Enter\n");
1183 
1184 	pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1185 
1186 	reinit_completion(&afx_hdl->act_frm_scan);
1187 	set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1188 	afx_hdl->is_active = true;
1189 	afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
1190 
1191 	/* Loop to wait until we find a peer's channel or the
1192 	 * pending action frame tx is cancelled.
1193 	 */
1194 	retry = 0;
1195 	while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
1196 	       (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
1197 		afx_hdl->is_listen = false;
1198 		brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
1199 			  retry);
1200 		/* search peer on peer's listen channel */
1201 		schedule_work(&afx_hdl->afx_work);
1202 		wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1203 					    P2P_AF_FRM_SCAN_MAX_WAIT);
1204 		if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1205 		    (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1206 			       &p2p->status)))
1207 			break;
1208 
1209 		if (afx_hdl->my_listen_chan) {
1210 			brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
1211 				  afx_hdl->my_listen_chan);
1212 			/* listen on my listen channel */
1213 			afx_hdl->is_listen = true;
1214 			schedule_work(&afx_hdl->afx_work);
1215 			wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1216 						    P2P_AF_FRM_SCAN_MAX_WAIT);
1217 		}
1218 		if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1219 		    (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1220 			       &p2p->status)))
1221 			break;
1222 		retry++;
1223 
1224 		/* if sta is connected or connecting, sleep for a while before
1225 		 * retry af tx or finding a peer
1226 		 */
1227 		if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
1228 		    test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
1229 			msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
1230 	}
1231 
1232 	brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1233 		  afx_hdl->peer_chan);
1234 	afx_hdl->is_active = false;
1235 
1236 	clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1237 
1238 	return afx_hdl->peer_chan;
1239 }
1240 
1241 
1242 /**
1243  * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1244  *
1245  * @cfg: common configuration struct.
1246  * @bi: bss info struct, result from scan.
1247  *
1248  */
brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info * cfg,struct brcmf_bss_info_le * bi)1249 bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1250 					   struct brcmf_bss_info_le *bi)
1251 
1252 {
1253 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1254 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1255 	struct brcmu_chan ch;
1256 	u8 *ie;
1257 	s32 err;
1258 	u8 p2p_dev_addr[ETH_ALEN];
1259 
1260 	if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
1261 		return false;
1262 
1263 	if (bi == NULL) {
1264 		brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
1265 		if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
1266 			complete(&afx_hdl->act_frm_scan);
1267 		return true;
1268 	}
1269 
1270 	ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
1271 	memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
1272 	err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1273 				    IEEE80211_P2P_ATTR_DEVICE_INFO,
1274 				    p2p_dev_addr, sizeof(p2p_dev_addr));
1275 	if (err < 0)
1276 		err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1277 					    IEEE80211_P2P_ATTR_DEVICE_ID,
1278 					    p2p_dev_addr, sizeof(p2p_dev_addr));
1279 	if ((err >= 0) &&
1280 	    (ether_addr_equal(p2p_dev_addr, afx_hdl->tx_dst_addr))) {
1281 		if (!bi->ctl_ch) {
1282 			ch.chspec = le16_to_cpu(bi->chanspec);
1283 			cfg->d11inf.decchspec(&ch);
1284 			bi->ctl_ch = ch.control_ch_num;
1285 		}
1286 		afx_hdl->peer_chan = bi->ctl_ch;
1287 		brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
1288 			  afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
1289 		complete(&afx_hdl->act_frm_scan);
1290 	}
1291 	return true;
1292 }
1293 
1294 /**
1295  * brcmf_p2p_abort_action_frame() - abort action frame.
1296  *
1297  * @cfg: common configuration struct.
1298  *
1299  */
brcmf_p2p_abort_action_frame(struct brcmf_cfg80211_info * cfg)1300 static s32 brcmf_p2p_abort_action_frame(struct brcmf_cfg80211_info *cfg)
1301 {
1302 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1303 	struct brcmf_cfg80211_vif *vif;
1304 	s32 err;
1305 	s32 int_val = 1;
1306 
1307 	brcmf_dbg(TRACE, "Enter\n");
1308 
1309 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1310 	if (!vif)
1311 		vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1312 
1313 	err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe_abort", &int_val,
1314 					sizeof(s32));
1315 	if (err)
1316 		brcmf_err(" aborting action frame has failed (%d)\n", err);
1317 
1318 	return err;
1319 }
1320 
1321 /**
1322  * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1323  *
1324  * @cfg: common configuration struct.
1325  *
1326  */
1327 static void
brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info * cfg)1328 brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1329 {
1330 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1331 	struct brcmf_if *ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1332 	s32 err;
1333 
1334 	if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
1335 	    (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
1336 	     test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
1337 		brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
1338 		/* if channel is not zero, "actfame" uses off channel scan.
1339 		 * So abort scan for off channel completion.
1340 		 */
1341 		if (p2p->af_sent_channel) {
1342 			/* abort actframe using actframe_abort or abort scan */
1343 			err = brcmf_p2p_abort_action_frame(cfg);
1344 			if (err)
1345 				brcmf_notify_escan_complete(cfg, ifp, true,
1346 							    true);
1347 		}
1348 	} else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1349 			    &p2p->status)) {
1350 		brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1351 		/* So abort scan to cancel listen */
1352 		brcmf_notify_escan_complete(cfg, ifp, true, true);
1353 	}
1354 }
1355 
1356 
1357 /**
1358  * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1359  *
1360  * @p2p: p2p device info struct.
1361  * @mac: MAC address.
1362  *
1363  * return true if recevied action frame is to be dropped.
1364  */
1365 static bool
brcmf_p2p_gon_req_collision(struct brcmf_p2p_info * p2p,u8 * mac)1366 brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1367 {
1368 	struct brcmf_cfg80211_info *cfg = p2p->cfg;
1369 	struct brcmf_if *ifp;
1370 
1371 	brcmf_dbg(TRACE, "Enter\n");
1372 
1373 	if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1374 	    !p2p->gon_req_action)
1375 		return false;
1376 
1377 	brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
1378 	/* if sa(peer) addr is less than da(my) addr, then this device
1379 	 * process peer's gon request and block to send gon req.
1380 	 * if not (sa addr > da addr),
1381 	 * this device will process gon request and drop gon req of peer.
1382 	 */
1383 	ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
1384 	if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
1385 		brcmf_dbg(INFO, "Block transmit gon req !!!\n");
1386 		p2p->block_gon_req_tx = true;
1387 		/* if we are finding a common channel for sending af,
1388 		 * do not scan more to block to send current gon req
1389 		 */
1390 		if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1391 				       &p2p->status))
1392 			complete(&p2p->afx_hdl.act_frm_scan);
1393 		if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1394 				       &p2p->status))
1395 			brcmf_p2p_stop_wait_next_action_frame(cfg);
1396 		return false;
1397 	}
1398 
1399 	/* drop gon request of peer to process gon request by this device. */
1400 	brcmf_dbg(INFO, "Drop received gon req !!!\n");
1401 
1402 	return true;
1403 }
1404 
1405 
1406 /**
1407  * brcmf_p2p_notify_action_frame_rx() - received action frame.
1408  *
1409  * @ifp: interfac control.
1410  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1411  * @data: payload of message, containing action frame data.
1412  *
1413  */
brcmf_p2p_notify_action_frame_rx(struct brcmf_if * ifp,const struct brcmf_event_msg * e,void * data)1414 int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1415 				     const struct brcmf_event_msg *e,
1416 				     void *data)
1417 {
1418 	struct brcmf_pub *drvr = ifp->drvr;
1419 	struct brcmf_cfg80211_info *cfg = drvr->config;
1420 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1421 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1422 	struct wireless_dev *wdev;
1423 	u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1424 	struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1425 	u8 *frame = (u8 *)(rxframe + 1);
1426 	struct brcmf_p2p_pub_act_frame *act_frm;
1427 	struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
1428 	struct brcmu_chan ch;
1429 	struct ieee80211_mgmt *mgmt_frame;
1430 	s32 freq;
1431 	u16 mgmt_type;
1432 	u8 action;
1433 
1434 	if (e->datalen < sizeof(*rxframe)) {
1435 		brcmf_dbg(SCAN, "Event data too small. Ignore\n");
1436 		return 0;
1437 	}
1438 
1439 	ch.chspec = be16_to_cpu(rxframe->chanspec);
1440 	cfg->d11inf.decchspec(&ch);
1441 	/* Check if wpa_supplicant has registered for this frame */
1442 	brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1443 	mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1444 	if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1445 		return 0;
1446 
1447 	brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
1448 
1449 	action = P2P_PAF_SUBTYPE_INVALID;
1450 	if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
1451 		act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
1452 		action = act_frm->subtype;
1453 		if ((action == P2P_PAF_GON_REQ) &&
1454 		    (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
1455 			if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1456 				     &p2p->status) &&
1457 			    (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
1458 				afx_hdl->peer_chan = ch.control_ch_num;
1459 				brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1460 					  afx_hdl->peer_chan);
1461 				complete(&afx_hdl->act_frm_scan);
1462 			}
1463 			return 0;
1464 		}
1465 		/* After complete GO Negotiation, roll back to mpc mode */
1466 		if ((action == P2P_PAF_GON_CONF) ||
1467 		    (action == P2P_PAF_PROVDIS_RSP))
1468 			brcmf_set_mpc(ifp, 1);
1469 		if (action == P2P_PAF_GON_CONF) {
1470 			brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1471 			clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1472 		}
1473 	} else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
1474 		sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
1475 		action = sd_act_frm->action;
1476 	}
1477 
1478 	if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1479 	    (p2p->next_af_subtype == action)) {
1480 		brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
1481 		clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1482 			  &p2p->status);
1483 		/* Stop waiting for next AF. */
1484 		brcmf_p2p_stop_wait_next_action_frame(cfg);
1485 	}
1486 
1487 	mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1488 			     mgmt_frame_len, GFP_KERNEL);
1489 	if (!mgmt_frame) {
1490 		bphy_err(drvr, "No memory available for action frame\n");
1491 		return -ENOMEM;
1492 	}
1493 	memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
1494 	brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1495 			       ETH_ALEN);
1496 	memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1497 	mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
1498 	memcpy(mgmt_frame->u.body, frame, mgmt_frame_len);
1499 	mgmt_frame_len += offsetof(struct ieee80211_mgmt, u.body);
1500 
1501 	freq = ieee80211_channel_to_frequency(ch.control_ch_num,
1502 					      ch.band == BRCMU_CHAN_BAND_2G ?
1503 					      NL80211_BAND_2GHZ :
1504 					      NL80211_BAND_5GHZ);
1505 
1506 	wdev = &ifp->vif->wdev;
1507 	cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len, 0);
1508 
1509 	kfree(mgmt_frame);
1510 	return 0;
1511 }
1512 
1513 
1514 /**
1515  * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1516  *
1517  * @ifp: interfac control.
1518  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1519  * @data: not used.
1520  *
1521  */
brcmf_p2p_notify_action_tx_complete(struct brcmf_if * ifp,const struct brcmf_event_msg * e,void * data)1522 int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1523 					const struct brcmf_event_msg *e,
1524 					void *data)
1525 {
1526 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1527 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1528 
1529 	brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1530 		  e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1531 		  "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1532 		  e->status);
1533 
1534 	if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1535 		return 0;
1536 
1537 	if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
1538 		if (e->status == BRCMF_E_STATUS_SUCCESS) {
1539 			set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1540 				&p2p->status);
1541 			if (!p2p->wait_for_offchan_complete)
1542 				complete(&p2p->send_af_done);
1543 		} else {
1544 			set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1545 			/* If there is no ack, we don't need to wait for
1546 			 * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1547 			 */
1548 			brcmf_p2p_stop_wait_next_action_frame(cfg);
1549 		}
1550 
1551 	} else {
1552 		complete(&p2p->send_af_done);
1553 	}
1554 	return 0;
1555 }
1556 
1557 
1558 /**
1559  * brcmf_p2p_tx_action_frame() - send action frame over fil.
1560  *
1561  * @ifp: interface to transmit on.
1562  * @p2p: p2p info struct for vif.
1563  * @af_params: action frame data/info.
1564  *
1565  * Send an action frame immediately without doing channel synchronization.
1566  *
1567  * This function waits for a completion event before returning.
1568  * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1569  * frame is transmitted.
1570  */
brcmf_p2p_tx_action_frame(struct brcmf_if * ifp,struct brcmf_p2p_info * p2p,struct brcmf_fil_af_params_le * af_params)1571 static s32 brcmf_p2p_tx_action_frame(struct brcmf_if *ifp,
1572 				     struct brcmf_p2p_info *p2p,
1573 				     struct brcmf_fil_af_params_le *af_params)
1574 {
1575 	struct brcmf_pub *drvr = p2p->cfg->pub;
1576 	s32 err = 0;
1577 
1578 	brcmf_dbg(TRACE, "Enter\n");
1579 
1580 	reinit_completion(&p2p->send_af_done);
1581 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1582 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1583 
1584 	err = brcmf_fil_bsscfg_data_set(ifp, "actframe", af_params,
1585 					sizeof(*af_params));
1586 	if (err) {
1587 		bphy_err(drvr, " sending action frame has failed\n");
1588 		goto exit;
1589 	}
1590 
1591 	p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1592 	p2p->af_tx_sent_jiffies = jiffies;
1593 
1594 	if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status) &&
1595 	    p2p->af_sent_channel ==
1596 	    ieee80211_frequency_to_channel(p2p->remain_on_channel.center_freq))
1597 		p2p->wait_for_offchan_complete = false;
1598 	else
1599 		p2p->wait_for_offchan_complete = true;
1600 
1601 	brcmf_dbg(TRACE, "Waiting for %s tx completion event\n",
1602 		  (p2p->wait_for_offchan_complete) ?
1603 		   "off-channel" : "on-channel");
1604 
1605 	wait_for_completion_timeout(&p2p->send_af_done, P2P_AF_MAX_WAIT_TIME);
1606 
1607 	if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1608 		brcmf_dbg(TRACE, "TX action frame operation is success\n");
1609 	} else {
1610 		err = -EIO;
1611 		brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1612 	}
1613 	/* clear status bit for action tx */
1614 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1615 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1616 
1617 exit:
1618 	return err;
1619 }
1620 
1621 
1622 /**
1623  * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1624  *
1625  * @cfg: driver private data for cfg80211 interface.
1626  * @af_params: action frame data/info.
1627  * @config_af_params: configuration data for action frame.
1628  *
1629  * routine which transmits ation frame public type.
1630  */
brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info * cfg,struct brcmf_fil_af_params_le * af_params,struct brcmf_config_af_params * config_af_params)1631 static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1632 			       struct brcmf_fil_af_params_le *af_params,
1633 			       struct brcmf_config_af_params *config_af_params)
1634 {
1635 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1636 	struct brcmf_pub *drvr = cfg->pub;
1637 	struct brcmf_fil_action_frame_le *action_frame;
1638 	struct brcmf_p2p_pub_act_frame *act_frm;
1639 	s32 err = 0;
1640 	u16 ie_len;
1641 
1642 	action_frame = &af_params->action_frame;
1643 	act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1644 
1645 	config_af_params->extra_listen = true;
1646 
1647 	switch (act_frm->subtype) {
1648 	case P2P_PAF_GON_REQ:
1649 		brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1650 		set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1651 		config_af_params->mpc_onoff = 0;
1652 		config_af_params->search_channel = true;
1653 		p2p->next_af_subtype = act_frm->subtype + 1;
1654 		p2p->gon_req_action = true;
1655 		/* increase dwell time to wait for RESP frame */
1656 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1657 		break;
1658 	case P2P_PAF_GON_RSP:
1659 		p2p->next_af_subtype = act_frm->subtype + 1;
1660 		/* increase dwell time to wait for CONF frame */
1661 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1662 		break;
1663 	case P2P_PAF_GON_CONF:
1664 		/* If we reached till GO Neg confirmation reset the filter */
1665 		brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1666 		clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1667 		/* turn on mpc again if go nego is done */
1668 		config_af_params->mpc_onoff = 1;
1669 		/* minimize dwell time */
1670 		af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1671 		config_af_params->extra_listen = false;
1672 		break;
1673 	case P2P_PAF_INVITE_REQ:
1674 		config_af_params->search_channel = true;
1675 		p2p->next_af_subtype = act_frm->subtype + 1;
1676 		/* increase dwell time */
1677 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1678 		break;
1679 	case P2P_PAF_INVITE_RSP:
1680 		/* minimize dwell time */
1681 		af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1682 		config_af_params->extra_listen = false;
1683 		break;
1684 	case P2P_PAF_DEVDIS_REQ:
1685 		config_af_params->search_channel = true;
1686 		p2p->next_af_subtype = act_frm->subtype + 1;
1687 		/* maximize dwell time to wait for RESP frame */
1688 		af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1689 		break;
1690 	case P2P_PAF_DEVDIS_RSP:
1691 		/* minimize dwell time */
1692 		af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1693 		config_af_params->extra_listen = false;
1694 		break;
1695 	case P2P_PAF_PROVDIS_REQ:
1696 		ie_len = le16_to_cpu(action_frame->len) -
1697 			 offsetof(struct brcmf_p2p_pub_act_frame, elts);
1698 		if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1699 					  IEEE80211_P2P_ATTR_GROUP_ID,
1700 					  NULL, 0) < 0)
1701 			config_af_params->search_channel = true;
1702 		config_af_params->mpc_onoff = 0;
1703 		p2p->next_af_subtype = act_frm->subtype + 1;
1704 		/* increase dwell time to wait for RESP frame */
1705 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1706 		break;
1707 	case P2P_PAF_PROVDIS_RSP:
1708 		/* wpa_supplicant send go nego req right after prov disc */
1709 		p2p->next_af_subtype = P2P_PAF_GON_REQ;
1710 		/* increase dwell time to MED level */
1711 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1712 		config_af_params->extra_listen = false;
1713 		break;
1714 	default:
1715 		bphy_err(drvr, "Unknown p2p pub act frame subtype: %d\n",
1716 			 act_frm->subtype);
1717 		err = -EINVAL;
1718 	}
1719 	return err;
1720 }
1721 
brcmf_p2p_check_dwell_overflow(u32 requested_dwell,unsigned long dwell_jiffies)1722 static bool brcmf_p2p_check_dwell_overflow(u32 requested_dwell,
1723 					   unsigned long dwell_jiffies)
1724 {
1725 	if ((requested_dwell & CUSTOM_RETRY_MASK) &&
1726 	    (jiffies_to_msecs(jiffies - dwell_jiffies) >
1727 	    (requested_dwell & ~CUSTOM_RETRY_MASK))) {
1728 		brcmf_err("Action frame TX retry time over dwell time!\n");
1729 		return true;
1730 	}
1731 	return false;
1732 }
1733 /**
1734  * brcmf_p2p_send_action_frame() - send action frame .
1735  *
1736  * @ifp: interface to transmit on.
1737  * @af_params: configuration data for action frame.
1738  */
brcmf_p2p_send_action_frame(struct brcmf_if * ifp,struct brcmf_fil_af_params_le * af_params)1739 bool brcmf_p2p_send_action_frame(struct brcmf_if *ifp,
1740 				 struct brcmf_fil_af_params_le *af_params)
1741 {
1742 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1743 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1744 	struct brcmf_fil_action_frame_le *action_frame;
1745 	struct brcmf_config_af_params config_af_params;
1746 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1747 	struct brcmf_pub *drvr = cfg->pub;
1748 	u16 action_frame_len;
1749 	bool ack = false;
1750 	u8 category;
1751 	u8 action;
1752 	s32 tx_retry;
1753 	s32 extra_listen_time;
1754 	uint delta_ms;
1755 	unsigned long dwell_jiffies = 0;
1756 	bool dwell_overflow = false;
1757 
1758 	u32 requested_dwell = le32_to_cpu(af_params->dwell_time);
1759 	action_frame = &af_params->action_frame;
1760 	action_frame_len = le16_to_cpu(action_frame->len);
1761 
1762 	brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1763 
1764 	/* Add the default dwell time. Dwell time to stay off-channel */
1765 	/* to wait for a response action frame after transmitting an  */
1766 	/* GO Negotiation action frame                                */
1767 	af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1768 
1769 	category = action_frame->data[DOT11_ACTION_CAT_OFF];
1770 	action = action_frame->data[DOT11_ACTION_ACT_OFF];
1771 
1772 	/* initialize variables */
1773 	p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
1774 	p2p->gon_req_action = false;
1775 
1776 	/* config parameters */
1777 	config_af_params.mpc_onoff = -1;
1778 	config_af_params.search_channel = false;
1779 	config_af_params.extra_listen = false;
1780 
1781 	if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1782 		/* p2p public action frame process */
1783 		if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1784 			/* Just send unknown subtype frame with */
1785 			/* default parameters.                  */
1786 			bphy_err(drvr, "P2P Public action frame, unknown subtype.\n");
1787 		}
1788 	} else if (brcmf_p2p_is_gas_action(action_frame->data,
1789 					   action_frame_len)) {
1790 		/* service discovery process */
1791 		if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1792 		    action == P2PSD_ACTION_ID_GAS_CREQ) {
1793 			/* configure service discovery query frame */
1794 			config_af_params.search_channel = true;
1795 
1796 			/* save next af suptype to cancel */
1797 			/* remaining dwell time           */
1798 			p2p->next_af_subtype = action + 1;
1799 
1800 			af_params->dwell_time =
1801 				cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1802 		} else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1803 			   action == P2PSD_ACTION_ID_GAS_CRESP) {
1804 			/* configure service discovery response frame */
1805 			af_params->dwell_time =
1806 				cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1807 		} else {
1808 			bphy_err(drvr, "Unknown action type: %d\n", action);
1809 			goto exit;
1810 		}
1811 	} else if (brcmf_p2p_is_p2p_action(action_frame->data,
1812 					   action_frame_len) ||
1813 		   brcmf_p2p_is_dpp_pub_action(action_frame->data,
1814 					       action_frame_len)) {
1815 		/* do not configure anything. it will be */
1816 		/* sent with a default configuration     */
1817 	} else {
1818 		bphy_err(drvr, "Unknown Frame: category 0x%x, action 0x%x\n",
1819 			 category, action);
1820 		return false;
1821 	}
1822 
1823 	/* if connecting on primary iface, sleep for a while before sending
1824 	 * af tx for VSDB
1825 	 */
1826 	if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1827 		     &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1828 		msleep(50);
1829 
1830 	/* if scan is ongoing, abort current scan. */
1831 	if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1832 		brcmf_abort_scanning(cfg);
1833 
1834 	memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1835 
1836 	/* To make sure to send successfully action frame, turn off mpc */
1837 	if (config_af_params.mpc_onoff == 0)
1838 		brcmf_set_mpc(ifp, 0);
1839 
1840 	/* set status and destination address before sending af */
1841 	if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1842 		/* set status to cancel the remained dwell time in rx process */
1843 		set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1844 	}
1845 
1846 	p2p->af_sent_channel = 0;
1847 	set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1848 	/* validate channel and p2p ies */
1849 	if (config_af_params.search_channel &&
1850 	    IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1851 	    p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif &&
1852 	    p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1853 		afx_hdl = &p2p->afx_hdl;
1854 		afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1855 
1856 		if (brcmf_p2p_af_searching_channel(p2p) ==
1857 							P2P_INVALID_CHANNEL) {
1858 			bphy_err(drvr, "Couldn't find peer's channel.\n");
1859 			goto exit;
1860 		}
1861 
1862 		/* Abort scan even for VSDB scenarios. Scan gets aborted in
1863 		 * firmware but after the check of piggyback algorithm. To take
1864 		 * care of current piggback algo, lets abort the scan here
1865 		 * itself.
1866 		 */
1867 		brcmf_notify_escan_complete(cfg, ifp, true, true);
1868 
1869 		/* update channel */
1870 		af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1871 	}
1872 	dwell_jiffies = jiffies;
1873 	dwell_overflow = brcmf_p2p_check_dwell_overflow(requested_dwell,
1874 							dwell_jiffies);
1875 
1876 	tx_retry = 0;
1877 	while (!p2p->block_gon_req_tx &&
1878 	       (!ack) && (tx_retry < P2P_AF_TX_MAX_RETRY) &&
1879 		!dwell_overflow) {
1880 		if (af_params->channel)
1881 			msleep(P2P_AF_RETRY_DELAY_TIME);
1882 
1883 		ack = !brcmf_p2p_tx_action_frame(ifp, p2p, af_params);
1884 		tx_retry++;
1885 		dwell_overflow = brcmf_p2p_check_dwell_overflow(requested_dwell,
1886 								dwell_jiffies);
1887 	}
1888 	if (!ack) {
1889 		bphy_err(drvr, "Failed to send Action Frame(retry %d)\n",
1890 			 tx_retry);
1891 		clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1892 	}
1893 
1894 exit:
1895 	clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1896 
1897 	/* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1898 	 * if we coundn't get the next action response frame and dongle does
1899 	 * not keep the dwell time, go to listen state again to get next action
1900 	 * response frame.
1901 	 */
1902 	if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1903 	    test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1904 	    p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1905 		delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1906 		if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1907 			extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1908 					    delta_ms;
1909 		else
1910 			extra_listen_time = 0;
1911 		if (extra_listen_time > 50) {
1912 			set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1913 				&p2p->status);
1914 			brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1915 				  le32_to_cpu(af_params->dwell_time),
1916 				  extra_listen_time);
1917 			extra_listen_time += 100;
1918 			if (!brcmf_p2p_discover_listen(p2p,
1919 						       p2p->af_sent_channel,
1920 						       extra_listen_time)) {
1921 				unsigned long duration;
1922 
1923 				extra_listen_time += 100;
1924 				duration = msecs_to_jiffies(extra_listen_time);
1925 				wait_for_completion_timeout(&p2p->wait_next_af,
1926 							    duration);
1927 			}
1928 			clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1929 				  &p2p->status);
1930 		}
1931 	}
1932 
1933 	if (p2p->block_gon_req_tx) {
1934 		/* if ack is true, supplicant will wait more time(100ms).
1935 		 * so we will return it as a success to get more time .
1936 		 */
1937 		p2p->block_gon_req_tx = false;
1938 		ack = true;
1939 	}
1940 
1941 	clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1942 	/* if all done, turn mpc on again */
1943 	if (config_af_params.mpc_onoff == 1)
1944 		brcmf_set_mpc(ifp, 1);
1945 
1946 	return ack;
1947 }
1948 
1949 /**
1950  * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1951  *
1952  * @ifp: interface pointer for which event was received.
1953  * @e: even message.
1954  * @data: payload of event message (probe request).
1955  */
brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if * ifp,const struct brcmf_event_msg * e,void * data)1956 s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1957 					  const struct brcmf_event_msg *e,
1958 					  void *data)
1959 {
1960 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1961 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1962 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1963 	struct brcmf_cfg80211_vif *vif = ifp->vif;
1964 	struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1965 	struct brcmu_chan ch;
1966 	u8 *mgmt_frame;
1967 	u32 mgmt_frame_len;
1968 	s32 freq;
1969 	u16 mgmt_type;
1970 
1971 	brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1972 		  e->reason);
1973 
1974 	if (e->datalen < sizeof(*rxframe)) {
1975 		brcmf_dbg(SCAN, "Event data too small. Ignore\n");
1976 		return 0;
1977 	}
1978 
1979 	ch.chspec = be16_to_cpu(rxframe->chanspec);
1980 	cfg->d11inf.decchspec(&ch);
1981 
1982 	if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
1983 	    (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
1984 		afx_hdl->peer_chan = ch.control_ch_num;
1985 		brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
1986 			  afx_hdl->peer_chan);
1987 		complete(&afx_hdl->act_frm_scan);
1988 	}
1989 
1990 	/* Firmware sends us two proberesponses for each idx one. At the */
1991 	/* moment anything but bsscfgidx 0 is passed up to supplicant    */
1992 	if (e->bsscfgidx == 0)
1993 		return 0;
1994 
1995 	/* Filter any P2P probe reqs arriving during the GO-NEG Phase */
1996 	if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
1997 		brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
1998 		return 0;
1999 	}
2000 
2001 	/* Check if wpa_supplicant has registered for this frame */
2002 	brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
2003 	mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
2004 	if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
2005 		return 0;
2006 
2007 	mgmt_frame = (u8 *)(rxframe + 1);
2008 	mgmt_frame_len = e->datalen - sizeof(*rxframe);
2009 	freq = ieee80211_channel_to_frequency(ch.control_ch_num,
2010 					      ch.band == BRCMU_CHAN_BAND_2G ?
2011 					      NL80211_BAND_2GHZ :
2012 					      NL80211_BAND_5GHZ);
2013 
2014 	cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len, 0);
2015 
2016 	brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
2017 		  mgmt_frame_len, e->datalen, ch.chspec, freq);
2018 
2019 	return 0;
2020 }
2021 
2022 
2023 /**
2024  * brcmf_p2p_get_current_chanspec() - Get current operation channel.
2025  *
2026  * @p2p: P2P specific data.
2027  * @chanspec: chanspec to be returned.
2028  */
brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info * p2p,u16 * chanspec)2029 static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
2030 					   u16 *chanspec)
2031 {
2032 	struct brcmf_if *ifp;
2033 	u8 mac_addr[ETH_ALEN];
2034 	struct brcmu_chan ch;
2035 	struct brcmf_bss_info_le *bi;
2036 	u8 *buf;
2037 
2038 	ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2039 
2040 	if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mac_addr,
2041 				   ETH_ALEN) == 0) {
2042 		buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
2043 		if (buf != NULL) {
2044 			*(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
2045 			if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
2046 						   buf, WL_BSS_INFO_MAX) == 0) {
2047 				bi = (struct brcmf_bss_info_le *)(buf + 4);
2048 				*chanspec = le16_to_cpu(bi->chanspec);
2049 				kfree(buf);
2050 				return;
2051 			}
2052 			kfree(buf);
2053 		}
2054 	}
2055 	/* Use default channel for P2P */
2056 	ch.chnum = BRCMF_P2P_TEMP_CHAN;
2057 	ch.bw = BRCMU_CHAN_BW_20;
2058 	p2p->cfg->d11inf.encchspec(&ch);
2059 	*chanspec = ch.chspec;
2060 }
2061 
2062 /**
2063  * brcmf_p2p_ifchange - Change a P2P Role.
2064  * @cfg: driver private data for cfg80211 interface.
2065  * @if_type: interface type.
2066  * Returns 0 if success.
2067  */
brcmf_p2p_ifchange(struct brcmf_cfg80211_info * cfg,enum brcmf_fil_p2p_if_types if_type)2068 int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
2069 		       enum brcmf_fil_p2p_if_types if_type)
2070 {
2071 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2072 	struct brcmf_pub *drvr = cfg->pub;
2073 	struct brcmf_cfg80211_vif *vif;
2074 	struct brcmf_fil_p2p_if_le if_request;
2075 	s32 err;
2076 	u16 chanspec;
2077 
2078 	brcmf_dbg(TRACE, "Enter\n");
2079 
2080 	vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
2081 	if (!vif) {
2082 		bphy_err(drvr, "vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
2083 		return -EPERM;
2084 	}
2085 	brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
2086 	vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
2087 	if (!vif) {
2088 		bphy_err(drvr, "vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
2089 		return -EPERM;
2090 	}
2091 	brcmf_set_mpc(vif->ifp, 0);
2092 
2093 	/* In concurrency case, STA may be already associated in a particular */
2094 	/* channel. so retrieve the current channel of primary interface and  */
2095 	/* then start the virtual interface on that.                          */
2096 	brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2097 
2098 	if_request.type = cpu_to_le16((u16)if_type);
2099 	if_request.chspec = cpu_to_le16(chanspec);
2100 	memcpy(if_request.addr, p2p->conn_int_addr, sizeof(if_request.addr));
2101 
2102 	brcmf_cfg80211_arm_vif_event(cfg, vif);
2103 	err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
2104 				       sizeof(if_request));
2105 	if (err) {
2106 		bphy_err(drvr, "p2p_ifupd FAILED, err=%d\n", err);
2107 		brcmf_cfg80211_arm_vif_event(cfg, NULL);
2108 		return err;
2109 	}
2110 	err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_CHANGE,
2111 					    BRCMF_VIF_EVENT_TIMEOUT);
2112 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
2113 	if (!err)  {
2114 		bphy_err(drvr, "No BRCMF_E_IF_CHANGE event received\n");
2115 		return -EIO;
2116 	}
2117 
2118 	err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2119 				    BRCMF_SCB_TIMEOUT_VALUE);
2120 
2121 	return err;
2122 }
2123 
brcmf_p2p_request_p2p_if(struct brcmf_p2p_info * p2p,struct brcmf_if * ifp,u8 ea[ETH_ALEN],enum brcmf_fil_p2p_if_types iftype)2124 static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2125 				    struct brcmf_if *ifp, u8 ea[ETH_ALEN],
2126 				    enum brcmf_fil_p2p_if_types iftype)
2127 {
2128 	struct brcmf_fil_p2p_if_le if_request;
2129 	int err;
2130 	u16 chanspec;
2131 
2132 	/* we need a default channel */
2133 	brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2134 
2135 	/* fill the firmware request */
2136 	memcpy(if_request.addr, ea, ETH_ALEN);
2137 	if_request.type = cpu_to_le16((u16)iftype);
2138 	if_request.chspec = cpu_to_le16(chanspec);
2139 
2140 	err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2141 				       sizeof(if_request));
2142 
2143 	return err;
2144 }
2145 
brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif * vif)2146 static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2147 {
2148 	struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2149 	struct net_device *pri_ndev = cfg_to_ndev(cfg);
2150 	struct brcmf_if *ifp = netdev_priv(pri_ndev);
2151 	const u8 *addr = vif->wdev.netdev->dev_addr;
2152 
2153 	return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2154 }
2155 
brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif * vif)2156 static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2157 {
2158 	struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2159 	struct net_device *pri_ndev = cfg_to_ndev(cfg);
2160 	struct brcmf_if *ifp = netdev_priv(pri_ndev);
2161 	const u8 *addr = vif->wdev.netdev->dev_addr;
2162 
2163 	return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2164 }
2165 
2166 /**
2167  * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2168  *
2169  * @p2p: P2P specific data.
2170  * @wiphy: wiphy device of new interface.
2171  * @addr: mac address for this new interface.
2172  */
brcmf_p2p_create_p2pdev(struct brcmf_p2p_info * p2p,struct wiphy * wiphy,u8 * addr)2173 static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2174 						    struct wiphy *wiphy,
2175 						    u8 *addr)
2176 {
2177 	struct brcmf_pub *drvr = p2p->cfg->pub;
2178 	struct brcmf_cfg80211_vif *p2p_vif;
2179 	struct brcmf_if *p2p_ifp;
2180 	struct brcmf_if *pri_ifp;
2181 	int err;
2182 	u32 bsscfgidx;
2183 
2184 	if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2185 		return ERR_PTR(-ENOSPC);
2186 
2187 	p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE);
2188 	if (IS_ERR(p2p_vif)) {
2189 		bphy_err(drvr, "could not create discovery vif\n");
2190 		return (struct wireless_dev *)p2p_vif;
2191 	}
2192 
2193 	pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2194 
2195 	/* firmware requires unique mac address for p2pdev interface */
2196 	if (addr && ether_addr_equal(addr, pri_ifp->mac_addr)) {
2197 		bphy_err(drvr, "discovery vif must be different from primary interface\n");
2198 		err = -EINVAL;
2199 		goto fail;
2200 	}
2201 
2202 	brcmf_p2p_generate_bss_mac(p2p, addr);
2203 	brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2204 
2205 	brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
2206 	brcmf_fweh_p2pdev_setup(pri_ifp, true);
2207 
2208 	/* Initialize P2P Discovery in the firmware */
2209 	err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2210 	if (err < 0) {
2211 		bphy_err(drvr, "set p2p_disc error\n");
2212 		brcmf_fweh_p2pdev_setup(pri_ifp, false);
2213 		brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2214 		goto fail;
2215 	}
2216 
2217 	/* wait for firmware event */
2218 	err = brcmf_cfg80211_wait_vif_event(p2p->cfg, BRCMF_E_IF_ADD,
2219 					    BRCMF_VIF_EVENT_TIMEOUT);
2220 	brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2221 	brcmf_fweh_p2pdev_setup(pri_ifp, false);
2222 	if (!err) {
2223 		bphy_err(drvr, "timeout occurred\n");
2224 		err = -EIO;
2225 		goto fail;
2226 	}
2227 
2228 	/* discovery interface created */
2229 	p2p_ifp = p2p_vif->ifp;
2230 	p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2231 	memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2232 	memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2233 
2234 	/* verify bsscfg index for P2P discovery */
2235 	err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bsscfgidx);
2236 	if (err < 0) {
2237 		bphy_err(drvr, "retrieving discover bsscfg index failed\n");
2238 		goto fail;
2239 	}
2240 
2241 	WARN_ON(p2p_ifp->bsscfgidx != bsscfgidx);
2242 
2243 	INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2244 	init_completion(&p2p->afx_hdl.act_frm_scan);
2245 	init_completion(&p2p->wait_next_af);
2246 
2247 	return &p2p_vif->wdev;
2248 
2249 fail:
2250 	brcmf_free_vif(p2p_vif);
2251 	return ERR_PTR(err);
2252 }
2253 
brcmf_p2p_get_conn_idx(struct brcmf_cfg80211_info * cfg)2254 static int brcmf_p2p_get_conn_idx(struct brcmf_cfg80211_info *cfg)
2255 {
2256 	int i;
2257 	struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2258 
2259 	if (!ifp)
2260 		return -ENODEV;
2261 
2262 	for (i = P2PAPI_BSSCFG_CONNECTION; i < P2PAPI_BSSCFG_MAX; i++) {
2263 		if (!cfg->p2p.bss_idx[i].vif) {
2264 			if (i == P2PAPI_BSSCFG_CONNECTION2 &&
2265 			    !(brcmf_feat_is_enabled(ifp, BRCMF_FEAT_RSDB))) {
2266 				brcmf_err("Multi p2p not supported");
2267 				return -EIO;
2268 			}
2269 			return i;
2270 		}
2271 	}
2272 	return -EIO;
2273 }
2274 
2275 /**
2276  * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2277  *
2278  * @wiphy: wiphy device of new interface.
2279  * @name: name of the new interface.
2280  * @name_assign_type: origin of the interface name
2281  * @type: nl80211 interface type.
2282  * @params: contains mac address for P2P device.
2283  */
brcmf_p2p_add_vif(struct wiphy * wiphy,const char * name,unsigned char name_assign_type,enum nl80211_iftype type,struct vif_params * params)2284 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
2285 				       unsigned char name_assign_type,
2286 				       enum nl80211_iftype type,
2287 				       struct vif_params *params)
2288 {
2289 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2290 	struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2291 	struct brcmf_pub *drvr = cfg->pub;
2292 	struct brcmf_cfg80211_vif *vif;
2293 	enum brcmf_fil_p2p_if_types iftype;
2294 	int err = 0;
2295 	int connidx;
2296 	u8 *p2p_intf_addr;
2297 
2298 	if (brcmf_cfg80211_vif_event_armed(cfg))
2299 		return ERR_PTR(-EBUSY);
2300 
2301 	brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
2302 
2303 	switch (type) {
2304 	case NL80211_IFTYPE_P2P_CLIENT:
2305 		iftype = BRCMF_FIL_P2P_IF_CLIENT;
2306 		break;
2307 	case NL80211_IFTYPE_P2P_GO:
2308 		iftype = BRCMF_FIL_P2P_IF_GO;
2309 		break;
2310 	case NL80211_IFTYPE_P2P_DEVICE:
2311 		return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2312 					       params->macaddr);
2313 	default:
2314 		return ERR_PTR(-EOPNOTSUPP);
2315 	}
2316 
2317 	vif = brcmf_alloc_vif(cfg, type);
2318 	if (IS_ERR(vif))
2319 		return (struct wireless_dev *)vif;
2320 	brcmf_cfg80211_arm_vif_event(cfg, vif);
2321 
2322 	connidx = brcmf_p2p_get_conn_idx(cfg);
2323 
2324 	if (connidx == P2PAPI_BSSCFG_CONNECTION)
2325 		p2p_intf_addr = cfg->p2p.conn_int_addr;
2326 	else if (connidx == P2PAPI_BSSCFG_CONNECTION2)
2327 		p2p_intf_addr = cfg->p2p.conn2_int_addr;
2328 	else
2329 		err = -EINVAL;
2330 
2331 	if (!err)
2332 		err =  brcmf_p2p_request_p2p_if(&cfg->p2p, ifp,
2333 						p2p_intf_addr, iftype);
2334 
2335 	if (err) {
2336 		brcmf_err("request p2p interface failed\n");
2337 		brcmf_cfg80211_arm_vif_event(cfg, NULL);
2338 		goto fail;
2339 	}
2340 
2341 	/* wait for firmware event */
2342 	err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_ADD,
2343 					    BRCMF_VIF_EVENT_TIMEOUT);
2344 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
2345 	if (!err) {
2346 		bphy_err(drvr, "timeout occurred\n");
2347 		err = -EIO;
2348 		goto fail;
2349 	}
2350 
2351 	/* interface created in firmware */
2352 	ifp = vif->ifp;
2353 	if (!ifp) {
2354 		bphy_err(drvr, "no if pointer provided\n");
2355 		err = -ENOENT;
2356 		goto fail;
2357 	}
2358 
2359 	strscpy(ifp->ndev->name, name, sizeof(ifp->ndev->name));
2360 	ifp->ndev->name_assign_type = name_assign_type;
2361 	err = brcmf_net_attach(ifp, true);
2362 	if (err) {
2363 		bphy_err(drvr, "Registering netdevice failed\n");
2364 		free_netdev(ifp->ndev);
2365 		goto fail;
2366 	}
2367 
2368 	cfg->p2p.bss_idx[connidx].vif = vif;
2369 	/* Disable firmware roaming for P2P interface  */
2370 	brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2371 	if (iftype == BRCMF_FIL_P2P_IF_GO) {
2372 		/* set station timeout for p2p */
2373 		brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2374 				      BRCMF_SCB_TIMEOUT_VALUE);
2375 	}
2376 	return &ifp->vif->wdev;
2377 
2378 fail:
2379 	brcmf_free_vif(vif);
2380 	return ERR_PTR(err);
2381 }
2382 
2383 /**
2384  * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2385  *
2386  * @wiphy: wiphy device of interface.
2387  * @wdev: wireless device of interface.
2388  */
brcmf_p2p_del_vif(struct wiphy * wiphy,struct wireless_dev * wdev)2389 int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2390 {
2391 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2392 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2393 	struct brcmf_cfg80211_vif *vif;
2394 	enum nl80211_iftype iftype;
2395 	bool wait_for_disable = false;
2396 	int err;
2397 
2398 	brcmf_dbg(TRACE, "delete P2P vif\n");
2399 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2400 
2401 	iftype = vif->wdev.iftype;
2402 	brcmf_cfg80211_arm_vif_event(cfg, vif);
2403 	switch (iftype) {
2404 	case NL80211_IFTYPE_P2P_CLIENT:
2405 		if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2406 			wait_for_disable = true;
2407 		break;
2408 
2409 	case NL80211_IFTYPE_P2P_GO:
2410 		if (!brcmf_p2p_disable_p2p_if(vif))
2411 			wait_for_disable = true;
2412 		break;
2413 
2414 	case NL80211_IFTYPE_P2P_DEVICE:
2415 		if (!p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2416 			return 0;
2417 		brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2418 		brcmf_p2p_deinit_discovery(p2p);
2419 		break;
2420 
2421 	default:
2422 		return -ENOTSUPP;
2423 	}
2424 
2425 	clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2426 	brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2427 
2428 	if (wait_for_disable)
2429 		wait_for_completion_timeout(&cfg->vif_disabled,
2430 					    BRCMF_P2P_DISABLE_TIMEOUT);
2431 
2432 	err = 0;
2433 	if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
2434 		brcmf_vif_clear_mgmt_ies(vif);
2435 		err = brcmf_p2p_release_p2p_if(vif);
2436 	}
2437 	if (!err) {
2438 		/* wait for firmware event */
2439 		err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_DEL,
2440 						    BRCMF_VIF_EVENT_TIMEOUT);
2441 		if (!err)
2442 			err = -EIO;
2443 		else
2444 			err = 0;
2445 	}
2446 	brcmf_remove_interface(vif->ifp, true);
2447 
2448 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
2449 	if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
2450 		if (vif == p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif)
2451 			p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
2452 		if (vif == p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION2].vif)
2453 			p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION2].vif = NULL;
2454 	}
2455 
2456 	return err;
2457 }
2458 
brcmf_p2p_ifp_removed(struct brcmf_if * ifp,bool locked)2459 void brcmf_p2p_ifp_removed(struct brcmf_if *ifp, bool locked)
2460 {
2461 	struct brcmf_cfg80211_info *cfg;
2462 	struct brcmf_cfg80211_vif *vif;
2463 
2464 	brcmf_dbg(INFO, "P2P: device interface removed\n");
2465 	vif = ifp->vif;
2466 	cfg = wdev_to_cfg(&vif->wdev);
2467 	cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
2468 	if (!locked) {
2469 		rtnl_lock();
2470 		wiphy_lock(cfg->wiphy);
2471 		cfg80211_unregister_wdev(&vif->wdev);
2472 		wiphy_unlock(cfg->wiphy);
2473 		rtnl_unlock();
2474 	} else {
2475 		cfg80211_unregister_wdev(&vif->wdev);
2476 	}
2477 	brcmf_free_vif(vif);
2478 }
2479 
brcmf_p2p_start_device(struct wiphy * wiphy,struct wireless_dev * wdev)2480 int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2481 {
2482 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2483 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2484 	struct brcmf_cfg80211_vif *vif;
2485 	int err;
2486 
2487 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2488 	mutex_lock(&cfg->usr_sync);
2489 	err = brcmf_p2p_enable_discovery(p2p);
2490 	if (!err)
2491 		set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2492 	mutex_unlock(&cfg->usr_sync);
2493 	return err;
2494 }
2495 
brcmf_p2p_stop_device(struct wiphy * wiphy,struct wireless_dev * wdev)2496 void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2497 {
2498 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2499 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2500 	struct brcmf_cfg80211_vif *vif;
2501 
2502 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2503 	/* This call can be result of the unregister_wdev call. In that case
2504 	 * we dont want to do anything anymore. Just return. The config vif
2505 	 * will have been cleared at this point.
2506 	 */
2507 	if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif == vif) {
2508 		mutex_lock(&cfg->usr_sync);
2509 		/* Set the discovery state to SCAN */
2510 		(void)brcmf_p2p_set_discover_state(vif->ifp,
2511 						   WL_P2P_DISC_ST_SCAN, 0, 0);
2512 		brcmf_abort_scanning(cfg);
2513 		clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2514 		mutex_unlock(&cfg->usr_sync);
2515 	}
2516 }
2517 
2518 /**
2519  * brcmf_p2p_attach() - attach for P2P.
2520  *
2521  * @cfg: driver private data for cfg80211 interface.
2522  * @p2pdev_forced: create p2p device interface at attach.
2523  */
brcmf_p2p_attach(struct brcmf_cfg80211_info * cfg,bool p2pdev_forced)2524 s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg, bool p2pdev_forced)
2525 {
2526 	struct brcmf_pub *drvr = cfg->pub;
2527 	struct brcmf_p2p_info *p2p;
2528 	struct brcmf_if *pri_ifp;
2529 	s32 err = 0;
2530 	void *err_ptr;
2531 
2532 	p2p = &cfg->p2p;
2533 	p2p->cfg = cfg;
2534 
2535 	pri_ifp = brcmf_get_ifp(cfg->pub, 0);
2536 	p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
2537 
2538 	init_completion(&p2p->send_af_done);
2539 
2540 	if (p2pdev_forced) {
2541 		err_ptr = brcmf_p2p_create_p2pdev(p2p, NULL, NULL);
2542 		if (IS_ERR(err_ptr)) {
2543 			bphy_err(drvr, "P2P device creation failed.\n");
2544 			err = PTR_ERR(err_ptr);
2545 		}
2546 	} else {
2547 		p2p->p2pdev_dynamically = true;
2548 	}
2549 	return err;
2550 }
2551 
2552 /**
2553  * brcmf_p2p_detach() - detach P2P.
2554  *
2555  * @p2p: P2P specific data.
2556  */
brcmf_p2p_detach(struct brcmf_p2p_info * p2p)2557 void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
2558 {
2559 	struct brcmf_cfg80211_vif *vif;
2560 
2561 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
2562 	if (vif != NULL) {
2563 		brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2564 		brcmf_p2p_deinit_discovery(p2p);
2565 		brcmf_remove_interface(vif->ifp, false);
2566 	}
2567 	/* just set it all to zero */
2568 	memset(p2p, 0, sizeof(*p2p));
2569 }
2570 
2571