blob: aac9c97d225573cae436ffe82f22d80b30f75331 [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
Chung-Hsien Hsub2143dc2018-09-27 14:59:44 +000077#define P2P_AF_FRM_SCAN_MAX_WAIT msecs_to_jiffies(450)
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 Spriel0a9be1b2018-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 Spriel0a9be1b2018-02-28 21:15:20 +0100470 bool random_addr = false;
Arend van Spriel27f10e32013-04-05 10:57:50 +0200471
Arend Van Spriel0a9be1b2018-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 Spriel0a9be1b2018-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 Spriel0a9be1b2018-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;
693 if (p2p->cfg->active_scan)
694 sparams->scan_type = 0;
695 else
696 sparams->scan_type = 1;
697
Joe Perches93803b32015-03-02 19:54:49 -0800698 eth_broadcast_addr(sparams->bssid);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100699 sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
700
701 /*
702 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
703 * supported by the supplicant.
704 */
705 if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
706 active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
707 else if (num_chans == AF_PEER_SEARCH_CNT)
708 active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
Rasmus Villemoese843bb12014-06-22 20:50:40 +0200709 else if (brcmf_get_vif_state_any(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
Arend van Spriel9f440b72013-02-08 15:53:36 +0100710 active = -1;
711 else
712 active = P2PAPI_SCAN_DWELL_TIME_MS;
713
714 /* Override scan params to find a peer for a connection */
715 if (num_chans == 1) {
716 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
Hante Meuleman18e2f612013-02-08 15:53:49 +0100717 /* WAR to sync with presence period of VSDB GO.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100718 * send probe request more frequently
719 */
720 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
721 } else {
722 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
723 }
724
725 if (nprobes <= 0)
726 nprobes = 1;
727
728 brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
729 sparams->active_time = cpu_to_le32(active);
730 sparams->nprobes = cpu_to_le32(nprobes);
731 sparams->passive_time = cpu_to_le32(-1);
732 sparams->channel_num = cpu_to_le32(num_chans &
733 BRCMF_SCAN_PARAMS_COUNT_MASK);
734 for (i = 0; i < num_chans; i++)
735 sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
736
737 /* set the escan specific parameters */
738 p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
Hante Meulemanc4958102015-11-25 11:32:41 +0100739 p2p_params->eparams.action = cpu_to_le16(WL_ESCAN_ACTION_START);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100740 p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
741 /* perform p2p scan on primary device */
742 ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
743 if (!ret)
744 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
745exit:
746 kfree(memblk);
747 return ret;
748}
749
750/**
751 * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
752 *
753 * @cfg: driver private data for cfg80211 interface.
754 * @ndev: net device for which scan is requested.
755 * @request: scan request from cfg80211.
756 * @action: scan action.
757 *
758 * Determines the P2P discovery state based to scan request parameters and
759 * validates the channels in the request.
760 */
761static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
Arend van Spriela0f472a2013-04-05 10:57:49 +0200762 struct brcmf_if *ifp,
Hante Meulemanc4958102015-11-25 11:32:41 +0100763 struct cfg80211_scan_request *request)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100764{
765 struct brcmf_p2p_info *p2p = &cfg->p2p;
766 s32 err = 0;
767 s32 search_state = WL_P2P_DISC_ST_SCAN;
768 struct brcmf_cfg80211_vif *vif;
769 struct net_device *dev = NULL;
770 int i, num_nodfs = 0;
771 u16 *chanspecs;
772
773 brcmf_dbg(TRACE, "enter\n");
774
775 if (!request) {
776 err = -EINVAL;
777 goto exit;
778 }
779
780 if (request->n_channels) {
781 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
782 GFP_KERNEL);
783 if (!chanspecs) {
784 err = -ENOMEM;
785 goto exit;
786 }
787 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
788 if (vif)
789 dev = vif->wdev.netdev;
790 if (request->n_channels == 3 &&
791 request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
792 request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
793 request->channels[2]->hw_value == SOCIAL_CHAN_3) {
794 /* SOCIAL CHANNELS 1, 6, 11 */
795 search_state = WL_P2P_DISC_ST_SEARCH;
796 brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
Arend van Spriel967fe2c2014-03-15 17:18:21 +0100797 } else if (dev != NULL &&
798 vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
Arend van Spriel9f440b72013-02-08 15:53:36 +0100799 /* If you are already a GO, then do SEARCH only */
800 brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
801 search_state = WL_P2P_DISC_ST_SEARCH;
802 } else {
803 brcmf_dbg(INFO, "P2P SCAN STATE START\n");
804 }
805
806 /*
807 * no P2P scanning on passive or DFS channels.
808 */
809 for (i = 0; i < request->n_channels; i++) {
810 struct ieee80211_channel *chan = request->channels[i];
811
812 if (chan->flags & (IEEE80211_CHAN_RADAR |
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200813 IEEE80211_CHAN_NO_IR))
Arend van Spriel9f440b72013-02-08 15:53:36 +0100814 continue;
815
Franky Lin83cf17a2013-04-11 13:28:50 +0200816 chanspecs[i] = channel_to_chanspec(&p2p->cfg->d11inf,
817 chan);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100818 brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
819 num_nodfs, chan->hw_value, chanspecs[i]);
820 num_nodfs++;
821 }
822 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
Hante Meulemanc4958102015-11-25 11:32:41 +0100823 P2PAPI_BSSCFG_DEVICE);
Geyslan G. Bem3b1bace2013-11-15 09:21:47 -0300824 kfree(chanspecs);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100825 }
826exit:
827 if (err)
828 brcmf_err("error (%d)\n", err);
829 return err;
830}
831
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100832
833/**
834 * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
835 *
836 * @ie: string of information elements.
837 * @ie_len: length of string.
838 *
839 * Scan ie for p2p ie and look for attribute 6 channel. If available determine
840 * channel and return it.
841 */
842static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
843{
844 u8 channel_ie[5];
845 s32 listen_channel;
846 s32 err;
847
848 err = cfg80211_get_p2p_attr(ie, ie_len,
849 IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
850 channel_ie, sizeof(channel_ie));
851 if (err < 0)
852 return err;
853
854 /* listen channel subel length format: */
855 /* 3(country) + 1(op. class) + 1(chan num) */
856 listen_channel = (s32)channel_ie[3 + 1];
857
858 if (listen_channel == SOCIAL_CHAN_1 ||
859 listen_channel == SOCIAL_CHAN_2 ||
860 listen_channel == SOCIAL_CHAN_3) {
861 brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
862 return listen_channel;
863 }
864
865 return -EPERM;
866}
867
868
Arend van Spriel9f440b72013-02-08 15:53:36 +0100869/**
870 * brcmf_p2p_scan_prep() - prepare scan based on request.
871 *
872 * @wiphy: wiphy device.
873 * @request: scan request from cfg80211.
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100874 * @vif: vif on which scan request is to be executed.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100875 *
876 * Prepare the scan appropriately for type of scan requested. Overrides the
877 * escan .run() callback for peer-to-peer scanning.
878 */
879int brcmf_p2p_scan_prep(struct wiphy *wiphy,
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100880 struct cfg80211_scan_request *request,
881 struct brcmf_cfg80211_vif *vif)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100882{
883 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
884 struct brcmf_p2p_info *p2p = &cfg->p2p;
885 int err = 0;
886
887 if (brcmf_p2p_scan_is_p2p_request(request)) {
888 /* find my listen channel */
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100889 err = brcmf_p2p_find_listen_channel(request->ie,
890 request->ie_len);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100891 if (err < 0)
892 return err;
893
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100894 p2p->afx_hdl.my_listen_chan = err;
895
Arend van Spriel9f440b72013-02-08 15:53:36 +0100896 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
897 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
898
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100899 err = brcmf_p2p_enable_discovery(p2p);
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100900 if (err)
901 return err;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100902
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100903 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
904
905 /* override .run_escan() callback. */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100906 cfg->escan_info.run = brcmf_p2p_run_escan;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100907 }
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100908 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
909 request->ie, request->ie_len);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100910 return err;
911}
912
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100913
914/**
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100915 * brcmf_p2p_discover_listen() - set firmware to discover listen state.
916 *
917 * @p2p: p2p device.
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100918 * @channel: channel nr for discover listen.
919 * @duration: time in ms to stay on channel.
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100920 *
921 */
922static s32
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100923brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100924{
925 struct brcmf_cfg80211_vif *vif;
Franky Lin83cf17a2013-04-11 13:28:50 +0200926 struct brcmu_chan ch;
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100927 s32 err = 0;
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100928
929 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
930 if (!vif) {
931 brcmf_err("Discovery is not set, so we have nothing to do\n");
932 err = -EPERM;
933 goto exit;
934 }
935
936 if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
937 brcmf_err("Previous LISTEN is not completed yet\n");
938 /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
939 goto exit;
940 }
941
Franky Lin83cf17a2013-04-11 13:28:50 +0200942 ch.chnum = channel;
943 ch.bw = BRCMU_CHAN_BW_20;
944 p2p->cfg->d11inf.encchspec(&ch);
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100945 err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
Franky Lin83cf17a2013-04-11 13:28:50 +0200946 ch.chspec, (u16)duration);
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100947 if (!err) {
948 set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
949 p2p->remain_on_channel_cookie++;
950 }
951exit:
952 return err;
953}
954
955
956/**
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100957 * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
958 *
959 * @wiphy: wiphy device.
960 * @channel: channel to stay on.
961 * @duration: time in ms to remain on channel.
962 *
963 */
964int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
965 struct ieee80211_channel *channel,
966 unsigned int duration, u64 *cookie)
967{
968 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
969 struct brcmf_p2p_info *p2p = &cfg->p2p;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100970 s32 err;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100971 u16 channel_nr;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100972
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100973 channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
974 brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100975 duration);
976
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100977 err = brcmf_p2p_enable_discovery(p2p);
978 if (err)
979 goto exit;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100980 err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100981 if (err)
982 goto exit;
983
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100984 memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
985 *cookie = p2p->remain_on_channel_cookie;
Arend van Spriel1ce30862013-02-08 15:53:51 +0100986 cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100987
988exit:
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100989 return err;
990}
991
992
993/**
994 * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
995 *
996 * @ifp: interfac control.
997 * @e: event message. Not used, to make it usable for fweh event dispatcher.
998 * @data: payload of message. Not used.
999 *
1000 */
1001int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
1002 const struct brcmf_event_msg *e,
1003 void *data)
1004{
1005 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1006 struct brcmf_p2p_info *p2p = &cfg->p2p;
1007
1008 brcmf_dbg(TRACE, "Enter\n");
Hante Meulemanf2058dd2013-02-08 15:53:50 +01001009 if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001010 &p2p->status)) {
1011 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1012 &p2p->status)) {
1013 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1014 &p2p->status);
1015 brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1016 complete(&p2p->wait_next_af);
1017 }
1018
Hante Meulemanf2058dd2013-02-08 15:53:50 +01001019 cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1020 p2p->remain_on_channel_cookie,
Hante Meuleman0de8aac2013-02-08 15:53:38 +01001021 &p2p->remain_on_channel,
1022 GFP_KERNEL);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001023 }
Hante Meuleman0de8aac2013-02-08 15:53:38 +01001024 return 0;
1025}
1026
1027
1028/**
1029 * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1030 *
1031 * @ifp: interfac control.
1032 *
1033 */
1034void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1035{
1036 if (!ifp)
1037 return;
1038 brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1039 brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1040}
1041
1042
Arend van Spriel9f440b72013-02-08 15:53:36 +01001043/**
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001044 * brcmf_p2p_act_frm_search() - search function for action frame.
1045 *
1046 * @p2p: p2p device.
1047 * channel: channel on which action frame is to be trasmitted.
1048 *
1049 * search function to reach at common channel to send action frame. When
1050 * channel is 0 then all social channels will be used to send af
1051 */
1052static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1053{
1054 s32 err;
1055 u32 channel_cnt;
1056 u16 *default_chan_list;
1057 u32 i;
Franky Lin83cf17a2013-04-11 13:28:50 +02001058 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001059
1060 brcmf_dbg(TRACE, "Enter\n");
1061
1062 if (channel)
1063 channel_cnt = AF_PEER_SEARCH_CNT;
1064 else
1065 channel_cnt = SOCIAL_CHAN_CNT;
1066 default_chan_list = kzalloc(channel_cnt * sizeof(*default_chan_list),
1067 GFP_KERNEL);
1068 if (default_chan_list == NULL) {
1069 brcmf_err("channel list allocation failed\n");
1070 err = -ENOMEM;
1071 goto exit;
1072 }
Franky Lin83cf17a2013-04-11 13:28:50 +02001073 ch.bw = BRCMU_CHAN_BW_20;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001074 if (channel) {
Franky Lin83cf17a2013-04-11 13:28:50 +02001075 ch.chnum = channel;
1076 p2p->cfg->d11inf.encchspec(&ch);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001077 /* insert same channel to the chan_list */
1078 for (i = 0; i < channel_cnt; i++)
Franky Lin83cf17a2013-04-11 13:28:50 +02001079 default_chan_list[i] = ch.chspec;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001080 } else {
Franky Lin83cf17a2013-04-11 13:28:50 +02001081 ch.chnum = SOCIAL_CHAN_1;
1082 p2p->cfg->d11inf.encchspec(&ch);
1083 default_chan_list[0] = ch.chspec;
1084 ch.chnum = SOCIAL_CHAN_2;
1085 p2p->cfg->d11inf.encchspec(&ch);
1086 default_chan_list[1] = ch.chspec;
1087 ch.chnum = SOCIAL_CHAN_3;
1088 p2p->cfg->d11inf.encchspec(&ch);
1089 default_chan_list[2] = ch.chspec;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001090 }
1091 err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
Hante Meulemanc4958102015-11-25 11:32:41 +01001092 WL_P2P_DISC_ST_SEARCH, P2PAPI_BSSCFG_DEVICE);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001093 kfree(default_chan_list);
1094exit:
1095 return err;
1096}
1097
1098
1099/**
1100 * brcmf_p2p_afx_handler() - afx worker thread.
1101 *
1102 * @work:
1103 *
1104 */
1105static void brcmf_p2p_afx_handler(struct work_struct *work)
1106{
1107 struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
1108 struct brcmf_p2p_info *p2p = container_of(afx_hdl,
1109 struct brcmf_p2p_info,
1110 afx_hdl);
1111 s32 err;
1112
1113 if (!afx_hdl->is_active)
1114 return;
1115
1116 if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1117 /* 100ms ~ 300ms */
1118 err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
Akinobu Mitae00adf32013-05-07 16:18:15 -07001119 100 * (1 + prandom_u32() % 3));
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001120 else
1121 err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1122
1123 if (err) {
1124 brcmf_err("ERROR occurred! value is (%d)\n", err);
1125 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1126 &p2p->status))
1127 complete(&afx_hdl->act_frm_scan);
1128 }
1129}
1130
1131
1132/**
1133 * brcmf_p2p_af_searching_channel() - search channel.
1134 *
1135 * @p2p: p2p device info struct.
1136 *
1137 */
1138static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1139{
1140 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1141 struct brcmf_cfg80211_vif *pri_vif;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001142 s32 retry;
1143
1144 brcmf_dbg(TRACE, "Enter\n");
1145
1146 pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1147
Wolfram Sang16735d02013-11-14 14:32:02 -08001148 reinit_completion(&afx_hdl->act_frm_scan);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001149 set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1150 afx_hdl->is_active = true;
1151 afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
1152
1153 /* Loop to wait until we find a peer's channel or the
1154 * pending action frame tx is cancelled.
1155 */
1156 retry = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001157 while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
1158 (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
1159 afx_hdl->is_listen = false;
1160 brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
1161 retry);
1162 /* search peer on peer's listen channel */
1163 schedule_work(&afx_hdl->afx_work);
Chung-Hsien Hsub2143dc2018-09-27 14:59:44 +00001164 wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1165 P2P_AF_FRM_SCAN_MAX_WAIT);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001166 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1167 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1168 &p2p->status)))
1169 break;
1170
1171 if (afx_hdl->my_listen_chan) {
1172 brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
1173 afx_hdl->my_listen_chan);
1174 /* listen on my listen channel */
1175 afx_hdl->is_listen = true;
1176 schedule_work(&afx_hdl->afx_work);
1177 wait_for_completion_timeout(&afx_hdl->act_frm_scan,
Chung-Hsien Hsub2143dc2018-09-27 14:59:44 +00001178 P2P_AF_FRM_SCAN_MAX_WAIT);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001179 }
1180 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1181 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1182 &p2p->status)))
1183 break;
1184 retry++;
1185
1186 /* if sta is connected or connecting, sleep for a while before
1187 * retry af tx or finding a peer
1188 */
1189 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
1190 test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
1191 msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
1192 }
1193
1194 brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1195 afx_hdl->peer_chan);
1196 afx_hdl->is_active = false;
1197
1198 clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1199
1200 return afx_hdl->peer_chan;
1201}
1202
1203
1204/**
1205 * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1206 *
1207 * @cfg: common configuration struct.
1208 * @bi: bss info struct, result from scan.
1209 *
1210 */
1211bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1212 struct brcmf_bss_info_le *bi)
1213
1214{
1215 struct brcmf_p2p_info *p2p = &cfg->p2p;
1216 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Franky Lin83cf17a2013-04-11 13:28:50 +02001217 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001218 u8 *ie;
1219 s32 err;
1220 u8 p2p_dev_addr[ETH_ALEN];
1221
1222 if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
1223 return false;
1224
1225 if (bi == NULL) {
1226 brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
1227 if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
1228 complete(&afx_hdl->act_frm_scan);
1229 return true;
1230 }
1231
1232 ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
1233 memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
1234 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1235 IEEE80211_P2P_ATTR_DEVICE_INFO,
1236 p2p_dev_addr, sizeof(p2p_dev_addr));
1237 if (err < 0)
1238 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1239 IEEE80211_P2P_ATTR_DEVICE_ID,
1240 p2p_dev_addr, sizeof(p2p_dev_addr));
1241 if ((err >= 0) &&
dingtianhong692e5162013-12-26 19:41:34 +08001242 (ether_addr_equal(p2p_dev_addr, afx_hdl->tx_dst_addr))) {
Franky Lin83cf17a2013-04-11 13:28:50 +02001243 if (!bi->ctl_ch) {
1244 ch.chspec = le16_to_cpu(bi->chanspec);
1245 cfg->d11inf.decchspec(&ch);
Rafał Miłecki4712d882016-05-20 13:38:57 +02001246 bi->ctl_ch = ch.control_ch_num;
Franky Lin83cf17a2013-04-11 13:28:50 +02001247 }
1248 afx_hdl->peer_chan = bi->ctl_ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001249 brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
1250 afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
1251 complete(&afx_hdl->act_frm_scan);
1252 }
1253 return true;
1254}
1255
1256/**
1257 * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1258 *
1259 * @cfg: common configuration struct.
1260 *
1261 */
1262static void
1263brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1264{
1265 struct brcmf_p2p_info *p2p = &cfg->p2p;
Hante Meuleman2aec2c92016-04-11 11:35:24 +02001266 struct brcmf_if *ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001267
1268 if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
1269 (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
1270 test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
1271 brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
1272 /* if channel is not zero, "actfame" uses off channel scan.
1273 * So abort scan for off channel completion.
1274 */
1275 if (p2p->af_sent_channel)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001276 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001277 } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1278 &p2p->status)) {
1279 brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1280 /* So abort scan to cancel listen */
Arend van Spriela0f472a2013-04-05 10:57:49 +02001281 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001282 }
1283}
1284
1285
1286/**
1287 * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1288 *
1289 * @p2p: p2p device info struct.
1290 *
1291 * return true if recevied action frame is to be dropped.
1292 */
1293static bool
1294brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1295{
1296 struct brcmf_cfg80211_info *cfg = p2p->cfg;
1297 struct brcmf_if *ifp;
1298
1299 brcmf_dbg(TRACE, "Enter\n");
1300
1301 if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1302 !p2p->gon_req_action)
1303 return false;
1304
1305 brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
1306 /* if sa(peer) addr is less than da(my) addr, then this device
1307 * process peer's gon request and block to send gon req.
1308 * if not (sa addr > da addr),
1309 * this device will process gon request and drop gon req of peer.
1310 */
1311 ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
1312 if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
1313 brcmf_dbg(INFO, "Block transmit gon req !!!\n");
1314 p2p->block_gon_req_tx = true;
1315 /* if we are finding a common channel for sending af,
1316 * do not scan more to block to send current gon req
1317 */
1318 if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1319 &p2p->status))
1320 complete(&p2p->afx_hdl.act_frm_scan);
1321 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1322 &p2p->status))
1323 brcmf_p2p_stop_wait_next_action_frame(cfg);
1324 return false;
1325 }
1326
1327 /* drop gon request of peer to process gon request by this device. */
1328 brcmf_dbg(INFO, "Drop received gon req !!!\n");
1329
1330 return true;
1331}
1332
1333
1334/**
Hante Meulemane6da3402013-02-08 15:53:48 +01001335 * brcmf_p2p_notify_action_frame_rx() - received action frame.
1336 *
1337 * @ifp: interfac control.
1338 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1339 * @data: payload of message, containing action frame data.
1340 *
1341 */
1342int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1343 const struct brcmf_event_msg *e,
1344 void *data)
1345{
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001346 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1347 struct brcmf_p2p_info *p2p = &cfg->p2p;
1348 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meulemane6da3402013-02-08 15:53:48 +01001349 struct wireless_dev *wdev;
1350 u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1351 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001352 u8 *frame = (u8 *)(rxframe + 1);
1353 struct brcmf_p2p_pub_act_frame *act_frm;
1354 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
Franky Lin83cf17a2013-04-11 13:28:50 +02001355 struct brcmu_chan ch;
Hante Meulemane6da3402013-02-08 15:53:48 +01001356 struct ieee80211_mgmt *mgmt_frame;
Hante Meulemane6da3402013-02-08 15:53:48 +01001357 s32 freq;
1358 u16 mgmt_type;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001359 u8 action;
Hante Meulemane6da3402013-02-08 15:53:48 +01001360
Hante Meuleman0aedbca2016-02-17 11:26:54 +01001361 if (e->datalen < sizeof(*rxframe)) {
1362 brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1363 return 0;
1364 }
1365
Franky Lin83cf17a2013-04-11 13:28:50 +02001366 ch.chspec = be16_to_cpu(rxframe->chanspec);
1367 cfg->d11inf.decchspec(&ch);
Hante Meulemane6da3402013-02-08 15:53:48 +01001368 /* Check if wpa_supplicant has registered for this frame */
1369 brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1370 mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1371 if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1372 return 0;
1373
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001374 brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
1375
1376 action = P2P_PAF_SUBTYPE_INVALID;
1377 if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
1378 act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
1379 action = act_frm->subtype;
1380 if ((action == P2P_PAF_GON_REQ) &&
1381 (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
1382 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1383 &p2p->status) &&
dingtianhong692e5162013-12-26 19:41:34 +08001384 (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
Rafał Miłecki4712d882016-05-20 13:38:57 +02001385 afx_hdl->peer_chan = ch.control_ch_num;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001386 brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1387 afx_hdl->peer_chan);
1388 complete(&afx_hdl->act_frm_scan);
1389 }
1390 return 0;
1391 }
1392 /* After complete GO Negotiation, roll back to mpc mode */
1393 if ((action == P2P_PAF_GON_CONF) ||
1394 (action == P2P_PAF_PROVDIS_RSP))
Arend van Sprielf96aa072013-04-05 10:57:48 +02001395 brcmf_set_mpc(ifp, 1);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001396 if (action == P2P_PAF_GON_CONF) {
1397 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1398 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1399 }
1400 } else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
1401 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
1402 action = sd_act_frm->action;
1403 }
1404
1405 if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1406 (p2p->next_af_subtype == action)) {
1407 brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
1408 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1409 &p2p->status);
1410 /* Stop waiting for next AF. */
1411 brcmf_p2p_stop_wait_next_action_frame(cfg);
1412 }
Hante Meulemane6da3402013-02-08 15:53:48 +01001413
1414 mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1415 mgmt_frame_len, GFP_KERNEL);
1416 if (!mgmt_frame) {
1417 brcmf_err("No memory available for action frame\n");
1418 return -ENOMEM;
1419 }
1420 memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001421 brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1422 ETH_ALEN);
Hante Meulemane6da3402013-02-08 15:53:48 +01001423 memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1424 mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001425 memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
Hante Meulemane6da3402013-02-08 15:53:48 +01001426 mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1427
Rafał Miłecki4712d882016-05-20 13:38:57 +02001428 freq = ieee80211_channel_to_frequency(ch.control_ch_num,
Franky Lin83cf17a2013-04-11 13:28:50 +02001429 ch.band == BRCMU_CHAN_BAND_2G ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001430 NL80211_BAND_2GHZ :
1431 NL80211_BAND_5GHZ);
Arend van Spriel5b57af62013-04-05 10:57:53 +02001432
1433 wdev = &ifp->vif->wdev;
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07001434 cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len, 0);
Hante Meulemane6da3402013-02-08 15:53:48 +01001435
1436 kfree(mgmt_frame);
1437 return 0;
1438}
1439
1440
1441/**
Hante Meuleman18e2f612013-02-08 15:53:49 +01001442 * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1443 *
1444 * @ifp: interfac control.
1445 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1446 * @data: not used.
1447 *
1448 */
1449int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1450 const struct brcmf_event_msg *e,
1451 void *data)
1452{
1453 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1454 struct brcmf_p2p_info *p2p = &cfg->p2p;
1455
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001456 brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1457 e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1458 "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1459 e->status);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001460
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001461 if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1462 return 0;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001463
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001464 if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
Chung-Hsien Hsu3b18e332018-09-27 14:59:49 +00001465 if (e->status == BRCMF_E_STATUS_SUCCESS) {
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001466 set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1467 &p2p->status);
Chung-Hsien Hsu3b18e332018-09-27 14:59:49 +00001468 if (!p2p->wait_for_offchan_complete)
1469 complete(&p2p->send_af_done);
1470 } else {
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001471 set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1472 /* If there is no ack, we don't need to wait for
1473 * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1474 */
1475 brcmf_p2p_stop_wait_next_action_frame(cfg);
1476 }
1477
1478 } else {
1479 complete(&p2p->send_af_done);
1480 }
Hante Meuleman18e2f612013-02-08 15:53:49 +01001481 return 0;
1482}
1483
1484
1485/**
1486 * brcmf_p2p_tx_action_frame() - send action frame over fil.
1487 *
1488 * @p2p: p2p info struct for vif.
1489 * @af_params: action frame data/info.
1490 *
1491 * Send an action frame immediately without doing channel synchronization.
1492 *
1493 * This function waits for a completion event before returning.
1494 * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1495 * frame is transmitted.
1496 */
1497static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1498 struct brcmf_fil_af_params_le *af_params)
1499{
1500 struct brcmf_cfg80211_vif *vif;
1501 s32 err = 0;
1502 s32 timeout = 0;
1503
1504 brcmf_dbg(TRACE, "Enter\n");
1505
Wolfram Sang16735d02013-11-14 14:32:02 -08001506 reinit_completion(&p2p->send_af_done);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001507 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1508 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1509
1510 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1511 err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1512 sizeof(*af_params));
1513 if (err) {
1514 brcmf_err(" sending action frame has failed\n");
1515 goto exit;
1516 }
1517
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001518 p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1519 p2p->af_tx_sent_jiffies = jiffies;
1520
Chung-Hsien Hsu3b18e332018-09-27 14:59:49 +00001521 if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status) &&
1522 p2p->af_sent_channel ==
1523 ieee80211_frequency_to_channel(p2p->remain_on_channel.center_freq))
1524 p2p->wait_for_offchan_complete = false;
1525 else
1526 p2p->wait_for_offchan_complete = true;
1527
1528 brcmf_dbg(TRACE, "Waiting for %s tx completion event\n",
1529 (p2p->wait_for_offchan_complete) ?
1530 "off-channel" : "on-channel");
1531
Hante Meuleman18e2f612013-02-08 15:53:49 +01001532 timeout = wait_for_completion_timeout(&p2p->send_af_done,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001533 P2P_AF_MAX_WAIT_TIME);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001534
1535 if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1536 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1537 } else {
1538 err = -EIO;
1539 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1540 }
1541 /* clear status bit for action tx */
1542 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1543 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1544
1545exit:
1546 return err;
1547}
1548
1549
1550/**
1551 * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1552 *
1553 * @cfg: driver private data for cfg80211 interface.
1554 * @af_params: action frame data/info.
1555 * @config_af_params: configuration data for action frame.
1556 *
1557 * routine which transmits ation frame public type.
1558 */
1559static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1560 struct brcmf_fil_af_params_le *af_params,
1561 struct brcmf_config_af_params *config_af_params)
1562{
1563 struct brcmf_p2p_info *p2p = &cfg->p2p;
1564 struct brcmf_fil_action_frame_le *action_frame;
1565 struct brcmf_p2p_pub_act_frame *act_frm;
1566 s32 err = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001567 u16 ie_len;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001568
1569 action_frame = &af_params->action_frame;
1570 act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1571
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001572 config_af_params->extra_listen = true;
1573
Hante Meuleman18e2f612013-02-08 15:53:49 +01001574 switch (act_frm->subtype) {
1575 case P2P_PAF_GON_REQ:
1576 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1577 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1578 config_af_params->mpc_onoff = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001579 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001580 p2p->next_af_subtype = act_frm->subtype + 1;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001581 p2p->gon_req_action = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001582 /* increase dwell time to wait for RESP frame */
1583 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1584 break;
1585 case P2P_PAF_GON_RSP:
1586 p2p->next_af_subtype = act_frm->subtype + 1;
1587 /* increase dwell time to wait for CONF frame */
1588 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1589 break;
1590 case P2P_PAF_GON_CONF:
1591 /* If we reached till GO Neg confirmation reset the filter */
1592 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1593 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1594 /* turn on mpc again if go nego is done */
1595 config_af_params->mpc_onoff = 1;
1596 /* minimize dwell time */
1597 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001598 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001599 break;
1600 case P2P_PAF_INVITE_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001601 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001602 p2p->next_af_subtype = act_frm->subtype + 1;
1603 /* increase dwell time */
1604 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1605 break;
1606 case P2P_PAF_INVITE_RSP:
1607 /* minimize dwell time */
1608 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001609 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001610 break;
1611 case P2P_PAF_DEVDIS_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001612 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001613 p2p->next_af_subtype = act_frm->subtype + 1;
1614 /* maximize dwell time to wait for RESP frame */
1615 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1616 break;
1617 case P2P_PAF_DEVDIS_RSP:
1618 /* minimize dwell time */
1619 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001620 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001621 break;
1622 case P2P_PAF_PROVDIS_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001623 ie_len = le16_to_cpu(action_frame->len) -
1624 offsetof(struct brcmf_p2p_pub_act_frame, elts);
1625 if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1626 IEEE80211_P2P_ATTR_GROUP_ID,
1627 NULL, 0) < 0)
1628 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001629 config_af_params->mpc_onoff = 0;
1630 p2p->next_af_subtype = act_frm->subtype + 1;
1631 /* increase dwell time to wait for RESP frame */
1632 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1633 break;
1634 case P2P_PAF_PROVDIS_RSP:
1635 /* wpa_supplicant send go nego req right after prov disc */
1636 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1637 /* increase dwell time to MED level */
1638 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001639 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001640 break;
1641 default:
1642 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1643 act_frm->subtype);
1644 err = -EINVAL;
1645 }
1646 return err;
1647}
1648
1649/**
1650 * brcmf_p2p_send_action_frame() - send action frame .
1651 *
1652 * @cfg: driver private data for cfg80211 interface.
1653 * @ndev: net device to transmit on.
1654 * @af_params: configuration data for action frame.
1655 */
1656bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1657 struct net_device *ndev,
1658 struct brcmf_fil_af_params_le *af_params)
1659{
1660 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend van Spriela0f472a2013-04-05 10:57:49 +02001661 struct brcmf_if *ifp = netdev_priv(ndev);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001662 struct brcmf_fil_action_frame_le *action_frame;
1663 struct brcmf_config_af_params config_af_params;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001664 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001665 u16 action_frame_len;
1666 bool ack = false;
1667 u8 category;
1668 u8 action;
1669 s32 tx_retry;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001670 s32 extra_listen_time;
1671 uint delta_ms;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001672
1673 action_frame = &af_params->action_frame;
1674 action_frame_len = le16_to_cpu(action_frame->len);
1675
1676 brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1677
1678 /* Add the default dwell time. Dwell time to stay off-channel */
1679 /* to wait for a response action frame after transmitting an */
1680 /* GO Negotiation action frame */
1681 af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1682
1683 category = action_frame->data[DOT11_ACTION_CAT_OFF];
1684 action = action_frame->data[DOT11_ACTION_ACT_OFF];
1685
1686 /* initialize variables */
1687 p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001688 p2p->gon_req_action = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001689
1690 /* config parameters */
Hante Meuleman18e2f612013-02-08 15:53:49 +01001691 config_af_params.mpc_onoff = -1;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001692 config_af_params.search_channel = false;
1693 config_af_params.extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001694
1695 if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1696 /* p2p public action frame process */
1697 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1698 /* Just send unknown subtype frame with */
1699 /* default parameters. */
1700 brcmf_err("P2P Public action frame, unknown subtype.\n");
1701 }
1702 } else if (brcmf_p2p_is_gas_action(action_frame->data,
1703 action_frame_len)) {
1704 /* service discovery process */
1705 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1706 action == P2PSD_ACTION_ID_GAS_CREQ) {
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001707 /* configure service discovery query frame */
1708 config_af_params.search_channel = true;
1709
Hante Meuleman18e2f612013-02-08 15:53:49 +01001710 /* save next af suptype to cancel */
1711 /* remaining dwell time */
1712 p2p->next_af_subtype = action + 1;
1713
1714 af_params->dwell_time =
1715 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1716 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1717 action == P2PSD_ACTION_ID_GAS_CRESP) {
1718 /* configure service discovery response frame */
1719 af_params->dwell_time =
1720 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1721 } else {
1722 brcmf_err("Unknown action type: %d\n", action);
1723 goto exit;
1724 }
1725 } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1726 action_frame_len)) {
1727 /* do not configure anything. it will be */
1728 /* sent with a default configuration */
1729 } else {
1730 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1731 category, action);
1732 return false;
1733 }
1734
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001735 /* if connecting on primary iface, sleep for a while before sending
1736 * af tx for VSDB
1737 */
1738 if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1739 &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1740 msleep(50);
1741
Hante Meuleman18e2f612013-02-08 15:53:49 +01001742 /* if scan is ongoing, abort current scan. */
1743 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1744 brcmf_abort_scanning(cfg);
1745
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001746 memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1747
Hante Meuleman18e2f612013-02-08 15:53:49 +01001748 /* To make sure to send successfully action frame, turn off mpc */
1749 if (config_af_params.mpc_onoff == 0)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001750 brcmf_set_mpc(ifp, 0);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001751
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001752 /* set status and destination address before sending af */
1753 if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1754 /* set status to cancel the remained dwell time in rx process */
1755 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1756 }
1757
1758 p2p->af_sent_channel = 0;
1759 set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1760 /* validate channel and p2p ies */
1761 if (config_af_params.search_channel &&
1762 IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1763 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1764 afx_hdl = &p2p->afx_hdl;
1765 afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1766
1767 if (brcmf_p2p_af_searching_channel(p2p) ==
1768 P2P_INVALID_CHANNEL) {
1769 brcmf_err("Couldn't find peer's channel.\n");
1770 goto exit;
1771 }
1772
1773 /* Abort scan even for VSDB scenarios. Scan gets aborted in
1774 * firmware but after the check of piggyback algorithm. To take
1775 * care of current piggback algo, lets abort the scan here
1776 * itself.
1777 */
Arend van Spriela0f472a2013-04-05 10:57:49 +02001778 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001779
1780 /* update channel */
1781 af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1782 }
1783
Hante Meuleman18e2f612013-02-08 15:53:49 +01001784 tx_retry = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001785 while (!p2p->block_gon_req_tx &&
1786 (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
Hante Meuleman18e2f612013-02-08 15:53:49 +01001787 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1788 tx_retry++;
1789 }
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001790 if (ack == false) {
Hante Meuleman18e2f612013-02-08 15:53:49 +01001791 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001792 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1793 }
Hante Meuleman18e2f612013-02-08 15:53:49 +01001794
1795exit:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001796 clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1797
1798 /* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1799 * if we coundn't get the next action response frame and dongle does
1800 * not keep the dwell time, go to listen state again to get next action
1801 * response frame.
1802 */
1803 if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1804 test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1805 p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1806 delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1807 if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1808 extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1809 delta_ms;
1810 else
1811 extra_listen_time = 0;
1812 if (extra_listen_time > 50) {
1813 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1814 &p2p->status);
1815 brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1816 le32_to_cpu(af_params->dwell_time),
1817 extra_listen_time);
1818 extra_listen_time += 100;
1819 if (!brcmf_p2p_discover_listen(p2p,
1820 p2p->af_sent_channel,
1821 extra_listen_time)) {
1822 unsigned long duration;
1823
1824 extra_listen_time += 100;
1825 duration = msecs_to_jiffies(extra_listen_time);
1826 wait_for_completion_timeout(&p2p->wait_next_af,
1827 duration);
1828 }
1829 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1830 &p2p->status);
1831 }
1832 }
1833
1834 if (p2p->block_gon_req_tx) {
1835 /* if ack is true, supplicant will wait more time(100ms).
1836 * so we will return it as a success to get more time .
1837 */
1838 p2p->block_gon_req_tx = false;
1839 ack = true;
1840 }
1841
1842 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001843 /* if all done, turn mpc on again */
1844 if (config_af_params.mpc_onoff == 1)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001845 brcmf_set_mpc(ifp, 1);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001846
1847 return ack;
1848}
1849
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001850/**
1851 * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1852 *
1853 * @ifp: interface pointer for which event was received.
1854 * @e: even message.
1855 * @data: payload of event message (probe request).
1856 */
1857s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1858 const struct brcmf_event_msg *e,
1859 void *data)
1860{
1861 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1862 struct brcmf_p2p_info *p2p = &cfg->p2p;
1863 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001864 struct brcmf_cfg80211_vif *vif = ifp->vif;
1865 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1866 u16 chanspec = be16_to_cpu(rxframe->chanspec);
Franky Lin83cf17a2013-04-11 13:28:50 +02001867 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001868 u8 *mgmt_frame;
1869 u32 mgmt_frame_len;
1870 s32 freq;
1871 u16 mgmt_type;
1872
1873 brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1874 e->reason);
1875
Hante Meuleman0aedbca2016-02-17 11:26:54 +01001876 if (e->datalen < sizeof(*rxframe)) {
1877 brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1878 return 0;
1879 }
1880
Franky Lin83cf17a2013-04-11 13:28:50 +02001881 ch.chspec = be16_to_cpu(rxframe->chanspec);
1882 cfg->d11inf.decchspec(&ch);
1883
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001884 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
dingtianhong692e5162013-12-26 19:41:34 +08001885 (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
Rafał Miłecki4712d882016-05-20 13:38:57 +02001886 afx_hdl->peer_chan = ch.control_ch_num;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001887 brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
1888 afx_hdl->peer_chan);
1889 complete(&afx_hdl->act_frm_scan);
1890 }
1891
1892 /* Firmware sends us two proberesponses for each idx one. At the */
1893 /* moment anything but bsscfgidx 0 is passed up to supplicant */
1894 if (e->bsscfgidx == 0)
1895 return 0;
1896
1897 /* Filter any P2P probe reqs arriving during the GO-NEG Phase */
1898 if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
1899 brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
1900 return 0;
1901 }
1902
1903 /* Check if wpa_supplicant has registered for this frame */
1904 brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
1905 mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
1906 if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1907 return 0;
1908
1909 mgmt_frame = (u8 *)(rxframe + 1);
1910 mgmt_frame_len = e->datalen - sizeof(*rxframe);
Rafał Miłecki4712d882016-05-20 13:38:57 +02001911 freq = ieee80211_channel_to_frequency(ch.control_ch_num,
Franky Lin83cf17a2013-04-11 13:28:50 +02001912 ch.band == BRCMU_CHAN_BAND_2G ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001913 NL80211_BAND_2GHZ :
1914 NL80211_BAND_5GHZ);
Arend van Sprielbffc61c2013-04-05 10:57:52 +02001915
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07001916 cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len, 0);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001917
1918 brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
1919 mgmt_frame_len, e->datalen, chanspec, freq);
1920
1921 return 0;
1922}
1923
Hante Meuleman18e2f612013-02-08 15:53:49 +01001924
1925/**
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001926 * brcmf_p2p_get_current_chanspec() - Get current operation channel.
1927 *
1928 * @p2p: P2P specific data.
1929 * @chanspec: chanspec to be returned.
1930 */
1931static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
1932 u16 *chanspec)
1933{
1934 struct brcmf_if *ifp;
Hante Meuleman15a953d2013-05-27 21:09:54 +02001935 u8 mac_addr[ETH_ALEN];
Franky Lin83cf17a2013-04-11 13:28:50 +02001936 struct brcmu_chan ch;
Hante Meuleman15a953d2013-05-27 21:09:54 +02001937 struct brcmf_bss_info_le *bi;
1938 u8 *buf;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001939
1940 ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1941
Hante Meuleman15a953d2013-05-27 21:09:54 +02001942 if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mac_addr,
1943 ETH_ALEN) == 0) {
1944 buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
1945 if (buf != NULL) {
1946 *(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
1947 if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
1948 buf, WL_BSS_INFO_MAX) == 0) {
1949 bi = (struct brcmf_bss_info_le *)(buf + 4);
1950 *chanspec = le16_to_cpu(bi->chanspec);
1951 kfree(buf);
1952 return;
1953 }
1954 kfree(buf);
1955 }
1956 }
1957 /* Use default channel for P2P */
1958 ch.chnum = BRCMF_P2P_TEMP_CHAN;
Franky Lin83cf17a2013-04-11 13:28:50 +02001959 ch.bw = BRCMU_CHAN_BW_20;
1960 p2p->cfg->d11inf.encchspec(&ch);
1961 *chanspec = ch.chspec;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001962}
1963
1964/**
1965 * Change a P2P Role.
1966 * Parameters:
1967 * @mac: MAC address of the BSS to change a role
1968 * Returns 0 if success.
1969 */
1970int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
1971 enum brcmf_fil_p2p_if_types if_type)
1972{
1973 struct brcmf_p2p_info *p2p = &cfg->p2p;
1974 struct brcmf_cfg80211_vif *vif;
1975 struct brcmf_fil_p2p_if_le if_request;
1976 s32 err;
1977 u16 chanspec;
1978
1979 brcmf_dbg(TRACE, "Enter\n");
1980
1981 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1982 if (!vif) {
1983 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
1984 return -EPERM;
1985 }
Arend van Spriela0f472a2013-04-05 10:57:49 +02001986 brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001987 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
1988 if (!vif) {
1989 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
1990 return -EPERM;
1991 }
Arend van Sprielf96aa072013-04-05 10:57:48 +02001992 brcmf_set_mpc(vif->ifp, 0);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001993
1994 /* In concurrency case, STA may be already associated in a particular */
1995 /* channel. so retrieve the current channel of primary interface and */
1996 /* then start the virtual interface on that. */
1997 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1998
1999 if_request.type = cpu_to_le16((u16)if_type);
2000 if_request.chspec = cpu_to_le16(chanspec);
2001 memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
2002
2003 brcmf_cfg80211_arm_vif_event(cfg, vif);
2004 err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
2005 sizeof(if_request));
2006 if (err) {
2007 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
2008 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2009 return err;
2010 }
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002011 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_CHANGE,
2012 BRCMF_VIF_EVENT_TIMEOUT);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002013 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2014 if (!err) {
2015 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
2016 return -EIO;
2017 }
2018
2019 err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2020 BRCMF_SCB_TIMEOUT_VALUE);
2021
2022 return err;
2023}
2024
2025static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2026 struct brcmf_if *ifp, u8 ea[ETH_ALEN],
Arend van Sprield3c0b632013-02-08 15:53:37 +01002027 enum brcmf_fil_p2p_if_types iftype)
2028{
2029 struct brcmf_fil_p2p_if_le if_request;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002030 int err;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002031 u16 chanspec;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002032
2033 /* we need a default channel */
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002034 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002035
2036 /* fill the firmware request */
2037 memcpy(if_request.addr, ea, ETH_ALEN);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002038 if_request.type = cpu_to_le16((u16)iftype);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002039 if_request.chspec = cpu_to_le16(chanspec);
2040
2041 err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2042 sizeof(if_request));
Arend van Sprield3c0b632013-02-08 15:53:37 +01002043
Arend van Sprield3c0b632013-02-08 15:53:37 +01002044 return err;
2045}
2046
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002047static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2048{
2049 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2050 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2051 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2052 u8 *addr = vif->wdev.netdev->dev_addr;
2053
2054 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2055}
2056
2057static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2058{
2059 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2060 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2061 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2062 u8 *addr = vif->wdev.netdev->dev_addr;
2063
2064 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2065}
2066
Arend van Spriel9f440b72013-02-08 15:53:36 +01002067/**
Arend van Spriel27f10e32013-04-05 10:57:50 +02002068 * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2069 *
2070 * @p2p: P2P specific data.
2071 * @wiphy: wiphy device of new interface.
2072 * @addr: mac address for this new interface.
2073 */
2074static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2075 struct wiphy *wiphy,
2076 u8 *addr)
2077{
2078 struct brcmf_cfg80211_vif *p2p_vif;
2079 struct brcmf_if *p2p_ifp;
2080 struct brcmf_if *pri_ifp;
2081 int err;
Hante Meuleman37a869e2015-10-29 20:33:17 +01002082 u32 bsscfgidx;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002083
2084 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2085 return ERR_PTR(-ENOSPC);
2086
Rafał Miłecki26072332016-06-06 23:03:55 +02002087 p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002088 if (IS_ERR(p2p_vif)) {
2089 brcmf_err("could not create discovery vif\n");
2090 return (struct wireless_dev *)p2p_vif;
2091 }
2092
Arend van Spriel27f10e32013-04-05 10:57:50 +02002093 pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002094 brcmf_p2p_generate_bss_mac(p2p, addr);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002095 brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2096
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002097 brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
Hante Meulemancb8dc712015-09-18 22:08:18 +02002098 brcmf_fweh_p2pdev_setup(pri_ifp, true);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002099
Arend van Spriel27f10e32013-04-05 10:57:50 +02002100 /* Initialize P2P Discovery in the firmware */
2101 err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2102 if (err < 0) {
2103 brcmf_err("set p2p_disc error\n");
Hante Meulemancb8dc712015-09-18 22:08:18 +02002104 brcmf_fweh_p2pdev_setup(pri_ifp, false);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002105 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2106 goto fail;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002107 }
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002108
2109 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002110 err = brcmf_cfg80211_wait_vif_event(p2p->cfg, BRCMF_E_IF_ADD,
2111 BRCMF_VIF_EVENT_TIMEOUT);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002112 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
Hante Meulemancb8dc712015-09-18 22:08:18 +02002113 brcmf_fweh_p2pdev_setup(pri_ifp, false);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002114 if (!err) {
2115 brcmf_err("timeout occurred\n");
2116 err = -EIO;
2117 goto fail;
2118 }
2119
2120 /* discovery interface created */
2121 p2p_ifp = p2p_vif->ifp;
2122 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2123 memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2124 memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2125
2126 /* verify bsscfg index for P2P discovery */
Hante Meuleman37a869e2015-10-29 20:33:17 +01002127 err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bsscfgidx);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002128 if (err < 0) {
2129 brcmf_err("retrieving discover bsscfg index failed\n");
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002130 goto fail;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002131 }
2132
Hante Meuleman37a869e2015-10-29 20:33:17 +01002133 WARN_ON(p2p_ifp->bsscfgidx != bsscfgidx);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002134
2135 init_completion(&p2p->send_af_done);
2136 INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2137 init_completion(&p2p->afx_hdl.act_frm_scan);
2138 init_completion(&p2p->wait_next_af);
2139
2140 return &p2p_vif->wdev;
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002141
2142fail:
Arend van Spriel427dec52014-01-06 12:40:47 +01002143 brcmf_free_vif(p2p_vif);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002144 return ERR_PTR(err);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002145}
2146
2147/**
Arend van Spriel9f440b72013-02-08 15:53:36 +01002148 * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2149 *
2150 * @wiphy: wiphy device of new interface.
2151 * @name: name of the new interface.
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002152 * @name_assign_type: origin of the interface name
Arend van Spriel9f440b72013-02-08 15:53:36 +01002153 * @type: nl80211 interface type.
Arend van Spriel27f10e32013-04-05 10:57:50 +02002154 * @flags: not used.
2155 * @params: contains mac address for P2P device.
Arend van Spriel9f440b72013-02-08 15:53:36 +01002156 */
2157struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002158 unsigned char name_assign_type,
Arend van Spriel9f440b72013-02-08 15:53:36 +01002159 enum nl80211_iftype type, u32 *flags,
2160 struct vif_params *params)
2161{
Arend van Sprield3c0b632013-02-08 15:53:37 +01002162 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2163 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2164 struct brcmf_cfg80211_vif *vif;
2165 enum brcmf_fil_p2p_if_types iftype;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002166 int err;
2167
2168 if (brcmf_cfg80211_vif_event_armed(cfg))
2169 return ERR_PTR(-EBUSY);
2170
Arend van Spriel9f440b72013-02-08 15:53:36 +01002171 brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002172
2173 switch (type) {
2174 case NL80211_IFTYPE_P2P_CLIENT:
2175 iftype = BRCMF_FIL_P2P_IF_CLIENT;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002176 break;
2177 case NL80211_IFTYPE_P2P_GO:
2178 iftype = BRCMF_FIL_P2P_IF_GO;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002179 break;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002180 case NL80211_IFTYPE_P2P_DEVICE:
2181 return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2182 params->macaddr);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002183 default:
2184 return ERR_PTR(-EOPNOTSUPP);
2185 }
2186
Rafał Miłecki26072332016-06-06 23:03:55 +02002187 vif = brcmf_alloc_vif(cfg, type);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002188 if (IS_ERR(vif))
2189 return (struct wireless_dev *)vif;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002190 brcmf_cfg80211_arm_vif_event(cfg, vif);
2191
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002192 err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
2193 iftype);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002194 if (err) {
2195 brcmf_cfg80211_arm_vif_event(cfg, NULL);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002196 goto fail;
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002197 }
Arend van Sprield3c0b632013-02-08 15:53:37 +01002198
2199 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002200 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_ADD,
2201 BRCMF_VIF_EVENT_TIMEOUT);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002202 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2203 if (!err) {
2204 brcmf_err("timeout occurred\n");
2205 err = -EIO;
2206 goto fail;
2207 }
2208
2209 /* interface created in firmware */
2210 ifp = vif->ifp;
2211 if (!ifp) {
2212 brcmf_err("no if pointer provided\n");
2213 err = -ENOENT;
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002214 goto fail;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002215 }
2216
2217 strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002218 ifp->ndev->name_assign_type = name_assign_type;
Hante Meuleman4b3a89d2013-02-08 15:54:01 +01002219 err = brcmf_net_attach(ifp, true);
2220 if (err) {
2221 brcmf_err("Registering netdevice failed\n");
2222 goto fail;
2223 }
Arend van Sprielcbb371d2013-05-27 21:09:57 +02002224
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002225 cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
2226 /* Disable firmware roaming for P2P interface */
2227 brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2228 if (iftype == BRCMF_FIL_P2P_IF_GO) {
2229 /* set station timeout for p2p */
2230 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2231 BRCMF_SCB_TIMEOUT_VALUE);
2232 }
Arend van Sprield3c0b632013-02-08 15:53:37 +01002233 return &ifp->vif->wdev;
2234
2235fail:
Arend van Spriel427dec52014-01-06 12:40:47 +01002236 brcmf_free_vif(vif);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002237 return ERR_PTR(err);
Arend van Spriel9f440b72013-02-08 15:53:36 +01002238}
2239
2240/**
2241 * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2242 *
2243 * @wiphy: wiphy device of interface.
2244 * @wdev: wireless device of interface.
Arend van Spriel9f440b72013-02-08 15:53:36 +01002245 */
2246int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2247{
Arend van Sprield3c0b632013-02-08 15:53:37 +01002248 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01002249 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002250 struct brcmf_cfg80211_vif *vif;
Arend Van Spriel9afc0762017-03-28 09:11:30 +01002251 enum nl80211_iftype iftype;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002252 bool wait_for_disable = false;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002253 int err;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002254
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002255 brcmf_dbg(TRACE, "delete P2P vif\n");
Arend van Spriel9f440b72013-02-08 15:53:36 +01002256 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002257
Arend Van Spriel9afc0762017-03-28 09:11:30 +01002258 iftype = vif->wdev.iftype;
Hante Meulemana2044d92015-09-18 22:08:16 +02002259 brcmf_cfg80211_arm_vif_event(cfg, vif);
Arend Van Spriel9afc0762017-03-28 09:11:30 +01002260 switch (iftype) {
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002261 case NL80211_IFTYPE_P2P_CLIENT:
2262 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2263 wait_for_disable = true;
2264 break;
2265
2266 case NL80211_IFTYPE_P2P_GO:
2267 if (!brcmf_p2p_disable_p2p_if(vif))
2268 wait_for_disable = true;
2269 break;
2270
2271 case NL80211_IFTYPE_P2P_DEVICE:
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002272 if (!p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2273 return 0;
Arend van Sprielf37d69a2015-06-11 00:12:21 +02002274 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2275 brcmf_p2p_deinit_discovery(p2p);
Rafał Miłecki20856ad2016-06-19 01:55:57 +02002276 break;
2277
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002278 default:
2279 return -ENOTSUPP;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002280 }
2281
Hante Meuleman6eda4e22013-02-08 15:54:02 +01002282 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2283 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2284
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002285 if (wait_for_disable)
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002286 wait_for_completion_timeout(&cfg->vif_disabled,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002287 BRCMF_P2P_DISABLE_TIMEOUT);
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002288
Hante Meulemana2044d92015-09-18 22:08:16 +02002289 err = 0;
Arend Van Spriel9afc0762017-03-28 09:11:30 +01002290 if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
Hante Meulemana2044d92015-09-18 22:08:16 +02002291 brcmf_vif_clear_mgmt_ies(vif);
2292 err = brcmf_p2p_release_p2p_if(vif);
2293 }
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002294 if (!err) {
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002295 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002296 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_DEL,
2297 BRCMF_VIF_EVENT_TIMEOUT);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002298 if (!err)
2299 err = -EIO;
2300 else
2301 err = 0;
2302 }
Rafał Miłeckia63b0982016-06-29 21:54:26 +02002303 brcmf_remove_interface(vif->ifp, true);
Hante Meulemana2044d92015-09-18 22:08:16 +02002304
Arend van Sprield3c0b632013-02-08 15:53:37 +01002305 brcmf_cfg80211_arm_vif_event(cfg, NULL);
Arend Van Spriel9afc0762017-03-28 09:11:30 +01002306 if (iftype != NL80211_IFTYPE_P2P_DEVICE)
Hante Meulemana2044d92015-09-18 22:08:16 +02002307 p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002308
2309 return err;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002310}
Arend van Spriel27f10e32013-04-05 10:57:50 +02002311
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002312void brcmf_p2p_ifp_removed(struct brcmf_if *ifp, bool rtnl_locked)
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002313{
2314 struct brcmf_cfg80211_info *cfg;
2315 struct brcmf_cfg80211_vif *vif;
2316
2317 brcmf_dbg(INFO, "P2P: device interface removed\n");
2318 vif = ifp->vif;
2319 cfg = wdev_to_cfg(&vif->wdev);
2320 cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002321 if (!rtnl_locked)
2322 rtnl_lock();
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002323 cfg80211_unregister_wdev(&vif->wdev);
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002324 if (!rtnl_locked)
2325 rtnl_unlock();
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002326 brcmf_free_vif(vif);
2327}
2328
Arend van Spriel27f10e32013-04-05 10:57:50 +02002329int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2330{
2331 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2332 struct brcmf_p2p_info *p2p = &cfg->p2p;
2333 struct brcmf_cfg80211_vif *vif;
2334 int err;
2335
2336 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2337 mutex_lock(&cfg->usr_sync);
2338 err = brcmf_p2p_enable_discovery(p2p);
2339 if (!err)
2340 set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2341 mutex_unlock(&cfg->usr_sync);
2342 return err;
2343}
2344
2345void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2346{
2347 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2348 struct brcmf_p2p_info *p2p = &cfg->p2p;
2349 struct brcmf_cfg80211_vif *vif;
2350
2351 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
Hante Meulemand1bb34c2015-09-18 22:08:13 +02002352 /* This call can be result of the unregister_wdev call. In that case
2353 * we dont want to do anything anymore. Just return. The config vif
2354 * will have been cleared at this point.
2355 */
2356 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif == vif) {
2357 mutex_lock(&cfg->usr_sync);
Hante Meulemana2044d92015-09-18 22:08:16 +02002358 /* Set the discovery state to SCAN */
2359 (void)brcmf_p2p_set_discover_state(vif->ifp,
2360 WL_P2P_DISC_ST_SCAN, 0, 0);
Hante Meulemand1bb34c2015-09-18 22:08:13 +02002361 brcmf_abort_scanning(cfg);
2362 clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2363 mutex_unlock(&cfg->usr_sync);
2364 }
Arend van Spriel27f10e32013-04-05 10:57:50 +02002365}
Arend van Spriel55479df2015-06-11 00:12:22 +02002366
2367/**
2368 * brcmf_p2p_attach() - attach for P2P.
2369 *
2370 * @cfg: driver private data for cfg80211 interface.
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002371 * @p2pdev_forced: create p2p device interface at attach.
Arend van Spriel55479df2015-06-11 00:12:22 +02002372 */
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002373s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg, bool p2pdev_forced)
Arend van Spriel55479df2015-06-11 00:12:22 +02002374{
Arend van Spriel55479df2015-06-11 00:12:22 +02002375 struct brcmf_p2p_info *p2p;
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002376 struct brcmf_if *pri_ifp;
Arend van Spriel55479df2015-06-11 00:12:22 +02002377 s32 err = 0;
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002378 void *err_ptr;
Arend van Spriel55479df2015-06-11 00:12:22 +02002379
2380 p2p = &cfg->p2p;
2381 p2p->cfg = cfg;
2382
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002383 pri_ifp = brcmf_get_ifp(cfg->pub, 0);
Arend van Spriel55479df2015-06-11 00:12:22 +02002384 p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
2385
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002386 if (p2pdev_forced) {
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002387 err_ptr = brcmf_p2p_create_p2pdev(p2p, NULL, NULL);
2388 if (IS_ERR(err_ptr)) {
2389 brcmf_err("P2P device creation failed.\n");
2390 err = PTR_ERR(err_ptr);
2391 }
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002392 } else {
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002393 p2p->p2pdev_dynamically = true;
2394 }
Arend van Spriel55479df2015-06-11 00:12:22 +02002395 return err;
2396}
2397
2398/**
2399 * brcmf_p2p_detach() - detach P2P.
2400 *
2401 * @p2p: P2P specific data.
2402 */
2403void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
2404{
2405 struct brcmf_cfg80211_vif *vif;
2406
2407 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
2408 if (vif != NULL) {
2409 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2410 brcmf_p2p_deinit_discovery(p2p);
Rafał Miłeckib50ddfa2016-06-17 12:29:21 +02002411 brcmf_remove_interface(vif->ifp, false);
Arend van Spriel55479df2015-06-11 00:12:22 +02002412 }
2413 /* just set it all to zero */
2414 memset(p2p, 0, sizeof(*p2p));
2415}
2416