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