blob: 3e9c4f2f5dd12673e8c96e6dcacbea50cf7bc45e [file] [log] [blame]
Arend van Spriel9f440b72013-02-08 15:53:36 +01001/*
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>
Arend van Spriel27f10e32013-04-05 10:57:50 +020018#include <linux/etherdevice.h>
Arend van Sprielf7a40872015-06-11 00:12:23 +020019#include <linux/rtnetlink.h>
Arend van Spriel9f440b72013-02-08 15:53:36 +010020#include <net/cfg80211.h>
21
22#include <brcmu_wifi.h>
23#include <brcmu_utils.h>
24#include <defs.h>
Hante Meuleman122d3d02014-10-28 14:56:18 +010025#include "core.h"
Hante Meulemana8e8ed32014-10-28 14:56:13 +010026#include "debug.h"
Arend van Spriel9f440b72013-02-08 15:53:36 +010027#include "fwil.h"
Arend van Sprield3c0b632013-02-08 15:53:37 +010028#include "fwil_types.h"
Arend van Spriel9f440b72013-02-08 15:53:36 +010029#include "p2p.h"
Hante Meulemanbfe81972014-10-28 14:56:16 +010030#include "cfg80211.h"
Arend van Spriel9f440b72013-02-08 15:53:36 +010031
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
Hante Meuleman6eda4e22013-02-08 15:54:02 +010048#define IS_P2P_SOCIAL_CHANNEL(channel) ((channel == SOCIAL_CHAN_1) || \
49 (channel == SOCIAL_CHAN_2) || \
50 (channel == SOCIAL_CHAN_3))
Hante Meuleman15a953d2013-05-27 21:09:54 +020051#define BRCMF_P2P_TEMP_CHAN SOCIAL_CHAN_3
Arend van Spriel9f440b72013-02-08 15:53:36 +010052#define SOCIAL_CHAN_CNT 3
53#define AF_PEER_SEARCH_CNT 2
54
Arend van Sprield3c0b632013-02-08 15:53:37 +010055#define BRCMF_SCB_TIMEOUT_VALUE 20
56
Hante Meulemane6da3402013-02-08 15:53:48 +010057#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
Hante Meuleman18e2f612013-02-08 15:53:49 +010064/* 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
Hante Meuleman6eda4e22013-02-08 15:54:02 +010073#define P2P_AF_TX_MAX_RETRY 1
Arend van Spriel63ce3d52016-01-05 11:05:48 +010074#define P2P_AF_MAX_WAIT_TIME msecs_to_jiffies(2000)
Hante Meuleman18e2f612013-02-08 15:53:49 +010075#define P2P_INVALID_CHANNEL -1
76#define P2P_CHANNEL_SYNC_RETRY 5
Arend van Spriel63ce3d52016-01-05 11:05:48 +010077#define P2P_AF_FRM_SCAN_MAX_WAIT msecs_to_jiffies(1500)
Hante Meuleman6eda4e22013-02-08 15:54:02 +010078#define P2P_DEFAULT_SLEEP_TIME_VSDB 200
Hante Meuleman18e2f612013-02-08 15:53:49 +010079
Hante Meulemane6da3402013-02-08 15:53:48 +010080/* 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
Arend van Spriel63ce3d52016-01-05 11:05:48 +0100105#define BRCMF_P2P_DISABLE_TIMEOUT msecs_to_jiffies(500)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100106/**
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 */
113struct 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 */
126enum 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 */
140struct 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
Arend van Spriel9f440b72013-02-08 15:53:36 +0100149/**
Hante Meulemane6da3402013-02-08 15:53:48 +0100150 * 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 */
160struct 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 */
180struct 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 */
197struct brcmf_p2psd_gas_pub_act_frame {
198 u8 category;
199 u8 action;
200 u8 dialog_token;
201 u8 query_data[1];
202};
203
Hante Meuleman18e2f612013-02-08 15:53:49 +0100204/**
205 * struct brcmf_config_af_params - Action Frame Parameters for tx.
206 *
Hante Meuleman18e2f612013-02-08 15:53:49 +0100207 * @mpc_onoff: To make sure to send successfully action frame, we have to
208 * turn off mpc 0: off, 1: on, (-1): do nothing
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100209 * @search_channel: 1: search peer's channel to send af
210 * extra_listen: keep the dwell time to get af response frame.
Hante Meuleman18e2f612013-02-08 15:53:49 +0100211 */
212struct brcmf_config_af_params {
Hante Meuleman18e2f612013-02-08 15:53:49 +0100213 s32 mpc_onoff;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100214 bool search_channel;
215 bool extra_listen;
Hante Meuleman18e2f612013-02-08 15:53:49 +0100216};
Hante Meulemane6da3402013-02-08 15:53:48 +0100217
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 */
226static 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 */
254static 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 */
281static 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 */
Hante Meuleman0a4cf482013-02-08 15:53:54 +0100313
314#ifdef DEBUG
315
Hante Meulemane6da3402013-02-08 15:53:48 +0100316static 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
Hante Meuleman0a4cf482013-02-08 15:53:54 +0100420#else
421
422static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
423{
424}
425
426#endif
427
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100428
429/**
Arend van Spriel9f440b72013-02-08 15:53:36 +0100430 * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
431 *
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100432 * @ifp: ifp to use for iovars (primary).
433 * @p2p_mac: mac address to configure for p2p_da_override
Arend van Spriel9f440b72013-02-08 15:53:36 +0100434 */
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100435static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100436{
Arend van Spriel9f440b72013-02-08 15:53:36 +0100437 s32 ret = 0;
438
Arend van Spriel27f10e32013-04-05 10:57:50 +0200439 brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100440 brcmf_fil_iovar_int_set(ifp, "apsta", 1);
Arend van Spriel27f10e32013-04-05 10:57:50 +0200441 brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100442
443 /* In case of COB type, firmware has default mac address
444 * After Initializing firmware, we have to set current mac address to
Arend van Sprielf48556e2014-09-30 10:23:16 +0200445 * firmware for P2P device address. This must be done with discovery
446 * disabled.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100447 */
Arend van Sprielf48556e2014-09-30 10:23:16 +0200448 brcmf_fil_iovar_int_set(ifp, "p2p_disc", 0);
449
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100450 ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
451 ETH_ALEN);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100452 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.
Arend van Spriel27f10e32013-04-05 10:57:50 +0200462 * @dev_addr: optional device address.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100463 *
Arend van Spriel27f10e32013-04-05 10:57:50 +0200464 * P2P needs mac addresses for P2P device and interface. If no device
Arend Van Spriel455f3e72018-02-28 21:15:20 +0100465 * address it specified, these are derived from a random ethernet
466 * address.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100467 */
Arend van Spriel27f10e32013-04-05 10:57:50 +0200468static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100469{
Arend Van Spriel455f3e72018-02-28 21:15:20 +0100470 bool random_addr = false;
Arend van Spriel27f10e32013-04-05 10:57:50 +0200471
Arend Van Spriel455f3e72018-02-28 21:15:20 +0100472 if (!dev_addr || is_zero_ether_addr(dev_addr))
473 random_addr = true;
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100474
Arend Van Spriel455f3e72018-02-28 21:15:20 +0100475 /* Generate the P2P Device Address obtaining a random ethernet
476 * address with the locally administered bit set.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100477 */
Arend Van Spriel455f3e72018-02-28 21:15:20 +0100478 if (random_addr)
479 eth_random_addr(p2p->dev_addr);
480 else
481 memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100482
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);
Arend van Spriel27f10e32013-04-05 10:57:50 +0200488 p2p->int_addr[0] |= 0x02;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100489 p2p->int_addr[4] ^= 0x80;
490}
491
492/**
493 * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
494 *
495 * @request: the scan request as received from cfg80211.
496 *
497 * returns true if one of the ssids in the request matches the
498 * P2P wildcard ssid; otherwise returns false.
499 */
500static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
501{
502 struct cfg80211_ssid *ssids = request->ssids;
503 int i;
504
505 for (i = 0; i < request->n_ssids; i++) {
506 if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
507 continue;
508
509 brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
510 if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
511 BRCMF_P2P_WILDCARD_SSID_LEN))
512 return true;
513 }
514 return false;
515}
516
517/**
518 * brcmf_p2p_set_discover_state - set discover state in firmware.
519 *
520 * @ifp: low-level interface object.
521 * @state: discover state to set.
522 * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
523 * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
524 */
525static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
526 u16 chanspec, u16 listen_ms)
527{
528 struct brcmf_p2p_disc_st_le discover_state;
529 s32 ret = 0;
530 brcmf_dbg(TRACE, "enter\n");
531
532 discover_state.state = state;
533 discover_state.chspec = cpu_to_le16(chanspec);
534 discover_state.dwell = cpu_to_le16(listen_ms);
535 ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
536 sizeof(discover_state));
537 return ret;
538}
539
540/**
Arend van Spriel9f440b72013-02-08 15:53:36 +0100541 * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
542 *
543 * @p2p: P2P specific data.
544 *
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100545 * Resets the discovery state and disables it in firmware.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100546 */
547static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
548{
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100549 struct brcmf_cfg80211_vif *vif;
550
Arend van Spriel9f440b72013-02-08 15:53:36 +0100551 brcmf_dbg(TRACE, "enter\n");
552
Arend van Spriel9f440b72013-02-08 15:53:36 +0100553 /* Set the discovery state to SCAN */
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100554 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
555 (void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100556
557 /* Disable P2P discovery in the firmware */
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100558 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
559 (void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100560
561 return 0;
562}
563
564/**
565 * brcmf_p2p_enable_discovery() - initialize and configure discovery.
566 *
567 * @p2p: P2P specific data.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100568 *
569 * Initializes the discovery device and configure the virtual interface.
570 */
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100571static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100572{
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;
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100578 if (!vif) {
579 brcmf_err("P2P config device not available\n");
580 ret = -EPERM;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100581 goto exit;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100582 }
583
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100584 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);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100592 if (ret < 0) {
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100593 brcmf_err("set p2p_disc error\n");
Arend van Spriel9f440b72013-02-08 15:53:36 +0100594 goto exit;
595 }
Arend van Spriel9f440b72013-02-08 15:53:36 +0100596 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100597 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 }
Arend van Spriel9f440b72013-02-08 15:53:36 +0100602
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);
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100610 if (ret < 0) {
Arend van Spriel9f440b72013-02-08 15:53:36 +0100611 brcmf_err("wsec error %d\n", ret);
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100612 goto exit;
613 }
Arend van Spriel9f440b72013-02-08 15:53:36 +0100614
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100615 set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100616exit:
617 return ret;
618}
619
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100620/**
Arend van Spriel9f440b72013-02-08 15:53:36 +0100621 * 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.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100627 * @bss_type: type of P2P bss.
628 */
629static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
Hante Meulemanc4958102015-11-25 11:32:41 +0100630 u16 chanspecs[], s32 search_state,
Arend van Spriel9f440b72013-02-08 15:53:36 +0100631 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;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100643
Arend van Spriel9f440b72013-02-08 15:53:36 +0100644 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 }
Hante Meulemane9a6ca82015-11-25 11:32:37 +0100655 p2p_params = (struct brcmf_p2p_scan_le *)memblk;
656 sparams = &p2p_params->eparams.params_le;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100657
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
Hante Meulemane9a6ca82015-11-25 11:32:37 +0100662 * because dongle use P2P WILDCARD internally by default, use
663 * null ssid, which it is already due to kzalloc.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100664 */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100665 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 */
Hante Meulemane9a6ca82015-11-25 11:32:37 +0100673 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);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100677 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 */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100689 p2p_params->type = 'E';
690
691 /* determine the scan engine parameters */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100692 sparams->bss_type = DOT11_BSSTYPE_ANY;
Arend Van Sprielbbf35412017-11-08 14:36:36 +0100693 sparams->scan_type = BRCMF_SCANTYPE_ACTIVE;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100694
Joe Perches93803b32015-03-02 19:54:49 -0800695 eth_broadcast_addr(sparams->bssid);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100696 sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
697
698 /*
699 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
700 * supported by the supplicant.
701 */
702 if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
703 active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
704 else if (num_chans == AF_PEER_SEARCH_CNT)
705 active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
Rasmus Villemoese843bb12014-06-22 20:50:40 +0200706 else if (brcmf_get_vif_state_any(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
Arend van Spriel9f440b72013-02-08 15:53:36 +0100707 active = -1;
708 else
709 active = P2PAPI_SCAN_DWELL_TIME_MS;
710
711 /* Override scan params to find a peer for a connection */
712 if (num_chans == 1) {
713 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
Hante Meuleman18e2f612013-02-08 15:53:49 +0100714 /* WAR to sync with presence period of VSDB GO.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100715 * send probe request more frequently
716 */
717 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
718 } else {
719 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
720 }
721
722 if (nprobes <= 0)
723 nprobes = 1;
724
725 brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
726 sparams->active_time = cpu_to_le32(active);
727 sparams->nprobes = cpu_to_le32(nprobes);
728 sparams->passive_time = cpu_to_le32(-1);
729 sparams->channel_num = cpu_to_le32(num_chans &
730 BRCMF_SCAN_PARAMS_COUNT_MASK);
731 for (i = 0; i < num_chans; i++)
732 sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
733
734 /* set the escan specific parameters */
735 p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
Hante Meulemanc4958102015-11-25 11:32:41 +0100736 p2p_params->eparams.action = cpu_to_le16(WL_ESCAN_ACTION_START);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100737 p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
738 /* perform p2p scan on primary device */
739 ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
740 if (!ret)
741 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
742exit:
743 kfree(memblk);
744 return ret;
745}
746
747/**
748 * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
749 *
750 * @cfg: driver private data for cfg80211 interface.
751 * @ndev: net device for which scan is requested.
752 * @request: scan request from cfg80211.
753 * @action: scan action.
754 *
755 * Determines the P2P discovery state based to scan request parameters and
756 * validates the channels in the request.
757 */
758static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
Arend van Spriela0f472a2013-04-05 10:57:49 +0200759 struct brcmf_if *ifp,
Hante Meulemanc4958102015-11-25 11:32:41 +0100760 struct cfg80211_scan_request *request)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100761{
762 struct brcmf_p2p_info *p2p = &cfg->p2p;
763 s32 err = 0;
764 s32 search_state = WL_P2P_DISC_ST_SCAN;
765 struct brcmf_cfg80211_vif *vif;
766 struct net_device *dev = NULL;
767 int i, num_nodfs = 0;
768 u16 *chanspecs;
769
770 brcmf_dbg(TRACE, "enter\n");
771
772 if (!request) {
773 err = -EINVAL;
774 goto exit;
775 }
776
777 if (request->n_channels) {
778 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
779 GFP_KERNEL);
780 if (!chanspecs) {
781 err = -ENOMEM;
782 goto exit;
783 }
784 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
785 if (vif)
786 dev = vif->wdev.netdev;
787 if (request->n_channels == 3 &&
788 request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
789 request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
790 request->channels[2]->hw_value == SOCIAL_CHAN_3) {
791 /* SOCIAL CHANNELS 1, 6, 11 */
792 search_state = WL_P2P_DISC_ST_SEARCH;
793 brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
Arend van Spriel967fe2c2014-03-15 17:18:21 +0100794 } else if (dev != NULL &&
795 vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
Arend van Spriel9f440b72013-02-08 15:53:36 +0100796 /* If you are already a GO, then do SEARCH only */
797 brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
798 search_state = WL_P2P_DISC_ST_SEARCH;
799 } else {
800 brcmf_dbg(INFO, "P2P SCAN STATE START\n");
801 }
802
803 /*
804 * no P2P scanning on passive or DFS channels.
805 */
806 for (i = 0; i < request->n_channels; i++) {
807 struct ieee80211_channel *chan = request->channels[i];
808
809 if (chan->flags & (IEEE80211_CHAN_RADAR |
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200810 IEEE80211_CHAN_NO_IR))
Arend van Spriel9f440b72013-02-08 15:53:36 +0100811 continue;
812
Franky Lin83cf17a2013-04-11 13:28:50 +0200813 chanspecs[i] = channel_to_chanspec(&p2p->cfg->d11inf,
814 chan);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100815 brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
816 num_nodfs, chan->hw_value, chanspecs[i]);
817 num_nodfs++;
818 }
819 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
Hante Meulemanc4958102015-11-25 11:32:41 +0100820 P2PAPI_BSSCFG_DEVICE);
Geyslan G. Bem3b1bace2013-11-15 09:21:47 -0300821 kfree(chanspecs);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100822 }
823exit:
824 if (err)
825 brcmf_err("error (%d)\n", err);
826 return err;
827}
828
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100829
830/**
831 * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
832 *
833 * @ie: string of information elements.
834 * @ie_len: length of string.
835 *
836 * Scan ie for p2p ie and look for attribute 6 channel. If available determine
837 * channel and return it.
838 */
839static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
840{
841 u8 channel_ie[5];
842 s32 listen_channel;
843 s32 err;
844
845 err = cfg80211_get_p2p_attr(ie, ie_len,
846 IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
847 channel_ie, sizeof(channel_ie));
848 if (err < 0)
849 return err;
850
851 /* listen channel subel length format: */
852 /* 3(country) + 1(op. class) + 1(chan num) */
853 listen_channel = (s32)channel_ie[3 + 1];
854
855 if (listen_channel == SOCIAL_CHAN_1 ||
856 listen_channel == SOCIAL_CHAN_2 ||
857 listen_channel == SOCIAL_CHAN_3) {
858 brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
859 return listen_channel;
860 }
861
862 return -EPERM;
863}
864
865
Arend van Spriel9f440b72013-02-08 15:53:36 +0100866/**
867 * brcmf_p2p_scan_prep() - prepare scan based on request.
868 *
869 * @wiphy: wiphy device.
870 * @request: scan request from cfg80211.
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100871 * @vif: vif on which scan request is to be executed.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100872 *
873 * Prepare the scan appropriately for type of scan requested. Overrides the
874 * escan .run() callback for peer-to-peer scanning.
875 */
876int brcmf_p2p_scan_prep(struct wiphy *wiphy,
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100877 struct cfg80211_scan_request *request,
878 struct brcmf_cfg80211_vif *vif)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100879{
880 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
881 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend Van Sprielbd99a302017-11-08 14:36:37 +0100882 int err;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100883
884 if (brcmf_p2p_scan_is_p2p_request(request)) {
885 /* find my listen channel */
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100886 err = brcmf_p2p_find_listen_channel(request->ie,
887 request->ie_len);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100888 if (err < 0)
889 return err;
890
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100891 p2p->afx_hdl.my_listen_chan = err;
892
Arend van Spriel9f440b72013-02-08 15:53:36 +0100893 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
894 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
895
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100896 err = brcmf_p2p_enable_discovery(p2p);
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100897 if (err)
898 return err;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100899
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100900 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
901
902 /* override .run_escan() callback. */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100903 cfg->escan_info.run = brcmf_p2p_run_escan;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100904 }
Arend Van Sprielbd99a302017-11-08 14:36:37 +0100905 return 0;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100906}
907
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100908
909/**
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100910 * brcmf_p2p_discover_listen() - set firmware to discover listen state.
911 *
912 * @p2p: p2p device.
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100913 * @channel: channel nr for discover listen.
914 * @duration: time in ms to stay on channel.
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100915 *
916 */
917static s32
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100918brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100919{
920 struct brcmf_cfg80211_vif *vif;
Franky Lin83cf17a2013-04-11 13:28:50 +0200921 struct brcmu_chan ch;
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100922 s32 err = 0;
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100923
924 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
925 if (!vif) {
926 brcmf_err("Discovery is not set, so we have nothing to do\n");
927 err = -EPERM;
928 goto exit;
929 }
930
931 if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
932 brcmf_err("Previous LISTEN is not completed yet\n");
933 /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
934 goto exit;
935 }
936
Franky Lin83cf17a2013-04-11 13:28:50 +0200937 ch.chnum = channel;
938 ch.bw = BRCMU_CHAN_BW_20;
939 p2p->cfg->d11inf.encchspec(&ch);
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100940 err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
Franky Lin83cf17a2013-04-11 13:28:50 +0200941 ch.chspec, (u16)duration);
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100942 if (!err) {
943 set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
944 p2p->remain_on_channel_cookie++;
945 }
946exit:
947 return err;
948}
949
950
951/**
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100952 * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
953 *
954 * @wiphy: wiphy device.
955 * @channel: channel to stay on.
956 * @duration: time in ms to remain on channel.
957 *
958 */
959int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
960 struct ieee80211_channel *channel,
961 unsigned int duration, u64 *cookie)
962{
963 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
964 struct brcmf_p2p_info *p2p = &cfg->p2p;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100965 s32 err;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100966 u16 channel_nr;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100967
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100968 channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
969 brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100970 duration);
971
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100972 err = brcmf_p2p_enable_discovery(p2p);
973 if (err)
974 goto exit;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100975 err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100976 if (err)
977 goto exit;
978
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100979 memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
980 *cookie = p2p->remain_on_channel_cookie;
Arend van Spriel1ce30862013-02-08 15:53:51 +0100981 cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100982
983exit:
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100984 return err;
985}
986
987
988/**
989 * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
990 *
991 * @ifp: interfac control.
992 * @e: event message. Not used, to make it usable for fweh event dispatcher.
993 * @data: payload of message. Not used.
994 *
995 */
996int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
997 const struct brcmf_event_msg *e,
998 void *data)
999{
1000 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1001 struct brcmf_p2p_info *p2p = &cfg->p2p;
1002
1003 brcmf_dbg(TRACE, "Enter\n");
Hante Meulemanf2058dd2013-02-08 15:53:50 +01001004 if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001005 &p2p->status)) {
1006 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1007 &p2p->status)) {
1008 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1009 &p2p->status);
1010 brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1011 complete(&p2p->wait_next_af);
1012 }
1013
Hante Meulemanf2058dd2013-02-08 15:53:50 +01001014 cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1015 p2p->remain_on_channel_cookie,
Hante Meuleman0de8aac2013-02-08 15:53:38 +01001016 &p2p->remain_on_channel,
1017 GFP_KERNEL);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001018 }
Hante Meuleman0de8aac2013-02-08 15:53:38 +01001019 return 0;
1020}
1021
1022
1023/**
1024 * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1025 *
1026 * @ifp: interfac control.
1027 *
1028 */
1029void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1030{
1031 if (!ifp)
1032 return;
1033 brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1034 brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1035}
1036
1037
Arend van Spriel9f440b72013-02-08 15:53:36 +01001038/**
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001039 * brcmf_p2p_act_frm_search() - search function for action frame.
1040 *
1041 * @p2p: p2p device.
1042 * channel: channel on which action frame is to be trasmitted.
1043 *
1044 * search function to reach at common channel to send action frame. When
1045 * channel is 0 then all social channels will be used to send af
1046 */
1047static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1048{
1049 s32 err;
1050 u32 channel_cnt;
1051 u16 *default_chan_list;
1052 u32 i;
Franky Lin83cf17a2013-04-11 13:28:50 +02001053 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001054
1055 brcmf_dbg(TRACE, "Enter\n");
1056
1057 if (channel)
1058 channel_cnt = AF_PEER_SEARCH_CNT;
1059 else
1060 channel_cnt = SOCIAL_CHAN_CNT;
Kees Cook6396bb22018-06-12 14:03:40 -07001061 default_chan_list = kcalloc(channel_cnt, sizeof(*default_chan_list),
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001062 GFP_KERNEL);
1063 if (default_chan_list == NULL) {
1064 brcmf_err("channel list allocation failed\n");
1065 err = -ENOMEM;
1066 goto exit;
1067 }
Franky Lin83cf17a2013-04-11 13:28:50 +02001068 ch.bw = BRCMU_CHAN_BW_20;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001069 if (channel) {
Franky Lin83cf17a2013-04-11 13:28:50 +02001070 ch.chnum = channel;
1071 p2p->cfg->d11inf.encchspec(&ch);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001072 /* insert same channel to the chan_list */
1073 for (i = 0; i < channel_cnt; i++)
Franky Lin83cf17a2013-04-11 13:28:50 +02001074 default_chan_list[i] = ch.chspec;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001075 } else {
Franky Lin83cf17a2013-04-11 13:28:50 +02001076 ch.chnum = SOCIAL_CHAN_1;
1077 p2p->cfg->d11inf.encchspec(&ch);
1078 default_chan_list[0] = ch.chspec;
1079 ch.chnum = SOCIAL_CHAN_2;
1080 p2p->cfg->d11inf.encchspec(&ch);
1081 default_chan_list[1] = ch.chspec;
1082 ch.chnum = SOCIAL_CHAN_3;
1083 p2p->cfg->d11inf.encchspec(&ch);
1084 default_chan_list[2] = ch.chspec;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001085 }
1086 err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
Hante Meulemanc4958102015-11-25 11:32:41 +01001087 WL_P2P_DISC_ST_SEARCH, P2PAPI_BSSCFG_DEVICE);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001088 kfree(default_chan_list);
1089exit:
1090 return err;
1091}
1092
1093
1094/**
1095 * brcmf_p2p_afx_handler() - afx worker thread.
1096 *
1097 * @work:
1098 *
1099 */
1100static void brcmf_p2p_afx_handler(struct work_struct *work)
1101{
1102 struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
1103 struct brcmf_p2p_info *p2p = container_of(afx_hdl,
1104 struct brcmf_p2p_info,
1105 afx_hdl);
1106 s32 err;
1107
1108 if (!afx_hdl->is_active)
1109 return;
1110
1111 if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1112 /* 100ms ~ 300ms */
1113 err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
Akinobu Mitae00adf32013-05-07 16:18:15 -07001114 100 * (1 + prandom_u32() % 3));
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001115 else
1116 err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1117
1118 if (err) {
1119 brcmf_err("ERROR occurred! value is (%d)\n", err);
1120 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1121 &p2p->status))
1122 complete(&afx_hdl->act_frm_scan);
1123 }
1124}
1125
1126
1127/**
1128 * brcmf_p2p_af_searching_channel() - search channel.
1129 *
1130 * @p2p: p2p device info struct.
1131 *
1132 */
1133static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1134{
1135 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1136 struct brcmf_cfg80211_vif *pri_vif;
1137 unsigned long duration;
1138 s32 retry;
1139
1140 brcmf_dbg(TRACE, "Enter\n");
1141
1142 pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1143
Wolfram Sang16735d02013-11-14 14:32:02 -08001144 reinit_completion(&afx_hdl->act_frm_scan);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001145 set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1146 afx_hdl->is_active = true;
1147 afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
1148
1149 /* Loop to wait until we find a peer's channel or the
1150 * pending action frame tx is cancelled.
1151 */
1152 retry = 0;
1153 duration = msecs_to_jiffies(P2P_AF_FRM_SCAN_MAX_WAIT);
1154 while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
1155 (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
1156 afx_hdl->is_listen = false;
1157 brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
1158 retry);
1159 /* search peer on peer's listen channel */
1160 schedule_work(&afx_hdl->afx_work);
1161 wait_for_completion_timeout(&afx_hdl->act_frm_scan, duration);
1162 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1163 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1164 &p2p->status)))
1165 break;
1166
1167 if (afx_hdl->my_listen_chan) {
1168 brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
1169 afx_hdl->my_listen_chan);
1170 /* listen on my listen channel */
1171 afx_hdl->is_listen = true;
1172 schedule_work(&afx_hdl->afx_work);
1173 wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1174 duration);
1175 }
1176 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1177 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1178 &p2p->status)))
1179 break;
1180 retry++;
1181
1182 /* if sta is connected or connecting, sleep for a while before
1183 * retry af tx or finding a peer
1184 */
1185 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
1186 test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
1187 msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
1188 }
1189
1190 brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1191 afx_hdl->peer_chan);
1192 afx_hdl->is_active = false;
1193
1194 clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1195
1196 return afx_hdl->peer_chan;
1197}
1198
1199
1200/**
1201 * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1202 *
1203 * @cfg: common configuration struct.
1204 * @bi: bss info struct, result from scan.
1205 *
1206 */
1207bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1208 struct brcmf_bss_info_le *bi)
1209
1210{
1211 struct brcmf_p2p_info *p2p = &cfg->p2p;
1212 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Franky Lin83cf17a2013-04-11 13:28:50 +02001213 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001214 u8 *ie;
1215 s32 err;
1216 u8 p2p_dev_addr[ETH_ALEN];
1217
1218 if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
1219 return false;
1220
1221 if (bi == NULL) {
1222 brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
1223 if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
1224 complete(&afx_hdl->act_frm_scan);
1225 return true;
1226 }
1227
1228 ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
1229 memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
1230 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1231 IEEE80211_P2P_ATTR_DEVICE_INFO,
1232 p2p_dev_addr, sizeof(p2p_dev_addr));
1233 if (err < 0)
1234 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1235 IEEE80211_P2P_ATTR_DEVICE_ID,
1236 p2p_dev_addr, sizeof(p2p_dev_addr));
1237 if ((err >= 0) &&
dingtianhong692e5162013-12-26 19:41:34 +08001238 (ether_addr_equal(p2p_dev_addr, afx_hdl->tx_dst_addr))) {
Franky Lin83cf17a2013-04-11 13:28:50 +02001239 if (!bi->ctl_ch) {
1240 ch.chspec = le16_to_cpu(bi->chanspec);
1241 cfg->d11inf.decchspec(&ch);
Rafał Miłecki4712d882016-05-20 13:38:57 +02001242 bi->ctl_ch = ch.control_ch_num;
Franky Lin83cf17a2013-04-11 13:28:50 +02001243 }
1244 afx_hdl->peer_chan = bi->ctl_ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001245 brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
1246 afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
1247 complete(&afx_hdl->act_frm_scan);
1248 }
1249 return true;
1250}
1251
1252/**
1253 * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1254 *
1255 * @cfg: common configuration struct.
1256 *
1257 */
1258static void
1259brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1260{
1261 struct brcmf_p2p_info *p2p = &cfg->p2p;
Hante Meuleman2aec2c92016-04-11 11:35:24 +02001262 struct brcmf_if *ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001263
1264 if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
1265 (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
1266 test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
1267 brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
1268 /* if channel is not zero, "actfame" uses off channel scan.
1269 * So abort scan for off channel completion.
1270 */
1271 if (p2p->af_sent_channel)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001272 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001273 } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1274 &p2p->status)) {
1275 brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1276 /* So abort scan to cancel listen */
Arend van Spriela0f472a2013-04-05 10:57:49 +02001277 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001278 }
1279}
1280
1281
1282/**
1283 * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1284 *
1285 * @p2p: p2p device info struct.
1286 *
1287 * return true if recevied action frame is to be dropped.
1288 */
1289static bool
1290brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1291{
1292 struct brcmf_cfg80211_info *cfg = p2p->cfg;
1293 struct brcmf_if *ifp;
1294
1295 brcmf_dbg(TRACE, "Enter\n");
1296
1297 if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1298 !p2p->gon_req_action)
1299 return false;
1300
1301 brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
1302 /* if sa(peer) addr is less than da(my) addr, then this device
1303 * process peer's gon request and block to send gon req.
1304 * if not (sa addr > da addr),
1305 * this device will process gon request and drop gon req of peer.
1306 */
1307 ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
1308 if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
1309 brcmf_dbg(INFO, "Block transmit gon req !!!\n");
1310 p2p->block_gon_req_tx = true;
1311 /* if we are finding a common channel for sending af,
1312 * do not scan more to block to send current gon req
1313 */
1314 if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1315 &p2p->status))
1316 complete(&p2p->afx_hdl.act_frm_scan);
1317 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1318 &p2p->status))
1319 brcmf_p2p_stop_wait_next_action_frame(cfg);
1320 return false;
1321 }
1322
1323 /* drop gon request of peer to process gon request by this device. */
1324 brcmf_dbg(INFO, "Drop received gon req !!!\n");
1325
1326 return true;
1327}
1328
1329
1330/**
Hante Meulemane6da3402013-02-08 15:53:48 +01001331 * brcmf_p2p_notify_action_frame_rx() - received action frame.
1332 *
1333 * @ifp: interfac control.
1334 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1335 * @data: payload of message, containing action frame data.
1336 *
1337 */
1338int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1339 const struct brcmf_event_msg *e,
1340 void *data)
1341{
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001342 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1343 struct brcmf_p2p_info *p2p = &cfg->p2p;
1344 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meulemane6da3402013-02-08 15:53:48 +01001345 struct wireless_dev *wdev;
1346 u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1347 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001348 u8 *frame = (u8 *)(rxframe + 1);
1349 struct brcmf_p2p_pub_act_frame *act_frm;
1350 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
Franky Lin83cf17a2013-04-11 13:28:50 +02001351 struct brcmu_chan ch;
Hante Meulemane6da3402013-02-08 15:53:48 +01001352 struct ieee80211_mgmt *mgmt_frame;
Hante Meulemane6da3402013-02-08 15:53:48 +01001353 s32 freq;
1354 u16 mgmt_type;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001355 u8 action;
Hante Meulemane6da3402013-02-08 15:53:48 +01001356
Hante Meuleman0aedbca2016-02-17 11:26:54 +01001357 if (e->datalen < sizeof(*rxframe)) {
1358 brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1359 return 0;
1360 }
1361
Franky Lin83cf17a2013-04-11 13:28:50 +02001362 ch.chspec = be16_to_cpu(rxframe->chanspec);
1363 cfg->d11inf.decchspec(&ch);
Hante Meulemane6da3402013-02-08 15:53:48 +01001364 /* Check if wpa_supplicant has registered for this frame */
1365 brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1366 mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1367 if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1368 return 0;
1369
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001370 brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
1371
1372 action = P2P_PAF_SUBTYPE_INVALID;
1373 if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
1374 act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
1375 action = act_frm->subtype;
1376 if ((action == P2P_PAF_GON_REQ) &&
1377 (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
1378 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1379 &p2p->status) &&
dingtianhong692e5162013-12-26 19:41:34 +08001380 (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
Rafał Miłecki4712d882016-05-20 13:38:57 +02001381 afx_hdl->peer_chan = ch.control_ch_num;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001382 brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1383 afx_hdl->peer_chan);
1384 complete(&afx_hdl->act_frm_scan);
1385 }
1386 return 0;
1387 }
1388 /* After complete GO Negotiation, roll back to mpc mode */
1389 if ((action == P2P_PAF_GON_CONF) ||
1390 (action == P2P_PAF_PROVDIS_RSP))
Arend van Sprielf96aa072013-04-05 10:57:48 +02001391 brcmf_set_mpc(ifp, 1);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001392 if (action == P2P_PAF_GON_CONF) {
1393 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1394 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1395 }
1396 } else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
1397 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
1398 action = sd_act_frm->action;
1399 }
1400
1401 if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1402 (p2p->next_af_subtype == action)) {
1403 brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
1404 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1405 &p2p->status);
1406 /* Stop waiting for next AF. */
1407 brcmf_p2p_stop_wait_next_action_frame(cfg);
1408 }
Hante Meulemane6da3402013-02-08 15:53:48 +01001409
1410 mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1411 mgmt_frame_len, GFP_KERNEL);
1412 if (!mgmt_frame) {
1413 brcmf_err("No memory available for action frame\n");
1414 return -ENOMEM;
1415 }
1416 memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001417 brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1418 ETH_ALEN);
Hante Meulemane6da3402013-02-08 15:53:48 +01001419 memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1420 mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001421 memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
Hante Meulemane6da3402013-02-08 15:53:48 +01001422 mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1423
Rafał Miłecki4712d882016-05-20 13:38:57 +02001424 freq = ieee80211_channel_to_frequency(ch.control_ch_num,
Franky Lin83cf17a2013-04-11 13:28:50 +02001425 ch.band == BRCMU_CHAN_BAND_2G ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001426 NL80211_BAND_2GHZ :
1427 NL80211_BAND_5GHZ);
Arend van Spriel5b57af62013-04-05 10:57:53 +02001428
1429 wdev = &ifp->vif->wdev;
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07001430 cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len, 0);
Hante Meulemane6da3402013-02-08 15:53:48 +01001431
1432 kfree(mgmt_frame);
1433 return 0;
1434}
1435
1436
1437/**
Hante Meuleman18e2f612013-02-08 15:53:49 +01001438 * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1439 *
1440 * @ifp: interfac control.
1441 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1442 * @data: not used.
1443 *
1444 */
1445int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1446 const struct brcmf_event_msg *e,
1447 void *data)
1448{
1449 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1450 struct brcmf_p2p_info *p2p = &cfg->p2p;
1451
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001452 brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1453 e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1454 "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1455 e->status);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001456
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001457 if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1458 return 0;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001459
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001460 if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
1461 if (e->status == BRCMF_E_STATUS_SUCCESS)
1462 set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1463 &p2p->status);
1464 else {
1465 set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1466 /* If there is no ack, we don't need to wait for
1467 * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1468 */
1469 brcmf_p2p_stop_wait_next_action_frame(cfg);
1470 }
1471
1472 } else {
1473 complete(&p2p->send_af_done);
1474 }
Hante Meuleman18e2f612013-02-08 15:53:49 +01001475 return 0;
1476}
1477
1478
1479/**
1480 * brcmf_p2p_tx_action_frame() - send action frame over fil.
1481 *
1482 * @p2p: p2p info struct for vif.
1483 * @af_params: action frame data/info.
1484 *
1485 * Send an action frame immediately without doing channel synchronization.
1486 *
1487 * This function waits for a completion event before returning.
1488 * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1489 * frame is transmitted.
1490 */
1491static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1492 struct brcmf_fil_af_params_le *af_params)
1493{
1494 struct brcmf_cfg80211_vif *vif;
1495 s32 err = 0;
1496 s32 timeout = 0;
1497
1498 brcmf_dbg(TRACE, "Enter\n");
1499
Wolfram Sang16735d02013-11-14 14:32:02 -08001500 reinit_completion(&p2p->send_af_done);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001501 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1502 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1503
1504 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1505 err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1506 sizeof(*af_params));
1507 if (err) {
1508 brcmf_err(" sending action frame has failed\n");
1509 goto exit;
1510 }
1511
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001512 p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1513 p2p->af_tx_sent_jiffies = jiffies;
1514
Hante Meuleman18e2f612013-02-08 15:53:49 +01001515 timeout = wait_for_completion_timeout(&p2p->send_af_done,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001516 P2P_AF_MAX_WAIT_TIME);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001517
1518 if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1519 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1520 } else {
1521 err = -EIO;
1522 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1523 }
1524 /* clear status bit for action tx */
1525 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1526 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1527
1528exit:
1529 return err;
1530}
1531
1532
1533/**
1534 * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1535 *
1536 * @cfg: driver private data for cfg80211 interface.
1537 * @af_params: action frame data/info.
1538 * @config_af_params: configuration data for action frame.
1539 *
1540 * routine which transmits ation frame public type.
1541 */
1542static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1543 struct brcmf_fil_af_params_le *af_params,
1544 struct brcmf_config_af_params *config_af_params)
1545{
1546 struct brcmf_p2p_info *p2p = &cfg->p2p;
1547 struct brcmf_fil_action_frame_le *action_frame;
1548 struct brcmf_p2p_pub_act_frame *act_frm;
1549 s32 err = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001550 u16 ie_len;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001551
1552 action_frame = &af_params->action_frame;
1553 act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1554
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001555 config_af_params->extra_listen = true;
1556
Hante Meuleman18e2f612013-02-08 15:53:49 +01001557 switch (act_frm->subtype) {
1558 case P2P_PAF_GON_REQ:
1559 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1560 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1561 config_af_params->mpc_onoff = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001562 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001563 p2p->next_af_subtype = act_frm->subtype + 1;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001564 p2p->gon_req_action = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001565 /* increase dwell time to wait for RESP frame */
1566 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1567 break;
1568 case P2P_PAF_GON_RSP:
1569 p2p->next_af_subtype = act_frm->subtype + 1;
1570 /* increase dwell time to wait for CONF frame */
1571 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1572 break;
1573 case P2P_PAF_GON_CONF:
1574 /* If we reached till GO Neg confirmation reset the filter */
1575 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1576 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1577 /* turn on mpc again if go nego is done */
1578 config_af_params->mpc_onoff = 1;
1579 /* minimize dwell time */
1580 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001581 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001582 break;
1583 case P2P_PAF_INVITE_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001584 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001585 p2p->next_af_subtype = act_frm->subtype + 1;
1586 /* increase dwell time */
1587 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1588 break;
1589 case P2P_PAF_INVITE_RSP:
1590 /* minimize dwell time */
1591 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001592 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001593 break;
1594 case P2P_PAF_DEVDIS_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001595 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001596 p2p->next_af_subtype = act_frm->subtype + 1;
1597 /* maximize dwell time to wait for RESP frame */
1598 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1599 break;
1600 case P2P_PAF_DEVDIS_RSP:
1601 /* minimize dwell time */
1602 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001603 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001604 break;
1605 case P2P_PAF_PROVDIS_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001606 ie_len = le16_to_cpu(action_frame->len) -
1607 offsetof(struct brcmf_p2p_pub_act_frame, elts);
1608 if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1609 IEEE80211_P2P_ATTR_GROUP_ID,
1610 NULL, 0) < 0)
1611 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001612 config_af_params->mpc_onoff = 0;
1613 p2p->next_af_subtype = act_frm->subtype + 1;
1614 /* increase dwell time to wait for RESP frame */
1615 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1616 break;
1617 case P2P_PAF_PROVDIS_RSP:
1618 /* wpa_supplicant send go nego req right after prov disc */
1619 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1620 /* increase dwell time to MED level */
1621 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001622 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001623 break;
1624 default:
1625 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1626 act_frm->subtype);
1627 err = -EINVAL;
1628 }
1629 return err;
1630}
1631
1632/**
1633 * brcmf_p2p_send_action_frame() - send action frame .
1634 *
1635 * @cfg: driver private data for cfg80211 interface.
1636 * @ndev: net device to transmit on.
1637 * @af_params: configuration data for action frame.
1638 */
1639bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1640 struct net_device *ndev,
1641 struct brcmf_fil_af_params_le *af_params)
1642{
1643 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend van Spriela0f472a2013-04-05 10:57:49 +02001644 struct brcmf_if *ifp = netdev_priv(ndev);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001645 struct brcmf_fil_action_frame_le *action_frame;
1646 struct brcmf_config_af_params config_af_params;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001647 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001648 u16 action_frame_len;
1649 bool ack = false;
1650 u8 category;
1651 u8 action;
1652 s32 tx_retry;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001653 s32 extra_listen_time;
1654 uint delta_ms;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001655
1656 action_frame = &af_params->action_frame;
1657 action_frame_len = le16_to_cpu(action_frame->len);
1658
1659 brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1660
1661 /* Add the default dwell time. Dwell time to stay off-channel */
1662 /* to wait for a response action frame after transmitting an */
1663 /* GO Negotiation action frame */
1664 af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1665
1666 category = action_frame->data[DOT11_ACTION_CAT_OFF];
1667 action = action_frame->data[DOT11_ACTION_ACT_OFF];
1668
1669 /* initialize variables */
1670 p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001671 p2p->gon_req_action = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001672
1673 /* config parameters */
Hante Meuleman18e2f612013-02-08 15:53:49 +01001674 config_af_params.mpc_onoff = -1;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001675 config_af_params.search_channel = false;
1676 config_af_params.extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001677
1678 if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1679 /* p2p public action frame process */
1680 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1681 /* Just send unknown subtype frame with */
1682 /* default parameters. */
1683 brcmf_err("P2P Public action frame, unknown subtype.\n");
1684 }
1685 } else if (brcmf_p2p_is_gas_action(action_frame->data,
1686 action_frame_len)) {
1687 /* service discovery process */
1688 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1689 action == P2PSD_ACTION_ID_GAS_CREQ) {
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001690 /* configure service discovery query frame */
1691 config_af_params.search_channel = true;
1692
Hante Meuleman18e2f612013-02-08 15:53:49 +01001693 /* save next af suptype to cancel */
1694 /* remaining dwell time */
1695 p2p->next_af_subtype = action + 1;
1696
1697 af_params->dwell_time =
1698 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1699 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1700 action == P2PSD_ACTION_ID_GAS_CRESP) {
1701 /* configure service discovery response frame */
1702 af_params->dwell_time =
1703 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1704 } else {
1705 brcmf_err("Unknown action type: %d\n", action);
1706 goto exit;
1707 }
1708 } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1709 action_frame_len)) {
1710 /* do not configure anything. it will be */
1711 /* sent with a default configuration */
1712 } else {
1713 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1714 category, action);
1715 return false;
1716 }
1717
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001718 /* if connecting on primary iface, sleep for a while before sending
1719 * af tx for VSDB
1720 */
1721 if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1722 &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1723 msleep(50);
1724
Hante Meuleman18e2f612013-02-08 15:53:49 +01001725 /* if scan is ongoing, abort current scan. */
1726 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1727 brcmf_abort_scanning(cfg);
1728
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001729 memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1730
Hante Meuleman18e2f612013-02-08 15:53:49 +01001731 /* To make sure to send successfully action frame, turn off mpc */
1732 if (config_af_params.mpc_onoff == 0)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001733 brcmf_set_mpc(ifp, 0);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001734
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001735 /* set status and destination address before sending af */
1736 if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1737 /* set status to cancel the remained dwell time in rx process */
1738 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1739 }
1740
1741 p2p->af_sent_channel = 0;
1742 set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1743 /* validate channel and p2p ies */
1744 if (config_af_params.search_channel &&
1745 IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1746 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1747 afx_hdl = &p2p->afx_hdl;
1748 afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1749
1750 if (brcmf_p2p_af_searching_channel(p2p) ==
1751 P2P_INVALID_CHANNEL) {
1752 brcmf_err("Couldn't find peer's channel.\n");
1753 goto exit;
1754 }
1755
1756 /* Abort scan even for VSDB scenarios. Scan gets aborted in
1757 * firmware but after the check of piggyback algorithm. To take
1758 * care of current piggback algo, lets abort the scan here
1759 * itself.
1760 */
Arend van Spriela0f472a2013-04-05 10:57:49 +02001761 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001762
1763 /* update channel */
1764 af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1765 }
1766
Hante Meuleman18e2f612013-02-08 15:53:49 +01001767 tx_retry = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001768 while (!p2p->block_gon_req_tx &&
1769 (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
Hante Meuleman18e2f612013-02-08 15:53:49 +01001770 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1771 tx_retry++;
1772 }
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001773 if (ack == false) {
Hante Meuleman18e2f612013-02-08 15:53:49 +01001774 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001775 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1776 }
Hante Meuleman18e2f612013-02-08 15:53:49 +01001777
1778exit:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001779 clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1780
1781 /* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1782 * if we coundn't get the next action response frame and dongle does
1783 * not keep the dwell time, go to listen state again to get next action
1784 * response frame.
1785 */
1786 if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1787 test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1788 p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1789 delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1790 if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1791 extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1792 delta_ms;
1793 else
1794 extra_listen_time = 0;
1795 if (extra_listen_time > 50) {
1796 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1797 &p2p->status);
1798 brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1799 le32_to_cpu(af_params->dwell_time),
1800 extra_listen_time);
1801 extra_listen_time += 100;
1802 if (!brcmf_p2p_discover_listen(p2p,
1803 p2p->af_sent_channel,
1804 extra_listen_time)) {
1805 unsigned long duration;
1806
1807 extra_listen_time += 100;
1808 duration = msecs_to_jiffies(extra_listen_time);
1809 wait_for_completion_timeout(&p2p->wait_next_af,
1810 duration);
1811 }
1812 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1813 &p2p->status);
1814 }
1815 }
1816
1817 if (p2p->block_gon_req_tx) {
1818 /* if ack is true, supplicant will wait more time(100ms).
1819 * so we will return it as a success to get more time .
1820 */
1821 p2p->block_gon_req_tx = false;
1822 ack = true;
1823 }
1824
1825 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001826 /* if all done, turn mpc on again */
1827 if (config_af_params.mpc_onoff == 1)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001828 brcmf_set_mpc(ifp, 1);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001829
1830 return ack;
1831}
1832
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001833/**
1834 * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1835 *
1836 * @ifp: interface pointer for which event was received.
1837 * @e: even message.
1838 * @data: payload of event message (probe request).
1839 */
1840s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1841 const struct brcmf_event_msg *e,
1842 void *data)
1843{
1844 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1845 struct brcmf_p2p_info *p2p = &cfg->p2p;
1846 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001847 struct brcmf_cfg80211_vif *vif = ifp->vif;
1848 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
Franky Lin83cf17a2013-04-11 13:28:50 +02001849 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001850 u8 *mgmt_frame;
1851 u32 mgmt_frame_len;
1852 s32 freq;
1853 u16 mgmt_type;
1854
1855 brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1856 e->reason);
1857
Hante Meuleman0aedbca2016-02-17 11:26:54 +01001858 if (e->datalen < sizeof(*rxframe)) {
1859 brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1860 return 0;
1861 }
1862
Franky Lin83cf17a2013-04-11 13:28:50 +02001863 ch.chspec = be16_to_cpu(rxframe->chanspec);
1864 cfg->d11inf.decchspec(&ch);
1865
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001866 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
dingtianhong692e5162013-12-26 19:41:34 +08001867 (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
Rafał Miłecki4712d882016-05-20 13:38:57 +02001868 afx_hdl->peer_chan = ch.control_ch_num;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001869 brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
1870 afx_hdl->peer_chan);
1871 complete(&afx_hdl->act_frm_scan);
1872 }
1873
1874 /* Firmware sends us two proberesponses for each idx one. At the */
1875 /* moment anything but bsscfgidx 0 is passed up to supplicant */
1876 if (e->bsscfgidx == 0)
1877 return 0;
1878
1879 /* Filter any P2P probe reqs arriving during the GO-NEG Phase */
1880 if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
1881 brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
1882 return 0;
1883 }
1884
1885 /* Check if wpa_supplicant has registered for this frame */
1886 brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
1887 mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
1888 if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1889 return 0;
1890
1891 mgmt_frame = (u8 *)(rxframe + 1);
1892 mgmt_frame_len = e->datalen - sizeof(*rxframe);
Rafał Miłecki4712d882016-05-20 13:38:57 +02001893 freq = ieee80211_channel_to_frequency(ch.control_ch_num,
Franky Lin83cf17a2013-04-11 13:28:50 +02001894 ch.band == BRCMU_CHAN_BAND_2G ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001895 NL80211_BAND_2GHZ :
1896 NL80211_BAND_5GHZ);
Arend van Sprielbffc61c2013-04-05 10:57:52 +02001897
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07001898 cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len, 0);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001899
1900 brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
Kevin Cernekee73f2c8e2017-09-16 21:08:22 -07001901 mgmt_frame_len, e->datalen, ch.chspec, freq);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001902
1903 return 0;
1904}
1905
Hante Meuleman18e2f612013-02-08 15:53:49 +01001906
1907/**
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001908 * brcmf_p2p_get_current_chanspec() - Get current operation channel.
1909 *
1910 * @p2p: P2P specific data.
1911 * @chanspec: chanspec to be returned.
1912 */
1913static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
1914 u16 *chanspec)
1915{
1916 struct brcmf_if *ifp;
Hante Meuleman15a953d2013-05-27 21:09:54 +02001917 u8 mac_addr[ETH_ALEN];
Franky Lin83cf17a2013-04-11 13:28:50 +02001918 struct brcmu_chan ch;
Hante Meuleman15a953d2013-05-27 21:09:54 +02001919 struct brcmf_bss_info_le *bi;
1920 u8 *buf;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001921
1922 ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1923
Hante Meuleman15a953d2013-05-27 21:09:54 +02001924 if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mac_addr,
1925 ETH_ALEN) == 0) {
1926 buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
1927 if (buf != NULL) {
1928 *(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
1929 if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
1930 buf, WL_BSS_INFO_MAX) == 0) {
1931 bi = (struct brcmf_bss_info_le *)(buf + 4);
1932 *chanspec = le16_to_cpu(bi->chanspec);
1933 kfree(buf);
1934 return;
1935 }
1936 kfree(buf);
1937 }
1938 }
1939 /* Use default channel for P2P */
1940 ch.chnum = BRCMF_P2P_TEMP_CHAN;
Franky Lin83cf17a2013-04-11 13:28:50 +02001941 ch.bw = BRCMU_CHAN_BW_20;
1942 p2p->cfg->d11inf.encchspec(&ch);
1943 *chanspec = ch.chspec;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001944}
1945
1946/**
1947 * Change a P2P Role.
1948 * Parameters:
1949 * @mac: MAC address of the BSS to change a role
1950 * Returns 0 if success.
1951 */
1952int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
1953 enum brcmf_fil_p2p_if_types if_type)
1954{
1955 struct brcmf_p2p_info *p2p = &cfg->p2p;
1956 struct brcmf_cfg80211_vif *vif;
1957 struct brcmf_fil_p2p_if_le if_request;
1958 s32 err;
1959 u16 chanspec;
1960
1961 brcmf_dbg(TRACE, "Enter\n");
1962
1963 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1964 if (!vif) {
1965 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
1966 return -EPERM;
1967 }
Arend van Spriela0f472a2013-04-05 10:57:49 +02001968 brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001969 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
1970 if (!vif) {
1971 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
1972 return -EPERM;
1973 }
Arend van Sprielf96aa072013-04-05 10:57:48 +02001974 brcmf_set_mpc(vif->ifp, 0);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001975
1976 /* In concurrency case, STA may be already associated in a particular */
1977 /* channel. so retrieve the current channel of primary interface and */
1978 /* then start the virtual interface on that. */
1979 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1980
1981 if_request.type = cpu_to_le16((u16)if_type);
1982 if_request.chspec = cpu_to_le16(chanspec);
1983 memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
1984
1985 brcmf_cfg80211_arm_vif_event(cfg, vif);
1986 err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
1987 sizeof(if_request));
1988 if (err) {
1989 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
1990 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1991 return err;
1992 }
Arend van Spriela9eb0c42016-02-17 11:26:50 +01001993 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_CHANGE,
1994 BRCMF_VIF_EVENT_TIMEOUT);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001995 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1996 if (!err) {
1997 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
1998 return -EIO;
1999 }
2000
2001 err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2002 BRCMF_SCB_TIMEOUT_VALUE);
2003
2004 return err;
2005}
2006
2007static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2008 struct brcmf_if *ifp, u8 ea[ETH_ALEN],
Arend van Sprield3c0b632013-02-08 15:53:37 +01002009 enum brcmf_fil_p2p_if_types iftype)
2010{
2011 struct brcmf_fil_p2p_if_le if_request;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002012 int err;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002013 u16 chanspec;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002014
2015 /* we need a default channel */
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002016 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002017
2018 /* fill the firmware request */
2019 memcpy(if_request.addr, ea, ETH_ALEN);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002020 if_request.type = cpu_to_le16((u16)iftype);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002021 if_request.chspec = cpu_to_le16(chanspec);
2022
2023 err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2024 sizeof(if_request));
Arend van Sprield3c0b632013-02-08 15:53:37 +01002025
Arend van Sprield3c0b632013-02-08 15:53:37 +01002026 return err;
2027}
2028
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002029static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2030{
2031 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2032 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2033 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2034 u8 *addr = vif->wdev.netdev->dev_addr;
2035
2036 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2037}
2038
2039static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2040{
2041 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2042 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2043 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2044 u8 *addr = vif->wdev.netdev->dev_addr;
2045
2046 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2047}
2048
Arend van Spriel9f440b72013-02-08 15:53:36 +01002049/**
Arend van Spriel27f10e32013-04-05 10:57:50 +02002050 * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2051 *
2052 * @p2p: P2P specific data.
2053 * @wiphy: wiphy device of new interface.
2054 * @addr: mac address for this new interface.
2055 */
2056static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2057 struct wiphy *wiphy,
2058 u8 *addr)
2059{
2060 struct brcmf_cfg80211_vif *p2p_vif;
2061 struct brcmf_if *p2p_ifp;
2062 struct brcmf_if *pri_ifp;
2063 int err;
Hante Meuleman37a869e2015-10-29 20:33:17 +01002064 u32 bsscfgidx;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002065
2066 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2067 return ERR_PTR(-ENOSPC);
2068
Rafał Miłecki26072332016-06-06 23:03:55 +02002069 p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002070 if (IS_ERR(p2p_vif)) {
2071 brcmf_err("could not create discovery vif\n");
2072 return (struct wireless_dev *)p2p_vif;
2073 }
2074
Arend van Spriel27f10e32013-04-05 10:57:50 +02002075 pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Arend Van Sprielcb746e42018-04-26 12:16:47 +02002076
2077 /* firmware requires unique mac address for p2pdev interface */
2078 if (addr && ether_addr_equal(addr, pri_ifp->mac_addr)) {
2079 brcmf_err("discovery vif must be different from primary interface\n");
2080 return ERR_PTR(-EINVAL);
2081 }
2082
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002083 brcmf_p2p_generate_bss_mac(p2p, addr);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002084 brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2085
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002086 brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
Hante Meulemancb8dc712015-09-18 22:08:18 +02002087 brcmf_fweh_p2pdev_setup(pri_ifp, true);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002088
Arend van Spriel27f10e32013-04-05 10:57:50 +02002089 /* Initialize P2P Discovery in the firmware */
2090 err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2091 if (err < 0) {
2092 brcmf_err("set p2p_disc error\n");
Hante Meulemancb8dc712015-09-18 22:08:18 +02002093 brcmf_fweh_p2pdev_setup(pri_ifp, false);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002094 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2095 goto fail;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002096 }
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002097
2098 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002099 err = brcmf_cfg80211_wait_vif_event(p2p->cfg, BRCMF_E_IF_ADD,
2100 BRCMF_VIF_EVENT_TIMEOUT);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002101 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
Hante Meulemancb8dc712015-09-18 22:08:18 +02002102 brcmf_fweh_p2pdev_setup(pri_ifp, false);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002103 if (!err) {
2104 brcmf_err("timeout occurred\n");
2105 err = -EIO;
2106 goto fail;
2107 }
2108
2109 /* discovery interface created */
2110 p2p_ifp = p2p_vif->ifp;
2111 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2112 memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2113 memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2114
2115 /* verify bsscfg index for P2P discovery */
Hante Meuleman37a869e2015-10-29 20:33:17 +01002116 err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bsscfgidx);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002117 if (err < 0) {
2118 brcmf_err("retrieving discover bsscfg index failed\n");
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002119 goto fail;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002120 }
2121
Hante Meuleman37a869e2015-10-29 20:33:17 +01002122 WARN_ON(p2p_ifp->bsscfgidx != bsscfgidx);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002123
2124 init_completion(&p2p->send_af_done);
2125 INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2126 init_completion(&p2p->afx_hdl.act_frm_scan);
2127 init_completion(&p2p->wait_next_af);
2128
2129 return &p2p_vif->wdev;
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002130
2131fail:
Arend van Spriel427dec52014-01-06 12:40:47 +01002132 brcmf_free_vif(p2p_vif);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002133 return ERR_PTR(err);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002134}
2135
2136/**
Arend van Spriel9f440b72013-02-08 15:53:36 +01002137 * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2138 *
2139 * @wiphy: wiphy device of new interface.
2140 * @name: name of the new interface.
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002141 * @name_assign_type: origin of the interface name
Arend van Spriel9f440b72013-02-08 15:53:36 +01002142 * @type: nl80211 interface type.
Arend van Spriel27f10e32013-04-05 10:57:50 +02002143 * @params: contains mac address for P2P device.
Arend van Spriel9f440b72013-02-08 15:53:36 +01002144 */
2145struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002146 unsigned char name_assign_type,
Johannes Berg818a9862017-04-12 11:23:28 +02002147 enum nl80211_iftype type,
Arend van Spriel9f440b72013-02-08 15:53:36 +01002148 struct vif_params *params)
2149{
Arend van Sprield3c0b632013-02-08 15:53:37 +01002150 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;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002154 int err;
2155
2156 if (brcmf_cfg80211_vif_event_armed(cfg))
2157 return ERR_PTR(-EBUSY);
2158
Arend van Spriel9f440b72013-02-08 15:53:36 +01002159 brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002160
2161 switch (type) {
2162 case NL80211_IFTYPE_P2P_CLIENT:
2163 iftype = BRCMF_FIL_P2P_IF_CLIENT;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002164 break;
2165 case NL80211_IFTYPE_P2P_GO:
2166 iftype = BRCMF_FIL_P2P_IF_GO;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002167 break;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002168 case NL80211_IFTYPE_P2P_DEVICE:
2169 return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2170 params->macaddr);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002171 default:
2172 return ERR_PTR(-EOPNOTSUPP);
2173 }
2174
Rafał Miłecki26072332016-06-06 23:03:55 +02002175 vif = brcmf_alloc_vif(cfg, type);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002176 if (IS_ERR(vif))
2177 return (struct wireless_dev *)vif;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002178 brcmf_cfg80211_arm_vif_event(cfg, vif);
2179
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002180 err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
2181 iftype);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002182 if (err) {
2183 brcmf_cfg80211_arm_vif_event(cfg, NULL);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002184 goto fail;
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002185 }
Arend van Sprield3c0b632013-02-08 15:53:37 +01002186
2187 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002188 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_ADD,
2189 BRCMF_VIF_EVENT_TIMEOUT);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002190 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;
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002202 goto fail;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002203 }
2204
2205 strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002206 ifp->ndev->name_assign_type = name_assign_type;
Hante Meuleman4b3a89d2013-02-08 15:54:01 +01002207 err = brcmf_net_attach(ifp, true);
2208 if (err) {
2209 brcmf_err("Registering netdevice failed\n");
Arend Van Sprieldca23072017-06-24 22:08:27 +01002210 free_netdev(ifp->ndev);
Hante Meuleman4b3a89d2013-02-08 15:54:01 +01002211 goto fail;
2212 }
Arend van Sprielcbb371d2013-05-27 21:09:57 +02002213
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002214 cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
2215 /* Disable firmware roaming for P2P interface */
2216 brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2217 if (iftype == BRCMF_FIL_P2P_IF_GO) {
2218 /* set station timeout for p2p */
2219 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2220 BRCMF_SCB_TIMEOUT_VALUE);
2221 }
Arend van Sprield3c0b632013-02-08 15:53:37 +01002222 return &ifp->vif->wdev;
2223
2224fail:
Arend van Spriel427dec52014-01-06 12:40:47 +01002225 brcmf_free_vif(vif);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002226 return ERR_PTR(err);
Arend van Spriel9f440b72013-02-08 15:53:36 +01002227}
2228
2229/**
2230 * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2231 *
2232 * @wiphy: wiphy device of interface.
2233 * @wdev: wireless device of interface.
Arend van Spriel9f440b72013-02-08 15:53:36 +01002234 */
2235int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2236{
Arend Van Spriel856d5a02018-03-22 21:28:23 +01002237 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01002238 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002239 struct brcmf_cfg80211_vif *vif;
Arend Van Sprield77facb2017-03-28 09:11:30 +01002240 enum nl80211_iftype iftype;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002241 bool wait_for_disable = false;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002242 int err;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002243
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002244 brcmf_dbg(TRACE, "delete P2P vif\n");
Arend van Spriel9f440b72013-02-08 15:53:36 +01002245 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002246
Arend Van Sprield77facb2017-03-28 09:11:30 +01002247 iftype = vif->wdev.iftype;
Hante Meulemana2044d92015-09-18 22:08:16 +02002248 brcmf_cfg80211_arm_vif_event(cfg, vif);
Arend Van Sprield77facb2017-03-28 09:11:30 +01002249 switch (iftype) {
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002250 case NL80211_IFTYPE_P2P_CLIENT:
2251 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2252 wait_for_disable = true;
2253 break;
2254
2255 case NL80211_IFTYPE_P2P_GO:
2256 if (!brcmf_p2p_disable_p2p_if(vif))
2257 wait_for_disable = true;
2258 break;
2259
2260 case NL80211_IFTYPE_P2P_DEVICE:
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002261 if (!p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2262 return 0;
Arend van Sprielf37d69a2015-06-11 00:12:21 +02002263 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2264 brcmf_p2p_deinit_discovery(p2p);
Rafał Miłecki20856ad2016-06-19 01:55:57 +02002265 break;
2266
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002267 default:
2268 return -ENOTSUPP;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002269 }
2270
Hante Meuleman6eda4e22013-02-08 15:54:02 +01002271 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2272 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2273
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002274 if (wait_for_disable)
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002275 wait_for_completion_timeout(&cfg->vif_disabled,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002276 BRCMF_P2P_DISABLE_TIMEOUT);
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002277
Hante Meulemana2044d92015-09-18 22:08:16 +02002278 err = 0;
Arend Van Sprield77facb2017-03-28 09:11:30 +01002279 if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
Hante Meulemana2044d92015-09-18 22:08:16 +02002280 brcmf_vif_clear_mgmt_ies(vif);
2281 err = brcmf_p2p_release_p2p_if(vif);
2282 }
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002283 if (!err) {
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002284 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002285 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_DEL,
2286 BRCMF_VIF_EVENT_TIMEOUT);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002287 if (!err)
2288 err = -EIO;
2289 else
2290 err = 0;
2291 }
Rafał Miłeckia63b0982016-06-29 21:54:26 +02002292 brcmf_remove_interface(vif->ifp, true);
Hante Meulemana2044d92015-09-18 22:08:16 +02002293
Arend van Sprield3c0b632013-02-08 15:53:37 +01002294 brcmf_cfg80211_arm_vif_event(cfg, NULL);
Arend Van Sprield77facb2017-03-28 09:11:30 +01002295 if (iftype != NL80211_IFTYPE_P2P_DEVICE)
Hante Meulemana2044d92015-09-18 22:08:16 +02002296 p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002297
2298 return err;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002299}
Arend van Spriel27f10e32013-04-05 10:57:50 +02002300
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002301void brcmf_p2p_ifp_removed(struct brcmf_if *ifp, bool rtnl_locked)
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002302{
2303 struct brcmf_cfg80211_info *cfg;
2304 struct brcmf_cfg80211_vif *vif;
2305
2306 brcmf_dbg(INFO, "P2P: device interface removed\n");
2307 vif = ifp->vif;
2308 cfg = wdev_to_cfg(&vif->wdev);
2309 cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002310 if (!rtnl_locked)
2311 rtnl_lock();
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002312 cfg80211_unregister_wdev(&vif->wdev);
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002313 if (!rtnl_locked)
2314 rtnl_unlock();
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002315 brcmf_free_vif(vif);
2316}
2317
Arend van Spriel27f10e32013-04-05 10:57:50 +02002318int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2319{
2320 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2321 struct brcmf_p2p_info *p2p = &cfg->p2p;
2322 struct brcmf_cfg80211_vif *vif;
2323 int err;
2324
2325 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2326 mutex_lock(&cfg->usr_sync);
2327 err = brcmf_p2p_enable_discovery(p2p);
2328 if (!err)
2329 set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2330 mutex_unlock(&cfg->usr_sync);
2331 return err;
2332}
2333
2334void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2335{
2336 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2337 struct brcmf_p2p_info *p2p = &cfg->p2p;
2338 struct brcmf_cfg80211_vif *vif;
2339
2340 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
Hante Meulemand1bb34c2015-09-18 22:08:13 +02002341 /* This call can be result of the unregister_wdev call. In that case
2342 * we dont want to do anything anymore. Just return. The config vif
2343 * will have been cleared at this point.
2344 */
2345 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif == vif) {
2346 mutex_lock(&cfg->usr_sync);
Hante Meulemana2044d92015-09-18 22:08:16 +02002347 /* Set the discovery state to SCAN */
2348 (void)brcmf_p2p_set_discover_state(vif->ifp,
2349 WL_P2P_DISC_ST_SCAN, 0, 0);
Hante Meulemand1bb34c2015-09-18 22:08:13 +02002350 brcmf_abort_scanning(cfg);
2351 clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2352 mutex_unlock(&cfg->usr_sync);
2353 }
Arend van Spriel27f10e32013-04-05 10:57:50 +02002354}
Arend van Spriel55479df2015-06-11 00:12:22 +02002355
2356/**
2357 * brcmf_p2p_attach() - attach for P2P.
2358 *
2359 * @cfg: driver private data for cfg80211 interface.
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002360 * @p2pdev_forced: create p2p device interface at attach.
Arend van Spriel55479df2015-06-11 00:12:22 +02002361 */
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002362s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg, bool p2pdev_forced)
Arend van Spriel55479df2015-06-11 00:12:22 +02002363{
Arend van Spriel55479df2015-06-11 00:12:22 +02002364 struct brcmf_p2p_info *p2p;
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002365 struct brcmf_if *pri_ifp;
Arend van Spriel55479df2015-06-11 00:12:22 +02002366 s32 err = 0;
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002367 void *err_ptr;
Arend van Spriel55479df2015-06-11 00:12:22 +02002368
2369 p2p = &cfg->p2p;
2370 p2p->cfg = cfg;
2371
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002372 pri_ifp = brcmf_get_ifp(cfg->pub, 0);
Arend van Spriel55479df2015-06-11 00:12:22 +02002373 p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
2374
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002375 if (p2pdev_forced) {
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002376 err_ptr = brcmf_p2p_create_p2pdev(p2p, NULL, NULL);
2377 if (IS_ERR(err_ptr)) {
2378 brcmf_err("P2P device creation failed.\n");
2379 err = PTR_ERR(err_ptr);
2380 }
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002381 } else {
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002382 p2p->p2pdev_dynamically = true;
2383 }
Arend van Spriel55479df2015-06-11 00:12:22 +02002384 return err;
2385}
2386
2387/**
2388 * brcmf_p2p_detach() - detach P2P.
2389 *
2390 * @p2p: P2P specific data.
2391 */
2392void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
2393{
2394 struct brcmf_cfg80211_vif *vif;
2395
2396 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
2397 if (vif != NULL) {
2398 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2399 brcmf_p2p_deinit_discovery(p2p);
Rafał Miłeckib50ddfa2016-06-17 12:29:21 +02002400 brcmf_remove_interface(vif->ifp, false);
Arend van Spriel55479df2015-06-11 00:12:22 +02002401 }
2402 /* just set it all to zero */
2403 memset(p2p, 0, sizeof(*p2p));
2404}
2405