2 * Copyright (c) 2012 Broadcom Corporation
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.
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.
16 #include <linux/slab.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <net/cfg80211.h>
21 #include <brcmu_wifi.h>
22 #include <brcmu_utils.h>
27 #include "fwil_types.h"
29 #include "wl_cfg80211.h"
31 /* parameters used for p2p escan */
32 #define P2PAPI_SCAN_NPROBES 1
33 #define P2PAPI_SCAN_DWELL_TIME_MS 80
34 #define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
35 #define P2PAPI_SCAN_HOME_TIME_MS 60
36 #define P2PAPI_SCAN_NPROBS_TIME_MS 30
37 #define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
38 #define WL_SCAN_CONNECT_DWELL_TIME_MS 200
39 #define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
41 #define BRCMF_P2P_WILDCARD_SSID "DIRECT-"
42 #define BRCMF_P2P_WILDCARD_SSID_LEN (sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
44 #define SOCIAL_CHAN_1 1
45 #define SOCIAL_CHAN_2 6
46 #define SOCIAL_CHAN_3 11
47 #define IS_P2P_SOCIAL_CHANNEL(channel) ((channel == SOCIAL_CHAN_1) || \
48 (channel == SOCIAL_CHAN_2) || \
49 (channel == SOCIAL_CHAN_3))
50 #define SOCIAL_CHAN_CNT 3
51 #define AF_PEER_SEARCH_CNT 2
53 #define BRCMF_SCB_TIMEOUT_VALUE 20
55 #define P2P_VER 9 /* P2P version: 9=WiFi P2P v1.0 */
56 #define P2P_PUB_AF_CATEGORY 0x04
57 #define P2P_PUB_AF_ACTION 0x09
58 #define P2P_AF_CATEGORY 0x7f
59 #define P2P_OUI "\x50\x6F\x9A" /* P2P OUI */
60 #define P2P_OUI_LEN 3 /* P2P OUI length */
62 /* Action Frame Constants */
63 #define DOT11_ACTION_HDR_LEN 2 /* action frame category + action */
64 #define DOT11_ACTION_CAT_OFF 0 /* category offset */
65 #define DOT11_ACTION_ACT_OFF 1 /* action offset */
67 #define P2P_AF_DWELL_TIME 200
68 #define P2P_AF_MIN_DWELL_TIME 100
69 #define P2P_AF_MED_DWELL_TIME 400
70 #define P2P_AF_LONG_DWELL_TIME 1000
71 #define P2P_AF_TX_MAX_RETRY 1
72 #define P2P_AF_MAX_WAIT_TIME 2000
73 #define P2P_INVALID_CHANNEL -1
74 #define P2P_CHANNEL_SYNC_RETRY 5
75 #define P2P_AF_FRM_SCAN_MAX_WAIT 1500
76 #define P2P_DEFAULT_SLEEP_TIME_VSDB 200
78 /* WiFi P2P Public Action Frame OUI Subtypes */
79 #define P2P_PAF_GON_REQ 0 /* Group Owner Negotiation Req */
80 #define P2P_PAF_GON_RSP 1 /* Group Owner Negotiation Rsp */
81 #define P2P_PAF_GON_CONF 2 /* Group Owner Negotiation Confirm */
82 #define P2P_PAF_INVITE_REQ 3 /* P2P Invitation Request */
83 #define P2P_PAF_INVITE_RSP 4 /* P2P Invitation Response */
84 #define P2P_PAF_DEVDIS_REQ 5 /* Device Discoverability Request */
85 #define P2P_PAF_DEVDIS_RSP 6 /* Device Discoverability Response */
86 #define P2P_PAF_PROVDIS_REQ 7 /* Provision Discovery Request */
87 #define P2P_PAF_PROVDIS_RSP 8 /* Provision Discovery Response */
88 #define P2P_PAF_SUBTYPE_INVALID 255 /* Invalid Subtype */
90 /* WiFi P2P Action Frame OUI Subtypes */
91 #define P2P_AF_NOTICE_OF_ABSENCE 0 /* Notice of Absence */
92 #define P2P_AF_PRESENCE_REQ 1 /* P2P Presence Request */
93 #define P2P_AF_PRESENCE_RSP 2 /* P2P Presence Response */
94 #define P2P_AF_GO_DISC_REQ 3 /* GO Discoverability Request */
96 /* P2P Service Discovery related */
97 #define P2PSD_ACTION_CATEGORY 0x04 /* Public action frame */
98 #define P2PSD_ACTION_ID_GAS_IREQ 0x0a /* GAS Initial Request AF */
99 #define P2PSD_ACTION_ID_GAS_IRESP 0x0b /* GAS Initial Response AF */
100 #define P2PSD_ACTION_ID_GAS_CREQ 0x0c /* GAS Comback Request AF */
101 #define P2PSD_ACTION_ID_GAS_CRESP 0x0d /* GAS Comback Response AF */
104 * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
106 * @state: requested discovery state (see enum brcmf_p2p_disc_state).
107 * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
108 * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
110 struct brcmf_p2p_disc_st_le {
117 * enum brcmf_p2p_disc_state - P2P discovery state values
119 * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
120 * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
121 * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
123 enum brcmf_p2p_disc_state {
125 WL_P2P_DISC_ST_LISTEN,
126 WL_P2P_DISC_ST_SEARCH
130 * struct brcmf_p2p_scan_le - P2P specific scan request.
132 * @type: type of scan method requested (values: 'E' or 'S').
133 * @reserved: reserved (ignored).
134 * @eparams: parameters used for type 'E'.
135 * @sparams: parameters used for type 'S'.
137 struct brcmf_p2p_scan_le {
141 struct brcmf_escan_params_le eparams;
142 struct brcmf_scan_params_le sparams;
147 * struct brcmf_p2p_pub_act_frame - WiFi P2P Public Action Frame
149 * @category: P2P_PUB_AF_CATEGORY
150 * @action: P2P_PUB_AF_ACTION
152 * @oui_type: OUI type - P2P_VER
153 * @subtype: OUI subtype - P2P_TYPE_*
154 * @dialog_token: nonzero, identifies req/rsp transaction
155 * @elts[1]: Variable length information elements.
157 struct brcmf_p2p_pub_act_frame {
168 * struct brcmf_p2p_action_frame - WiFi P2P Action Frame
170 * @category: P2P_AF_CATEGORY
171 * @OUI[3]: OUI - P2P_OUI
172 * @type: OUI Type - P2P_VER
173 * @subtype: OUI Subtype - P2P_AF_*
174 * @dialog_token: nonzero, identifies req/resp tranaction
175 * @elts[1]: Variable length information elements.
177 struct brcmf_p2p_action_frame {
187 * struct brcmf_p2psd_gas_pub_act_frame - Wi-Fi GAS Public Action Frame
189 * @category: 0x04 Public Action Frame
190 * @action: 0x6c Advertisement Protocol
191 * @dialog_token: nonzero, identifies req/rsp transaction
192 * @query_data[1]: Query Data. SD gas ireq SD gas iresp
194 struct brcmf_p2psd_gas_pub_act_frame {
202 * struct brcmf_config_af_params - Action Frame Parameters for tx.
204 * @mpc_onoff: To make sure to send successfully action frame, we have to
205 * turn off mpc 0: off, 1: on, (-1): do nothing
206 * @search_channel: 1: search peer's channel to send af
207 * extra_listen: keep the dwell time to get af response frame.
209 struct brcmf_config_af_params {
216 * brcmf_p2p_is_pub_action() - true if p2p public type frame.
218 * @frame: action frame data.
219 * @frame_len: length of action frame data.
221 * Determine if action frame is p2p public action type
223 static bool brcmf_p2p_is_pub_action(void *frame, u32 frame_len)
225 struct brcmf_p2p_pub_act_frame *pact_frm;
230 pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
231 if (frame_len < sizeof(struct brcmf_p2p_pub_act_frame) - 1)
234 if (pact_frm->category == P2P_PUB_AF_CATEGORY &&
235 pact_frm->action == P2P_PUB_AF_ACTION &&
236 pact_frm->oui_type == P2P_VER &&
237 memcmp(pact_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
244 * brcmf_p2p_is_p2p_action() - true if p2p action type frame.
246 * @frame: action frame data.
247 * @frame_len: length of action frame data.
249 * Determine if action frame is p2p action type
251 static bool brcmf_p2p_is_p2p_action(void *frame, u32 frame_len)
253 struct brcmf_p2p_action_frame *act_frm;
258 act_frm = (struct brcmf_p2p_action_frame *)frame;
259 if (frame_len < sizeof(struct brcmf_p2p_action_frame) - 1)
262 if (act_frm->category == P2P_AF_CATEGORY &&
263 act_frm->type == P2P_VER &&
264 memcmp(act_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
271 * brcmf_p2p_is_gas_action() - true if p2p gas action type frame.
273 * @frame: action frame data.
274 * @frame_len: length of action frame data.
276 * Determine if action frame is p2p gas action type
278 static bool brcmf_p2p_is_gas_action(void *frame, u32 frame_len)
280 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
285 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
286 if (frame_len < sizeof(struct brcmf_p2psd_gas_pub_act_frame) - 1)
289 if (sd_act_frm->category != P2PSD_ACTION_CATEGORY)
292 if (sd_act_frm->action == P2PSD_ACTION_ID_GAS_IREQ ||
293 sd_act_frm->action == P2PSD_ACTION_ID_GAS_IRESP ||
294 sd_act_frm->action == P2PSD_ACTION_ID_GAS_CREQ ||
295 sd_act_frm->action == P2PSD_ACTION_ID_GAS_CRESP)
302 * brcmf_p2p_print_actframe() - debug print routine.
304 * @tx: Received or to be transmitted
305 * @frame: action frame data.
306 * @frame_len: length of action frame data.
308 * Print information about the p2p action frame
313 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
315 struct brcmf_p2p_pub_act_frame *pact_frm;
316 struct brcmf_p2p_action_frame *act_frm;
317 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
319 if (!frame || frame_len <= 2)
322 if (brcmf_p2p_is_pub_action(frame, frame_len)) {
323 pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
324 switch (pact_frm->subtype) {
325 case P2P_PAF_GON_REQ:
326 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
329 case P2P_PAF_GON_RSP:
330 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
333 case P2P_PAF_GON_CONF:
334 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
337 case P2P_PAF_INVITE_REQ:
338 brcmf_dbg(TRACE, "%s P2P Invitation Request Frame\n",
341 case P2P_PAF_INVITE_RSP:
342 brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
345 case P2P_PAF_DEVDIS_REQ:
346 brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
349 case P2P_PAF_DEVDIS_RSP:
350 brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
353 case P2P_PAF_PROVDIS_REQ:
354 brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
357 case P2P_PAF_PROVDIS_RSP:
358 brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
362 brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
366 } else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
367 act_frm = (struct brcmf_p2p_action_frame *)frame;
368 switch (act_frm->subtype) {
369 case P2P_AF_NOTICE_OF_ABSENCE:
370 brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
373 case P2P_AF_PRESENCE_REQ:
374 brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
377 case P2P_AF_PRESENCE_RSP:
378 brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
381 case P2P_AF_GO_DISC_REQ:
382 brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
386 brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
390 } else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
391 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
392 switch (sd_act_frm->action) {
393 case P2PSD_ACTION_ID_GAS_IREQ:
394 brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
397 case P2PSD_ACTION_ID_GAS_IRESP:
398 brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
401 case P2PSD_ACTION_ID_GAS_CREQ:
402 brcmf_dbg(TRACE, "%s P2P GAS Comback Request\n",
405 case P2PSD_ACTION_ID_GAS_CRESP:
406 brcmf_dbg(TRACE, "%s P2P GAS Comback Response\n",
410 brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
419 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
427 * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
429 * @ifp: ifp to use for iovars (primary).
430 * @p2p_mac: mac address to configure for p2p_da_override
432 static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
436 brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
437 brcmf_fil_iovar_int_set(ifp, "apsta", 1);
438 brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
440 /* In case of COB type, firmware has default mac address
441 * After Initializing firmware, we have to set current mac address to
442 * firmware for P2P device address
444 ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
447 brcmf_err("failed to update device address ret %d\n", ret);
453 * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
455 * @p2p: P2P specific data.
456 * @dev_addr: optional device address.
458 * P2P needs mac addresses for P2P device and interface. If no device
459 * address it specified, these are derived from the primary net device, ie.
460 * the permanent ethernet address of the device.
462 static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
464 struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
465 bool local_admin = false;
467 if (!dev_addr || is_zero_ether_addr(dev_addr)) {
468 dev_addr = pri_ifp->mac_addr;
472 /* Generate the P2P Device Address. This consists of the device's
473 * primary MAC address with the locally administered bit set.
475 memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
477 p2p->dev_addr[0] |= 0x02;
479 /* Generate the P2P Interface Address. If the discovery and connection
480 * BSSCFGs need to simultaneously co-exist, then this address must be
481 * different from the P2P Device Address, but also locally administered.
483 memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
484 p2p->int_addr[0] |= 0x02;
485 p2p->int_addr[4] ^= 0x80;
489 * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
491 * @request: the scan request as received from cfg80211.
493 * returns true if one of the ssids in the request matches the
494 * P2P wildcard ssid; otherwise returns false.
496 static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
498 struct cfg80211_ssid *ssids = request->ssids;
501 for (i = 0; i < request->n_ssids; i++) {
502 if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
505 brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
506 if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
507 BRCMF_P2P_WILDCARD_SSID_LEN))
514 * brcmf_p2p_set_discover_state - set discover state in firmware.
516 * @ifp: low-level interface object.
517 * @state: discover state to set.
518 * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
519 * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
521 static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
522 u16 chanspec, u16 listen_ms)
524 struct brcmf_p2p_disc_st_le discover_state;
526 brcmf_dbg(TRACE, "enter\n");
528 discover_state.state = state;
529 discover_state.chspec = cpu_to_le16(chanspec);
530 discover_state.dwell = cpu_to_le16(listen_ms);
531 ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
532 sizeof(discover_state));
537 * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
539 * @p2p: P2P specific data.
541 * Resets the discovery state and disables it in firmware.
543 static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
545 struct brcmf_cfg80211_vif *vif;
547 brcmf_dbg(TRACE, "enter\n");
549 /* Set the discovery state to SCAN */
550 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
551 (void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
553 /* Disable P2P discovery in the firmware */
554 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
555 (void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
561 * brcmf_p2p_enable_discovery() - initialize and configure discovery.
563 * @p2p: P2P specific data.
565 * Initializes the discovery device and configure the virtual interface.
567 static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
569 struct brcmf_cfg80211_vif *vif;
572 brcmf_dbg(TRACE, "enter\n");
573 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
575 brcmf_err("P2P config device not available\n");
580 if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
581 brcmf_dbg(INFO, "P2P config device already configured\n");
585 /* Re-initialize P2P Discovery in the firmware */
586 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
587 ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
589 brcmf_err("set p2p_disc error\n");
592 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
593 ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
595 brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
600 * Set wsec to any non-zero value in the discovery bsscfg
601 * to ensure our P2P probe responses have the privacy bit
602 * set in the 802.11 WPA IE. Some peer devices may not
603 * initiate WPS with us if this bit is not set.
605 ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
607 brcmf_err("wsec error %d\n", ret);
611 set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
617 * brcmf_p2p_escan() - initiate a P2P scan.
619 * @p2p: P2P specific data.
620 * @num_chans: number of channels to scan.
621 * @chanspecs: channel parameters for @num_chans channels.
622 * @search_state: P2P discover state to use.
623 * @action: scan action to pass to firmware.
624 * @bss_type: type of P2P bss.
626 static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
627 u16 chanspecs[], s32 search_state, u16 action,
628 enum p2p_bss_type bss_type)
631 s32 memsize = offsetof(struct brcmf_p2p_scan_le,
632 eparams.params_le.channel_list);
637 struct brcmf_cfg80211_vif *vif;
638 struct brcmf_p2p_scan_le *p2p_params;
639 struct brcmf_scan_params_le *sparams;
640 struct brcmf_ssid ssid;
642 memsize += num_chans * sizeof(__le16);
643 memblk = kzalloc(memsize, GFP_KERNEL);
647 vif = p2p->bss_idx[bss_type].vif;
649 brcmf_err("no vif for bss type %d\n", bss_type);
654 switch (search_state) {
655 case WL_P2P_DISC_ST_SEARCH:
657 * If we in SEARCH STATE, we don't need to set SSID explictly
658 * because dongle use P2P WILDCARD internally by default
662 memset(ssid.SSID, 0, sizeof(ssid.SSID));
664 case WL_P2P_DISC_ST_SCAN:
666 * wpa_supplicant has p2p_find command with type social or
667 * progressive. For progressive, we need to set the ssid to
668 * P2P WILDCARD because we just do broadcast scan unless
671 ssid.SSID_len = BRCMF_P2P_WILDCARD_SSID_LEN;
672 memcpy(ssid.SSID, BRCMF_P2P_WILDCARD_SSID, ssid.SSID_len);
675 brcmf_err(" invalid search state %d\n", search_state);
680 brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
683 * set p2p scan parameters.
685 p2p_params = (struct brcmf_p2p_scan_le *)memblk;
686 p2p_params->type = 'E';
688 /* determine the scan engine parameters */
689 sparams = &p2p_params->eparams.params_le;
690 sparams->bss_type = DOT11_BSSTYPE_ANY;
691 if (p2p->cfg->active_scan)
692 sparams->scan_type = 0;
694 sparams->scan_type = 1;
696 memset(&sparams->bssid, 0xFF, ETH_ALEN);
698 memcpy(sparams->ssid_le.SSID, ssid.SSID, ssid.SSID_len);
699 sparams->ssid_le.SSID_len = cpu_to_le32(ssid.SSID_len);
700 sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
703 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
704 * supported by the supplicant.
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;
710 else if (wl_get_vif_state_all(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
713 active = P2PAPI_SCAN_DWELL_TIME_MS;
715 /* Override scan params to find a peer for a connection */
716 if (num_chans == 1) {
717 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
718 /* WAR to sync with presence period of VSDB GO.
719 * send probe request more frequently
721 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
723 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
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]);
738 /* set the escan specific parameters */
739 p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
740 p2p_params->eparams.action = cpu_to_le16(action);
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);
745 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
752 * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
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.
759 * Determines the P2P discovery state based to scan request parameters and
760 * validates the channels in the request.
762 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
763 struct brcmf_if *ifp,
764 struct cfg80211_scan_request *request,
767 struct brcmf_p2p_info *p2p = &cfg->p2p;
769 s32 search_state = WL_P2P_DISC_ST_SCAN;
770 struct brcmf_cfg80211_vif *vif;
771 struct net_device *dev = NULL;
772 int i, num_nodfs = 0;
775 brcmf_dbg(TRACE, "enter\n");
782 if (request->n_channels) {
783 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
789 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
791 dev = vif->wdev.netdev;
792 if (request->n_channels == 3 &&
793 request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
794 request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
795 request->channels[2]->hw_value == SOCIAL_CHAN_3) {
796 /* SOCIAL CHANNELS 1, 6, 11 */
797 search_state = WL_P2P_DISC_ST_SEARCH;
798 brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
799 } else if (dev != NULL && vif->mode == WL_MODE_AP) {
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;
804 brcmf_dbg(INFO, "P2P SCAN STATE START\n");
808 * no P2P scanning on passive or DFS channels.
810 for (i = 0; i < request->n_channels; i++) {
811 struct ieee80211_channel *chan = request->channels[i];
813 if (chan->flags & (IEEE80211_CHAN_RADAR |
814 IEEE80211_CHAN_PASSIVE_SCAN))
817 chanspecs[i] = channel_to_chanspec(&p2p->cfg->d11inf,
819 brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
820 num_nodfs, chan->hw_value, chanspecs[i]);
823 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
824 action, P2PAPI_BSSCFG_DEVICE);
828 brcmf_err("error (%d)\n", err);
834 * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
836 * @ie: string of information elements.
837 * @ie_len: length of string.
839 * Scan ie for p2p ie and look for attribute 6 channel. If available determine
840 * channel and return it.
842 static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
848 err = cfg80211_get_p2p_attr(ie, ie_len,
849 IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
850 channel_ie, sizeof(channel_ie));
854 /* listen channel subel length format: */
855 /* 3(country) + 1(op. class) + 1(chan num) */
856 listen_channel = (s32)channel_ie[3 + 1];
858 if (listen_channel == SOCIAL_CHAN_1 ||
859 listen_channel == SOCIAL_CHAN_2 ||
860 listen_channel == SOCIAL_CHAN_3) {
861 brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
862 return listen_channel;
870 * brcmf_p2p_scan_prep() - prepare scan based on request.
872 * @wiphy: wiphy device.
873 * @request: scan request from cfg80211.
874 * @vif: vif on which scan request is to be executed.
876 * Prepare the scan appropriately for type of scan requested. Overrides the
877 * escan .run() callback for peer-to-peer scanning.
879 int brcmf_p2p_scan_prep(struct wiphy *wiphy,
880 struct cfg80211_scan_request *request,
881 struct brcmf_cfg80211_vif *vif)
883 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
884 struct brcmf_p2p_info *p2p = &cfg->p2p;
887 if (brcmf_p2p_scan_is_p2p_request(request)) {
888 /* find my listen channel */
889 err = brcmf_p2p_find_listen_channel(request->ie,
894 p2p->afx_hdl.my_listen_chan = err;
896 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
897 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
899 err = brcmf_p2p_enable_discovery(p2p);
903 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
905 /* override .run_escan() callback. */
906 cfg->escan_info.run = brcmf_p2p_run_escan;
908 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
909 request->ie, request->ie_len);
915 * brcmf_p2p_discover_listen() - set firmware to discover listen state.
918 * @channel: channel nr for discover listen.
919 * @duration: time in ms to stay on channel.
923 brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
925 struct brcmf_cfg80211_vif *vif;
926 struct brcmu_chan ch;
929 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
931 brcmf_err("Discovery is not set, so we have nothing to do\n");
936 if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
937 brcmf_err("Previous LISTEN is not completed yet\n");
938 /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
943 ch.bw = BRCMU_CHAN_BW_20;
944 p2p->cfg->d11inf.encchspec(&ch);
945 err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
946 ch.chspec, (u16)duration);
948 set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
949 p2p->remain_on_channel_cookie++;
957 * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
959 * @wiphy: wiphy device.
960 * @channel: channel to stay on.
961 * @duration: time in ms to remain on channel.
964 int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
965 struct ieee80211_channel *channel,
966 unsigned int duration, u64 *cookie)
968 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
969 struct brcmf_p2p_info *p2p = &cfg->p2p;
973 channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
974 brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
977 err = brcmf_p2p_enable_discovery(p2p);
980 err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
984 memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
985 *cookie = p2p->remain_on_channel_cookie;
986 cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
994 * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
996 * @ifp: interfac control.
997 * @e: event message. Not used, to make it usable for fweh event dispatcher.
998 * @data: payload of message. Not used.
1001 int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
1002 const struct brcmf_event_msg *e,
1005 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1006 struct brcmf_p2p_info *p2p = &cfg->p2p;
1008 brcmf_dbg(TRACE, "Enter\n");
1009 if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
1011 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1013 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1015 brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1016 complete(&p2p->wait_next_af);
1019 cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1020 p2p->remain_on_channel_cookie,
1021 &p2p->remain_on_channel,
1029 * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1031 * @ifp: interfac control.
1034 void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1038 brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1039 brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1044 * brcmf_p2p_act_frm_search() - search function for action frame.
1047 * channel: channel on which action frame is to be trasmitted.
1049 * search function to reach at common channel to send action frame. When
1050 * channel is 0 then all social channels will be used to send af
1052 static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1056 u16 *default_chan_list;
1058 struct brcmu_chan ch;
1060 brcmf_dbg(TRACE, "Enter\n");
1063 channel_cnt = AF_PEER_SEARCH_CNT;
1065 channel_cnt = SOCIAL_CHAN_CNT;
1066 default_chan_list = kzalloc(channel_cnt * sizeof(*default_chan_list),
1068 if (default_chan_list == NULL) {
1069 brcmf_err("channel list allocation failed\n");
1073 ch.bw = BRCMU_CHAN_BW_20;
1076 p2p->cfg->d11inf.encchspec(&ch);
1077 /* insert same channel to the chan_list */
1078 for (i = 0; i < channel_cnt; i++)
1079 default_chan_list[i] = ch.chspec;
1081 ch.chnum = SOCIAL_CHAN_1;
1082 p2p->cfg->d11inf.encchspec(&ch);
1083 default_chan_list[0] = ch.chspec;
1084 ch.chnum = SOCIAL_CHAN_2;
1085 p2p->cfg->d11inf.encchspec(&ch);
1086 default_chan_list[1] = ch.chspec;
1087 ch.chnum = SOCIAL_CHAN_3;
1088 p2p->cfg->d11inf.encchspec(&ch);
1089 default_chan_list[2] = ch.chspec;
1091 err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
1092 WL_P2P_DISC_ST_SEARCH, WL_ESCAN_ACTION_START,
1093 P2PAPI_BSSCFG_DEVICE);
1094 kfree(default_chan_list);
1101 * brcmf_p2p_afx_handler() - afx worker thread.
1106 static void brcmf_p2p_afx_handler(struct work_struct *work)
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,
1114 if (!afx_hdl->is_active)
1117 if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1119 err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
1120 100 * (1 + prandom_u32() % 3));
1122 err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1125 brcmf_err("ERROR occurred! value is (%d)\n", err);
1126 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1128 complete(&afx_hdl->act_frm_scan);
1134 * brcmf_p2p_af_searching_channel() - search channel.
1136 * @p2p: p2p device info struct.
1139 static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1141 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1142 struct brcmf_cfg80211_vif *pri_vif;
1143 unsigned long duration;
1146 brcmf_dbg(TRACE, "Enter\n");
1148 pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1150 INIT_COMPLETION(afx_hdl->act_frm_scan);
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;
1155 /* Loop to wait until we find a peer's channel or the
1156 * pending action frame tx is cancelled.
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",
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,
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,
1182 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1183 (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1188 /* if sta is connected or connecting, sleep for a while before
1189 * retry af tx or finding a peer
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);
1196 brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1197 afx_hdl->peer_chan);
1198 afx_hdl->is_active = false;
1200 clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1202 return afx_hdl->peer_chan;
1207 * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1209 * @cfg: common configuration struct.
1210 * @bi: bss info struct, result from scan.
1213 bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1214 struct brcmf_bss_info_le *bi)
1217 struct brcmf_p2p_info *p2p = &cfg->p2p;
1218 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1219 struct brcmu_chan ch;
1222 u8 p2p_dev_addr[ETH_ALEN];
1224 if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
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);
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));
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));
1244 (!memcmp(p2p_dev_addr, afx_hdl->tx_dst_addr, ETH_ALEN))) {
1246 ch.chspec = le16_to_cpu(bi->chanspec);
1247 cfg->d11inf.decchspec(&ch);
1248 bi->ctl_ch = ch.chnum;
1250 afx_hdl->peer_chan = bi->ctl_ch;
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);
1259 * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1261 * @cfg: common configuration struct.
1265 brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1267 struct brcmf_p2p_info *p2p = &cfg->p2p;
1268 struct brcmf_if *ifp = cfg->escan_info.ifp;
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.
1277 if (p2p->af_sent_channel)
1278 brcmf_notify_escan_complete(cfg, ifp, true, true);
1279 } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1281 brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1282 /* So abort scan to cancel listen */
1283 brcmf_notify_escan_complete(cfg, ifp, true, true);
1289 * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1291 * @p2p: p2p device info struct.
1293 * return true if recevied action frame is to be dropped.
1296 brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1298 struct brcmf_cfg80211_info *cfg = p2p->cfg;
1299 struct brcmf_if *ifp;
1301 brcmf_dbg(TRACE, "Enter\n");
1303 if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1304 !p2p->gon_req_action)
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.
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
1320 if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1322 complete(&p2p->afx_hdl.act_frm_scan);
1323 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1325 brcmf_p2p_stop_wait_next_action_frame(cfg);
1329 /* drop gon request of peer to process gon request by this device. */
1330 brcmf_dbg(INFO, "Drop received gon req !!!\n");
1337 * brcmf_p2p_notify_action_frame_rx() - received action frame.
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.
1344 int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1345 const struct brcmf_event_msg *e,
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;
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;
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;
1357 struct brcmu_chan ch;
1358 struct ieee80211_mgmt *mgmt_frame;
1363 ch.chspec = be16_to_cpu(rxframe->chanspec);
1364 cfg->d11inf.decchspec(&ch);
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)
1371 brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
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,
1381 (memcmp(afx_hdl->tx_dst_addr, e->addr,
1383 afx_hdl->peer_chan = ch.chnum;
1384 brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1385 afx_hdl->peer_chan);
1386 complete(&afx_hdl->act_frm_scan);
1390 /* After complete GO Negotiation, roll back to mpc mode */
1391 if ((action == P2P_PAF_GON_CONF) ||
1392 (action == P2P_PAF_PROVDIS_RSP))
1393 brcmf_set_mpc(ifp, 1);
1394 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);
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;
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,
1408 /* Stop waiting for next AF. */
1409 brcmf_p2p_stop_wait_next_action_frame(cfg);
1412 mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1413 mgmt_frame_len, GFP_KERNEL);
1415 brcmf_err("No memory available for action frame\n");
1418 memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
1419 brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1421 memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1422 mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
1423 memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
1424 mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1426 freq = ieee80211_channel_to_frequency(ch.chnum,
1427 ch.band == BRCMU_CHAN_BAND_2G ?
1428 IEEE80211_BAND_2GHZ :
1429 IEEE80211_BAND_5GHZ);
1431 wdev = &ifp->vif->wdev;
1432 cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len,
1441 * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1443 * @ifp: interfac control.
1444 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1448 int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1449 const struct brcmf_event_msg *e,
1452 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1453 struct brcmf_p2p_info *p2p = &cfg->p2p;
1455 brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1456 e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1457 "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1460 if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1463 if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
1464 if (e->status == BRCMF_E_STATUS_SUCCESS)
1465 set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1468 set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1469 /* If there is no ack, we don't need to wait for
1470 * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1472 brcmf_p2p_stop_wait_next_action_frame(cfg);
1476 complete(&p2p->send_af_done);
1483 * brcmf_p2p_tx_action_frame() - send action frame over fil.
1485 * @p2p: p2p info struct for vif.
1486 * @af_params: action frame data/info.
1488 * Send an action frame immediately without doing channel synchronization.
1490 * This function waits for a completion event before returning.
1491 * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1492 * frame is transmitted.
1494 static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1495 struct brcmf_fil_af_params_le *af_params)
1497 struct brcmf_cfg80211_vif *vif;
1501 brcmf_dbg(TRACE, "Enter\n");
1503 INIT_COMPLETION(p2p->send_af_done);
1504 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1505 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1507 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1508 err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1509 sizeof(*af_params));
1511 brcmf_err(" sending action frame has failed\n");
1515 p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1516 p2p->af_tx_sent_jiffies = jiffies;
1518 timeout = wait_for_completion_timeout(&p2p->send_af_done,
1519 msecs_to_jiffies(P2P_AF_MAX_WAIT_TIME));
1521 if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1522 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1525 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1527 /* clear status bit for action tx */
1528 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1529 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1537 * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1539 * @cfg: driver private data for cfg80211 interface.
1540 * @af_params: action frame data/info.
1541 * @config_af_params: configuration data for action frame.
1543 * routine which transmits ation frame public type.
1545 static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1546 struct brcmf_fil_af_params_le *af_params,
1547 struct brcmf_config_af_params *config_af_params)
1549 struct brcmf_p2p_info *p2p = &cfg->p2p;
1550 struct brcmf_fil_action_frame_le *action_frame;
1551 struct brcmf_p2p_pub_act_frame *act_frm;
1555 action_frame = &af_params->action_frame;
1556 act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1558 config_af_params->extra_listen = true;
1560 switch (act_frm->subtype) {
1561 case P2P_PAF_GON_REQ:
1562 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1563 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1564 config_af_params->mpc_onoff = 0;
1565 config_af_params->search_channel = true;
1566 p2p->next_af_subtype = act_frm->subtype + 1;
1567 p2p->gon_req_action = true;
1568 /* increase dwell time to wait for RESP frame */
1569 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1571 case P2P_PAF_GON_RSP:
1572 p2p->next_af_subtype = act_frm->subtype + 1;
1573 /* increase dwell time to wait for CONF frame */
1574 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1576 case P2P_PAF_GON_CONF:
1577 /* If we reached till GO Neg confirmation reset the filter */
1578 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1579 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1580 /* turn on mpc again if go nego is done */
1581 config_af_params->mpc_onoff = 1;
1582 /* minimize dwell time */
1583 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1584 config_af_params->extra_listen = false;
1586 case P2P_PAF_INVITE_REQ:
1587 config_af_params->search_channel = true;
1588 p2p->next_af_subtype = act_frm->subtype + 1;
1589 /* increase dwell time */
1590 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1592 case P2P_PAF_INVITE_RSP:
1593 /* minimize dwell time */
1594 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1595 config_af_params->extra_listen = false;
1597 case P2P_PAF_DEVDIS_REQ:
1598 config_af_params->search_channel = true;
1599 p2p->next_af_subtype = act_frm->subtype + 1;
1600 /* maximize dwell time to wait for RESP frame */
1601 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1603 case P2P_PAF_DEVDIS_RSP:
1604 /* minimize dwell time */
1605 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1606 config_af_params->extra_listen = false;
1608 case P2P_PAF_PROVDIS_REQ:
1609 ie_len = le16_to_cpu(action_frame->len) -
1610 offsetof(struct brcmf_p2p_pub_act_frame, elts);
1611 if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1612 IEEE80211_P2P_ATTR_GROUP_ID,
1614 config_af_params->search_channel = true;
1615 config_af_params->mpc_onoff = 0;
1616 p2p->next_af_subtype = act_frm->subtype + 1;
1617 /* increase dwell time to wait for RESP frame */
1618 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1620 case P2P_PAF_PROVDIS_RSP:
1621 /* wpa_supplicant send go nego req right after prov disc */
1622 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1623 /* increase dwell time to MED level */
1624 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1625 config_af_params->extra_listen = false;
1628 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1636 * brcmf_p2p_send_action_frame() - send action frame .
1638 * @cfg: driver private data for cfg80211 interface.
1639 * @ndev: net device to transmit on.
1640 * @af_params: configuration data for action frame.
1642 bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1643 struct net_device *ndev,
1644 struct brcmf_fil_af_params_le *af_params)
1646 struct brcmf_p2p_info *p2p = &cfg->p2p;
1647 struct brcmf_if *ifp = netdev_priv(ndev);
1648 struct brcmf_fil_action_frame_le *action_frame;
1649 struct brcmf_config_af_params config_af_params;
1650 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1651 u16 action_frame_len;
1656 s32 extra_listen_time;
1659 action_frame = &af_params->action_frame;
1660 action_frame_len = le16_to_cpu(action_frame->len);
1662 brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1664 /* Add the default dwell time. Dwell time to stay off-channel */
1665 /* to wait for a response action frame after transmitting an */
1666 /* GO Negotiation action frame */
1667 af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1669 category = action_frame->data[DOT11_ACTION_CAT_OFF];
1670 action = action_frame->data[DOT11_ACTION_ACT_OFF];
1672 /* initialize variables */
1673 p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
1674 p2p->gon_req_action = false;
1676 /* config parameters */
1677 config_af_params.mpc_onoff = -1;
1678 config_af_params.search_channel = false;
1679 config_af_params.extra_listen = false;
1681 if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1682 /* p2p public action frame process */
1683 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1684 /* Just send unknown subtype frame with */
1685 /* default parameters. */
1686 brcmf_err("P2P Public action frame, unknown subtype.\n");
1688 } else if (brcmf_p2p_is_gas_action(action_frame->data,
1689 action_frame_len)) {
1690 /* service discovery process */
1691 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1692 action == P2PSD_ACTION_ID_GAS_CREQ) {
1693 /* configure service discovery query frame */
1694 config_af_params.search_channel = true;
1696 /* save next af suptype to cancel */
1697 /* remaining dwell time */
1698 p2p->next_af_subtype = action + 1;
1700 af_params->dwell_time =
1701 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1702 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1703 action == P2PSD_ACTION_ID_GAS_CRESP) {
1704 /* configure service discovery response frame */
1705 af_params->dwell_time =
1706 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1708 brcmf_err("Unknown action type: %d\n", action);
1711 } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1712 action_frame_len)) {
1713 /* do not configure anything. it will be */
1714 /* sent with a default configuration */
1716 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1721 /* if connecting on primary iface, sleep for a while before sending
1724 if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1725 &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1728 /* if scan is ongoing, abort current scan. */
1729 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1730 brcmf_abort_scanning(cfg);
1732 memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1734 /* To make sure to send successfully action frame, turn off mpc */
1735 if (config_af_params.mpc_onoff == 0)
1736 brcmf_set_mpc(ifp, 0);
1738 /* set status and destination address before sending af */
1739 if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1740 /* set status to cancel the remained dwell time in rx process */
1741 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1744 p2p->af_sent_channel = 0;
1745 set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1746 /* validate channel and p2p ies */
1747 if (config_af_params.search_channel &&
1748 IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1749 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1750 afx_hdl = &p2p->afx_hdl;
1751 afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1753 if (brcmf_p2p_af_searching_channel(p2p) ==
1754 P2P_INVALID_CHANNEL) {
1755 brcmf_err("Couldn't find peer's channel.\n");
1759 /* Abort scan even for VSDB scenarios. Scan gets aborted in
1760 * firmware but after the check of piggyback algorithm. To take
1761 * care of current piggback algo, lets abort the scan here
1764 brcmf_notify_escan_complete(cfg, ifp, true, true);
1766 /* update channel */
1767 af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1771 while (!p2p->block_gon_req_tx &&
1772 (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
1773 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1777 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
1778 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1782 clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1784 /* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1785 * if we coundn't get the next action response frame and dongle does
1786 * not keep the dwell time, go to listen state again to get next action
1789 if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1790 test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1791 p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1792 delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1793 if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1794 extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1797 extra_listen_time = 0;
1798 if (extra_listen_time > 50) {
1799 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1801 brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1802 le32_to_cpu(af_params->dwell_time),
1804 extra_listen_time += 100;
1805 if (!brcmf_p2p_discover_listen(p2p,
1806 p2p->af_sent_channel,
1807 extra_listen_time)) {
1808 unsigned long duration;
1810 extra_listen_time += 100;
1811 duration = msecs_to_jiffies(extra_listen_time);
1812 wait_for_completion_timeout(&p2p->wait_next_af,
1815 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1820 if (p2p->block_gon_req_tx) {
1821 /* if ack is true, supplicant will wait more time(100ms).
1822 * so we will return it as a success to get more time .
1824 p2p->block_gon_req_tx = false;
1828 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1829 /* if all done, turn mpc on again */
1830 if (config_af_params.mpc_onoff == 1)
1831 brcmf_set_mpc(ifp, 1);
1837 * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1839 * @ifp: interface pointer for which event was received.
1841 * @data: payload of event message (probe request).
1843 s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1844 const struct brcmf_event_msg *e,
1847 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1848 struct brcmf_p2p_info *p2p = &cfg->p2p;
1849 struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1850 struct brcmf_cfg80211_vif *vif = ifp->vif;
1851 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1852 u16 chanspec = be16_to_cpu(rxframe->chanspec);
1853 struct brcmu_chan ch;
1859 brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1862 ch.chspec = be16_to_cpu(rxframe->chanspec);
1863 cfg->d11inf.decchspec(&ch);
1865 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
1866 (memcmp(afx_hdl->tx_dst_addr, e->addr, ETH_ALEN) == 0)) {
1867 afx_hdl->peer_chan = ch.chnum;
1868 brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
1869 afx_hdl->peer_chan);
1870 complete(&afx_hdl->act_frm_scan);
1873 /* Firmware sends us two proberesponses for each idx one. At the */
1874 /* moment anything but bsscfgidx 0 is passed up to supplicant */
1875 if (e->bsscfgidx == 0)
1878 /* Filter any P2P probe reqs arriving during the GO-NEG Phase */
1879 if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
1880 brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
1884 /* Check if wpa_supplicant has registered for this frame */
1885 brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
1886 mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
1887 if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1890 mgmt_frame = (u8 *)(rxframe + 1);
1891 mgmt_frame_len = e->datalen - sizeof(*rxframe);
1892 freq = ieee80211_channel_to_frequency(ch.chnum,
1893 ch.band == BRCMU_CHAN_BAND_2G ?
1894 IEEE80211_BAND_2GHZ :
1895 IEEE80211_BAND_5GHZ);
1897 cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len,
1900 brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
1901 mgmt_frame_len, e->datalen, chanspec, freq);
1908 * brcmf_p2p_attach() - attach for P2P.
1910 * @cfg: driver private data for cfg80211 interface.
1912 s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg)
1914 struct brcmf_if *pri_ifp;
1915 struct brcmf_if *p2p_ifp;
1916 struct brcmf_cfg80211_vif *p2p_vif;
1917 struct brcmf_p2p_info *p2p;
1918 struct brcmf_pub *drvr;
1927 pri_ifp = drvr->iflist[0];
1928 p2p_ifp = drvr->iflist[1];
1930 p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
1933 p2p_vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_P2P_DEVICE,
1935 if (IS_ERR(p2p_vif)) {
1936 brcmf_err("could not create discovery vif\n");
1941 p2p_vif->ifp = p2p_ifp;
1942 p2p_ifp->vif = p2p_vif;
1943 p2p_vif->wdev.netdev = p2p_ifp->ndev;
1944 p2p_ifp->ndev->ieee80211_ptr = &p2p_vif->wdev;
1945 SET_NETDEV_DEV(p2p_ifp->ndev, wiphy_dev(cfg->wiphy));
1947 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
1949 brcmf_p2p_generate_bss_mac(p2p, NULL);
1950 memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
1951 brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
1953 /* Initialize P2P Discovery in the firmware */
1954 err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
1956 brcmf_err("set p2p_disc error\n");
1957 brcmf_free_vif(p2p_vif);
1960 /* obtain bsscfg index for P2P discovery */
1961 err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
1963 brcmf_err("retrieving discover bsscfg index failed\n");
1964 brcmf_free_vif(p2p_vif);
1967 /* Verify that firmware uses same bssidx as driver !! */
1968 if (p2p_ifp->bssidx != bssidx) {
1969 brcmf_err("Incorrect bssidx=%d, compared to p2p_ifp->bssidx=%d\n",
1970 bssidx, p2p_ifp->bssidx);
1971 brcmf_free_vif(p2p_vif);
1975 init_completion(&p2p->send_af_done);
1976 INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
1977 init_completion(&p2p->afx_hdl.act_frm_scan);
1978 init_completion(&p2p->wait_next_af);
1986 * brcmf_p2p_detach() - detach P2P.
1988 * @p2p: P2P specific data.
1990 void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
1992 struct brcmf_cfg80211_vif *vif;
1994 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1996 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
1997 brcmf_p2p_deinit_discovery(p2p);
1998 /* remove discovery interface */
1999 brcmf_free_vif(vif);
2000 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
2002 /* just set it all to zero */
2003 memset(p2p, 0, sizeof(*p2p));
2007 * brcmf_p2p_get_current_chanspec() - Get current operation channel.
2009 * @p2p: P2P specific data.
2010 * @chanspec: chanspec to be returned.
2012 static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
2015 struct brcmf_if *ifp;
2016 struct brcmf_fil_chan_info_le ci;
2017 struct brcmu_chan ch;
2020 ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2024 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_CHANNEL, &ci, sizeof(ci));
2026 ch.chnum = le32_to_cpu(ci.hw_channel);
2027 ch.bw = BRCMU_CHAN_BW_20;
2028 p2p->cfg->d11inf.encchspec(&ch);
2029 *chanspec = ch.chspec;
2033 * Change a P2P Role.
2035 * @mac: MAC address of the BSS to change a role
2036 * Returns 0 if success.
2038 int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
2039 enum brcmf_fil_p2p_if_types if_type)
2041 struct brcmf_p2p_info *p2p = &cfg->p2p;
2042 struct brcmf_cfg80211_vif *vif;
2043 struct brcmf_fil_p2p_if_le if_request;
2047 brcmf_dbg(TRACE, "Enter\n");
2049 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
2051 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
2054 brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
2055 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
2057 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
2060 brcmf_set_mpc(vif->ifp, 0);
2062 /* In concurrency case, STA may be already associated in a particular */
2063 /* channel. so retrieve the current channel of primary interface and */
2064 /* then start the virtual interface on that. */
2065 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2067 if_request.type = cpu_to_le16((u16)if_type);
2068 if_request.chspec = cpu_to_le16(chanspec);
2069 memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
2071 brcmf_cfg80211_arm_vif_event(cfg, vif);
2072 err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
2073 sizeof(if_request));
2075 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
2076 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2079 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_CHANGE,
2080 msecs_to_jiffies(1500));
2081 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2083 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
2087 err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2088 BRCMF_SCB_TIMEOUT_VALUE);
2093 static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2094 struct brcmf_if *ifp, u8 ea[ETH_ALEN],
2095 enum brcmf_fil_p2p_if_types iftype)
2097 struct brcmf_fil_p2p_if_le if_request;
2101 /* we need a default channel */
2102 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2104 /* fill the firmware request */
2105 memcpy(if_request.addr, ea, ETH_ALEN);
2106 if_request.type = cpu_to_le16((u16)iftype);
2107 if_request.chspec = cpu_to_le16(chanspec);
2109 err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2110 sizeof(if_request));
2117 static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2119 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2120 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2121 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2122 u8 *addr = vif->wdev.netdev->dev_addr;
2124 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2127 static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2129 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2130 struct net_device *pri_ndev = cfg_to_ndev(cfg);
2131 struct brcmf_if *ifp = netdev_priv(pri_ndev);
2132 u8 *addr = vif->wdev.netdev->dev_addr;
2134 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2138 * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2140 * @p2p: P2P specific data.
2141 * @wiphy: wiphy device of new interface.
2142 * @addr: mac address for this new interface.
2144 static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2145 struct wiphy *wiphy,
2148 struct brcmf_cfg80211_vif *p2p_vif;
2149 struct brcmf_if *p2p_ifp;
2150 struct brcmf_if *pri_ifp;
2154 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2155 return ERR_PTR(-ENOSPC);
2157 p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE,
2159 if (IS_ERR(p2p_vif)) {
2160 brcmf_err("could not create discovery vif\n");
2161 return (struct wireless_dev *)p2p_vif;
2164 pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2165 brcmf_p2p_generate_bss_mac(p2p, addr);
2166 brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2168 brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
2170 /* Initialize P2P Discovery in the firmware */
2171 err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2173 brcmf_err("set p2p_disc error\n");
2174 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2178 /* wait for firmware event */
2179 err = brcmf_cfg80211_wait_vif_event_timeout(p2p->cfg, BRCMF_E_IF_ADD,
2180 msecs_to_jiffies(1500));
2181 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2183 brcmf_err("timeout occurred\n");
2188 /* discovery interface created */
2189 p2p_ifp = p2p_vif->ifp;
2190 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2191 memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2192 memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2194 /* verify bsscfg index for P2P discovery */
2195 err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
2197 brcmf_err("retrieving discover bsscfg index failed\n");
2201 WARN_ON(p2p_ifp->bssidx != bssidx);
2203 init_completion(&p2p->send_af_done);
2204 INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2205 init_completion(&p2p->afx_hdl.act_frm_scan);
2206 init_completion(&p2p->wait_next_af);
2208 return &p2p_vif->wdev;
2211 brcmf_free_vif(p2p_vif);
2212 return ERR_PTR(err);
2216 * brcmf_p2p_delete_p2pdev() - delete P2P_DEVICE virtual interface.
2218 * @vif: virtual interface object to delete.
2220 static void brcmf_p2p_delete_p2pdev(struct brcmf_cfg80211_vif *vif)
2222 struct brcmf_p2p_info *p2p = &vif->ifp->drvr->config->p2p;
2224 cfg80211_unregister_wdev(&vif->wdev);
2225 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
2226 brcmf_free_vif(vif);
2230 * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2232 * @wiphy: wiphy device of new interface.
2233 * @name: name of the new interface.
2234 * @type: nl80211 interface type.
2236 * @params: contains mac address for P2P device.
2238 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
2239 enum nl80211_iftype type, u32 *flags,
2240 struct vif_params *params)
2242 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2243 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2244 struct brcmf_cfg80211_vif *vif;
2245 enum brcmf_fil_p2p_if_types iftype;
2249 if (brcmf_cfg80211_vif_event_armed(cfg))
2250 return ERR_PTR(-EBUSY);
2252 brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
2255 case NL80211_IFTYPE_P2P_CLIENT:
2256 iftype = BRCMF_FIL_P2P_IF_CLIENT;
2259 case NL80211_IFTYPE_P2P_GO:
2260 iftype = BRCMF_FIL_P2P_IF_GO;
2263 case NL80211_IFTYPE_P2P_DEVICE:
2264 return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2267 return ERR_PTR(-EOPNOTSUPP);
2270 vif = brcmf_alloc_vif(cfg, type, false);
2272 return (struct wireless_dev *)vif;
2273 brcmf_cfg80211_arm_vif_event(cfg, vif);
2275 err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
2278 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2282 /* wait for firmware event */
2283 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
2284 msecs_to_jiffies(1500));
2285 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2287 brcmf_err("timeout occurred\n");
2292 /* interface created in firmware */
2295 brcmf_err("no if pointer provided\n");
2300 strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
2301 err = brcmf_net_attach(ifp, true);
2303 brcmf_err("Registering netdevice failed\n");
2306 cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
2307 /* Disable firmware roaming for P2P interface */
2308 brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2309 if (iftype == BRCMF_FIL_P2P_IF_GO) {
2310 /* set station timeout for p2p */
2311 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2312 BRCMF_SCB_TIMEOUT_VALUE);
2314 return &ifp->vif->wdev;
2317 brcmf_free_vif(vif);
2318 return ERR_PTR(err);
2322 * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2324 * @wiphy: wiphy device of interface.
2325 * @wdev: wireless device of interface.
2327 * TODO: not yet supported.
2329 int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2331 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
2332 struct brcmf_p2p_info *p2p = &cfg->p2p;
2333 struct brcmf_cfg80211_vif *vif;
2334 unsigned long jiffie_timeout = msecs_to_jiffies(1500);
2335 bool wait_for_disable = false;
2338 brcmf_dbg(TRACE, "delete P2P vif\n");
2339 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2341 switch (vif->wdev.iftype) {
2342 case NL80211_IFTYPE_P2P_CLIENT:
2343 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2344 wait_for_disable = true;
2347 case NL80211_IFTYPE_P2P_GO:
2348 if (!brcmf_p2p_disable_p2p_if(vif))
2349 wait_for_disable = true;
2352 case NL80211_IFTYPE_P2P_DEVICE:
2353 brcmf_p2p_delete_p2pdev(vif);
2360 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2361 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2363 if (wait_for_disable)
2364 wait_for_completion_timeout(&cfg->vif_disabled,
2365 msecs_to_jiffies(500));
2367 brcmf_vif_clear_mgmt_ies(vif);
2369 brcmf_cfg80211_arm_vif_event(cfg, vif);
2370 err = brcmf_p2p_release_p2p_if(vif);
2372 /* wait for firmware event */
2373 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_DEL,
2380 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2381 brcmf_free_vif(vif);
2382 p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
2387 int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2389 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2390 struct brcmf_p2p_info *p2p = &cfg->p2p;
2391 struct brcmf_cfg80211_vif *vif;
2394 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2395 mutex_lock(&cfg->usr_sync);
2396 err = brcmf_p2p_enable_discovery(p2p);
2398 set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2399 mutex_unlock(&cfg->usr_sync);
2403 void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2405 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2406 struct brcmf_p2p_info *p2p = &cfg->p2p;
2407 struct brcmf_cfg80211_vif *vif;
2409 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2410 mutex_lock(&cfg->usr_sync);
2411 (void)brcmf_p2p_deinit_discovery(p2p);
2412 brcmf_abort_scanning(cfg);
2413 clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2414 mutex_unlock(&cfg->usr_sync);