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