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