blob: 2ee54133efa1c05a3f30aacb1491cde4d270dfef [file] [log] [blame]
Arend van Spriel9f440b72013-02-08 15:53:36 +01001/*
2 * Copyright (c) 2012 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16#include <linux/slab.h>
17#include <linux/netdevice.h>
Arend van Spriel27f10e32013-04-05 10:57:50 +020018#include <linux/etherdevice.h>
Arend van Sprielf7a40872015-06-11 00:12:23 +020019#include <linux/rtnetlink.h>
Arend van Spriel9f440b72013-02-08 15:53:36 +010020#include <net/cfg80211.h>
21
22#include <brcmu_wifi.h>
23#include <brcmu_utils.h>
24#include <defs.h>
Hante Meuleman122d3d02014-10-28 14:56:18 +010025#include "core.h"
Hante Meulemana8e8ed32014-10-28 14:56:13 +010026#include "debug.h"
Arend van Spriel9f440b72013-02-08 15:53:36 +010027#include "fwil.h"
Arend van Sprield3c0b632013-02-08 15:53:37 +010028#include "fwil_types.h"
Arend van Spriel9f440b72013-02-08 15:53:36 +010029#include "p2p.h"
Hante Meulemanbfe81972014-10-28 14:56:16 +010030#include "cfg80211.h"
Arend van Spriel9f440b72013-02-08 15:53:36 +010031
32/* parameters used for p2p escan */
33#define P2PAPI_SCAN_NPROBES 1
34#define P2PAPI_SCAN_DWELL_TIME_MS 80
35#define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
36#define P2PAPI_SCAN_HOME_TIME_MS 60
37#define P2PAPI_SCAN_NPROBS_TIME_MS 30
38#define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
39#define WL_SCAN_CONNECT_DWELL_TIME_MS 200
40#define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
41
42#define BRCMF_P2P_WILDCARD_SSID "DIRECT-"
43#define BRCMF_P2P_WILDCARD_SSID_LEN (sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
44
45#define SOCIAL_CHAN_1 1
46#define SOCIAL_CHAN_2 6
47#define SOCIAL_CHAN_3 11
Hante Meuleman6eda4e22013-02-08 15:54:02 +010048#define IS_P2P_SOCIAL_CHANNEL(channel) ((channel == SOCIAL_CHAN_1) || \
49 (channel == SOCIAL_CHAN_2) || \
50 (channel == SOCIAL_CHAN_3))
Hante Meuleman15a953d2013-05-27 21:09:54 +020051#define BRCMF_P2P_TEMP_CHAN SOCIAL_CHAN_3
Arend van Spriel9f440b72013-02-08 15:53:36 +010052#define SOCIAL_CHAN_CNT 3
53#define AF_PEER_SEARCH_CNT 2
54
Arend van Sprield3c0b632013-02-08 15:53:37 +010055#define BRCMF_SCB_TIMEOUT_VALUE 20
56
Hante Meulemane6da3402013-02-08 15:53:48 +010057#define P2P_VER 9 /* P2P version: 9=WiFi P2P v1.0 */
58#define P2P_PUB_AF_CATEGORY 0x04
59#define P2P_PUB_AF_ACTION 0x09
60#define P2P_AF_CATEGORY 0x7f
61#define P2P_OUI "\x50\x6F\x9A" /* P2P OUI */
62#define P2P_OUI_LEN 3 /* P2P OUI length */
63
Hante Meuleman18e2f612013-02-08 15:53:49 +010064/* Action Frame Constants */
65#define DOT11_ACTION_HDR_LEN 2 /* action frame category + action */
66#define DOT11_ACTION_CAT_OFF 0 /* category offset */
67#define DOT11_ACTION_ACT_OFF 1 /* action offset */
68
69#define P2P_AF_DWELL_TIME 200
70#define P2P_AF_MIN_DWELL_TIME 100
71#define P2P_AF_MED_DWELL_TIME 400
72#define P2P_AF_LONG_DWELL_TIME 1000
Hante Meuleman6eda4e22013-02-08 15:54:02 +010073#define P2P_AF_TX_MAX_RETRY 1
Arend van Spriel63ce3d52016-01-05 11:05:48 +010074#define P2P_AF_MAX_WAIT_TIME msecs_to_jiffies(2000)
Hante Meuleman18e2f612013-02-08 15:53:49 +010075#define P2P_INVALID_CHANNEL -1
76#define P2P_CHANNEL_SYNC_RETRY 5
Arend van Spriel63ce3d52016-01-05 11:05:48 +010077#define P2P_AF_FRM_SCAN_MAX_WAIT msecs_to_jiffies(1500)
Hante Meuleman6eda4e22013-02-08 15:54:02 +010078#define P2P_DEFAULT_SLEEP_TIME_VSDB 200
Hante Meuleman18e2f612013-02-08 15:53:49 +010079
Hante Meulemane6da3402013-02-08 15:53:48 +010080/* WiFi P2P Public Action Frame OUI Subtypes */
81#define P2P_PAF_GON_REQ 0 /* Group Owner Negotiation Req */
82#define P2P_PAF_GON_RSP 1 /* Group Owner Negotiation Rsp */
83#define P2P_PAF_GON_CONF 2 /* Group Owner Negotiation Confirm */
84#define P2P_PAF_INVITE_REQ 3 /* P2P Invitation Request */
85#define P2P_PAF_INVITE_RSP 4 /* P2P Invitation Response */
86#define P2P_PAF_DEVDIS_REQ 5 /* Device Discoverability Request */
87#define P2P_PAF_DEVDIS_RSP 6 /* Device Discoverability Response */
88#define P2P_PAF_PROVDIS_REQ 7 /* Provision Discovery Request */
89#define P2P_PAF_PROVDIS_RSP 8 /* Provision Discovery Response */
90#define P2P_PAF_SUBTYPE_INVALID 255 /* Invalid Subtype */
91
92/* WiFi P2P Action Frame OUI Subtypes */
93#define P2P_AF_NOTICE_OF_ABSENCE 0 /* Notice of Absence */
94#define P2P_AF_PRESENCE_REQ 1 /* P2P Presence Request */
95#define P2P_AF_PRESENCE_RSP 2 /* P2P Presence Response */
96#define P2P_AF_GO_DISC_REQ 3 /* GO Discoverability Request */
97
98/* P2P Service Discovery related */
99#define P2PSD_ACTION_CATEGORY 0x04 /* Public action frame */
100#define P2PSD_ACTION_ID_GAS_IREQ 0x0a /* GAS Initial Request AF */
101#define P2PSD_ACTION_ID_GAS_IRESP 0x0b /* GAS Initial Response AF */
102#define P2PSD_ACTION_ID_GAS_CREQ 0x0c /* GAS Comback Request AF */
103#define P2PSD_ACTION_ID_GAS_CRESP 0x0d /* GAS Comback Response AF */
104
Arend van Spriel63ce3d52016-01-05 11:05:48 +0100105#define BRCMF_P2P_DISABLE_TIMEOUT msecs_to_jiffies(500)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100106/**
107 * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
108 *
109 * @state: requested discovery state (see enum brcmf_p2p_disc_state).
110 * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
111 * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
112 */
113struct brcmf_p2p_disc_st_le {
114 u8 state;
115 __le16 chspec;
116 __le16 dwell;
117};
118
119/**
120 * enum brcmf_p2p_disc_state - P2P discovery state values
121 *
122 * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
123 * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
124 * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
125 */
126enum brcmf_p2p_disc_state {
127 WL_P2P_DISC_ST_SCAN,
128 WL_P2P_DISC_ST_LISTEN,
129 WL_P2P_DISC_ST_SEARCH
130};
131
132/**
133 * struct brcmf_p2p_scan_le - P2P specific scan request.
134 *
135 * @type: type of scan method requested (values: 'E' or 'S').
136 * @reserved: reserved (ignored).
137 * @eparams: parameters used for type 'E'.
138 * @sparams: parameters used for type 'S'.
139 */
140struct brcmf_p2p_scan_le {
141 u8 type;
142 u8 reserved[3];
143 union {
144 struct brcmf_escan_params_le eparams;
145 struct brcmf_scan_params_le sparams;
146 };
147};
148
Arend van Spriel9f440b72013-02-08 15:53:36 +0100149/**
Hante Meulemane6da3402013-02-08 15:53:48 +0100150 * struct brcmf_p2p_pub_act_frame - WiFi P2P Public Action Frame
151 *
152 * @category: P2P_PUB_AF_CATEGORY
153 * @action: P2P_PUB_AF_ACTION
154 * @oui[3]: P2P_OUI
155 * @oui_type: OUI type - P2P_VER
156 * @subtype: OUI subtype - P2P_TYPE_*
157 * @dialog_token: nonzero, identifies req/rsp transaction
158 * @elts[1]: Variable length information elements.
159 */
160struct brcmf_p2p_pub_act_frame {
161 u8 category;
162 u8 action;
163 u8 oui[3];
164 u8 oui_type;
165 u8 subtype;
166 u8 dialog_token;
167 u8 elts[1];
168};
169
170/**
171 * struct brcmf_p2p_action_frame - WiFi P2P Action Frame
172 *
173 * @category: P2P_AF_CATEGORY
174 * @OUI[3]: OUI - P2P_OUI
175 * @type: OUI Type - P2P_VER
176 * @subtype: OUI Subtype - P2P_AF_*
177 * @dialog_token: nonzero, identifies req/resp tranaction
178 * @elts[1]: Variable length information elements.
179 */
180struct brcmf_p2p_action_frame {
181 u8 category;
182 u8 oui[3];
183 u8 type;
184 u8 subtype;
185 u8 dialog_token;
186 u8 elts[1];
187};
188
189/**
190 * struct brcmf_p2psd_gas_pub_act_frame - Wi-Fi GAS Public Action Frame
191 *
192 * @category: 0x04 Public Action Frame
193 * @action: 0x6c Advertisement Protocol
194 * @dialog_token: nonzero, identifies req/rsp transaction
195 * @query_data[1]: Query Data. SD gas ireq SD gas iresp
196 */
197struct brcmf_p2psd_gas_pub_act_frame {
198 u8 category;
199 u8 action;
200 u8 dialog_token;
201 u8 query_data[1];
202};
203
Hante Meuleman18e2f612013-02-08 15:53:49 +0100204/**
205 * struct brcmf_config_af_params - Action Frame Parameters for tx.
206 *
Hante Meuleman18e2f612013-02-08 15:53:49 +0100207 * @mpc_onoff: To make sure to send successfully action frame, we have to
208 * turn off mpc 0: off, 1: on, (-1): do nothing
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100209 * @search_channel: 1: search peer's channel to send af
210 * extra_listen: keep the dwell time to get af response frame.
Hante Meuleman18e2f612013-02-08 15:53:49 +0100211 */
212struct brcmf_config_af_params {
Hante Meuleman18e2f612013-02-08 15:53:49 +0100213 s32 mpc_onoff;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100214 bool search_channel;
215 bool extra_listen;
Hante Meuleman18e2f612013-02-08 15:53:49 +0100216};
Hante Meulemane6da3402013-02-08 15:53:48 +0100217
218/**
219 * brcmf_p2p_is_pub_action() - true if p2p public type frame.
220 *
221 * @frame: action frame data.
222 * @frame_len: length of action frame data.
223 *
224 * Determine if action frame is p2p public action type
225 */
226static bool brcmf_p2p_is_pub_action(void *frame, u32 frame_len)
227{
228 struct brcmf_p2p_pub_act_frame *pact_frm;
229
230 if (frame == NULL)
231 return false;
232
233 pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
234 if (frame_len < sizeof(struct brcmf_p2p_pub_act_frame) - 1)
235 return false;
236
237 if (pact_frm->category == P2P_PUB_AF_CATEGORY &&
238 pact_frm->action == P2P_PUB_AF_ACTION &&
239 pact_frm->oui_type == P2P_VER &&
240 memcmp(pact_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
241 return true;
242
243 return false;
244}
245
246/**
247 * brcmf_p2p_is_p2p_action() - true if p2p action type frame.
248 *
249 * @frame: action frame data.
250 * @frame_len: length of action frame data.
251 *
252 * Determine if action frame is p2p action type
253 */
254static bool brcmf_p2p_is_p2p_action(void *frame, u32 frame_len)
255{
256 struct brcmf_p2p_action_frame *act_frm;
257
258 if (frame == NULL)
259 return false;
260
261 act_frm = (struct brcmf_p2p_action_frame *)frame;
262 if (frame_len < sizeof(struct brcmf_p2p_action_frame) - 1)
263 return false;
264
265 if (act_frm->category == P2P_AF_CATEGORY &&
266 act_frm->type == P2P_VER &&
267 memcmp(act_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
268 return true;
269
270 return false;
271}
272
273/**
274 * brcmf_p2p_is_gas_action() - true if p2p gas action type frame.
275 *
276 * @frame: action frame data.
277 * @frame_len: length of action frame data.
278 *
279 * Determine if action frame is p2p gas action type
280 */
281static bool brcmf_p2p_is_gas_action(void *frame, u32 frame_len)
282{
283 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
284
285 if (frame == NULL)
286 return false;
287
288 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
289 if (frame_len < sizeof(struct brcmf_p2psd_gas_pub_act_frame) - 1)
290 return false;
291
292 if (sd_act_frm->category != P2PSD_ACTION_CATEGORY)
293 return false;
294
295 if (sd_act_frm->action == P2PSD_ACTION_ID_GAS_IREQ ||
296 sd_act_frm->action == P2PSD_ACTION_ID_GAS_IRESP ||
297 sd_act_frm->action == P2PSD_ACTION_ID_GAS_CREQ ||
298 sd_act_frm->action == P2PSD_ACTION_ID_GAS_CRESP)
299 return true;
300
301 return false;
302}
303
304/**
305 * brcmf_p2p_print_actframe() - debug print routine.
306 *
307 * @tx: Received or to be transmitted
308 * @frame: action frame data.
309 * @frame_len: length of action frame data.
310 *
311 * Print information about the p2p action frame
312 */
Hante Meuleman0a4cf482013-02-08 15:53:54 +0100313
314#ifdef DEBUG
315
Hante Meulemane6da3402013-02-08 15:53:48 +0100316static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
317{
318 struct brcmf_p2p_pub_act_frame *pact_frm;
319 struct brcmf_p2p_action_frame *act_frm;
320 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
321
322 if (!frame || frame_len <= 2)
323 return;
324
325 if (brcmf_p2p_is_pub_action(frame, frame_len)) {
326 pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
327 switch (pact_frm->subtype) {
328 case P2P_PAF_GON_REQ:
329 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
330 (tx) ? "TX" : "RX");
331 break;
332 case P2P_PAF_GON_RSP:
333 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
334 (tx) ? "TX" : "RX");
335 break;
336 case P2P_PAF_GON_CONF:
337 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
338 (tx) ? "TX" : "RX");
339 break;
340 case P2P_PAF_INVITE_REQ:
341 brcmf_dbg(TRACE, "%s P2P Invitation Request Frame\n",
342 (tx) ? "TX" : "RX");
343 break;
344 case P2P_PAF_INVITE_RSP:
345 brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
346 (tx) ? "TX" : "RX");
347 break;
348 case P2P_PAF_DEVDIS_REQ:
349 brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
350 (tx) ? "TX" : "RX");
351 break;
352 case P2P_PAF_DEVDIS_RSP:
353 brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
354 (tx) ? "TX" : "RX");
355 break;
356 case P2P_PAF_PROVDIS_REQ:
357 brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
358 (tx) ? "TX" : "RX");
359 break;
360 case P2P_PAF_PROVDIS_RSP:
361 brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
362 (tx) ? "TX" : "RX");
363 break;
364 default:
365 brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
366 (tx) ? "TX" : "RX");
367 break;
368 }
369 } else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
370 act_frm = (struct brcmf_p2p_action_frame *)frame;
371 switch (act_frm->subtype) {
372 case P2P_AF_NOTICE_OF_ABSENCE:
373 brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
374 (tx) ? "TX" : "RX");
375 break;
376 case P2P_AF_PRESENCE_REQ:
377 brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
378 (tx) ? "TX" : "RX");
379 break;
380 case P2P_AF_PRESENCE_RSP:
381 brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
382 (tx) ? "TX" : "RX");
383 break;
384 case P2P_AF_GO_DISC_REQ:
385 brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
386 (tx) ? "TX" : "RX");
387 break;
388 default:
389 brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
390 (tx) ? "TX" : "RX");
391 }
392
393 } else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
394 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
395 switch (sd_act_frm->action) {
396 case P2PSD_ACTION_ID_GAS_IREQ:
397 brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
398 (tx) ? "TX" : "RX");
399 break;
400 case P2PSD_ACTION_ID_GAS_IRESP:
401 brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
402 (tx) ? "TX" : "RX");
403 break;
404 case P2PSD_ACTION_ID_GAS_CREQ:
405 brcmf_dbg(TRACE, "%s P2P GAS Comback Request\n",
406 (tx) ? "TX" : "RX");
407 break;
408 case P2PSD_ACTION_ID_GAS_CRESP:
409 brcmf_dbg(TRACE, "%s P2P GAS Comback Response\n",
410 (tx) ? "TX" : "RX");
411 break;
412 default:
413 brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
414 (tx) ? "TX" : "RX");
415 break;
416 }
417 }
418}
419
Hante Meuleman0a4cf482013-02-08 15:53:54 +0100420#else
421
422static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
423{
424}
425
426#endif
427
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100428
429/**
Arend van Spriel9f440b72013-02-08 15:53:36 +0100430 * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
431 *
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100432 * @ifp: ifp to use for iovars (primary).
433 * @p2p_mac: mac address to configure for p2p_da_override
Arend van Spriel9f440b72013-02-08 15:53:36 +0100434 */
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100435static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100436{
Arend van Spriel9f440b72013-02-08 15:53:36 +0100437 s32 ret = 0;
438
Arend van Spriel27f10e32013-04-05 10:57:50 +0200439 brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100440 brcmf_fil_iovar_int_set(ifp, "apsta", 1);
Arend van Spriel27f10e32013-04-05 10:57:50 +0200441 brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100442
443 /* In case of COB type, firmware has default mac address
444 * After Initializing firmware, we have to set current mac address to
Arend van Sprielf48556e2014-09-30 10:23:16 +0200445 * firmware for P2P device address. This must be done with discovery
446 * disabled.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100447 */
Arend van Sprielf48556e2014-09-30 10:23:16 +0200448 brcmf_fil_iovar_int_set(ifp, "p2p_disc", 0);
449
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100450 ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
451 ETH_ALEN);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100452 if (ret)
453 brcmf_err("failed to update device address ret %d\n", ret);
454
455 return ret;
456}
457
458/**
459 * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
460 *
461 * @p2p: P2P specific data.
Arend van Spriel27f10e32013-04-05 10:57:50 +0200462 * @dev_addr: optional device address.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100463 *
Arend van Spriel27f10e32013-04-05 10:57:50 +0200464 * P2P needs mac addresses for P2P device and interface. If no device
465 * address it specified, these are derived from the primary net device, ie.
466 * the permanent ethernet address of the device.
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{
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100470 struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Arend van Spriel27f10e32013-04-05 10:57:50 +0200471 bool local_admin = false;
472
473 if (!dev_addr || is_zero_ether_addr(dev_addr)) {
474 dev_addr = pri_ifp->mac_addr;
475 local_admin = true;
476 }
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100477
Arend van Spriel9f440b72013-02-08 15:53:36 +0100478 /* Generate the P2P Device Address. This consists of the device's
479 * primary MAC address with the locally administered bit set.
480 */
Arend van Spriel27f10e32013-04-05 10:57:50 +0200481 memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
482 if (local_admin)
483 p2p->dev_addr[0] |= 0x02;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100484
485 /* Generate the P2P Interface Address. If the discovery and connection
486 * BSSCFGs need to simultaneously co-exist, then this address must be
487 * different from the P2P Device Address, but also locally administered.
488 */
489 memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
Arend van Spriel27f10e32013-04-05 10:57:50 +0200490 p2p->int_addr[0] |= 0x02;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100491 p2p->int_addr[4] ^= 0x80;
492}
493
494/**
495 * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
496 *
497 * @request: the scan request as received from cfg80211.
498 *
499 * returns true if one of the ssids in the request matches the
500 * P2P wildcard ssid; otherwise returns false.
501 */
502static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
503{
504 struct cfg80211_ssid *ssids = request->ssids;
505 int i;
506
507 for (i = 0; i < request->n_ssids; i++) {
508 if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
509 continue;
510
511 brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
512 if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
513 BRCMF_P2P_WILDCARD_SSID_LEN))
514 return true;
515 }
516 return false;
517}
518
519/**
520 * brcmf_p2p_set_discover_state - set discover state in firmware.
521 *
522 * @ifp: low-level interface object.
523 * @state: discover state to set.
524 * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
525 * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
526 */
527static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
528 u16 chanspec, u16 listen_ms)
529{
530 struct brcmf_p2p_disc_st_le discover_state;
531 s32 ret = 0;
532 brcmf_dbg(TRACE, "enter\n");
533
534 discover_state.state = state;
535 discover_state.chspec = cpu_to_le16(chanspec);
536 discover_state.dwell = cpu_to_le16(listen_ms);
537 ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
538 sizeof(discover_state));
539 return ret;
540}
541
542/**
Arend van Spriel9f440b72013-02-08 15:53:36 +0100543 * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
544 *
545 * @p2p: P2P specific data.
546 *
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100547 * Resets the discovery state and disables it in firmware.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100548 */
549static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
550{
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100551 struct brcmf_cfg80211_vif *vif;
552
Arend van Spriel9f440b72013-02-08 15:53:36 +0100553 brcmf_dbg(TRACE, "enter\n");
554
Arend van Spriel9f440b72013-02-08 15:53:36 +0100555 /* Set the discovery state to SCAN */
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100556 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
557 (void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100558
559 /* Disable P2P discovery in the firmware */
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100560 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
561 (void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100562
563 return 0;
564}
565
566/**
567 * brcmf_p2p_enable_discovery() - initialize and configure discovery.
568 *
569 * @p2p: P2P specific data.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100570 *
571 * Initializes the discovery device and configure the virtual interface.
572 */
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100573static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100574{
575 struct brcmf_cfg80211_vif *vif;
576 s32 ret = 0;
577
578 brcmf_dbg(TRACE, "enter\n");
579 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100580 if (!vif) {
581 brcmf_err("P2P config device not available\n");
582 ret = -EPERM;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100583 goto exit;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100584 }
585
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100586 if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
587 brcmf_dbg(INFO, "P2P config device already configured\n");
588 goto exit;
589 }
590
591 /* Re-initialize P2P Discovery in the firmware */
592 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
593 ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100594 if (ret < 0) {
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100595 brcmf_err("set p2p_disc error\n");
Arend van Spriel9f440b72013-02-08 15:53:36 +0100596 goto exit;
597 }
Arend van Spriel9f440b72013-02-08 15:53:36 +0100598 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100599 ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
600 if (ret < 0) {
601 brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
602 goto exit;
603 }
Arend van Spriel9f440b72013-02-08 15:53:36 +0100604
605 /*
606 * Set wsec to any non-zero value in the discovery bsscfg
607 * to ensure our P2P probe responses have the privacy bit
608 * set in the 802.11 WPA IE. Some peer devices may not
609 * initiate WPS with us if this bit is not set.
610 */
611 ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100612 if (ret < 0) {
Arend van Spriel9f440b72013-02-08 15:53:36 +0100613 brcmf_err("wsec error %d\n", ret);
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100614 goto exit;
615 }
Arend van Spriel9f440b72013-02-08 15:53:36 +0100616
Hante Meuleman2fde59d2013-02-08 15:53:52 +0100617 set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100618exit:
619 return ret;
620}
621
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100622/**
Arend van Spriel9f440b72013-02-08 15:53:36 +0100623 * brcmf_p2p_escan() - initiate a P2P scan.
624 *
625 * @p2p: P2P specific data.
626 * @num_chans: number of channels to scan.
627 * @chanspecs: channel parameters for @num_chans channels.
628 * @search_state: P2P discover state to use.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100629 * @bss_type: type of P2P bss.
630 */
631static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
Hante Meulemanc4958102015-11-25 11:32:41 +0100632 u16 chanspecs[], s32 search_state,
Arend van Spriel9f440b72013-02-08 15:53:36 +0100633 enum p2p_bss_type bss_type)
634{
635 s32 ret = 0;
636 s32 memsize = offsetof(struct brcmf_p2p_scan_le,
637 eparams.params_le.channel_list);
638 s32 nprobes;
639 s32 active;
640 u32 i;
641 u8 *memblk;
642 struct brcmf_cfg80211_vif *vif;
643 struct brcmf_p2p_scan_le *p2p_params;
644 struct brcmf_scan_params_le *sparams;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100645
Arend van Spriel9f440b72013-02-08 15:53:36 +0100646 memsize += num_chans * sizeof(__le16);
647 memblk = kzalloc(memsize, GFP_KERNEL);
648 if (!memblk)
649 return -ENOMEM;
650
651 vif = p2p->bss_idx[bss_type].vif;
652 if (vif == NULL) {
653 brcmf_err("no vif for bss type %d\n", bss_type);
654 ret = -EINVAL;
655 goto exit;
656 }
Hante Meulemane9a6ca82015-11-25 11:32:37 +0100657 p2p_params = (struct brcmf_p2p_scan_le *)memblk;
658 sparams = &p2p_params->eparams.params_le;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100659
660 switch (search_state) {
661 case WL_P2P_DISC_ST_SEARCH:
662 /*
663 * If we in SEARCH STATE, we don't need to set SSID explictly
Hante Meulemane9a6ca82015-11-25 11:32:37 +0100664 * because dongle use P2P WILDCARD internally by default, use
665 * null ssid, which it is already due to kzalloc.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100666 */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100667 break;
668 case WL_P2P_DISC_ST_SCAN:
669 /*
670 * wpa_supplicant has p2p_find command with type social or
671 * progressive. For progressive, we need to set the ssid to
672 * P2P WILDCARD because we just do broadcast scan unless
673 * setting SSID.
674 */
Hante Meulemane9a6ca82015-11-25 11:32:37 +0100675 sparams->ssid_le.SSID_len =
676 cpu_to_le32(BRCMF_P2P_WILDCARD_SSID_LEN);
677 memcpy(sparams->ssid_le.SSID, BRCMF_P2P_WILDCARD_SSID,
678 BRCMF_P2P_WILDCARD_SSID_LEN);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100679 break;
680 default:
681 brcmf_err(" invalid search state %d\n", search_state);
682 ret = -EINVAL;
683 goto exit;
684 }
685
686 brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
687
688 /*
689 * set p2p scan parameters.
690 */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100691 p2p_params->type = 'E';
692
693 /* determine the scan engine parameters */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100694 sparams->bss_type = DOT11_BSSTYPE_ANY;
Arend Van Sprielbbf35412017-11-08 14:36:36 +0100695 sparams->scan_type = BRCMF_SCANTYPE_ACTIVE;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100696
Joe Perches93803b32015-03-02 19:54:49 -0800697 eth_broadcast_addr(sparams->bssid);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100698 sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
699
700 /*
701 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
702 * supported by the supplicant.
703 */
704 if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
705 active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
706 else if (num_chans == AF_PEER_SEARCH_CNT)
707 active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
Rasmus Villemoese843bb12014-06-22 20:50:40 +0200708 else if (brcmf_get_vif_state_any(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
Arend van Spriel9f440b72013-02-08 15:53:36 +0100709 active = -1;
710 else
711 active = P2PAPI_SCAN_DWELL_TIME_MS;
712
713 /* Override scan params to find a peer for a connection */
714 if (num_chans == 1) {
715 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
Hante Meuleman18e2f612013-02-08 15:53:49 +0100716 /* WAR to sync with presence period of VSDB GO.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100717 * send probe request more frequently
718 */
719 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
720 } else {
721 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
722 }
723
724 if (nprobes <= 0)
725 nprobes = 1;
726
727 brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
728 sparams->active_time = cpu_to_le32(active);
729 sparams->nprobes = cpu_to_le32(nprobes);
730 sparams->passive_time = cpu_to_le32(-1);
731 sparams->channel_num = cpu_to_le32(num_chans &
732 BRCMF_SCAN_PARAMS_COUNT_MASK);
733 for (i = 0; i < num_chans; i++)
734 sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
735
736 /* set the escan specific parameters */
737 p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
Hante Meulemanc4958102015-11-25 11:32:41 +0100738 p2p_params->eparams.action = cpu_to_le16(WL_ESCAN_ACTION_START);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100739 p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
740 /* perform p2p scan on primary device */
741 ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
742 if (!ret)
743 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
744exit:
745 kfree(memblk);
746 return ret;
747}
748
749/**
750 * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
751 *
752 * @cfg: driver private data for cfg80211 interface.
753 * @ndev: net device for which scan is requested.
754 * @request: scan request from cfg80211.
755 * @action: scan action.
756 *
757 * Determines the P2P discovery state based to scan request parameters and
758 * validates the channels in the request.
759 */
760static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
Arend van Spriela0f472a2013-04-05 10:57:49 +0200761 struct brcmf_if *ifp,
Hante Meulemanc4958102015-11-25 11:32:41 +0100762 struct cfg80211_scan_request *request)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100763{
764 struct brcmf_p2p_info *p2p = &cfg->p2p;
765 s32 err = 0;
766 s32 search_state = WL_P2P_DISC_ST_SCAN;
767 struct brcmf_cfg80211_vif *vif;
768 struct net_device *dev = NULL;
769 int i, num_nodfs = 0;
770 u16 *chanspecs;
771
772 brcmf_dbg(TRACE, "enter\n");
773
774 if (!request) {
775 err = -EINVAL;
776 goto exit;
777 }
778
779 if (request->n_channels) {
780 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
781 GFP_KERNEL);
782 if (!chanspecs) {
783 err = -ENOMEM;
784 goto exit;
785 }
786 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
787 if (vif)
788 dev = vif->wdev.netdev;
789 if (request->n_channels == 3 &&
790 request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
791 request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
792 request->channels[2]->hw_value == SOCIAL_CHAN_3) {
793 /* SOCIAL CHANNELS 1, 6, 11 */
794 search_state = WL_P2P_DISC_ST_SEARCH;
795 brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
Arend van Spriel967fe2c2014-03-15 17:18:21 +0100796 } else if (dev != NULL &&
797 vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
Arend van Spriel9f440b72013-02-08 15:53:36 +0100798 /* If you are already a GO, then do SEARCH only */
799 brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
800 search_state = WL_P2P_DISC_ST_SEARCH;
801 } else {
802 brcmf_dbg(INFO, "P2P SCAN STATE START\n");
803 }
804
805 /*
806 * no P2P scanning on passive or DFS channels.
807 */
808 for (i = 0; i < request->n_channels; i++) {
809 struct ieee80211_channel *chan = request->channels[i];
810
811 if (chan->flags & (IEEE80211_CHAN_RADAR |
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200812 IEEE80211_CHAN_NO_IR))
Arend van Spriel9f440b72013-02-08 15:53:36 +0100813 continue;
814
Franky Lin83cf17a2013-04-11 13:28:50 +0200815 chanspecs[i] = channel_to_chanspec(&p2p->cfg->d11inf,
816 chan);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100817 brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
818 num_nodfs, chan->hw_value, chanspecs[i]);
819 num_nodfs++;
820 }
821 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
Hante Meulemanc4958102015-11-25 11:32:41 +0100822 P2PAPI_BSSCFG_DEVICE);
Geyslan G. Bem3b1bace2013-11-15 09:21:47 -0300823 kfree(chanspecs);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100824 }
825exit:
826 if (err)
827 brcmf_err("error (%d)\n", err);
828 return err;
829}
830
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100831
832/**
833 * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
834 *
835 * @ie: string of information elements.
836 * @ie_len: length of string.
837 *
838 * Scan ie for p2p ie and look for attribute 6 channel. If available determine
839 * channel and return it.
840 */
841static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
842{
843 u8 channel_ie[5];
844 s32 listen_channel;
845 s32 err;
846
847 err = cfg80211_get_p2p_attr(ie, ie_len,
848 IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
849 channel_ie, sizeof(channel_ie));
850 if (err < 0)
851 return err;
852
853 /* listen channel subel length format: */
854 /* 3(country) + 1(op. class) + 1(chan num) */
855 listen_channel = (s32)channel_ie[3 + 1];
856
857 if (listen_channel == SOCIAL_CHAN_1 ||
858 listen_channel == SOCIAL_CHAN_2 ||
859 listen_channel == SOCIAL_CHAN_3) {
860 brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
861 return listen_channel;
862 }
863
864 return -EPERM;
865}
866
867
Arend van Spriel9f440b72013-02-08 15:53:36 +0100868/**
869 * brcmf_p2p_scan_prep() - prepare scan based on request.
870 *
871 * @wiphy: wiphy device.
872 * @request: scan request from cfg80211.
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100873 * @vif: vif on which scan request is to be executed.
Arend van Spriel9f440b72013-02-08 15:53:36 +0100874 *
875 * Prepare the scan appropriately for type of scan requested. Overrides the
876 * escan .run() callback for peer-to-peer scanning.
877 */
878int brcmf_p2p_scan_prep(struct wiphy *wiphy,
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100879 struct cfg80211_scan_request *request,
880 struct brcmf_cfg80211_vif *vif)
Arend van Spriel9f440b72013-02-08 15:53:36 +0100881{
882 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
883 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend Van Sprielbd99a302017-11-08 14:36:37 +0100884 int err;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100885
886 if (brcmf_p2p_scan_is_p2p_request(request)) {
887 /* find my listen channel */
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100888 err = brcmf_p2p_find_listen_channel(request->ie,
889 request->ie_len);
Arend van Spriel9f440b72013-02-08 15:53:36 +0100890 if (err < 0)
891 return err;
892
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100893 p2p->afx_hdl.my_listen_chan = err;
894
Arend van Spriel9f440b72013-02-08 15:53:36 +0100895 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
896 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
897
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100898 err = brcmf_p2p_enable_discovery(p2p);
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100899 if (err)
900 return err;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100901
Hante Meuleman0f8ffe12013-02-08 15:53:42 +0100902 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
903
904 /* override .run_escan() callback. */
Arend van Spriel9f440b72013-02-08 15:53:36 +0100905 cfg->escan_info.run = brcmf_p2p_run_escan;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100906 }
Arend Van Sprielbd99a302017-11-08 14:36:37 +0100907 return 0;
Arend van Spriel9f440b72013-02-08 15:53:36 +0100908}
909
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100910
911/**
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100912 * brcmf_p2p_discover_listen() - set firmware to discover listen state.
913 *
914 * @p2p: p2p device.
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100915 * @channel: channel nr for discover listen.
916 * @duration: time in ms to stay on channel.
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100917 *
918 */
919static s32
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100920brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100921{
922 struct brcmf_cfg80211_vif *vif;
Franky Lin83cf17a2013-04-11 13:28:50 +0200923 struct brcmu_chan ch;
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100924 s32 err = 0;
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100925
926 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
927 if (!vif) {
928 brcmf_err("Discovery is not set, so we have nothing to do\n");
929 err = -EPERM;
930 goto exit;
931 }
932
933 if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
934 brcmf_err("Previous LISTEN is not completed yet\n");
935 /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
936 goto exit;
937 }
938
Franky Lin83cf17a2013-04-11 13:28:50 +0200939 ch.chnum = channel;
940 ch.bw = BRCMU_CHAN_BW_20;
941 p2p->cfg->d11inf.encchspec(&ch);
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100942 err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
Franky Lin83cf17a2013-04-11 13:28:50 +0200943 ch.chspec, (u16)duration);
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100944 if (!err) {
945 set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
946 p2p->remain_on_channel_cookie++;
947 }
948exit:
949 return err;
950}
951
952
953/**
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100954 * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
955 *
956 * @wiphy: wiphy device.
957 * @channel: channel to stay on.
958 * @duration: time in ms to remain on channel.
959 *
960 */
961int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
962 struct ieee80211_channel *channel,
963 unsigned int duration, u64 *cookie)
964{
965 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
966 struct brcmf_p2p_info *p2p = &cfg->p2p;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100967 s32 err;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100968 u16 channel_nr;
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100969
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100970 channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
971 brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100972 duration);
973
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100974 err = brcmf_p2p_enable_discovery(p2p);
975 if (err)
976 goto exit;
Hante Meuleman6eda4e22013-02-08 15:54:02 +0100977 err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100978 if (err)
979 goto exit;
980
Hante Meulemanf2058dd2013-02-08 15:53:50 +0100981 memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
982 *cookie = p2p->remain_on_channel_cookie;
Arend van Spriel1ce30862013-02-08 15:53:51 +0100983 cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100984
985exit:
Hante Meuleman0de8aac2013-02-08 15:53:38 +0100986 return err;
987}
988
989
990/**
991 * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
992 *
993 * @ifp: interfac control.
994 * @e: event message. Not used, to make it usable for fweh event dispatcher.
995 * @data: payload of message. Not used.
996 *
997 */
998int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
999 const struct brcmf_event_msg *e,
1000 void *data)
1001{
1002 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1003 struct brcmf_p2p_info *p2p = &cfg->p2p;
1004
1005 brcmf_dbg(TRACE, "Enter\n");
Hante Meulemanf2058dd2013-02-08 15:53:50 +01001006 if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001007 &p2p->status)) {
1008 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1009 &p2p->status)) {
1010 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1011 &p2p->status);
1012 brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1013 complete(&p2p->wait_next_af);
1014 }
1015
Hante Meulemanf2058dd2013-02-08 15:53:50 +01001016 cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1017 p2p->remain_on_channel_cookie,
Hante Meuleman0de8aac2013-02-08 15:53:38 +01001018 &p2p->remain_on_channel,
1019 GFP_KERNEL);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001020 }
Hante Meuleman0de8aac2013-02-08 15:53:38 +01001021 return 0;
1022}
1023
1024
1025/**
1026 * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1027 *
1028 * @ifp: interfac control.
1029 *
1030 */
1031void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1032{
1033 if (!ifp)
1034 return;
1035 brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1036 brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1037}
1038
1039
Arend van Spriel9f440b72013-02-08 15:53:36 +01001040/**
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001041 * brcmf_p2p_act_frm_search() - search function for action frame.
1042 *
1043 * @p2p: p2p device.
1044 * channel: channel on which action frame is to be trasmitted.
1045 *
1046 * search function to reach at common channel to send action frame. When
1047 * channel is 0 then all social channels will be used to send af
1048 */
1049static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1050{
1051 s32 err;
1052 u32 channel_cnt;
1053 u16 *default_chan_list;
1054 u32 i;
Franky Lin83cf17a2013-04-11 13:28:50 +02001055 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001056
1057 brcmf_dbg(TRACE, "Enter\n");
1058
1059 if (channel)
1060 channel_cnt = AF_PEER_SEARCH_CNT;
1061 else
1062 channel_cnt = SOCIAL_CHAN_CNT;
1063 default_chan_list = kzalloc(channel_cnt * sizeof(*default_chan_list),
1064 GFP_KERNEL);
1065 if (default_chan_list == NULL) {
1066 brcmf_err("channel list allocation failed\n");
1067 err = -ENOMEM;
1068 goto exit;
1069 }
Franky Lin83cf17a2013-04-11 13:28:50 +02001070 ch.bw = BRCMU_CHAN_BW_20;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001071 if (channel) {
Franky Lin83cf17a2013-04-11 13:28:50 +02001072 ch.chnum = channel;
1073 p2p->cfg->d11inf.encchspec(&ch);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001074 /* insert same channel to the chan_list */
1075 for (i = 0; i < channel_cnt; i++)
Franky Lin83cf17a2013-04-11 13:28:50 +02001076 default_chan_list[i] = ch.chspec;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001077 } else {
Franky Lin83cf17a2013-04-11 13:28:50 +02001078 ch.chnum = SOCIAL_CHAN_1;
1079 p2p->cfg->d11inf.encchspec(&ch);
1080 default_chan_list[0] = ch.chspec;
1081 ch.chnum = SOCIAL_CHAN_2;
1082 p2p->cfg->d11inf.encchspec(&ch);
1083 default_chan_list[1] = ch.chspec;
1084 ch.chnum = SOCIAL_CHAN_3;
1085 p2p->cfg->d11inf.encchspec(&ch);
1086 default_chan_list[2] = ch.chspec;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001087 }
1088 err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
Hante Meulemanc4958102015-11-25 11:32:41 +01001089 WL_P2P_DISC_ST_SEARCH, P2PAPI_BSSCFG_DEVICE);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001090 kfree(default_chan_list);
1091exit:
1092 return err;
1093}
1094
1095
1096/**
1097 * brcmf_p2p_afx_handler() - afx worker thread.
1098 *
1099 * @work:
1100 *
1101 */
1102static void brcmf_p2p_afx_handler(struct work_struct *work)
1103{
1104 struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
1105 struct brcmf_p2p_info *p2p = container_of(afx_hdl,
1106 struct brcmf_p2p_info,
1107 afx_hdl);
1108 s32 err;
1109
1110 if (!afx_hdl->is_active)
1111 return;
1112
1113 if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1114 /* 100ms ~ 300ms */
1115 err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
Akinobu Mitae00adf32013-05-07 16:18:15 -07001116 100 * (1 + prandom_u32() % 3));
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001117 else
1118 err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1119
1120 if (err) {
1121 brcmf_err("ERROR occurred! value is (%d)\n", err);
1122 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1123 &p2p->status))
1124 complete(&afx_hdl->act_frm_scan);
1125 }
1126}
1127
1128
1129/**
1130 * brcmf_p2p_af_searching_channel() - search channel.
1131 *
1132 * @p2p: p2p device info struct.
1133 *
1134 */
1135static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1136{
1137 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1138 struct brcmf_cfg80211_vif *pri_vif;
1139 unsigned long duration;
1140 s32 retry;
1141
1142 brcmf_dbg(TRACE, "Enter\n");
1143
1144 pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1145
Wolfram Sang16735d02013-11-14 14:32:02 -08001146 reinit_completion(&afx_hdl->act_frm_scan);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001147 set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1148 afx_hdl->is_active = true;
1149 afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
1150
1151 /* Loop to wait until we find a peer's channel or the
1152 * pending action frame tx is cancelled.
1153 */
1154 retry = 0;
1155 duration = msecs_to_jiffies(P2P_AF_FRM_SCAN_MAX_WAIT);
1156 while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
1157 (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
1158 afx_hdl->is_listen = false;
1159 brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
1160 retry);
1161 /* search peer on peer's listen channel */
1162 schedule_work(&afx_hdl->afx_work);
1163 wait_for_completion_timeout(&afx_hdl->act_frm_scan, duration);
1164 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1165 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1166 &p2p->status)))
1167 break;
1168
1169 if (afx_hdl->my_listen_chan) {
1170 brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
1171 afx_hdl->my_listen_chan);
1172 /* listen on my listen channel */
1173 afx_hdl->is_listen = true;
1174 schedule_work(&afx_hdl->afx_work);
1175 wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1176 duration);
1177 }
1178 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1179 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1180 &p2p->status)))
1181 break;
1182 retry++;
1183
1184 /* if sta is connected or connecting, sleep for a while before
1185 * retry af tx or finding a peer
1186 */
1187 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
1188 test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
1189 msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
1190 }
1191
1192 brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1193 afx_hdl->peer_chan);
1194 afx_hdl->is_active = false;
1195
1196 clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1197
1198 return afx_hdl->peer_chan;
1199}
1200
1201
1202/**
1203 * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1204 *
1205 * @cfg: common configuration struct.
1206 * @bi: bss info struct, result from scan.
1207 *
1208 */
1209bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1210 struct brcmf_bss_info_le *bi)
1211
1212{
1213 struct brcmf_p2p_info *p2p = &cfg->p2p;
1214 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Franky Lin83cf17a2013-04-11 13:28:50 +02001215 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001216 u8 *ie;
1217 s32 err;
1218 u8 p2p_dev_addr[ETH_ALEN];
1219
1220 if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
1221 return false;
1222
1223 if (bi == NULL) {
1224 brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
1225 if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
1226 complete(&afx_hdl->act_frm_scan);
1227 return true;
1228 }
1229
1230 ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
1231 memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
1232 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1233 IEEE80211_P2P_ATTR_DEVICE_INFO,
1234 p2p_dev_addr, sizeof(p2p_dev_addr));
1235 if (err < 0)
1236 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1237 IEEE80211_P2P_ATTR_DEVICE_ID,
1238 p2p_dev_addr, sizeof(p2p_dev_addr));
1239 if ((err >= 0) &&
dingtianhong692e5162013-12-26 19:41:34 +08001240 (ether_addr_equal(p2p_dev_addr, afx_hdl->tx_dst_addr))) {
Franky Lin83cf17a2013-04-11 13:28:50 +02001241 if (!bi->ctl_ch) {
1242 ch.chspec = le16_to_cpu(bi->chanspec);
1243 cfg->d11inf.decchspec(&ch);
Rafał Miłecki4712d882016-05-20 13:38:57 +02001244 bi->ctl_ch = ch.control_ch_num;
Franky Lin83cf17a2013-04-11 13:28:50 +02001245 }
1246 afx_hdl->peer_chan = bi->ctl_ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001247 brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
1248 afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
1249 complete(&afx_hdl->act_frm_scan);
1250 }
1251 return true;
1252}
1253
1254/**
1255 * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1256 *
1257 * @cfg: common configuration struct.
1258 *
1259 */
1260static void
1261brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1262{
1263 struct brcmf_p2p_info *p2p = &cfg->p2p;
Hante Meuleman2aec2c92016-04-11 11:35:24 +02001264 struct brcmf_if *ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001265
1266 if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
1267 (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
1268 test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
1269 brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
1270 /* if channel is not zero, "actfame" uses off channel scan.
1271 * So abort scan for off channel completion.
1272 */
1273 if (p2p->af_sent_channel)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001274 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001275 } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1276 &p2p->status)) {
1277 brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1278 /* So abort scan to cancel listen */
Arend van Spriela0f472a2013-04-05 10:57:49 +02001279 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001280 }
1281}
1282
1283
1284/**
1285 * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1286 *
1287 * @p2p: p2p device info struct.
1288 *
1289 * return true if recevied action frame is to be dropped.
1290 */
1291static bool
1292brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1293{
1294 struct brcmf_cfg80211_info *cfg = p2p->cfg;
1295 struct brcmf_if *ifp;
1296
1297 brcmf_dbg(TRACE, "Enter\n");
1298
1299 if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1300 !p2p->gon_req_action)
1301 return false;
1302
1303 brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
1304 /* if sa(peer) addr is less than da(my) addr, then this device
1305 * process peer's gon request and block to send gon req.
1306 * if not (sa addr > da addr),
1307 * this device will process gon request and drop gon req of peer.
1308 */
1309 ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
1310 if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
1311 brcmf_dbg(INFO, "Block transmit gon req !!!\n");
1312 p2p->block_gon_req_tx = true;
1313 /* if we are finding a common channel for sending af,
1314 * do not scan more to block to send current gon req
1315 */
1316 if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1317 &p2p->status))
1318 complete(&p2p->afx_hdl.act_frm_scan);
1319 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1320 &p2p->status))
1321 brcmf_p2p_stop_wait_next_action_frame(cfg);
1322 return false;
1323 }
1324
1325 /* drop gon request of peer to process gon request by this device. */
1326 brcmf_dbg(INFO, "Drop received gon req !!!\n");
1327
1328 return true;
1329}
1330
1331
1332/**
Hante Meulemane6da3402013-02-08 15:53:48 +01001333 * brcmf_p2p_notify_action_frame_rx() - received action frame.
1334 *
1335 * @ifp: interfac control.
1336 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1337 * @data: payload of message, containing action frame data.
1338 *
1339 */
1340int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1341 const struct brcmf_event_msg *e,
1342 void *data)
1343{
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001344 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1345 struct brcmf_p2p_info *p2p = &cfg->p2p;
1346 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meulemane6da3402013-02-08 15:53:48 +01001347 struct wireless_dev *wdev;
1348 u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1349 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001350 u8 *frame = (u8 *)(rxframe + 1);
1351 struct brcmf_p2p_pub_act_frame *act_frm;
1352 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
Franky Lin83cf17a2013-04-11 13:28:50 +02001353 struct brcmu_chan ch;
Hante Meulemane6da3402013-02-08 15:53:48 +01001354 struct ieee80211_mgmt *mgmt_frame;
Hante Meulemane6da3402013-02-08 15:53:48 +01001355 s32 freq;
1356 u16 mgmt_type;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001357 u8 action;
Hante Meulemane6da3402013-02-08 15:53:48 +01001358
Hante Meuleman0aedbca2016-02-17 11:26:54 +01001359 if (e->datalen < sizeof(*rxframe)) {
1360 brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1361 return 0;
1362 }
1363
Franky Lin83cf17a2013-04-11 13:28:50 +02001364 ch.chspec = be16_to_cpu(rxframe->chanspec);
1365 cfg->d11inf.decchspec(&ch);
Hante Meulemane6da3402013-02-08 15:53:48 +01001366 /* Check if wpa_supplicant has registered for this frame */
1367 brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1368 mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1369 if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1370 return 0;
1371
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001372 brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
1373
1374 action = P2P_PAF_SUBTYPE_INVALID;
1375 if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
1376 act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
1377 action = act_frm->subtype;
1378 if ((action == P2P_PAF_GON_REQ) &&
1379 (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
1380 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1381 &p2p->status) &&
dingtianhong692e5162013-12-26 19:41:34 +08001382 (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
Rafał Miłecki4712d882016-05-20 13:38:57 +02001383 afx_hdl->peer_chan = ch.control_ch_num;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001384 brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1385 afx_hdl->peer_chan);
1386 complete(&afx_hdl->act_frm_scan);
1387 }
1388 return 0;
1389 }
1390 /* After complete GO Negotiation, roll back to mpc mode */
1391 if ((action == P2P_PAF_GON_CONF) ||
1392 (action == P2P_PAF_PROVDIS_RSP))
Arend van Sprielf96aa072013-04-05 10:57:48 +02001393 brcmf_set_mpc(ifp, 1);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001394 if (action == P2P_PAF_GON_CONF) {
1395 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1396 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1397 }
1398 } else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
1399 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
1400 action = sd_act_frm->action;
1401 }
1402
1403 if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1404 (p2p->next_af_subtype == action)) {
1405 brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
1406 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1407 &p2p->status);
1408 /* Stop waiting for next AF. */
1409 brcmf_p2p_stop_wait_next_action_frame(cfg);
1410 }
Hante Meulemane6da3402013-02-08 15:53:48 +01001411
1412 mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1413 mgmt_frame_len, GFP_KERNEL);
1414 if (!mgmt_frame) {
1415 brcmf_err("No memory available for action frame\n");
1416 return -ENOMEM;
1417 }
1418 memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001419 brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1420 ETH_ALEN);
Hante Meulemane6da3402013-02-08 15:53:48 +01001421 memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1422 mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001423 memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
Hante Meulemane6da3402013-02-08 15:53:48 +01001424 mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1425
Rafał Miłecki4712d882016-05-20 13:38:57 +02001426 freq = ieee80211_channel_to_frequency(ch.control_ch_num,
Franky Lin83cf17a2013-04-11 13:28:50 +02001427 ch.band == BRCMU_CHAN_BAND_2G ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001428 NL80211_BAND_2GHZ :
1429 NL80211_BAND_5GHZ);
Arend van Spriel5b57af62013-04-05 10:57:53 +02001430
1431 wdev = &ifp->vif->wdev;
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07001432 cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len, 0);
Hante Meulemane6da3402013-02-08 15:53:48 +01001433
1434 kfree(mgmt_frame);
1435 return 0;
1436}
1437
1438
1439/**
Hante Meuleman18e2f612013-02-08 15:53:49 +01001440 * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1441 *
1442 * @ifp: interfac control.
1443 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1444 * @data: not used.
1445 *
1446 */
1447int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1448 const struct brcmf_event_msg *e,
1449 void *data)
1450{
1451 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1452 struct brcmf_p2p_info *p2p = &cfg->p2p;
1453
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001454 brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1455 e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1456 "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1457 e->status);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001458
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001459 if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1460 return 0;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001461
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001462 if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
1463 if (e->status == BRCMF_E_STATUS_SUCCESS)
1464 set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1465 &p2p->status);
1466 else {
1467 set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1468 /* If there is no ack, we don't need to wait for
1469 * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1470 */
1471 brcmf_p2p_stop_wait_next_action_frame(cfg);
1472 }
1473
1474 } else {
1475 complete(&p2p->send_af_done);
1476 }
Hante Meuleman18e2f612013-02-08 15:53:49 +01001477 return 0;
1478}
1479
1480
1481/**
1482 * brcmf_p2p_tx_action_frame() - send action frame over fil.
1483 *
1484 * @p2p: p2p info struct for vif.
1485 * @af_params: action frame data/info.
1486 *
1487 * Send an action frame immediately without doing channel synchronization.
1488 *
1489 * This function waits for a completion event before returning.
1490 * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1491 * frame is transmitted.
1492 */
1493static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1494 struct brcmf_fil_af_params_le *af_params)
1495{
1496 struct brcmf_cfg80211_vif *vif;
1497 s32 err = 0;
1498 s32 timeout = 0;
1499
1500 brcmf_dbg(TRACE, "Enter\n");
1501
Wolfram Sang16735d02013-11-14 14:32:02 -08001502 reinit_completion(&p2p->send_af_done);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001503 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1504 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1505
1506 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1507 err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1508 sizeof(*af_params));
1509 if (err) {
1510 brcmf_err(" sending action frame has failed\n");
1511 goto exit;
1512 }
1513
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001514 p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1515 p2p->af_tx_sent_jiffies = jiffies;
1516
Hante Meuleman18e2f612013-02-08 15:53:49 +01001517 timeout = wait_for_completion_timeout(&p2p->send_af_done,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001518 P2P_AF_MAX_WAIT_TIME);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001519
1520 if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1521 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1522 } else {
1523 err = -EIO;
1524 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1525 }
1526 /* clear status bit for action tx */
1527 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1528 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1529
1530exit:
1531 return err;
1532}
1533
1534
1535/**
1536 * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1537 *
1538 * @cfg: driver private data for cfg80211 interface.
1539 * @af_params: action frame data/info.
1540 * @config_af_params: configuration data for action frame.
1541 *
1542 * routine which transmits ation frame public type.
1543 */
1544static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1545 struct brcmf_fil_af_params_le *af_params,
1546 struct brcmf_config_af_params *config_af_params)
1547{
1548 struct brcmf_p2p_info *p2p = &cfg->p2p;
1549 struct brcmf_fil_action_frame_le *action_frame;
1550 struct brcmf_p2p_pub_act_frame *act_frm;
1551 s32 err = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001552 u16 ie_len;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001553
1554 action_frame = &af_params->action_frame;
1555 act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1556
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001557 config_af_params->extra_listen = true;
1558
Hante Meuleman18e2f612013-02-08 15:53:49 +01001559 switch (act_frm->subtype) {
1560 case P2P_PAF_GON_REQ:
1561 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1562 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1563 config_af_params->mpc_onoff = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001564 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001565 p2p->next_af_subtype = act_frm->subtype + 1;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001566 p2p->gon_req_action = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001567 /* increase dwell time to wait for RESP frame */
1568 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1569 break;
1570 case P2P_PAF_GON_RSP:
1571 p2p->next_af_subtype = act_frm->subtype + 1;
1572 /* increase dwell time to wait for CONF frame */
1573 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1574 break;
1575 case P2P_PAF_GON_CONF:
1576 /* If we reached till GO Neg confirmation reset the filter */
1577 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1578 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1579 /* turn on mpc again if go nego is done */
1580 config_af_params->mpc_onoff = 1;
1581 /* minimize dwell time */
1582 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001583 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001584 break;
1585 case P2P_PAF_INVITE_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001586 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001587 p2p->next_af_subtype = act_frm->subtype + 1;
1588 /* increase dwell time */
1589 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1590 break;
1591 case P2P_PAF_INVITE_RSP:
1592 /* minimize dwell time */
1593 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001594 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001595 break;
1596 case P2P_PAF_DEVDIS_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001597 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001598 p2p->next_af_subtype = act_frm->subtype + 1;
1599 /* maximize dwell time to wait for RESP frame */
1600 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1601 break;
1602 case P2P_PAF_DEVDIS_RSP:
1603 /* minimize dwell time */
1604 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001605 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001606 break;
1607 case P2P_PAF_PROVDIS_REQ:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001608 ie_len = le16_to_cpu(action_frame->len) -
1609 offsetof(struct brcmf_p2p_pub_act_frame, elts);
1610 if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1611 IEEE80211_P2P_ATTR_GROUP_ID,
1612 NULL, 0) < 0)
1613 config_af_params->search_channel = true;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001614 config_af_params->mpc_onoff = 0;
1615 p2p->next_af_subtype = act_frm->subtype + 1;
1616 /* increase dwell time to wait for RESP frame */
1617 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1618 break;
1619 case P2P_PAF_PROVDIS_RSP:
1620 /* wpa_supplicant send go nego req right after prov disc */
1621 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1622 /* increase dwell time to MED level */
1623 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001624 config_af_params->extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001625 break;
1626 default:
1627 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1628 act_frm->subtype);
1629 err = -EINVAL;
1630 }
1631 return err;
1632}
1633
1634/**
1635 * brcmf_p2p_send_action_frame() - send action frame .
1636 *
1637 * @cfg: driver private data for cfg80211 interface.
1638 * @ndev: net device to transmit on.
1639 * @af_params: configuration data for action frame.
1640 */
1641bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1642 struct net_device *ndev,
1643 struct brcmf_fil_af_params_le *af_params)
1644{
1645 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend van Spriela0f472a2013-04-05 10:57:49 +02001646 struct brcmf_if *ifp = netdev_priv(ndev);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001647 struct brcmf_fil_action_frame_le *action_frame;
1648 struct brcmf_config_af_params config_af_params;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001649 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001650 u16 action_frame_len;
1651 bool ack = false;
1652 u8 category;
1653 u8 action;
1654 s32 tx_retry;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001655 s32 extra_listen_time;
1656 uint delta_ms;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001657
1658 action_frame = &af_params->action_frame;
1659 action_frame_len = le16_to_cpu(action_frame->len);
1660
1661 brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1662
1663 /* Add the default dwell time. Dwell time to stay off-channel */
1664 /* to wait for a response action frame after transmitting an */
1665 /* GO Negotiation action frame */
1666 af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1667
1668 category = action_frame->data[DOT11_ACTION_CAT_OFF];
1669 action = action_frame->data[DOT11_ACTION_ACT_OFF];
1670
1671 /* initialize variables */
1672 p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001673 p2p->gon_req_action = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001674
1675 /* config parameters */
Hante Meuleman18e2f612013-02-08 15:53:49 +01001676 config_af_params.mpc_onoff = -1;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001677 config_af_params.search_channel = false;
1678 config_af_params.extra_listen = false;
Hante Meuleman18e2f612013-02-08 15:53:49 +01001679
1680 if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1681 /* p2p public action frame process */
1682 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1683 /* Just send unknown subtype frame with */
1684 /* default parameters. */
1685 brcmf_err("P2P Public action frame, unknown subtype.\n");
1686 }
1687 } else if (brcmf_p2p_is_gas_action(action_frame->data,
1688 action_frame_len)) {
1689 /* service discovery process */
1690 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1691 action == P2PSD_ACTION_ID_GAS_CREQ) {
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001692 /* configure service discovery query frame */
1693 config_af_params.search_channel = true;
1694
Hante Meuleman18e2f612013-02-08 15:53:49 +01001695 /* save next af suptype to cancel */
1696 /* remaining dwell time */
1697 p2p->next_af_subtype = action + 1;
1698
1699 af_params->dwell_time =
1700 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1701 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1702 action == P2PSD_ACTION_ID_GAS_CRESP) {
1703 /* configure service discovery response frame */
1704 af_params->dwell_time =
1705 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1706 } else {
1707 brcmf_err("Unknown action type: %d\n", action);
1708 goto exit;
1709 }
1710 } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1711 action_frame_len)) {
1712 /* do not configure anything. it will be */
1713 /* sent with a default configuration */
1714 } else {
1715 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1716 category, action);
1717 return false;
1718 }
1719
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001720 /* if connecting on primary iface, sleep for a while before sending
1721 * af tx for VSDB
1722 */
1723 if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1724 &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1725 msleep(50);
1726
Hante Meuleman18e2f612013-02-08 15:53:49 +01001727 /* if scan is ongoing, abort current scan. */
1728 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1729 brcmf_abort_scanning(cfg);
1730
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001731 memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1732
Hante Meuleman18e2f612013-02-08 15:53:49 +01001733 /* To make sure to send successfully action frame, turn off mpc */
1734 if (config_af_params.mpc_onoff == 0)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001735 brcmf_set_mpc(ifp, 0);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001736
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001737 /* set status and destination address before sending af */
1738 if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1739 /* set status to cancel the remained dwell time in rx process */
1740 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1741 }
1742
1743 p2p->af_sent_channel = 0;
1744 set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1745 /* validate channel and p2p ies */
1746 if (config_af_params.search_channel &&
1747 IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1748 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1749 afx_hdl = &p2p->afx_hdl;
1750 afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1751
1752 if (brcmf_p2p_af_searching_channel(p2p) ==
1753 P2P_INVALID_CHANNEL) {
1754 brcmf_err("Couldn't find peer's channel.\n");
1755 goto exit;
1756 }
1757
1758 /* Abort scan even for VSDB scenarios. Scan gets aborted in
1759 * firmware but after the check of piggyback algorithm. To take
1760 * care of current piggback algo, lets abort the scan here
1761 * itself.
1762 */
Arend van Spriela0f472a2013-04-05 10:57:49 +02001763 brcmf_notify_escan_complete(cfg, ifp, true, true);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001764
1765 /* update channel */
1766 af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1767 }
1768
Hante Meuleman18e2f612013-02-08 15:53:49 +01001769 tx_retry = 0;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001770 while (!p2p->block_gon_req_tx &&
1771 (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
Hante Meuleman18e2f612013-02-08 15:53:49 +01001772 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1773 tx_retry++;
1774 }
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001775 if (ack == false) {
Hante Meuleman18e2f612013-02-08 15:53:49 +01001776 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001777 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1778 }
Hante Meuleman18e2f612013-02-08 15:53:49 +01001779
1780exit:
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001781 clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1782
1783 /* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1784 * if we coundn't get the next action response frame and dongle does
1785 * not keep the dwell time, go to listen state again to get next action
1786 * response frame.
1787 */
1788 if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1789 test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1790 p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1791 delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1792 if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1793 extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1794 delta_ms;
1795 else
1796 extra_listen_time = 0;
1797 if (extra_listen_time > 50) {
1798 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1799 &p2p->status);
1800 brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1801 le32_to_cpu(af_params->dwell_time),
1802 extra_listen_time);
1803 extra_listen_time += 100;
1804 if (!brcmf_p2p_discover_listen(p2p,
1805 p2p->af_sent_channel,
1806 extra_listen_time)) {
1807 unsigned long duration;
1808
1809 extra_listen_time += 100;
1810 duration = msecs_to_jiffies(extra_listen_time);
1811 wait_for_completion_timeout(&p2p->wait_next_af,
1812 duration);
1813 }
1814 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1815 &p2p->status);
1816 }
1817 }
1818
1819 if (p2p->block_gon_req_tx) {
1820 /* if ack is true, supplicant will wait more time(100ms).
1821 * so we will return it as a success to get more time .
1822 */
1823 p2p->block_gon_req_tx = false;
1824 ack = true;
1825 }
1826
1827 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001828 /* if all done, turn mpc on again */
1829 if (config_af_params.mpc_onoff == 1)
Arend van Spriela0f472a2013-04-05 10:57:49 +02001830 brcmf_set_mpc(ifp, 1);
Hante Meuleman18e2f612013-02-08 15:53:49 +01001831
1832 return ack;
1833}
1834
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001835/**
1836 * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1837 *
1838 * @ifp: interface pointer for which event was received.
1839 * @e: even message.
1840 * @data: payload of event message (probe request).
1841 */
1842s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1843 const struct brcmf_event_msg *e,
1844 void *data)
1845{
1846 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1847 struct brcmf_p2p_info *p2p = &cfg->p2p;
1848 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001849 struct brcmf_cfg80211_vif *vif = ifp->vif;
1850 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
Franky Lin83cf17a2013-04-11 13:28:50 +02001851 struct brcmu_chan ch;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001852 u8 *mgmt_frame;
1853 u32 mgmt_frame_len;
1854 s32 freq;
1855 u16 mgmt_type;
1856
1857 brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1858 e->reason);
1859
Hante Meuleman0aedbca2016-02-17 11:26:54 +01001860 if (e->datalen < sizeof(*rxframe)) {
1861 brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1862 return 0;
1863 }
1864
Franky Lin83cf17a2013-04-11 13:28:50 +02001865 ch.chspec = be16_to_cpu(rxframe->chanspec);
1866 cfg->d11inf.decchspec(&ch);
1867
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001868 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
dingtianhong692e5162013-12-26 19:41:34 +08001869 (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
Rafał Miłecki4712d882016-05-20 13:38:57 +02001870 afx_hdl->peer_chan = ch.control_ch_num;
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001871 brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
1872 afx_hdl->peer_chan);
1873 complete(&afx_hdl->act_frm_scan);
1874 }
1875
1876 /* Firmware sends us two proberesponses for each idx one. At the */
1877 /* moment anything but bsscfgidx 0 is passed up to supplicant */
1878 if (e->bsscfgidx == 0)
1879 return 0;
1880
1881 /* Filter any P2P probe reqs arriving during the GO-NEG Phase */
1882 if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
1883 brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
1884 return 0;
1885 }
1886
1887 /* Check if wpa_supplicant has registered for this frame */
1888 brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
1889 mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
1890 if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1891 return 0;
1892
1893 mgmt_frame = (u8 *)(rxframe + 1);
1894 mgmt_frame_len = e->datalen - sizeof(*rxframe);
Rafał Miłecki4712d882016-05-20 13:38:57 +02001895 freq = ieee80211_channel_to_frequency(ch.control_ch_num,
Franky Lin83cf17a2013-04-11 13:28:50 +02001896 ch.band == BRCMU_CHAN_BAND_2G ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001897 NL80211_BAND_2GHZ :
1898 NL80211_BAND_5GHZ);
Arend van Sprielbffc61c2013-04-05 10:57:52 +02001899
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07001900 cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len, 0);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001901
1902 brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
Kevin Cernekee73f2c8e2017-09-16 21:08:22 -07001903 mgmt_frame_len, e->datalen, ch.chspec, freq);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01001904
1905 return 0;
1906}
1907
Hante Meuleman18e2f612013-02-08 15:53:49 +01001908
1909/**
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001910 * brcmf_p2p_get_current_chanspec() - Get current operation channel.
1911 *
1912 * @p2p: P2P specific data.
1913 * @chanspec: chanspec to be returned.
1914 */
1915static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
1916 u16 *chanspec)
1917{
1918 struct brcmf_if *ifp;
Hante Meuleman15a953d2013-05-27 21:09:54 +02001919 u8 mac_addr[ETH_ALEN];
Franky Lin83cf17a2013-04-11 13:28:50 +02001920 struct brcmu_chan ch;
Hante Meuleman15a953d2013-05-27 21:09:54 +02001921 struct brcmf_bss_info_le *bi;
1922 u8 *buf;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001923
1924 ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1925
Hante Meuleman15a953d2013-05-27 21:09:54 +02001926 if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mac_addr,
1927 ETH_ALEN) == 0) {
1928 buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
1929 if (buf != NULL) {
1930 *(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
1931 if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
1932 buf, WL_BSS_INFO_MAX) == 0) {
1933 bi = (struct brcmf_bss_info_le *)(buf + 4);
1934 *chanspec = le16_to_cpu(bi->chanspec);
1935 kfree(buf);
1936 return;
1937 }
1938 kfree(buf);
1939 }
1940 }
1941 /* Use default channel for P2P */
1942 ch.chnum = BRCMF_P2P_TEMP_CHAN;
Franky Lin83cf17a2013-04-11 13:28:50 +02001943 ch.bw = BRCMU_CHAN_BW_20;
1944 p2p->cfg->d11inf.encchspec(&ch);
1945 *chanspec = ch.chspec;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001946}
1947
1948/**
1949 * Change a P2P Role.
1950 * Parameters:
1951 * @mac: MAC address of the BSS to change a role
1952 * Returns 0 if success.
1953 */
1954int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
1955 enum brcmf_fil_p2p_if_types if_type)
1956{
1957 struct brcmf_p2p_info *p2p = &cfg->p2p;
1958 struct brcmf_cfg80211_vif *vif;
1959 struct brcmf_fil_p2p_if_le if_request;
1960 s32 err;
1961 u16 chanspec;
1962
1963 brcmf_dbg(TRACE, "Enter\n");
1964
1965 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1966 if (!vif) {
1967 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
1968 return -EPERM;
1969 }
Arend van Spriela0f472a2013-04-05 10:57:49 +02001970 brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001971 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
1972 if (!vif) {
1973 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
1974 return -EPERM;
1975 }
Arend van Sprielf96aa072013-04-05 10:57:48 +02001976 brcmf_set_mpc(vif->ifp, 0);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001977
1978 /* In concurrency case, STA may be already associated in a particular */
1979 /* channel. so retrieve the current channel of primary interface and */
1980 /* then start the virtual interface on that. */
1981 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1982
1983 if_request.type = cpu_to_le16((u16)if_type);
1984 if_request.chspec = cpu_to_le16(chanspec);
1985 memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
1986
1987 brcmf_cfg80211_arm_vif_event(cfg, vif);
1988 err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
1989 sizeof(if_request));
1990 if (err) {
1991 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
1992 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1993 return err;
1994 }
Arend van Spriela9eb0c42016-02-17 11:26:50 +01001995 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_CHANGE,
1996 BRCMF_VIF_EVENT_TIMEOUT);
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01001997 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1998 if (!err) {
1999 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
2000 return -EIO;
2001 }
2002
2003 err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2004 BRCMF_SCB_TIMEOUT_VALUE);
2005
2006 return err;
2007}
2008
2009static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2010 struct brcmf_if *ifp, u8 ea[ETH_ALEN],
Arend van Sprield3c0b632013-02-08 15:53:37 +01002011 enum brcmf_fil_p2p_if_types iftype)
2012{
2013 struct brcmf_fil_p2p_if_le if_request;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002014 int err;
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002015 u16 chanspec;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002016
2017 /* we need a default channel */
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002018 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002019
2020 /* fill the firmware request */
2021 memcpy(if_request.addr, ea, ETH_ALEN);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002022 if_request.type = cpu_to_le16((u16)iftype);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002023 if_request.chspec = cpu_to_le16(chanspec);
2024
2025 err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2026 sizeof(if_request));
Arend van Sprield3c0b632013-02-08 15:53:37 +01002027
Arend van Sprield3c0b632013-02-08 15:53:37 +01002028 return err;
2029}
2030
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002031static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2032{
2033 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2034 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2035 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2036 u8 *addr = vif->wdev.netdev->dev_addr;
2037
2038 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2039}
2040
2041static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2042{
2043 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2044 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2045 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2046 u8 *addr = vif->wdev.netdev->dev_addr;
2047
2048 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2049}
2050
Arend van Spriel9f440b72013-02-08 15:53:36 +01002051/**
Arend van Spriel27f10e32013-04-05 10:57:50 +02002052 * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2053 *
2054 * @p2p: P2P specific data.
2055 * @wiphy: wiphy device of new interface.
2056 * @addr: mac address for this new interface.
2057 */
2058static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2059 struct wiphy *wiphy,
2060 u8 *addr)
2061{
2062 struct brcmf_cfg80211_vif *p2p_vif;
2063 struct brcmf_if *p2p_ifp;
2064 struct brcmf_if *pri_ifp;
2065 int err;
Hante Meuleman37a869e2015-10-29 20:33:17 +01002066 u32 bsscfgidx;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002067
2068 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2069 return ERR_PTR(-ENOSPC);
2070
Rafał Miłecki26072332016-06-06 23:03:55 +02002071 p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002072 if (IS_ERR(p2p_vif)) {
2073 brcmf_err("could not create discovery vif\n");
2074 return (struct wireless_dev *)p2p_vif;
2075 }
2076
Arend van Spriel27f10e32013-04-05 10:57:50 +02002077 pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002078 brcmf_p2p_generate_bss_mac(p2p, addr);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002079 brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2080
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002081 brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
Hante Meulemancb8dc712015-09-18 22:08:18 +02002082 brcmf_fweh_p2pdev_setup(pri_ifp, true);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002083
Arend van Spriel27f10e32013-04-05 10:57:50 +02002084 /* Initialize P2P Discovery in the firmware */
2085 err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2086 if (err < 0) {
2087 brcmf_err("set p2p_disc error\n");
Hante Meulemancb8dc712015-09-18 22:08:18 +02002088 brcmf_fweh_p2pdev_setup(pri_ifp, false);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002089 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2090 goto fail;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002091 }
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002092
2093 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002094 err = brcmf_cfg80211_wait_vif_event(p2p->cfg, BRCMF_E_IF_ADD,
2095 BRCMF_VIF_EVENT_TIMEOUT);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002096 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
Hante Meulemancb8dc712015-09-18 22:08:18 +02002097 brcmf_fweh_p2pdev_setup(pri_ifp, false);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002098 if (!err) {
2099 brcmf_err("timeout occurred\n");
2100 err = -EIO;
2101 goto fail;
2102 }
2103
2104 /* discovery interface created */
2105 p2p_ifp = p2p_vif->ifp;
2106 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2107 memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2108 memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2109
2110 /* verify bsscfg index for P2P discovery */
Hante Meuleman37a869e2015-10-29 20:33:17 +01002111 err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bsscfgidx);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002112 if (err < 0) {
2113 brcmf_err("retrieving discover bsscfg index failed\n");
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002114 goto fail;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002115 }
2116
Hante Meuleman37a869e2015-10-29 20:33:17 +01002117 WARN_ON(p2p_ifp->bsscfgidx != bsscfgidx);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002118
2119 init_completion(&p2p->send_af_done);
2120 INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2121 init_completion(&p2p->afx_hdl.act_frm_scan);
2122 init_completion(&p2p->wait_next_af);
2123
2124 return &p2p_vif->wdev;
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002125
2126fail:
Arend van Spriel427dec52014-01-06 12:40:47 +01002127 brcmf_free_vif(p2p_vif);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02002128 return ERR_PTR(err);
Arend van Spriel27f10e32013-04-05 10:57:50 +02002129}
2130
2131/**
Arend van Spriel9f440b72013-02-08 15:53:36 +01002132 * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2133 *
2134 * @wiphy: wiphy device of new interface.
2135 * @name: name of the new interface.
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002136 * @name_assign_type: origin of the interface name
Arend van Spriel9f440b72013-02-08 15:53:36 +01002137 * @type: nl80211 interface type.
Arend van Spriel27f10e32013-04-05 10:57:50 +02002138 * @params: contains mac address for P2P device.
Arend van Spriel9f440b72013-02-08 15:53:36 +01002139 */
2140struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002141 unsigned char name_assign_type,
Johannes Berg818a9862017-04-12 11:23:28 +02002142 enum nl80211_iftype type,
Arend van Spriel9f440b72013-02-08 15:53:36 +01002143 struct vif_params *params)
2144{
Arend van Sprield3c0b632013-02-08 15:53:37 +01002145 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2146 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2147 struct brcmf_cfg80211_vif *vif;
2148 enum brcmf_fil_p2p_if_types iftype;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002149 int err;
2150
2151 if (brcmf_cfg80211_vif_event_armed(cfg))
2152 return ERR_PTR(-EBUSY);
2153
Arend van Spriel9f440b72013-02-08 15:53:36 +01002154 brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002155
2156 switch (type) {
2157 case NL80211_IFTYPE_P2P_CLIENT:
2158 iftype = BRCMF_FIL_P2P_IF_CLIENT;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002159 break;
2160 case NL80211_IFTYPE_P2P_GO:
2161 iftype = BRCMF_FIL_P2P_IF_GO;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002162 break;
Arend van Spriel27f10e32013-04-05 10:57:50 +02002163 case NL80211_IFTYPE_P2P_DEVICE:
2164 return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2165 params->macaddr);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002166 default:
2167 return ERR_PTR(-EOPNOTSUPP);
2168 }
2169
Rafał Miłecki26072332016-06-06 23:03:55 +02002170 vif = brcmf_alloc_vif(cfg, type);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002171 if (IS_ERR(vif))
2172 return (struct wireless_dev *)vif;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002173 brcmf_cfg80211_arm_vif_event(cfg, vif);
2174
Hante Meuleman7a5c1f62013-02-08 15:53:44 +01002175 err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
2176 iftype);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002177 if (err) {
2178 brcmf_cfg80211_arm_vif_event(cfg, NULL);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002179 goto fail;
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002180 }
Arend van Sprield3c0b632013-02-08 15:53:37 +01002181
2182 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002183 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_ADD,
2184 BRCMF_VIF_EVENT_TIMEOUT);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002185 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2186 if (!err) {
2187 brcmf_err("timeout occurred\n");
2188 err = -EIO;
2189 goto fail;
2190 }
2191
2192 /* interface created in firmware */
2193 ifp = vif->ifp;
2194 if (!ifp) {
2195 brcmf_err("no if pointer provided\n");
2196 err = -ENOENT;
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002197 goto fail;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002198 }
2199
2200 strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
Tom Gundersen6bab2e192015-03-18 11:13:39 +01002201 ifp->ndev->name_assign_type = name_assign_type;
Hante Meuleman4b3a89d2013-02-08 15:54:01 +01002202 err = brcmf_net_attach(ifp, true);
2203 if (err) {
2204 brcmf_err("Registering netdevice failed\n");
Arend Van Sprieldca23072017-06-24 22:08:27 +01002205 free_netdev(ifp->ndev);
Hante Meuleman4b3a89d2013-02-08 15:54:01 +01002206 goto fail;
2207 }
Arend van Sprielcbb371d2013-05-27 21:09:57 +02002208
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002209 cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
2210 /* Disable firmware roaming for P2P interface */
2211 brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2212 if (iftype == BRCMF_FIL_P2P_IF_GO) {
2213 /* set station timeout for p2p */
2214 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2215 BRCMF_SCB_TIMEOUT_VALUE);
2216 }
Arend van Sprield3c0b632013-02-08 15:53:37 +01002217 return &ifp->vif->wdev;
2218
2219fail:
Arend van Spriel427dec52014-01-06 12:40:47 +01002220 brcmf_free_vif(vif);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002221 return ERR_PTR(err);
Arend van Spriel9f440b72013-02-08 15:53:36 +01002222}
2223
2224/**
2225 * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2226 *
2227 * @wiphy: wiphy device of interface.
2228 * @wdev: wireless device of interface.
Arend van Spriel9f440b72013-02-08 15:53:36 +01002229 */
2230int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2231{
Arend van Sprield3c0b632013-02-08 15:53:37 +01002232 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
Hante Meuleman6eda4e22013-02-08 15:54:02 +01002233 struct brcmf_p2p_info *p2p = &cfg->p2p;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002234 struct brcmf_cfg80211_vif *vif;
Arend Van Sprield77facb2017-03-28 09:11:30 +01002235 enum nl80211_iftype iftype;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002236 bool wait_for_disable = false;
Arend van Sprield3c0b632013-02-08 15:53:37 +01002237 int err;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002238
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002239 brcmf_dbg(TRACE, "delete P2P vif\n");
Arend van Spriel9f440b72013-02-08 15:53:36 +01002240 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
Arend van Sprield3c0b632013-02-08 15:53:37 +01002241
Arend Van Sprield77facb2017-03-28 09:11:30 +01002242 iftype = vif->wdev.iftype;
Hante Meulemana2044d92015-09-18 22:08:16 +02002243 brcmf_cfg80211_arm_vif_event(cfg, vif);
Arend Van Sprield77facb2017-03-28 09:11:30 +01002244 switch (iftype) {
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002245 case NL80211_IFTYPE_P2P_CLIENT:
2246 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2247 wait_for_disable = true;
2248 break;
2249
2250 case NL80211_IFTYPE_P2P_GO:
2251 if (!brcmf_p2p_disable_p2p_if(vif))
2252 wait_for_disable = true;
2253 break;
2254
2255 case NL80211_IFTYPE_P2P_DEVICE:
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002256 if (!p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2257 return 0;
Arend van Sprielf37d69a2015-06-11 00:12:21 +02002258 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2259 brcmf_p2p_deinit_discovery(p2p);
Rafał Miłecki20856ad2016-06-19 01:55:57 +02002260 break;
2261
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002262 default:
2263 return -ENOTSUPP;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002264 }
2265
Hante Meuleman6eda4e22013-02-08 15:54:02 +01002266 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2267 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2268
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002269 if (wait_for_disable)
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002270 wait_for_completion_timeout(&cfg->vif_disabled,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002271 BRCMF_P2P_DISABLE_TIMEOUT);
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002272
Hante Meulemana2044d92015-09-18 22:08:16 +02002273 err = 0;
Arend Van Sprield77facb2017-03-28 09:11:30 +01002274 if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
Hante Meulemana2044d92015-09-18 22:08:16 +02002275 brcmf_vif_clear_mgmt_ies(vif);
2276 err = brcmf_p2p_release_p2p_if(vif);
2277 }
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002278 if (!err) {
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002279 /* wait for firmware event */
Arend van Spriela9eb0c42016-02-17 11:26:50 +01002280 err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_DEL,
2281 BRCMF_VIF_EVENT_TIMEOUT);
Hante Meuleman7ee2d922013-02-08 15:53:43 +01002282 if (!err)
2283 err = -EIO;
2284 else
2285 err = 0;
2286 }
Rafał Miłeckia63b0982016-06-29 21:54:26 +02002287 brcmf_remove_interface(vif->ifp, true);
Hante Meulemana2044d92015-09-18 22:08:16 +02002288
Arend van Sprield3c0b632013-02-08 15:53:37 +01002289 brcmf_cfg80211_arm_vif_event(cfg, NULL);
Arend Van Sprield77facb2017-03-28 09:11:30 +01002290 if (iftype != NL80211_IFTYPE_P2P_DEVICE)
Hante Meulemana2044d92015-09-18 22:08:16 +02002291 p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
Arend van Spriel5f4f9f12013-02-08 15:53:41 +01002292
2293 return err;
Arend van Spriel9f440b72013-02-08 15:53:36 +01002294}
Arend van Spriel27f10e32013-04-05 10:57:50 +02002295
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002296void brcmf_p2p_ifp_removed(struct brcmf_if *ifp, bool rtnl_locked)
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002297{
2298 struct brcmf_cfg80211_info *cfg;
2299 struct brcmf_cfg80211_vif *vif;
2300
2301 brcmf_dbg(INFO, "P2P: device interface removed\n");
2302 vif = ifp->vif;
2303 cfg = wdev_to_cfg(&vif->wdev);
2304 cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002305 if (!rtnl_locked)
2306 rtnl_lock();
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002307 cfg80211_unregister_wdev(&vif->wdev);
mhiramat@kernel.org15dacf82016-08-15 18:40:57 +09002308 if (!rtnl_locked)
2309 rtnl_unlock();
Hante Meuleman9831bcb2015-09-18 22:08:12 +02002310 brcmf_free_vif(vif);
2311}
2312
Arend van Spriel27f10e32013-04-05 10:57:50 +02002313int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2314{
2315 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2316 struct brcmf_p2p_info *p2p = &cfg->p2p;
2317 struct brcmf_cfg80211_vif *vif;
2318 int err;
2319
2320 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2321 mutex_lock(&cfg->usr_sync);
2322 err = brcmf_p2p_enable_discovery(p2p);
2323 if (!err)
2324 set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2325 mutex_unlock(&cfg->usr_sync);
2326 return err;
2327}
2328
2329void brcmf_p2p_stop_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
2335 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
Hante Meulemand1bb34c2015-09-18 22:08:13 +02002336 /* This call can be result of the unregister_wdev call. In that case
2337 * we dont want to do anything anymore. Just return. The config vif
2338 * will have been cleared at this point.
2339 */
2340 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif == vif) {
2341 mutex_lock(&cfg->usr_sync);
Hante Meulemana2044d92015-09-18 22:08:16 +02002342 /* Set the discovery state to SCAN */
2343 (void)brcmf_p2p_set_discover_state(vif->ifp,
2344 WL_P2P_DISC_ST_SCAN, 0, 0);
Hante Meulemand1bb34c2015-09-18 22:08:13 +02002345 brcmf_abort_scanning(cfg);
2346 clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2347 mutex_unlock(&cfg->usr_sync);
2348 }
Arend van Spriel27f10e32013-04-05 10:57:50 +02002349}
Arend van Spriel55479df2015-06-11 00:12:22 +02002350
2351/**
2352 * brcmf_p2p_attach() - attach for P2P.
2353 *
2354 * @cfg: driver private data for cfg80211 interface.
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002355 * @p2pdev_forced: create p2p device interface at attach.
Arend van Spriel55479df2015-06-11 00:12:22 +02002356 */
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002357s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg, bool p2pdev_forced)
Arend van Spriel55479df2015-06-11 00:12:22 +02002358{
Arend van Spriel55479df2015-06-11 00:12:22 +02002359 struct brcmf_p2p_info *p2p;
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002360 struct brcmf_if *pri_ifp;
Arend van Spriel55479df2015-06-11 00:12:22 +02002361 s32 err = 0;
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002362 void *err_ptr;
Arend van Spriel55479df2015-06-11 00:12:22 +02002363
2364 p2p = &cfg->p2p;
2365 p2p->cfg = cfg;
2366
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002367 pri_ifp = brcmf_get_ifp(cfg->pub, 0);
Arend van Spriel55479df2015-06-11 00:12:22 +02002368 p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
2369
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002370 if (p2pdev_forced) {
Hante Meuleman2b76acd2015-10-08 20:33:15 +02002371 err_ptr = brcmf_p2p_create_p2pdev(p2p, NULL, NULL);
2372 if (IS_ERR(err_ptr)) {
2373 brcmf_err("P2P device creation failed.\n");
2374 err = PTR_ERR(err_ptr);
2375 }
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002376 } else {
Hante Meulemanae7c03f2015-09-18 22:08:08 +02002377 p2p->p2pdev_dynamically = true;
2378 }
Arend van Spriel55479df2015-06-11 00:12:22 +02002379 return err;
2380}
2381
2382/**
2383 * brcmf_p2p_detach() - detach P2P.
2384 *
2385 * @p2p: P2P specific data.
2386 */
2387void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
2388{
2389 struct brcmf_cfg80211_vif *vif;
2390
2391 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
2392 if (vif != NULL) {
2393 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2394 brcmf_p2p_deinit_discovery(p2p);
Rafał Miłeckib50ddfa2016-06-17 12:29:21 +02002395 brcmf_remove_interface(vif->ifp, false);
Arend van Spriel55479df2015-06-11 00:12:22 +02002396 }
2397 /* just set it all to zero */
2398 memset(p2p, 0, sizeof(*p2p));
2399}
2400