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