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