Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / brcm80211 / brcmfmac / p2p.c
index 454a8deb0f102776f0f9f213622454bae6a14628..94ff045df2b3a29716a01bb25058403cae8e74e0 100644 (file)
@@ -15,6 +15,7 @@
  */
 #include <linux/slab.h>
 #include <linux/netdevice.h>
+#include <linux/etherdevice.h>
 #include <net/cfg80211.h>
 
 #include <brcmu_wifi.h>
@@ -455,7 +456,9 @@ static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
 {
        s32 ret = 0;
 
+       brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
        brcmf_fil_iovar_int_set(ifp, "apsta", 1);
+       brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
 
        /* In case of COB type, firmware has default mac address
         * After Initializing firmware, we have to set current mac address to
@@ -473,28 +476,35 @@ static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
  * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
  *
  * @p2p: P2P specific data.
+ * @dev_addr: optional device address.
  *
- * P2P needs mac addresses for P2P device and interface. These are
- * derived from the primary net device, ie. the permanent ethernet
- * address of the device.
+ * P2P needs mac addresses for P2P device and interface. If no device
+ * address it specified, these are derived from the primary net device, ie.
+ * the permanent ethernet address of the device.
  */
-static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p)
+static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
 {
        struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
-       struct brcmf_if *p2p_ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
+       bool local_admin = false;
+
+       if (!dev_addr || is_zero_ether_addr(dev_addr)) {
+               dev_addr = pri_ifp->mac_addr;
+               local_admin = true;
+       }
 
        /* Generate the P2P Device Address.  This consists of the device's
         * primary MAC address with the locally administered bit set.
         */
-       memcpy(p2p->dev_addr, pri_ifp->mac_addr, ETH_ALEN);
-       p2p->dev_addr[0] |= 0x02;
-       memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
+       memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
+       if (local_admin)
+               p2p->dev_addr[0] |= 0x02;
 
        /* Generate the P2P Interface Address.  If the discovery and connection
         * BSSCFGs need to simultaneously co-exist, then this address must be
         * different from the P2P Device Address, but also locally administered.
         */
        memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
+       p2p->int_addr[0] |= 0x02;
        p2p->int_addr[4] ^= 0x80;
 }
 
@@ -773,7 +783,7 @@ exit:
  * validates the channels in the request.
  */
 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
-                              struct net_device *ndev,
+                              struct brcmf_if *ifp,
                               struct cfg80211_scan_request *request,
                               u16 action)
 {
@@ -1261,7 +1271,7 @@ static void
 brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
 {
        struct brcmf_p2p_info *p2p = &cfg->p2p;
-       struct net_device *ndev = cfg->escan_info.ndev;
+       struct brcmf_if *ifp = cfg->escan_info.ifp;
 
        if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
            (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
@@ -1271,12 +1281,12 @@ brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
                 * So abort scan for off channel completion.
                 */
                if (p2p->af_sent_channel)
-                       brcmf_notify_escan_complete(cfg, ndev, true, true);
+                       brcmf_notify_escan_complete(cfg, ifp, true, true);
        } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
                            &p2p->status)) {
                brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
                /* So abort scan to cancel listen */
-               brcmf_notify_escan_complete(cfg, ndev, true, true);
+               brcmf_notify_escan_complete(cfg, ifp, true, true);
        }
 }
 
@@ -1421,7 +1431,8 @@ int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
                                              CHSPEC_IS2G(chanspec) ?
                                              IEEE80211_BAND_2GHZ :
                                              IEEE80211_BAND_5GHZ);
-       wdev = ifp->ndev->ieee80211_ptr;
+
+       wdev = &ifp->vif->wdev;
        cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len,
                         GFP_ATOMIC);
 
@@ -1637,6 +1648,7 @@ bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
                                 struct brcmf_fil_af_params_le *af_params)
 {
        struct brcmf_p2p_info *p2p = &cfg->p2p;
+       struct brcmf_if *ifp = netdev_priv(ndev);
        struct brcmf_fil_action_frame_le *action_frame;
        struct brcmf_config_af_params config_af_params;
        struct afx_hdl *afx_hdl = &p2p->afx_hdl;
@@ -1725,7 +1737,7 @@ bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
 
        /* To make sure to send successfully action frame, turn off mpc */
        if (config_af_params.mpc_onoff == 0)
-               brcmf_set_mpc(netdev_priv(ndev), 0);
+               brcmf_set_mpc(ifp, 0);
 
        /* set status and destination address before sending af */
        if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
@@ -1753,7 +1765,7 @@ bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
                 * care of current piggback algo, lets abort the scan here
                 * itself.
                 */
-               brcmf_notify_escan_complete(cfg, ndev, true, true);
+               brcmf_notify_escan_complete(cfg, ifp, true, true);
 
                /* update channel */
                af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
@@ -1820,7 +1832,7 @@ exit:
        clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
        /* if all done, turn mpc on again */
        if (config_af_params.mpc_onoff == 1)
-               brcmf_set_mpc(netdev_priv(ndev), 1);
+               brcmf_set_mpc(ifp, 1);
 
        return ack;
 }
@@ -1839,7 +1851,6 @@ s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
        struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
        struct brcmf_p2p_info *p2p = &cfg->p2p;
        struct afx_hdl *afx_hdl = &p2p->afx_hdl;
-       struct wireless_dev *wdev;
        struct brcmf_cfg80211_vif *vif = ifp->vif;
        struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
        u16 chanspec = be16_to_cpu(rxframe->chanspec);
@@ -1882,8 +1893,9 @@ s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
                                              CHSPEC_IS2G(chanspec) ?
                                              IEEE80211_BAND_2GHZ :
                                              IEEE80211_BAND_5GHZ);
-       wdev = ifp->ndev->ieee80211_ptr;
-       cfg80211_rx_mgmt(wdev, freq, 0, mgmt_frame, mgmt_frame_len, GFP_ATOMIC);
+
+       cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len,
+                        GFP_ATOMIC);
 
        brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
                  mgmt_frame_len, e->datalen, chanspec, freq);
@@ -1934,7 +1946,8 @@ s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg)
 
                p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
 
-               brcmf_p2p_generate_bss_mac(p2p);
+               brcmf_p2p_generate_bss_mac(p2p, NULL);
+               memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
                brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
 
                /* Initialize P2P Discovery in the firmware */
@@ -2040,7 +2053,7 @@ int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
                brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
                return -EPERM;
        }
-       brcmf_notify_escan_complete(cfg, vif->ifp->ndev, true, true);
+       brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
        vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
        if (!vif) {
                brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
@@ -2123,14 +2136,106 @@ static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
        return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
 }
 
+/**
+ * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
+ *
+ * @p2p: P2P specific data.
+ * @wiphy: wiphy device of new interface.
+ * @addr: mac address for this new interface.
+ */
+static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
+                                                   struct wiphy *wiphy,
+                                                   u8 *addr)
+{
+       struct brcmf_cfg80211_vif *p2p_vif;
+       struct brcmf_if *p2p_ifp;
+       struct brcmf_if *pri_ifp;
+       int err;
+       u32 bssidx;
+
+       if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
+               return ERR_PTR(-ENOSPC);
+
+       p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE,
+                                 false);
+       if (IS_ERR(p2p_vif)) {
+               brcmf_err("could not create discovery vif\n");
+               return (struct wireless_dev *)p2p_vif;
+       }
+
+       pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
+       brcmf_p2p_generate_bss_mac(p2p, addr);
+       brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
+
+       brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
+
+       /* Initialize P2P Discovery in the firmware */
+       err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
+       if (err < 0) {
+               brcmf_err("set p2p_disc error\n");
+               brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
+               goto fail;
+       }
+
+       /* wait for firmware event */
+       err = brcmf_cfg80211_wait_vif_event_timeout(p2p->cfg, BRCMF_E_IF_ADD,
+                                                   msecs_to_jiffies(1500));
+       brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
+       if (!err) {
+               brcmf_err("timeout occurred\n");
+               err = -EIO;
+               goto fail;
+       }
+
+       /* discovery interface created */
+       p2p_ifp = p2p_vif->ifp;
+       p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
+       memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
+       memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
+
+       /* verify bsscfg index for P2P discovery */
+       err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
+       if (err < 0) {
+               brcmf_err("retrieving discover bsscfg index failed\n");
+               goto fail;
+       }
+
+       WARN_ON(p2p_ifp->bssidx != bssidx);
+
+       init_completion(&p2p->send_af_done);
+       INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
+       init_completion(&p2p->afx_hdl.act_frm_scan);
+       init_completion(&p2p->wait_next_af);
+
+       return &p2p_vif->wdev;
+
+fail:
+       brcmf_free_vif(p2p_vif);
+       return ERR_PTR(err);
+}
+
+/**
+ * brcmf_p2p_delete_p2pdev() - delete P2P_DEVICE virtual interface.
+ *
+ * @vif: virtual interface object to delete.
+ */
+static void brcmf_p2p_delete_p2pdev(struct brcmf_cfg80211_vif *vif)
+{
+       struct brcmf_p2p_info *p2p = &vif->ifp->drvr->config->p2p;
+
+       cfg80211_unregister_wdev(&vif->wdev);
+       p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
+       brcmf_free_vif(vif);
+}
+
 /**
  * brcmf_p2p_add_vif() - create a new P2P virtual interface.
  *
  * @wiphy: wiphy device of new interface.
  * @name: name of the new interface.
  * @type: nl80211 interface type.
- * @flags: TBD
- * @params: TBD
+ * @flags: not used.
+ * @params: contains mac address for P2P device.
  */
 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
                                       enum nl80211_iftype type, u32 *flags,
@@ -2157,6 +2262,9 @@ struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
                iftype = BRCMF_FIL_P2P_IF_GO;
                mode = WL_MODE_AP;
                break;
+       case NL80211_IFTYPE_P2P_DEVICE:
+               return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
+                                              params->macaddr);
        default:
                return ERR_PTR(-EOPNOTSUPP);
        }
@@ -2244,6 +2352,8 @@ int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
                break;
 
        case NL80211_IFTYPE_P2P_DEVICE:
+               brcmf_p2p_delete_p2pdev(vif);
+               return 0;
        default:
                return -ENOTSUPP;
                break;
@@ -2275,3 +2385,33 @@ int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
 
        return err;
 }
+
+int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
+{
+       struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
+       struct brcmf_p2p_info *p2p = &cfg->p2p;
+       struct brcmf_cfg80211_vif *vif;
+       int err;
+
+       vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
+       mutex_lock(&cfg->usr_sync);
+       err = brcmf_p2p_enable_discovery(p2p);
+       if (!err)
+               set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
+       mutex_unlock(&cfg->usr_sync);
+       return err;
+}
+
+void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
+{
+       struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
+       struct brcmf_p2p_info *p2p = &cfg->p2p;
+       struct brcmf_cfg80211_vif *vif;
+
+       vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
+       mutex_lock(&cfg->usr_sync);
+       (void)brcmf_p2p_deinit_discovery(p2p);
+       brcmf_abort_scanning(cfg);
+       clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
+       mutex_unlock(&cfg->usr_sync);
+}