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