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