94ff045df2b3a29716a01bb25058403cae8e74e0
[cascardo/linux.git] / drivers / net / wireless / brcm80211 / brcmfmac / p2p.c
1 /*
2  * Copyright (c) 2012 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/slab.h>
17 #include <linux/netdevice.h>
18 #include <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_chnr_to_chspec() - convert channel number to chanspec.
428  *
429  * @channel: channel number
430  */
431 static u16 brcmf_p2p_chnr_to_chspec(u16 channel)
432 {
433         u16 chanspec;
434
435         chanspec = channel & WL_CHANSPEC_CHAN_MASK;
436
437         if (channel <= CH_MAX_2G_CHANNEL)
438                 chanspec |= WL_CHANSPEC_BAND_2G;
439         else
440                 chanspec |= WL_CHANSPEC_BAND_5G;
441
442         chanspec |= WL_CHANSPEC_BW_20;
443         chanspec |= WL_CHANSPEC_CTL_SB_NONE;
444
445         return chanspec;
446 }
447
448
449 /**
450  * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
451  *
452  * @ifp: ifp to use for iovars (primary).
453  * @p2p_mac: mac address to configure for p2p_da_override
454  */
455 static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
456 {
457         s32 ret = 0;
458
459         brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
460         brcmf_fil_iovar_int_set(ifp, "apsta", 1);
461         brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
462
463         /* In case of COB type, firmware has default mac address
464          * After Initializing firmware, we have to set current mac address to
465          * firmware for P2P device address
466          */
467         ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
468                                        ETH_ALEN);
469         if (ret)
470                 brcmf_err("failed to update device address ret %d\n", ret);
471
472         return ret;
473 }
474
475 /**
476  * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
477  *
478  * @p2p: P2P specific data.
479  * @dev_addr: optional device address.
480  *
481  * P2P needs mac addresses for P2P device and interface. If no device
482  * address it specified, these are derived from the primary net device, ie.
483  * the permanent ethernet address of the device.
484  */
485 static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
486 {
487         struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
488         bool local_admin = false;
489
490         if (!dev_addr || is_zero_ether_addr(dev_addr)) {
491                 dev_addr = pri_ifp->mac_addr;
492                 local_admin = true;
493         }
494
495         /* Generate the P2P Device Address.  This consists of the device's
496          * primary MAC address with the locally administered bit set.
497          */
498         memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
499         if (local_admin)
500                 p2p->dev_addr[0] |= 0x02;
501
502         /* Generate the P2P Interface Address.  If the discovery and connection
503          * BSSCFGs need to simultaneously co-exist, then this address must be
504          * different from the P2P Device Address, but also locally administered.
505          */
506         memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
507         p2p->int_addr[0] |= 0x02;
508         p2p->int_addr[4] ^= 0x80;
509 }
510
511 /**
512  * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
513  *
514  * @request: the scan request as received from cfg80211.
515  *
516  * returns true if one of the ssids in the request matches the
517  * P2P wildcard ssid; otherwise returns false.
518  */
519 static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
520 {
521         struct cfg80211_ssid *ssids = request->ssids;
522         int i;
523
524         for (i = 0; i < request->n_ssids; i++) {
525                 if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
526                         continue;
527
528                 brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
529                 if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
530                             BRCMF_P2P_WILDCARD_SSID_LEN))
531                         return true;
532         }
533         return false;
534 }
535
536 /**
537  * brcmf_p2p_set_discover_state - set discover state in firmware.
538  *
539  * @ifp: low-level interface object.
540  * @state: discover state to set.
541  * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
542  * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
543  */
544 static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
545                                         u16 chanspec, u16 listen_ms)
546 {
547         struct brcmf_p2p_disc_st_le discover_state;
548         s32 ret = 0;
549         brcmf_dbg(TRACE, "enter\n");
550
551         discover_state.state = state;
552         discover_state.chspec = cpu_to_le16(chanspec);
553         discover_state.dwell = cpu_to_le16(listen_ms);
554         ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
555                                         sizeof(discover_state));
556         return ret;
557 }
558
559 /**
560  * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
561  *
562  * @p2p: P2P specific data.
563  *
564  * Resets the discovery state and disables it in firmware.
565  */
566 static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
567 {
568         struct brcmf_cfg80211_vif *vif;
569
570         brcmf_dbg(TRACE, "enter\n");
571
572         /* Set the discovery state to SCAN */
573         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
574         (void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
575
576         /* Disable P2P discovery in the firmware */
577         vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
578         (void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
579
580         return 0;
581 }
582
583 /**
584  * brcmf_p2p_enable_discovery() - initialize and configure discovery.
585  *
586  * @p2p: P2P specific data.
587  *
588  * Initializes the discovery device and configure the virtual interface.
589  */
590 static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
591 {
592         struct brcmf_cfg80211_vif *vif;
593         s32 ret = 0;
594
595         brcmf_dbg(TRACE, "enter\n");
596         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
597         if (!vif) {
598                 brcmf_err("P2P config device not available\n");
599                 ret = -EPERM;
600                 goto exit;
601         }
602
603         if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
604                 brcmf_dbg(INFO, "P2P config device already configured\n");
605                 goto exit;
606         }
607
608         /* Re-initialize P2P Discovery in the firmware */
609         vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
610         ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
611         if (ret < 0) {
612                 brcmf_err("set p2p_disc error\n");
613                 goto exit;
614         }
615         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
616         ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
617         if (ret < 0) {
618                 brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
619                 goto exit;
620         }
621
622         /*
623          * Set wsec to any non-zero value in the discovery bsscfg
624          * to ensure our P2P probe responses have the privacy bit
625          * set in the 802.11 WPA IE. Some peer devices may not
626          * initiate WPS with us if this bit is not set.
627          */
628         ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
629         if (ret < 0) {
630                 brcmf_err("wsec error %d\n", ret);
631                 goto exit;
632         }
633
634         set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
635 exit:
636         return ret;
637 }
638
639 /**
640  * brcmf_p2p_escan() - initiate a P2P scan.
641  *
642  * @p2p: P2P specific data.
643  * @num_chans: number of channels to scan.
644  * @chanspecs: channel parameters for @num_chans channels.
645  * @search_state: P2P discover state to use.
646  * @action: scan action to pass to firmware.
647  * @bss_type: type of P2P bss.
648  */
649 static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
650                            u16 chanspecs[], s32 search_state, u16 action,
651                            enum p2p_bss_type bss_type)
652 {
653         s32 ret = 0;
654         s32 memsize = offsetof(struct brcmf_p2p_scan_le,
655                                eparams.params_le.channel_list);
656         s32 nprobes;
657         s32 active;
658         u32 i;
659         u8 *memblk;
660         struct brcmf_cfg80211_vif *vif;
661         struct brcmf_p2p_scan_le *p2p_params;
662         struct brcmf_scan_params_le *sparams;
663         struct brcmf_ssid ssid;
664
665         memsize += num_chans * sizeof(__le16);
666         memblk = kzalloc(memsize, GFP_KERNEL);
667         if (!memblk)
668                 return -ENOMEM;
669
670         vif = p2p->bss_idx[bss_type].vif;
671         if (vif == NULL) {
672                 brcmf_err("no vif for bss type %d\n", bss_type);
673                 ret = -EINVAL;
674                 goto exit;
675         }
676
677         switch (search_state) {
678         case WL_P2P_DISC_ST_SEARCH:
679                 /*
680                  * If we in SEARCH STATE, we don't need to set SSID explictly
681                  * because dongle use P2P WILDCARD internally by default
682                  */
683                 /* use null ssid */
684                 ssid.SSID_len = 0;
685                 memset(ssid.SSID, 0, sizeof(ssid.SSID));
686                 break;
687         case WL_P2P_DISC_ST_SCAN:
688                 /*
689                  * wpa_supplicant has p2p_find command with type social or
690                  * progressive. For progressive, we need to set the ssid to
691                  * P2P WILDCARD because we just do broadcast scan unless
692                  * setting SSID.
693                  */
694                 ssid.SSID_len = BRCMF_P2P_WILDCARD_SSID_LEN;
695                 memcpy(ssid.SSID, BRCMF_P2P_WILDCARD_SSID, ssid.SSID_len);
696                 break;
697         default:
698                 brcmf_err(" invalid search state %d\n", search_state);
699                 ret = -EINVAL;
700                 goto exit;
701         }
702
703         brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
704
705         /*
706          * set p2p scan parameters.
707          */
708         p2p_params = (struct brcmf_p2p_scan_le *)memblk;
709         p2p_params->type = 'E';
710
711         /* determine the scan engine parameters */
712         sparams = &p2p_params->eparams.params_le;
713         sparams->bss_type = DOT11_BSSTYPE_ANY;
714         if (p2p->cfg->active_scan)
715                 sparams->scan_type = 0;
716         else
717                 sparams->scan_type = 1;
718
719         memset(&sparams->bssid, 0xFF, ETH_ALEN);
720         if (ssid.SSID_len)
721                 memcpy(sparams->ssid_le.SSID, ssid.SSID, ssid.SSID_len);
722         sparams->ssid_le.SSID_len = cpu_to_le32(ssid.SSID_len);
723         sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
724
725         /*
726          * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
727          * supported by the supplicant.
728          */
729         if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
730                 active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
731         else if (num_chans == AF_PEER_SEARCH_CNT)
732                 active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
733         else if (wl_get_vif_state_all(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
734                 active = -1;
735         else
736                 active = P2PAPI_SCAN_DWELL_TIME_MS;
737
738         /* Override scan params to find a peer for a connection */
739         if (num_chans == 1) {
740                 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
741                 /* WAR to sync with presence period of VSDB GO.
742                  * send probe request more frequently
743                  */
744                 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
745         } else {
746                 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
747         }
748
749         if (nprobes <= 0)
750                 nprobes = 1;
751
752         brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
753         sparams->active_time = cpu_to_le32(active);
754         sparams->nprobes = cpu_to_le32(nprobes);
755         sparams->passive_time = cpu_to_le32(-1);
756         sparams->channel_num = cpu_to_le32(num_chans &
757                                            BRCMF_SCAN_PARAMS_COUNT_MASK);
758         for (i = 0; i < num_chans; i++)
759                 sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
760
761         /* set the escan specific parameters */
762         p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
763         p2p_params->eparams.action =  cpu_to_le16(action);
764         p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
765         /* perform p2p scan on primary device */
766         ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
767         if (!ret)
768                 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
769 exit:
770         kfree(memblk);
771         return ret;
772 }
773
774 /**
775  * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
776  *
777  * @cfg: driver private data for cfg80211 interface.
778  * @ndev: net device for which scan is requested.
779  * @request: scan request from cfg80211.
780  * @action: scan action.
781  *
782  * Determines the P2P discovery state based to scan request parameters and
783  * validates the channels in the request.
784  */
785 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
786                                struct brcmf_if *ifp,
787                                struct cfg80211_scan_request *request,
788                                u16 action)
789 {
790         struct brcmf_p2p_info *p2p = &cfg->p2p;
791         s32 err = 0;
792         s32 search_state = WL_P2P_DISC_ST_SCAN;
793         struct brcmf_cfg80211_vif *vif;
794         struct net_device *dev = NULL;
795         int i, num_nodfs = 0;
796         u16 *chanspecs;
797
798         brcmf_dbg(TRACE, "enter\n");
799
800         if (!request) {
801                 err = -EINVAL;
802                 goto exit;
803         }
804
805         if (request->n_channels) {
806                 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
807                                     GFP_KERNEL);
808                 if (!chanspecs) {
809                         err = -ENOMEM;
810                         goto exit;
811                 }
812                 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
813                 if (vif)
814                         dev = vif->wdev.netdev;
815                 if (request->n_channels == 3 &&
816                     request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
817                     request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
818                     request->channels[2]->hw_value == SOCIAL_CHAN_3) {
819                         /* SOCIAL CHANNELS 1, 6, 11 */
820                         search_state = WL_P2P_DISC_ST_SEARCH;
821                         brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
822                 } else if (dev != NULL && vif->mode == WL_MODE_AP) {
823                         /* If you are already a GO, then do SEARCH only */
824                         brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
825                         search_state = WL_P2P_DISC_ST_SEARCH;
826                 } else {
827                         brcmf_dbg(INFO, "P2P SCAN STATE START\n");
828                 }
829
830                 /*
831                  * no P2P scanning on passive or DFS channels.
832                  */
833                 for (i = 0; i < request->n_channels; i++) {
834                         struct ieee80211_channel *chan = request->channels[i];
835
836                         if (chan->flags & (IEEE80211_CHAN_RADAR |
837                                            IEEE80211_CHAN_PASSIVE_SCAN))
838                                 continue;
839
840                         chanspecs[i] = channel_to_chanspec(chan);
841                         brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
842                                   num_nodfs, chan->hw_value, chanspecs[i]);
843                         num_nodfs++;
844                 }
845                 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
846                                       action, P2PAPI_BSSCFG_DEVICE);
847         }
848 exit:
849         if (err)
850                 brcmf_err("error (%d)\n", err);
851         return err;
852 }
853
854
855 /**
856  * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
857  *
858  * @ie: string of information elements.
859  * @ie_len: length of string.
860  *
861  * Scan ie for p2p ie and look for attribute 6 channel. If available determine
862  * channel and return it.
863  */
864 static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
865 {
866         u8 channel_ie[5];
867         s32 listen_channel;
868         s32 err;
869
870         err = cfg80211_get_p2p_attr(ie, ie_len,
871                                     IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
872                                     channel_ie, sizeof(channel_ie));
873         if (err < 0)
874                 return err;
875
876         /* listen channel subel length format:     */
877         /* 3(country) + 1(op. class) + 1(chan num) */
878         listen_channel = (s32)channel_ie[3 + 1];
879
880         if (listen_channel == SOCIAL_CHAN_1 ||
881             listen_channel == SOCIAL_CHAN_2 ||
882             listen_channel == SOCIAL_CHAN_3) {
883                 brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
884                 return listen_channel;
885         }
886
887         return -EPERM;
888 }
889
890
891 /**
892  * brcmf_p2p_scan_prep() - prepare scan based on request.
893  *
894  * @wiphy: wiphy device.
895  * @request: scan request from cfg80211.
896  * @vif: vif on which scan request is to be executed.
897  *
898  * Prepare the scan appropriately for type of scan requested. Overrides the
899  * escan .run() callback for peer-to-peer scanning.
900  */
901 int brcmf_p2p_scan_prep(struct wiphy *wiphy,
902                         struct cfg80211_scan_request *request,
903                         struct brcmf_cfg80211_vif *vif)
904 {
905         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
906         struct brcmf_p2p_info *p2p = &cfg->p2p;
907         int err = 0;
908
909         if (brcmf_p2p_scan_is_p2p_request(request)) {
910                 /* find my listen channel */
911                 err = brcmf_p2p_find_listen_channel(request->ie,
912                                                     request->ie_len);
913                 if (err < 0)
914                         return err;
915
916                 p2p->afx_hdl.my_listen_chan = err;
917
918                 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
919                 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
920
921                 err = brcmf_p2p_enable_discovery(p2p);
922                 if (err)
923                         return err;
924
925                 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
926
927                 /* override .run_escan() callback. */
928                 cfg->escan_info.run = brcmf_p2p_run_escan;
929         }
930         err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
931                                     request->ie, request->ie_len);
932         return err;
933 }
934
935
936 /**
937  * brcmf_p2p_discover_listen() - set firmware to discover listen state.
938  *
939  * @p2p: p2p device.
940  * @channel: channel nr for discover listen.
941  * @duration: time in ms to stay on channel.
942  *
943  */
944 static s32
945 brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
946 {
947         struct brcmf_cfg80211_vif *vif;
948         s32 err = 0;
949         u16 chanspec;
950
951         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
952         if (!vif) {
953                 brcmf_err("Discovery is not set, so we have nothing to do\n");
954                 err = -EPERM;
955                 goto exit;
956         }
957
958         if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
959                 brcmf_err("Previous LISTEN is not completed yet\n");
960                 /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
961                 goto exit;
962         }
963
964         chanspec = brcmf_p2p_chnr_to_chspec(channel);
965         err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
966                                            chanspec, (u16)duration);
967         if (!err) {
968                 set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
969                 p2p->remain_on_channel_cookie++;
970         }
971 exit:
972         return err;
973 }
974
975
976 /**
977  * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
978  *
979  * @wiphy: wiphy device.
980  * @channel: channel to stay on.
981  * @duration: time in ms to remain on channel.
982  *
983  */
984 int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
985                                 struct ieee80211_channel *channel,
986                                 unsigned int duration, u64 *cookie)
987 {
988         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
989         struct brcmf_p2p_info *p2p = &cfg->p2p;
990         s32 err;
991         u16 channel_nr;
992
993         channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
994         brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
995                   duration);
996
997         err = brcmf_p2p_enable_discovery(p2p);
998         if (err)
999                 goto exit;
1000         err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
1001         if (err)
1002                 goto exit;
1003
1004         memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
1005         *cookie = p2p->remain_on_channel_cookie;
1006         cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
1007
1008 exit:
1009         return err;
1010 }
1011
1012
1013 /**
1014  * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
1015  *
1016  * @ifp: interfac control.
1017  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1018  * @data: payload of message. Not used.
1019  *
1020  */
1021 int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
1022                                      const struct brcmf_event_msg *e,
1023                                      void *data)
1024 {
1025         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1026         struct brcmf_p2p_info *p2p = &cfg->p2p;
1027
1028         brcmf_dbg(TRACE, "Enter\n");
1029         if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
1030                                &p2p->status)) {
1031                 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1032                                        &p2p->status)) {
1033                         clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1034                                   &p2p->status);
1035                         brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1036                         complete(&p2p->wait_next_af);
1037                 }
1038
1039                 cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1040                                                    p2p->remain_on_channel_cookie,
1041                                                    &p2p->remain_on_channel,
1042                                                    GFP_KERNEL);
1043         }
1044         return 0;
1045 }
1046
1047
1048 /**
1049  * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1050  *
1051  * @ifp: interfac control.
1052  *
1053  */
1054 void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1055 {
1056         if (!ifp)
1057                 return;
1058         brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1059         brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1060 }
1061
1062
1063 /**
1064  * brcmf_p2p_act_frm_search() - search function for action frame.
1065  *
1066  * @p2p: p2p device.
1067  * channel: channel on which action frame is to be trasmitted.
1068  *
1069  * search function to reach at common channel to send action frame. When
1070  * channel is 0 then all social channels will be used to send af
1071  */
1072 static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1073 {
1074         s32 err;
1075         u32 channel_cnt;
1076         u16 *default_chan_list;
1077         u32 i;
1078
1079         brcmf_dbg(TRACE, "Enter\n");
1080
1081         if (channel)
1082                 channel_cnt = AF_PEER_SEARCH_CNT;
1083         else
1084                 channel_cnt = SOCIAL_CHAN_CNT;
1085         default_chan_list = kzalloc(channel_cnt * sizeof(*default_chan_list),
1086                                     GFP_KERNEL);
1087         if (default_chan_list == NULL) {
1088                 brcmf_err("channel list allocation failed\n");
1089                 err = -ENOMEM;
1090                 goto exit;
1091         }
1092         if (channel) {
1093                 /* insert same channel to the chan_list */
1094                 for (i = 0; i < channel_cnt; i++)
1095                         default_chan_list[i] =
1096                                         brcmf_p2p_chnr_to_chspec(channel);
1097         } else {
1098                 default_chan_list[0] = brcmf_p2p_chnr_to_chspec(SOCIAL_CHAN_1);
1099                 default_chan_list[1] = brcmf_p2p_chnr_to_chspec(SOCIAL_CHAN_2);
1100                 default_chan_list[2] = brcmf_p2p_chnr_to_chspec(SOCIAL_CHAN_3);
1101         }
1102         err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
1103                               WL_P2P_DISC_ST_SEARCH, WL_ESCAN_ACTION_START,
1104                               P2PAPI_BSSCFG_DEVICE);
1105         kfree(default_chan_list);
1106 exit:
1107         return err;
1108 }
1109
1110
1111 /**
1112  * brcmf_p2p_afx_handler() - afx worker thread.
1113  *
1114  * @work:
1115  *
1116  */
1117 static void brcmf_p2p_afx_handler(struct work_struct *work)
1118 {
1119         struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
1120         struct brcmf_p2p_info *p2p = container_of(afx_hdl,
1121                                                   struct brcmf_p2p_info,
1122                                                   afx_hdl);
1123         s32 err;
1124
1125         if (!afx_hdl->is_active)
1126                 return;
1127
1128         if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1129                 /* 100ms ~ 300ms */
1130                 err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
1131                                                 100 * (1 + (random32() % 3)));
1132         else
1133                 err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1134
1135         if (err) {
1136                 brcmf_err("ERROR occurred! value is (%d)\n", err);
1137                 if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1138                              &p2p->status))
1139                         complete(&afx_hdl->act_frm_scan);
1140         }
1141 }
1142
1143
1144 /**
1145  * brcmf_p2p_af_searching_channel() - search channel.
1146  *
1147  * @p2p: p2p device info struct.
1148  *
1149  */
1150 static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1151 {
1152         struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1153         struct brcmf_cfg80211_vif *pri_vif;
1154         unsigned long duration;
1155         s32 retry;
1156
1157         brcmf_dbg(TRACE, "Enter\n");
1158
1159         pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1160
1161         INIT_COMPLETION(afx_hdl->act_frm_scan);
1162         set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1163         afx_hdl->is_active = true;
1164         afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
1165
1166         /* Loop to wait until we find a peer's channel or the
1167          * pending action frame tx is cancelled.
1168          */
1169         retry = 0;
1170         duration = msecs_to_jiffies(P2P_AF_FRM_SCAN_MAX_WAIT);
1171         while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
1172                (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
1173                 afx_hdl->is_listen = false;
1174                 brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
1175                           retry);
1176                 /* search peer on peer's listen channel */
1177                 schedule_work(&afx_hdl->afx_work);
1178                 wait_for_completion_timeout(&afx_hdl->act_frm_scan, duration);
1179                 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1180                     (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1181                                &p2p->status)))
1182                         break;
1183
1184                 if (afx_hdl->my_listen_chan) {
1185                         brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
1186                                   afx_hdl->my_listen_chan);
1187                         /* listen on my listen channel */
1188                         afx_hdl->is_listen = true;
1189                         schedule_work(&afx_hdl->afx_work);
1190                         wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1191                                                     duration);
1192                 }
1193                 if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1194                     (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1195                                &p2p->status)))
1196                         break;
1197                 retry++;
1198
1199                 /* if sta is connected or connecting, sleep for a while before
1200                  * retry af tx or finding a peer
1201                  */
1202                 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
1203                     test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
1204                         msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
1205         }
1206
1207         brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1208                   afx_hdl->peer_chan);
1209         afx_hdl->is_active = false;
1210
1211         clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1212
1213         return afx_hdl->peer_chan;
1214 }
1215
1216
1217 /**
1218  * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1219  *
1220  * @cfg: common configuration struct.
1221  * @bi: bss info struct, result from scan.
1222  *
1223  */
1224 bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1225                                            struct brcmf_bss_info_le *bi)
1226
1227 {
1228         struct brcmf_p2p_info *p2p = &cfg->p2p;
1229         struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1230         u8 *ie;
1231         s32 err;
1232         u8 p2p_dev_addr[ETH_ALEN];
1233
1234         if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
1235                 return false;
1236
1237         if (bi == NULL) {
1238                 brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
1239                 if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
1240                         complete(&afx_hdl->act_frm_scan);
1241                 return true;
1242         }
1243
1244         ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
1245         memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
1246         err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1247                                     IEEE80211_P2P_ATTR_DEVICE_INFO,
1248                                     p2p_dev_addr, sizeof(p2p_dev_addr));
1249         if (err < 0)
1250                 err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1251                                             IEEE80211_P2P_ATTR_DEVICE_ID,
1252                                             p2p_dev_addr, sizeof(p2p_dev_addr));
1253         if ((err >= 0) &&
1254             (!memcmp(p2p_dev_addr, afx_hdl->tx_dst_addr, ETH_ALEN))) {
1255                 afx_hdl->peer_chan = bi->ctl_ch ? bi->ctl_ch :
1256                                       CHSPEC_CHANNEL(le16_to_cpu(bi->chanspec));
1257                 brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
1258                           afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
1259                 complete(&afx_hdl->act_frm_scan);
1260         }
1261         return true;
1262 }
1263
1264 /**
1265  * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1266  *
1267  * @cfg: common configuration struct.
1268  *
1269  */
1270 static void
1271 brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1272 {
1273         struct brcmf_p2p_info *p2p = &cfg->p2p;
1274         struct brcmf_if *ifp = cfg->escan_info.ifp;
1275
1276         if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
1277             (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
1278              test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
1279                 brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
1280                 /* if channel is not zero, "actfame" uses off channel scan.
1281                  * So abort scan for off channel completion.
1282                  */
1283                 if (p2p->af_sent_channel)
1284                         brcmf_notify_escan_complete(cfg, ifp, true, true);
1285         } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1286                             &p2p->status)) {
1287                 brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1288                 /* So abort scan to cancel listen */
1289                 brcmf_notify_escan_complete(cfg, ifp, true, true);
1290         }
1291 }
1292
1293
1294 /**
1295  * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1296  *
1297  * @p2p: p2p device info struct.
1298  *
1299  * return true if recevied action frame is to be dropped.
1300  */
1301 static bool
1302 brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1303 {
1304         struct brcmf_cfg80211_info *cfg = p2p->cfg;
1305         struct brcmf_if *ifp;
1306
1307         brcmf_dbg(TRACE, "Enter\n");
1308
1309         if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1310             !p2p->gon_req_action)
1311                 return false;
1312
1313         brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
1314         /* if sa(peer) addr is less than da(my) addr, then this device
1315          * process peer's gon request and block to send gon req.
1316          * if not (sa addr > da addr),
1317          * this device will process gon request and drop gon req of peer.
1318          */
1319         ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
1320         if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
1321                 brcmf_dbg(INFO, "Block transmit gon req !!!\n");
1322                 p2p->block_gon_req_tx = true;
1323                 /* if we are finding a common channel for sending af,
1324                  * do not scan more to block to send current gon req
1325                  */
1326                 if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1327                                        &p2p->status))
1328                         complete(&p2p->afx_hdl.act_frm_scan);
1329                 if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1330                                        &p2p->status))
1331                         brcmf_p2p_stop_wait_next_action_frame(cfg);
1332                 return false;
1333         }
1334
1335         /* drop gon request of peer to process gon request by this device. */
1336         brcmf_dbg(INFO, "Drop received gon req !!!\n");
1337
1338         return true;
1339 }
1340
1341
1342 /**
1343  * brcmf_p2p_notify_action_frame_rx() - received action frame.
1344  *
1345  * @ifp: interfac control.
1346  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1347  * @data: payload of message, containing action frame data.
1348  *
1349  */
1350 int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1351                                      const struct brcmf_event_msg *e,
1352                                      void *data)
1353 {
1354         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1355         struct brcmf_p2p_info *p2p = &cfg->p2p;
1356         struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1357         struct wireless_dev *wdev;
1358         u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1359         struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1360         u8 *frame = (u8 *)(rxframe + 1);
1361         struct brcmf_p2p_pub_act_frame *act_frm;
1362         struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
1363         u16 chanspec = be16_to_cpu(rxframe->chanspec);
1364         struct ieee80211_mgmt *mgmt_frame;
1365         s32 freq;
1366         u16 mgmt_type;
1367         u8 action;
1368
1369         /* Check if wpa_supplicant has registered for this frame */
1370         brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1371         mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1372         if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1373                 return 0;
1374
1375         brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
1376
1377         action = P2P_PAF_SUBTYPE_INVALID;
1378         if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
1379                 act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
1380                 action = act_frm->subtype;
1381                 if ((action == P2P_PAF_GON_REQ) &&
1382                     (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
1383                         if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1384                                      &p2p->status) &&
1385                             (memcmp(afx_hdl->tx_dst_addr, e->addr,
1386                                     ETH_ALEN) == 0)) {
1387                                 afx_hdl->peer_chan = CHSPEC_CHANNEL(chanspec);
1388                                 brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1389                                           afx_hdl->peer_chan);
1390                                 complete(&afx_hdl->act_frm_scan);
1391                         }
1392                         return 0;
1393                 }
1394                 /* After complete GO Negotiation, roll back to mpc mode */
1395                 if ((action == P2P_PAF_GON_CONF) ||
1396                     (action == P2P_PAF_PROVDIS_RSP))
1397                         brcmf_set_mpc(ifp, 1);
1398                 if (action == P2P_PAF_GON_CONF) {
1399                         brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1400                         clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1401                 }
1402         } else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
1403                 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
1404                 action = sd_act_frm->action;
1405         }
1406
1407         if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1408             (p2p->next_af_subtype == action)) {
1409                 brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
1410                 clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1411                           &p2p->status);
1412                 /* Stop waiting for next AF. */
1413                 brcmf_p2p_stop_wait_next_action_frame(cfg);
1414         }
1415
1416         mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1417                              mgmt_frame_len, GFP_KERNEL);
1418         if (!mgmt_frame) {
1419                 brcmf_err("No memory available for action frame\n");
1420                 return -ENOMEM;
1421         }
1422         memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
1423         brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1424                                ETH_ALEN);
1425         memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1426         mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
1427         memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
1428         mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1429
1430         freq = ieee80211_channel_to_frequency(CHSPEC_CHANNEL(chanspec),
1431                                               CHSPEC_IS2G(chanspec) ?
1432                                               IEEE80211_BAND_2GHZ :
1433                                               IEEE80211_BAND_5GHZ);
1434
1435         wdev = &ifp->vif->wdev;
1436         cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len,
1437                          GFP_ATOMIC);
1438
1439         kfree(mgmt_frame);
1440         return 0;
1441 }
1442
1443
1444 /**
1445  * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1446  *
1447  * @ifp: interfac control.
1448  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1449  * @data: not used.
1450  *
1451  */
1452 int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1453                                         const struct brcmf_event_msg *e,
1454                                         void *data)
1455 {
1456         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1457         struct brcmf_p2p_info *p2p = &cfg->p2p;
1458
1459         brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1460                   e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1461                   "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1462                   e->status);
1463
1464         if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1465                 return 0;
1466
1467         if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
1468                 if (e->status == BRCMF_E_STATUS_SUCCESS)
1469                         set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1470                                 &p2p->status);
1471                 else {
1472                         set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1473                         /* If there is no ack, we don't need to wait for
1474                          * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1475                          */
1476                         brcmf_p2p_stop_wait_next_action_frame(cfg);
1477                 }
1478
1479         } else {
1480                 complete(&p2p->send_af_done);
1481         }
1482         return 0;
1483 }
1484
1485
1486 /**
1487  * brcmf_p2p_tx_action_frame() - send action frame over fil.
1488  *
1489  * @p2p: p2p info struct for vif.
1490  * @af_params: action frame data/info.
1491  *
1492  * Send an action frame immediately without doing channel synchronization.
1493  *
1494  * This function waits for a completion event before returning.
1495  * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1496  * frame is transmitted.
1497  */
1498 static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1499                                      struct brcmf_fil_af_params_le *af_params)
1500 {
1501         struct brcmf_cfg80211_vif *vif;
1502         s32 err = 0;
1503         s32 timeout = 0;
1504
1505         brcmf_dbg(TRACE, "Enter\n");
1506
1507         INIT_COMPLETION(p2p->send_af_done);
1508         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1509         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1510
1511         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1512         err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1513                                         sizeof(*af_params));
1514         if (err) {
1515                 brcmf_err(" sending action frame has failed\n");
1516                 goto exit;
1517         }
1518
1519         p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1520         p2p->af_tx_sent_jiffies = jiffies;
1521
1522         timeout = wait_for_completion_timeout(&p2p->send_af_done,
1523                                         msecs_to_jiffies(P2P_AF_MAX_WAIT_TIME));
1524
1525         if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1526                 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1527         } else {
1528                 err = -EIO;
1529                 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1530         }
1531         /* clear status bit for action tx */
1532         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1533         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1534
1535 exit:
1536         return err;
1537 }
1538
1539
1540 /**
1541  * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1542  *
1543  * @cfg: driver private data for cfg80211 interface.
1544  * @af_params: action frame data/info.
1545  * @config_af_params: configuration data for action frame.
1546  *
1547  * routine which transmits ation frame public type.
1548  */
1549 static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1550                                struct brcmf_fil_af_params_le *af_params,
1551                                struct brcmf_config_af_params *config_af_params)
1552 {
1553         struct brcmf_p2p_info *p2p = &cfg->p2p;
1554         struct brcmf_fil_action_frame_le *action_frame;
1555         struct brcmf_p2p_pub_act_frame *act_frm;
1556         s32 err = 0;
1557         u16 ie_len;
1558
1559         action_frame = &af_params->action_frame;
1560         act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1561
1562         config_af_params->extra_listen = true;
1563
1564         switch (act_frm->subtype) {
1565         case P2P_PAF_GON_REQ:
1566                 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1567                 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1568                 config_af_params->mpc_onoff = 0;
1569                 config_af_params->search_channel = true;
1570                 p2p->next_af_subtype = act_frm->subtype + 1;
1571                 p2p->gon_req_action = true;
1572                 /* increase dwell time to wait for RESP frame */
1573                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1574                 break;
1575         case P2P_PAF_GON_RSP:
1576                 p2p->next_af_subtype = act_frm->subtype + 1;
1577                 /* increase dwell time to wait for CONF frame */
1578                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1579                 break;
1580         case P2P_PAF_GON_CONF:
1581                 /* If we reached till GO Neg confirmation reset the filter */
1582                 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1583                 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1584                 /* turn on mpc again if go nego is done */
1585                 config_af_params->mpc_onoff = 1;
1586                 /* minimize dwell time */
1587                 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1588                 config_af_params->extra_listen = false;
1589                 break;
1590         case P2P_PAF_INVITE_REQ:
1591                 config_af_params->search_channel = true;
1592                 p2p->next_af_subtype = act_frm->subtype + 1;
1593                 /* increase dwell time */
1594                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1595                 break;
1596         case P2P_PAF_INVITE_RSP:
1597                 /* minimize dwell time */
1598                 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1599                 config_af_params->extra_listen = false;
1600                 break;
1601         case P2P_PAF_DEVDIS_REQ:
1602                 config_af_params->search_channel = true;
1603                 p2p->next_af_subtype = act_frm->subtype + 1;
1604                 /* maximize dwell time to wait for RESP frame */
1605                 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1606                 break;
1607         case P2P_PAF_DEVDIS_RSP:
1608                 /* minimize dwell time */
1609                 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1610                 config_af_params->extra_listen = false;
1611                 break;
1612         case P2P_PAF_PROVDIS_REQ:
1613                 ie_len = le16_to_cpu(action_frame->len) -
1614                          offsetof(struct brcmf_p2p_pub_act_frame, elts);
1615                 if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1616                                           IEEE80211_P2P_ATTR_GROUP_ID,
1617                                           NULL, 0) < 0)
1618                         config_af_params->search_channel = true;
1619                 config_af_params->mpc_onoff = 0;
1620                 p2p->next_af_subtype = act_frm->subtype + 1;
1621                 /* increase dwell time to wait for RESP frame */
1622                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1623                 break;
1624         case P2P_PAF_PROVDIS_RSP:
1625                 /* wpa_supplicant send go nego req right after prov disc */
1626                 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1627                 /* increase dwell time to MED level */
1628                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1629                 config_af_params->extra_listen = false;
1630                 break;
1631         default:
1632                 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1633                           act_frm->subtype);
1634                 err = -EINVAL;
1635         }
1636         return err;
1637 }
1638
1639 /**
1640  * brcmf_p2p_send_action_frame() - send action frame .
1641  *
1642  * @cfg: driver private data for cfg80211 interface.
1643  * @ndev: net device to transmit on.
1644  * @af_params: configuration data for action frame.
1645  */
1646 bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1647                                  struct net_device *ndev,
1648                                  struct brcmf_fil_af_params_le *af_params)
1649 {
1650         struct brcmf_p2p_info *p2p = &cfg->p2p;
1651         struct brcmf_if *ifp = netdev_priv(ndev);
1652         struct brcmf_fil_action_frame_le *action_frame;
1653         struct brcmf_config_af_params config_af_params;
1654         struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1655         u16 action_frame_len;
1656         bool ack = false;
1657         u8 category;
1658         u8 action;
1659         s32 tx_retry;
1660         s32 extra_listen_time;
1661         uint delta_ms;
1662
1663         action_frame = &af_params->action_frame;
1664         action_frame_len = le16_to_cpu(action_frame->len);
1665
1666         brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1667
1668         /* Add the default dwell time. Dwell time to stay off-channel */
1669         /* to wait for a response action frame after transmitting an  */
1670         /* GO Negotiation action frame                                */
1671         af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1672
1673         category = action_frame->data[DOT11_ACTION_CAT_OFF];
1674         action = action_frame->data[DOT11_ACTION_ACT_OFF];
1675
1676         /* initialize variables */
1677         p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
1678         p2p->gon_req_action = false;
1679
1680         /* config parameters */
1681         config_af_params.mpc_onoff = -1;
1682         config_af_params.search_channel = false;
1683         config_af_params.extra_listen = false;
1684
1685         if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1686                 /* p2p public action frame process */
1687                 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1688                         /* Just send unknown subtype frame with */
1689                         /* default parameters.                  */
1690                         brcmf_err("P2P Public action frame, unknown subtype.\n");
1691                 }
1692         } else if (brcmf_p2p_is_gas_action(action_frame->data,
1693                                            action_frame_len)) {
1694                 /* service discovery process */
1695                 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1696                     action == P2PSD_ACTION_ID_GAS_CREQ) {
1697                         /* configure service discovery query frame */
1698                         config_af_params.search_channel = true;
1699
1700                         /* save next af suptype to cancel */
1701                         /* remaining dwell time           */
1702                         p2p->next_af_subtype = action + 1;
1703
1704                         af_params->dwell_time =
1705                                 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1706                 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1707                            action == P2PSD_ACTION_ID_GAS_CRESP) {
1708                         /* configure service discovery response frame */
1709                         af_params->dwell_time =
1710                                 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1711                 } else {
1712                         brcmf_err("Unknown action type: %d\n", action);
1713                         goto exit;
1714                 }
1715         } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1716                                            action_frame_len)) {
1717                 /* do not configure anything. it will be */
1718                 /* sent with a default configuration     */
1719         } else {
1720                 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1721                           category, action);
1722                 return false;
1723         }
1724
1725         /* if connecting on primary iface, sleep for a while before sending
1726          * af tx for VSDB
1727          */
1728         if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1729                      &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1730                 msleep(50);
1731
1732         /* if scan is ongoing, abort current scan. */
1733         if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1734                 brcmf_abort_scanning(cfg);
1735
1736         memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1737
1738         /* To make sure to send successfully action frame, turn off mpc */
1739         if (config_af_params.mpc_onoff == 0)
1740                 brcmf_set_mpc(ifp, 0);
1741
1742         /* set status and destination address before sending af */
1743         if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1744                 /* set status to cancel the remained dwell time in rx process */
1745                 set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1746         }
1747
1748         p2p->af_sent_channel = 0;
1749         set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1750         /* validate channel and p2p ies */
1751         if (config_af_params.search_channel &&
1752             IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1753             p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1754                 afx_hdl = &p2p->afx_hdl;
1755                 afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1756
1757                 if (brcmf_p2p_af_searching_channel(p2p) ==
1758                                                         P2P_INVALID_CHANNEL) {
1759                         brcmf_err("Couldn't find peer's channel.\n");
1760                         goto exit;
1761                 }
1762
1763                 /* Abort scan even for VSDB scenarios. Scan gets aborted in
1764                  * firmware but after the check of piggyback algorithm. To take
1765                  * care of current piggback algo, lets abort the scan here
1766                  * itself.
1767                  */
1768                 brcmf_notify_escan_complete(cfg, ifp, true, true);
1769
1770                 /* update channel */
1771                 af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1772         }
1773
1774         tx_retry = 0;
1775         while (!p2p->block_gon_req_tx &&
1776                (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
1777                 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1778                 tx_retry++;
1779         }
1780         if (ack == false) {
1781                 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
1782                 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1783         }
1784
1785 exit:
1786         clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1787
1788         /* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1789          * if we coundn't get the next action response frame and dongle does
1790          * not keep the dwell time, go to listen state again to get next action
1791          * response frame.
1792          */
1793         if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1794             test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1795             p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1796                 delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1797                 if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1798                         extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1799                                             delta_ms;
1800                 else
1801                         extra_listen_time = 0;
1802                 if (extra_listen_time > 50) {
1803                         set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1804                                 &p2p->status);
1805                         brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1806                                   le32_to_cpu(af_params->dwell_time),
1807                                   extra_listen_time);
1808                         extra_listen_time += 100;
1809                         if (!brcmf_p2p_discover_listen(p2p,
1810                                                        p2p->af_sent_channel,
1811                                                        extra_listen_time)) {
1812                                 unsigned long duration;
1813
1814                                 extra_listen_time += 100;
1815                                 duration = msecs_to_jiffies(extra_listen_time);
1816                                 wait_for_completion_timeout(&p2p->wait_next_af,
1817                                                             duration);
1818                         }
1819                         clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1820                                   &p2p->status);
1821                 }
1822         }
1823
1824         if (p2p->block_gon_req_tx) {
1825                 /* if ack is true, supplicant will wait more time(100ms).
1826                  * so we will return it as a success to get more time .
1827                  */
1828                 p2p->block_gon_req_tx = false;
1829                 ack = true;
1830         }
1831
1832         clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1833         /* if all done, turn mpc on again */
1834         if (config_af_params.mpc_onoff == 1)
1835                 brcmf_set_mpc(ifp, 1);
1836
1837         return ack;
1838 }
1839
1840 /**
1841  * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1842  *
1843  * @ifp: interface pointer for which event was received.
1844  * @e: even message.
1845  * @data: payload of event message (probe request).
1846  */
1847 s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1848                                           const struct brcmf_event_msg *e,
1849                                           void *data)
1850 {
1851         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1852         struct brcmf_p2p_info *p2p = &cfg->p2p;
1853         struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1854         struct brcmf_cfg80211_vif *vif = ifp->vif;
1855         struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1856         u16 chanspec = be16_to_cpu(rxframe->chanspec);
1857         u8 *mgmt_frame;
1858         u32 mgmt_frame_len;
1859         s32 freq;
1860         u16 mgmt_type;
1861
1862         brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1863                   e->reason);
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 = CHSPEC_CHANNEL(chanspec);
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(CHSPEC_CHANNEL(chanspec),
1893                                               CHSPEC_IS2G(chanspec) ?
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         s32 err;
2018
2019         ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2020
2021         *chanspec = 11 & WL_CHANSPEC_CHAN_MASK;
2022
2023         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_CHANNEL, &ci, sizeof(ci));
2024         if (!err) {
2025                 *chanspec = le32_to_cpu(ci.hw_channel) & WL_CHANSPEC_CHAN_MASK;
2026                 if (*chanspec < CH_MAX_2G_CHANNEL)
2027                         *chanspec |= WL_CHANSPEC_BAND_2G;
2028                 else
2029                         *chanspec |= WL_CHANSPEC_BAND_5G;
2030         }
2031         *chanspec |= WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
2032 }
2033
2034 /**
2035  * Change a P2P Role.
2036  * Parameters:
2037  * @mac: MAC address of the BSS to change a role
2038  * Returns 0 if success.
2039  */
2040 int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
2041                        enum brcmf_fil_p2p_if_types if_type)
2042 {
2043         struct brcmf_p2p_info *p2p = &cfg->p2p;
2044         struct brcmf_cfg80211_vif *vif;
2045         struct brcmf_fil_p2p_if_le if_request;
2046         s32 err;
2047         u16 chanspec;
2048
2049         brcmf_dbg(TRACE, "Enter\n");
2050
2051         vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
2052         if (!vif) {
2053                 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
2054                 return -EPERM;
2055         }
2056         brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
2057         vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
2058         if (!vif) {
2059                 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
2060                 return -EPERM;
2061         }
2062         brcmf_set_mpc(vif->ifp, 0);
2063
2064         /* In concurrency case, STA may be already associated in a particular */
2065         /* channel. so retrieve the current channel of primary interface and  */
2066         /* then start the virtual interface on that.                          */
2067         brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2068
2069         if_request.type = cpu_to_le16((u16)if_type);
2070         if_request.chspec = cpu_to_le16(chanspec);
2071         memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
2072
2073         brcmf_cfg80211_arm_vif_event(cfg, vif);
2074         err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
2075                                        sizeof(if_request));
2076         if (err) {
2077                 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
2078                 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2079                 return err;
2080         }
2081         err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_CHANGE,
2082                                                     msecs_to_jiffies(1500));
2083         brcmf_cfg80211_arm_vif_event(cfg, NULL);
2084         if (!err)  {
2085                 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
2086                 return -EIO;
2087         }
2088
2089         err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2090                                     BRCMF_SCB_TIMEOUT_VALUE);
2091
2092         return err;
2093 }
2094
2095 static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2096                                     struct brcmf_if *ifp, u8 ea[ETH_ALEN],
2097                                     enum brcmf_fil_p2p_if_types iftype)
2098 {
2099         struct brcmf_fil_p2p_if_le if_request;
2100         int err;
2101         u16 chanspec;
2102
2103         /* we need a default channel */
2104         brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2105
2106         /* fill the firmware request */
2107         memcpy(if_request.addr, ea, ETH_ALEN);
2108         if_request.type = cpu_to_le16((u16)iftype);
2109         if_request.chspec = cpu_to_le16(chanspec);
2110
2111         err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2112                                        sizeof(if_request));
2113         if (err)
2114                 return err;
2115
2116         return err;
2117 }
2118
2119 static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2120 {
2121         struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2122         struct net_device *pri_ndev = cfg_to_ndev(cfg);
2123         struct brcmf_if *ifp = netdev_priv(pri_ndev);
2124         u8 *addr = vif->wdev.netdev->dev_addr;
2125
2126         return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2127 }
2128
2129 static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2130 {
2131         struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2132         struct net_device *pri_ndev = cfg_to_ndev(cfg);
2133         struct brcmf_if *ifp = netdev_priv(pri_ndev);
2134         u8 *addr = vif->wdev.netdev->dev_addr;
2135
2136         return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2137 }
2138
2139 /**
2140  * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2141  *
2142  * @p2p: P2P specific data.
2143  * @wiphy: wiphy device of new interface.
2144  * @addr: mac address for this new interface.
2145  */
2146 static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2147                                                     struct wiphy *wiphy,
2148                                                     u8 *addr)
2149 {
2150         struct brcmf_cfg80211_vif *p2p_vif;
2151         struct brcmf_if *p2p_ifp;
2152         struct brcmf_if *pri_ifp;
2153         int err;
2154         u32 bssidx;
2155
2156         if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2157                 return ERR_PTR(-ENOSPC);
2158
2159         p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE,
2160                                   false);
2161         if (IS_ERR(p2p_vif)) {
2162                 brcmf_err("could not create discovery vif\n");
2163                 return (struct wireless_dev *)p2p_vif;
2164         }
2165
2166         pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2167         brcmf_p2p_generate_bss_mac(p2p, addr);
2168         brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2169
2170         brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
2171
2172         /* Initialize P2P Discovery in the firmware */
2173         err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2174         if (err < 0) {
2175                 brcmf_err("set p2p_disc error\n");
2176                 brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2177                 goto fail;
2178         }
2179
2180         /* wait for firmware event */
2181         err = brcmf_cfg80211_wait_vif_event_timeout(p2p->cfg, BRCMF_E_IF_ADD,
2182                                                     msecs_to_jiffies(1500));
2183         brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2184         if (!err) {
2185                 brcmf_err("timeout occurred\n");
2186                 err = -EIO;
2187                 goto fail;
2188         }
2189
2190         /* discovery interface created */
2191         p2p_ifp = p2p_vif->ifp;
2192         p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2193         memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2194         memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2195
2196         /* verify bsscfg index for P2P discovery */
2197         err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
2198         if (err < 0) {
2199                 brcmf_err("retrieving discover bsscfg index failed\n");
2200                 goto fail;
2201         }
2202
2203         WARN_ON(p2p_ifp->bssidx != bssidx);
2204
2205         init_completion(&p2p->send_af_done);
2206         INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2207         init_completion(&p2p->afx_hdl.act_frm_scan);
2208         init_completion(&p2p->wait_next_af);
2209
2210         return &p2p_vif->wdev;
2211
2212 fail:
2213         brcmf_free_vif(p2p_vif);
2214         return ERR_PTR(err);
2215 }
2216
2217 /**
2218  * brcmf_p2p_delete_p2pdev() - delete P2P_DEVICE virtual interface.
2219  *
2220  * @vif: virtual interface object to delete.
2221  */
2222 static void brcmf_p2p_delete_p2pdev(struct brcmf_cfg80211_vif *vif)
2223 {
2224         struct brcmf_p2p_info *p2p = &vif->ifp->drvr->config->p2p;
2225
2226         cfg80211_unregister_wdev(&vif->wdev);
2227         p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
2228         brcmf_free_vif(vif);
2229 }
2230
2231 /**
2232  * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2233  *
2234  * @wiphy: wiphy device of new interface.
2235  * @name: name of the new interface.
2236  * @type: nl80211 interface type.
2237  * @flags: not used.
2238  * @params: contains mac address for P2P device.
2239  */
2240 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
2241                                        enum nl80211_iftype type, u32 *flags,
2242                                        struct vif_params *params)
2243 {
2244         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2245         struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2246         struct brcmf_cfg80211_vif *vif;
2247         enum brcmf_fil_p2p_if_types iftype;
2248         enum wl_mode mode;
2249         int err;
2250
2251         if (brcmf_cfg80211_vif_event_armed(cfg))
2252                 return ERR_PTR(-EBUSY);
2253
2254         brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
2255
2256         switch (type) {
2257         case NL80211_IFTYPE_P2P_CLIENT:
2258                 iftype = BRCMF_FIL_P2P_IF_CLIENT;
2259                 mode = WL_MODE_BSS;
2260                 break;
2261         case NL80211_IFTYPE_P2P_GO:
2262                 iftype = BRCMF_FIL_P2P_IF_GO;
2263                 mode = WL_MODE_AP;
2264                 break;
2265         case NL80211_IFTYPE_P2P_DEVICE:
2266                 return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2267                                                params->macaddr);
2268         default:
2269                 return ERR_PTR(-EOPNOTSUPP);
2270         }
2271
2272         vif = brcmf_alloc_vif(cfg, type, false);
2273         if (IS_ERR(vif))
2274                 return (struct wireless_dev *)vif;
2275         brcmf_cfg80211_arm_vif_event(cfg, vif);
2276
2277         err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
2278                                        iftype);
2279         if (err) {
2280                 brcmf_cfg80211_arm_vif_event(cfg, NULL);
2281                 goto fail;
2282         }
2283
2284         /* wait for firmware event */
2285         err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
2286                                                     msecs_to_jiffies(1500));
2287         brcmf_cfg80211_arm_vif_event(cfg, NULL);
2288         if (!err) {
2289                 brcmf_err("timeout occurred\n");
2290                 err = -EIO;
2291                 goto fail;
2292         }
2293
2294         /* interface created in firmware */
2295         ifp = vif->ifp;
2296         if (!ifp) {
2297                 brcmf_err("no if pointer provided\n");
2298                 err = -ENOENT;
2299                 goto fail;
2300         }
2301
2302         strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
2303         err = brcmf_net_attach(ifp, true);
2304         if (err) {
2305                 brcmf_err("Registering netdevice failed\n");
2306                 goto fail;
2307         }
2308         cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
2309         /* Disable firmware roaming for P2P interface  */
2310         brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2311         if (iftype == BRCMF_FIL_P2P_IF_GO) {
2312                 /* set station timeout for p2p */
2313                 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2314                                       BRCMF_SCB_TIMEOUT_VALUE);
2315         }
2316         return &ifp->vif->wdev;
2317
2318 fail:
2319         brcmf_free_vif(vif);
2320         return ERR_PTR(err);
2321 }
2322
2323 /**
2324  * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2325  *
2326  * @wiphy: wiphy device of interface.
2327  * @wdev: wireless device of interface.
2328  *
2329  * TODO: not yet supported.
2330  */
2331 int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2332 {
2333         struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
2334         struct brcmf_p2p_info *p2p = &cfg->p2p;
2335         struct brcmf_cfg80211_vif *vif;
2336         unsigned long jiffie_timeout = msecs_to_jiffies(1500);
2337         bool wait_for_disable = false;
2338         int err;
2339
2340         brcmf_dbg(TRACE, "delete P2P vif\n");
2341         vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2342
2343         switch (vif->wdev.iftype) {
2344         case NL80211_IFTYPE_P2P_CLIENT:
2345                 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2346                         wait_for_disable = true;
2347                 break;
2348
2349         case NL80211_IFTYPE_P2P_GO:
2350                 if (!brcmf_p2p_disable_p2p_if(vif))
2351                         wait_for_disable = true;
2352                 break;
2353
2354         case NL80211_IFTYPE_P2P_DEVICE:
2355                 brcmf_p2p_delete_p2pdev(vif);
2356                 return 0;
2357         default:
2358                 return -ENOTSUPP;
2359                 break;
2360         }
2361
2362         clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2363         brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2364
2365         if (wait_for_disable)
2366                 wait_for_completion_timeout(&cfg->vif_disabled,
2367                                             msecs_to_jiffies(500));
2368
2369         brcmf_vif_clear_mgmt_ies(vif);
2370
2371         brcmf_cfg80211_arm_vif_event(cfg, vif);
2372         err = brcmf_p2p_release_p2p_if(vif);
2373         if (!err) {
2374                 /* wait for firmware event */
2375                 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_DEL,
2376                                                             jiffie_timeout);
2377                 if (!err)
2378                         err = -EIO;
2379                 else
2380                         err = 0;
2381         }
2382         brcmf_cfg80211_arm_vif_event(cfg, NULL);
2383         brcmf_free_vif(vif);
2384         p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
2385
2386         return err;
2387 }
2388
2389 int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2390 {
2391         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2392         struct brcmf_p2p_info *p2p = &cfg->p2p;
2393         struct brcmf_cfg80211_vif *vif;
2394         int err;
2395
2396         vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2397         mutex_lock(&cfg->usr_sync);
2398         err = brcmf_p2p_enable_discovery(p2p);
2399         if (!err)
2400                 set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2401         mutex_unlock(&cfg->usr_sync);
2402         return err;
2403 }
2404
2405 void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2406 {
2407         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2408         struct brcmf_p2p_info *p2p = &cfg->p2p;
2409         struct brcmf_cfg80211_vif *vif;
2410
2411         vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2412         mutex_lock(&cfg->usr_sync);
2413         (void)brcmf_p2p_deinit_discovery(p2p);
2414         brcmf_abort_scanning(cfg);
2415         clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2416         mutex_unlock(&cfg->usr_sync);
2417 }