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