brcmfmac: Put printing action frames code under debug flag.
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / net / wireless / brcm80211 / brcmfmac / p2p.c
1 /*
2  * Copyright (c) 2012 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/slab.h>
17 #include <linux/netdevice.h>
18 #include <net/cfg80211.h>
19
20 #include <brcmu_wifi.h>
21 #include <brcmu_utils.h>
22 #include <defs.h>
23 #include <dhd.h>
24 #include <dhd_dbg.h>
25 #include "fwil.h"
26 #include "fwil_types.h"
27 #include "p2p.h"
28 #include "wl_cfg80211.h"
29
30 /* parameters used for p2p escan */
31 #define P2PAPI_SCAN_NPROBES 1
32 #define P2PAPI_SCAN_DWELL_TIME_MS 80
33 #define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
34 #define P2PAPI_SCAN_HOME_TIME_MS 60
35 #define P2PAPI_SCAN_NPROBS_TIME_MS 30
36 #define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
37 #define WL_SCAN_CONNECT_DWELL_TIME_MS 200
38 #define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
39
40 #define BRCMF_P2P_WILDCARD_SSID         "DIRECT-"
41 #define BRCMF_P2P_WILDCARD_SSID_LEN     (sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
42
43 #define SOCIAL_CHAN_1           1
44 #define SOCIAL_CHAN_2           6
45 #define SOCIAL_CHAN_3           11
46 #define SOCIAL_CHAN_CNT         3
47 #define AF_PEER_SEARCH_CNT      2
48
49 #define BRCMF_SCB_TIMEOUT_VALUE 20
50
51 #define P2P_VER                 9       /* P2P version: 9=WiFi P2P v1.0 */
52 #define P2P_PUB_AF_CATEGORY     0x04
53 #define P2P_PUB_AF_ACTION       0x09
54 #define P2P_AF_CATEGORY         0x7f
55 #define P2P_OUI                 "\x50\x6F\x9A"  /* P2P OUI */
56 #define P2P_OUI_LEN             3               /* P2P OUI length */
57
58 /* Action Frame Constants */
59 #define DOT11_ACTION_HDR_LEN    2       /* action frame category + action */
60 #define DOT11_ACTION_CAT_OFF    0       /* category offset */
61 #define DOT11_ACTION_ACT_OFF    1       /* action offset */
62
63 #define P2P_AF_DWELL_TIME               200
64 #define P2P_AF_MIN_DWELL_TIME           100
65 #define P2P_AF_MED_DWELL_TIME           400
66 #define P2P_AF_LONG_DWELL_TIME          1000
67 #define P2P_AF_TX_MAX_RETRY             5
68 #define P2P_AF_MAX_WAIT_TIME            2000
69 #define P2P_INVALID_CHANNEL             -1
70 #define P2P_CHANNEL_SYNC_RETRY          5
71 #define P2P_AF_FRM_SCAN_MAX_WAIT        1500
72
73 /* WiFi P2P Public Action Frame OUI Subtypes */
74 #define P2P_PAF_GON_REQ         0       /* Group Owner Negotiation Req */
75 #define P2P_PAF_GON_RSP         1       /* Group Owner Negotiation Rsp */
76 #define P2P_PAF_GON_CONF        2       /* Group Owner Negotiation Confirm */
77 #define P2P_PAF_INVITE_REQ      3       /* P2P Invitation Request */
78 #define P2P_PAF_INVITE_RSP      4       /* P2P Invitation Response */
79 #define P2P_PAF_DEVDIS_REQ      5       /* Device Discoverability Request */
80 #define P2P_PAF_DEVDIS_RSP      6       /* Device Discoverability Response */
81 #define P2P_PAF_PROVDIS_REQ     7       /* Provision Discovery Request */
82 #define P2P_PAF_PROVDIS_RSP     8       /* Provision Discovery Response */
83 #define P2P_PAF_SUBTYPE_INVALID 255     /* Invalid Subtype */
84
85 /* WiFi P2P Action Frame OUI Subtypes */
86 #define P2P_AF_NOTICE_OF_ABSENCE        0       /* Notice of Absence */
87 #define P2P_AF_PRESENCE_REQ             1       /* P2P Presence Request */
88 #define P2P_AF_PRESENCE_RSP             2       /* P2P Presence Response */
89 #define P2P_AF_GO_DISC_REQ              3       /* GO Discoverability Request */
90
91 /* P2P Service Discovery related */
92 #define P2PSD_ACTION_CATEGORY           0x04    /* Public action frame */
93 #define P2PSD_ACTION_ID_GAS_IREQ        0x0a    /* GAS Initial Request AF */
94 #define P2PSD_ACTION_ID_GAS_IRESP       0x0b    /* GAS Initial Response AF */
95 #define P2PSD_ACTION_ID_GAS_CREQ        0x0c    /* GAS Comback Request AF */
96 #define P2PSD_ACTION_ID_GAS_CRESP       0x0d    /* GAS Comback Response AF */
97
98 /**
99  * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
100  *
101  * @state: requested discovery state (see enum brcmf_p2p_disc_state).
102  * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
103  * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
104  */
105 struct brcmf_p2p_disc_st_le {
106         u8 state;
107         __le16 chspec;
108         __le16 dwell;
109 };
110
111 /**
112  * enum brcmf_p2p_disc_state - P2P discovery state values
113  *
114  * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
115  * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
116  * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
117  */
118 enum brcmf_p2p_disc_state {
119         WL_P2P_DISC_ST_SCAN,
120         WL_P2P_DISC_ST_LISTEN,
121         WL_P2P_DISC_ST_SEARCH
122 };
123
124 /**
125  * struct brcmf_p2p_scan_le - P2P specific scan request.
126  *
127  * @type: type of scan method requested (values: 'E' or 'S').
128  * @reserved: reserved (ignored).
129  * @eparams: parameters used for type 'E'.
130  * @sparams: parameters used for type 'S'.
131  */
132 struct brcmf_p2p_scan_le {
133         u8 type;
134         u8 reserved[3];
135         union {
136                 struct brcmf_escan_params_le eparams;
137                 struct brcmf_scan_params_le sparams;
138         };
139 };
140
141 /**
142  * struct brcmf_p2p_pub_act_frame - WiFi P2P Public Action Frame
143  *
144  * @category: P2P_PUB_AF_CATEGORY
145  * @action: P2P_PUB_AF_ACTION
146  * @oui[3]: P2P_OUI
147  * @oui_type: OUI type - P2P_VER
148  * @subtype: OUI subtype - P2P_TYPE_*
149  * @dialog_token: nonzero, identifies req/rsp transaction
150  * @elts[1]: Variable length information elements.
151  */
152 struct brcmf_p2p_pub_act_frame {
153         u8      category;
154         u8      action;
155         u8      oui[3];
156         u8      oui_type;
157         u8      subtype;
158         u8      dialog_token;
159         u8      elts[1];
160 };
161
162 /**
163  * struct brcmf_p2p_action_frame - WiFi P2P Action Frame
164  *
165  * @category: P2P_AF_CATEGORY
166  * @OUI[3]: OUI - P2P_OUI
167  * @type: OUI Type - P2P_VER
168  * @subtype: OUI Subtype - P2P_AF_*
169  * @dialog_token: nonzero, identifies req/resp tranaction
170  * @elts[1]: Variable length information elements.
171  */
172 struct brcmf_p2p_action_frame {
173         u8      category;
174         u8      oui[3];
175         u8      type;
176         u8      subtype;
177         u8      dialog_token;
178         u8      elts[1];
179 };
180
181 /**
182  * struct brcmf_p2psd_gas_pub_act_frame - Wi-Fi GAS Public Action Frame
183  *
184  * @category: 0x04 Public Action Frame
185  * @action: 0x6c Advertisement Protocol
186  * @dialog_token: nonzero, identifies req/rsp transaction
187  * @query_data[1]: Query Data. SD gas ireq SD gas iresp
188  */
189 struct brcmf_p2psd_gas_pub_act_frame {
190         u8      category;
191         u8      action;
192         u8      dialog_token;
193         u8      query_data[1];
194 };
195
196 /**
197  * struct brcmf_config_af_params - Action Frame Parameters for tx.
198  *
199  * @max_tx_retry: max tx retry count if tx no ack.
200  * @mpc_onoff: To make sure to send successfully action frame, we have to
201  *             turn off mpc  0: off, 1: on,  (-1): do nothing
202  */
203 struct brcmf_config_af_params {
204         s32 max_tx_retry;
205         s32 mpc_onoff;
206 };
207
208 /**
209  * brcmf_p2p_is_pub_action() - true if p2p public type frame.
210  *
211  * @frame: action frame data.
212  * @frame_len: length of action frame data.
213  *
214  * Determine if action frame is p2p public action type
215  */
216 static bool brcmf_p2p_is_pub_action(void *frame, u32 frame_len)
217 {
218         struct brcmf_p2p_pub_act_frame *pact_frm;
219
220         if (frame == NULL)
221                 return false;
222
223         pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
224         if (frame_len < sizeof(struct brcmf_p2p_pub_act_frame) - 1)
225                 return false;
226
227         if (pact_frm->category == P2P_PUB_AF_CATEGORY &&
228             pact_frm->action == P2P_PUB_AF_ACTION &&
229             pact_frm->oui_type == P2P_VER &&
230             memcmp(pact_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
231                 return true;
232
233         return false;
234 }
235
236 /**
237  * brcmf_p2p_is_p2p_action() - true if p2p action type frame.
238  *
239  * @frame: action frame data.
240  * @frame_len: length of action frame data.
241  *
242  * Determine if action frame is p2p action type
243  */
244 static bool brcmf_p2p_is_p2p_action(void *frame, u32 frame_len)
245 {
246         struct brcmf_p2p_action_frame *act_frm;
247
248         if (frame == NULL)
249                 return false;
250
251         act_frm = (struct brcmf_p2p_action_frame *)frame;
252         if (frame_len < sizeof(struct brcmf_p2p_action_frame) - 1)
253                 return false;
254
255         if (act_frm->category == P2P_AF_CATEGORY &&
256             act_frm->type  == P2P_VER &&
257             memcmp(act_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
258                 return true;
259
260         return false;
261 }
262
263 /**
264  * brcmf_p2p_is_gas_action() - true if p2p gas action type frame.
265  *
266  * @frame: action frame data.
267  * @frame_len: length of action frame data.
268  *
269  * Determine if action frame is p2p gas action type
270  */
271 static bool brcmf_p2p_is_gas_action(void *frame, u32 frame_len)
272 {
273         struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
274
275         if (frame == NULL)
276                 return false;
277
278         sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
279         if (frame_len < sizeof(struct brcmf_p2psd_gas_pub_act_frame) - 1)
280                 return false;
281
282         if (sd_act_frm->category != P2PSD_ACTION_CATEGORY)
283                 return false;
284
285         if (sd_act_frm->action == P2PSD_ACTION_ID_GAS_IREQ ||
286             sd_act_frm->action == P2PSD_ACTION_ID_GAS_IRESP ||
287             sd_act_frm->action == P2PSD_ACTION_ID_GAS_CREQ ||
288             sd_act_frm->action == P2PSD_ACTION_ID_GAS_CRESP)
289                 return true;
290
291         return false;
292 }
293
294 /**
295  * brcmf_p2p_print_actframe() - debug print routine.
296  *
297  * @tx: Received or to be transmitted
298  * @frame: action frame data.
299  * @frame_len: length of action frame data.
300  *
301  * Print information about the p2p action frame
302  */
303
304 #ifdef DEBUG
305
306 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
307 {
308         struct brcmf_p2p_pub_act_frame *pact_frm;
309         struct brcmf_p2p_action_frame *act_frm;
310         struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
311
312         if (!frame || frame_len <= 2)
313                 return;
314
315         if (brcmf_p2p_is_pub_action(frame, frame_len)) {
316                 pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
317                 switch (pact_frm->subtype) {
318                 case P2P_PAF_GON_REQ:
319                         brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
320                                   (tx) ? "TX" : "RX");
321                         break;
322                 case P2P_PAF_GON_RSP:
323                         brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
324                                   (tx) ? "TX" : "RX");
325                         break;
326                 case P2P_PAF_GON_CONF:
327                         brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
328                                   (tx) ? "TX" : "RX");
329                         break;
330                 case P2P_PAF_INVITE_REQ:
331                         brcmf_dbg(TRACE, "%s P2P Invitation Request  Frame\n",
332                                   (tx) ? "TX" : "RX");
333                         break;
334                 case P2P_PAF_INVITE_RSP:
335                         brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
336                                   (tx) ? "TX" : "RX");
337                         break;
338                 case P2P_PAF_DEVDIS_REQ:
339                         brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
340                                   (tx) ? "TX" : "RX");
341                         break;
342                 case P2P_PAF_DEVDIS_RSP:
343                         brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
344                                   (tx) ? "TX" : "RX");
345                         break;
346                 case P2P_PAF_PROVDIS_REQ:
347                         brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
348                                   (tx) ? "TX" : "RX");
349                         break;
350                 case P2P_PAF_PROVDIS_RSP:
351                         brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
352                                   (tx) ? "TX" : "RX");
353                         break;
354                 default:
355                         brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
356                                   (tx) ? "TX" : "RX");
357                         break;
358                 }
359         } else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
360                 act_frm = (struct brcmf_p2p_action_frame *)frame;
361                 switch (act_frm->subtype) {
362                 case P2P_AF_NOTICE_OF_ABSENCE:
363                         brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
364                                   (tx) ? "TX" : "RX");
365                         break;
366                 case P2P_AF_PRESENCE_REQ:
367                         brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
368                                   (tx) ? "TX" : "RX");
369                         break;
370                 case P2P_AF_PRESENCE_RSP:
371                         brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
372                                   (tx) ? "TX" : "RX");
373                         break;
374                 case P2P_AF_GO_DISC_REQ:
375                         brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
376                                   (tx) ? "TX" : "RX");
377                         break;
378                 default:
379                         brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
380                                   (tx) ? "TX" : "RX");
381                 }
382
383         } else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
384                 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
385                 switch (sd_act_frm->action) {
386                 case P2PSD_ACTION_ID_GAS_IREQ:
387                         brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
388                                   (tx) ? "TX" : "RX");
389                         break;
390                 case P2PSD_ACTION_ID_GAS_IRESP:
391                         brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
392                                   (tx) ? "TX" : "RX");
393                         break;
394                 case P2PSD_ACTION_ID_GAS_CREQ:
395                         brcmf_dbg(TRACE, "%s P2P GAS Comback Request\n",
396                                   (tx) ? "TX" : "RX");
397                         break;
398                 case P2PSD_ACTION_ID_GAS_CRESP:
399                         brcmf_dbg(TRACE, "%s P2P GAS Comback Response\n",
400                                   (tx) ? "TX" : "RX");
401                         break;
402                 default:
403                         brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
404                                   (tx) ? "TX" : "RX");
405                         break;
406                 }
407         }
408 }
409
410 #else
411
412 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
413 {
414 }
415
416 #endif
417
418 /**
419  * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
420  *
421  * @ifp: ifp to use for iovars (primary).
422  * @p2p_mac: mac address to configure for p2p_da_override
423  */
424 static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
425 {
426         s32 ret = 0;
427
428         brcmf_fil_iovar_int_set(ifp, "apsta", 1);
429
430         /* In case of COB type, firmware has default mac address
431          * After Initializing firmware, we have to set current mac address to
432          * firmware for P2P device address
433          */
434         ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
435                                        ETH_ALEN);
436         if (ret)
437                 brcmf_err("failed to update device address ret %d\n", ret);
438
439         return ret;
440 }
441
442 /**
443  * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
444  *
445  * @p2p: P2P specific data.
446  *
447  * P2P needs mac addresses for P2P device and interface. These are
448  * derived from the primary net device, ie. the permanent ethernet
449  * address of the device.
450  */
451 static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p)
452 {
453         struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
454         struct brcmf_if *p2p_ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
455
456         /* Generate the P2P Device Address.  This consists of the device's
457          * primary MAC address with the locally administered bit set.
458          */
459         memcpy(p2p->dev_addr, pri_ifp->mac_addr, ETH_ALEN);
460         p2p->dev_addr[0] |= 0x02;
461         memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
462
463         /* Generate the P2P Interface Address.  If the discovery and connection
464          * BSSCFGs need to simultaneously co-exist, then this address must be
465          * different from the P2P Device Address, but also locally administered.
466          */
467         memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
468         p2p->int_addr[4] ^= 0x80;
469 }
470
471 /**
472  * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
473  *
474  * @request: the scan request as received from cfg80211.
475  *
476  * returns true if one of the ssids in the request matches the
477  * P2P wildcard ssid; otherwise returns false.
478  */
479 static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
480 {
481         struct cfg80211_ssid *ssids = request->ssids;
482         int i;
483
484         for (i = 0; i < request->n_ssids; i++) {
485                 if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
486                         continue;
487
488                 brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
489                 if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
490                             BRCMF_P2P_WILDCARD_SSID_LEN))
491                         return true;
492         }
493         return false;
494 }
495
496 /**
497  * brcmf_p2p_set_discover_state - set discover state in firmware.
498  *
499  * @ifp: low-level interface object.
500  * @state: discover state to set.
501  * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
502  * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
503  */
504 static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
505                                         u16 chanspec, u16 listen_ms)
506 {
507         struct brcmf_p2p_disc_st_le discover_state;
508         s32 ret = 0;
509         brcmf_dbg(TRACE, "enter\n");
510
511         discover_state.state = state;
512         discover_state.chspec = cpu_to_le16(chanspec);
513         discover_state.dwell = cpu_to_le16(listen_ms);
514         ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
515                                         sizeof(discover_state));
516         return ret;
517 }
518
519 /**
520  * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
521  *
522  * @p2p: P2P specific data.
523  *
524  * Resets the discovery state and disables it in firmware.
525  */
526 static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
527 {
528         struct brcmf_cfg80211_vif *vif;
529
530         brcmf_dbg(TRACE, "enter\n");
531
532         /* Set the discovery state to SCAN */
533         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
534         (void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
535
536         /* Disable P2P discovery in the firmware */
537         vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
538         (void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
539
540         return 0;
541 }
542
543 /**
544  * brcmf_p2p_enable_discovery() - initialize and configure discovery.
545  *
546  * @p2p: P2P specific data.
547  *
548  * Initializes the discovery device and configure the virtual interface.
549  */
550 static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
551 {
552         struct brcmf_cfg80211_vif *vif;
553         s32 ret = 0;
554
555         brcmf_dbg(TRACE, "enter\n");
556         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
557         if (!vif) {
558                 brcmf_err("P2P config device not available\n");
559                 ret = -EPERM;
560                 goto exit;
561         }
562
563         if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
564                 brcmf_dbg(INFO, "P2P config device already configured\n");
565                 goto exit;
566         }
567
568         /* Re-initialize P2P Discovery in the firmware */
569         vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
570         ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
571         if (ret < 0) {
572                 brcmf_err("set p2p_disc error\n");
573                 goto exit;
574         }
575         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
576         ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
577         if (ret < 0) {
578                 brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
579                 goto exit;
580         }
581
582         /*
583          * Set wsec to any non-zero value in the discovery bsscfg
584          * to ensure our P2P probe responses have the privacy bit
585          * set in the 802.11 WPA IE. Some peer devices may not
586          * initiate WPS with us if this bit is not set.
587          */
588         ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
589         if (ret < 0) {
590                 brcmf_err("wsec error %d\n", ret);
591                 goto exit;
592         }
593
594         set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
595 exit:
596         return ret;
597 }
598
599 /**
600  * brcmf_p2p_escan() - initiate a P2P scan.
601  *
602  * @p2p: P2P specific data.
603  * @num_chans: number of channels to scan.
604  * @chanspecs: channel parameters for @num_chans channels.
605  * @search_state: P2P discover state to use.
606  * @action: scan action to pass to firmware.
607  * @bss_type: type of P2P bss.
608  */
609 static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
610                            u16 chanspecs[], s32 search_state, u16 action,
611                            enum p2p_bss_type bss_type)
612 {
613         s32 ret = 0;
614         s32 memsize = offsetof(struct brcmf_p2p_scan_le,
615                                eparams.params_le.channel_list);
616         s32 nprobes;
617         s32 active;
618         u32 i;
619         u8 *memblk;
620         struct brcmf_cfg80211_vif *vif;
621         struct brcmf_p2p_scan_le *p2p_params;
622         struct brcmf_scan_params_le *sparams;
623         struct brcmf_ssid ssid;
624
625         memsize += num_chans * sizeof(__le16);
626         memblk = kzalloc(memsize, GFP_KERNEL);
627         if (!memblk)
628                 return -ENOMEM;
629
630         vif = p2p->bss_idx[bss_type].vif;
631         if (vif == NULL) {
632                 brcmf_err("no vif for bss type %d\n", bss_type);
633                 ret = -EINVAL;
634                 goto exit;
635         }
636
637         switch (search_state) {
638         case WL_P2P_DISC_ST_SEARCH:
639                 /*
640                  * If we in SEARCH STATE, we don't need to set SSID explictly
641                  * because dongle use P2P WILDCARD internally by default
642                  */
643                 /* use null ssid */
644                 ssid.SSID_len = 0;
645                 memset(ssid.SSID, 0, sizeof(ssid.SSID));
646                 break;
647         case WL_P2P_DISC_ST_SCAN:
648                 /*
649                  * wpa_supplicant has p2p_find command with type social or
650                  * progressive. For progressive, we need to set the ssid to
651                  * P2P WILDCARD because we just do broadcast scan unless
652                  * setting SSID.
653                  */
654                 ssid.SSID_len = BRCMF_P2P_WILDCARD_SSID_LEN;
655                 memcpy(ssid.SSID, BRCMF_P2P_WILDCARD_SSID, ssid.SSID_len);
656                 break;
657         default:
658                 brcmf_err(" invalid search state %d\n", search_state);
659                 ret = -EINVAL;
660                 goto exit;
661         }
662
663         brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
664
665         /*
666          * set p2p scan parameters.
667          */
668         p2p_params = (struct brcmf_p2p_scan_le *)memblk;
669         p2p_params->type = 'E';
670
671         /* determine the scan engine parameters */
672         sparams = &p2p_params->eparams.params_le;
673         sparams->bss_type = DOT11_BSSTYPE_ANY;
674         if (p2p->cfg->active_scan)
675                 sparams->scan_type = 0;
676         else
677                 sparams->scan_type = 1;
678
679         memset(&sparams->bssid, 0xFF, ETH_ALEN);
680         if (ssid.SSID_len)
681                 memcpy(sparams->ssid_le.SSID, ssid.SSID, ssid.SSID_len);
682         sparams->ssid_le.SSID_len = cpu_to_le32(ssid.SSID_len);
683         sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
684
685         /*
686          * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
687          * supported by the supplicant.
688          */
689         if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
690                 active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
691         else if (num_chans == AF_PEER_SEARCH_CNT)
692                 active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
693         else if (wl_get_vif_state_all(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
694                 active = -1;
695         else
696                 active = P2PAPI_SCAN_DWELL_TIME_MS;
697
698         /* Override scan params to find a peer for a connection */
699         if (num_chans == 1) {
700                 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
701                 /* WAR to sync with presence period of VSDB GO.
702                  * send probe request more frequently
703                  */
704                 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
705         } else {
706                 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
707         }
708
709         if (nprobes <= 0)
710                 nprobes = 1;
711
712         brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
713         sparams->active_time = cpu_to_le32(active);
714         sparams->nprobes = cpu_to_le32(nprobes);
715         sparams->passive_time = cpu_to_le32(-1);
716         sparams->channel_num = cpu_to_le32(num_chans &
717                                            BRCMF_SCAN_PARAMS_COUNT_MASK);
718         for (i = 0; i < num_chans; i++)
719                 sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
720
721         /* set the escan specific parameters */
722         p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
723         p2p_params->eparams.action =  cpu_to_le16(action);
724         p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
725         /* perform p2p scan on primary device */
726         ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
727         if (!ret)
728                 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
729 exit:
730         kfree(memblk);
731         return ret;
732 }
733
734 /**
735  * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
736  *
737  * @cfg: driver private data for cfg80211 interface.
738  * @ndev: net device for which scan is requested.
739  * @request: scan request from cfg80211.
740  * @action: scan action.
741  *
742  * Determines the P2P discovery state based to scan request parameters and
743  * validates the channels in the request.
744  */
745 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
746                                struct net_device *ndev,
747                                struct cfg80211_scan_request *request,
748                                u16 action)
749 {
750         struct brcmf_p2p_info *p2p = &cfg->p2p;
751         s32 err = 0;
752         s32 search_state = WL_P2P_DISC_ST_SCAN;
753         struct brcmf_cfg80211_vif *vif;
754         struct net_device *dev = NULL;
755         int i, num_nodfs = 0;
756         u16 *chanspecs;
757
758         brcmf_dbg(TRACE, "enter\n");
759
760         if (!request) {
761                 err = -EINVAL;
762                 goto exit;
763         }
764
765         if (request->n_channels) {
766                 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
767                                     GFP_KERNEL);
768                 if (!chanspecs) {
769                         err = -ENOMEM;
770                         goto exit;
771                 }
772                 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
773                 if (vif)
774                         dev = vif->wdev.netdev;
775                 if (request->n_channels == 3 &&
776                     request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
777                     request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
778                     request->channels[2]->hw_value == SOCIAL_CHAN_3) {
779                         /* SOCIAL CHANNELS 1, 6, 11 */
780                         search_state = WL_P2P_DISC_ST_SEARCH;
781                         brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
782                 } else if (dev != NULL && vif->mode == WL_MODE_AP) {
783                         /* If you are already a GO, then do SEARCH only */
784                         brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
785                         search_state = WL_P2P_DISC_ST_SEARCH;
786                 } else {
787                         brcmf_dbg(INFO, "P2P SCAN STATE START\n");
788                 }
789
790                 /*
791                  * no P2P scanning on passive or DFS channels.
792                  */
793                 for (i = 0; i < request->n_channels; i++) {
794                         struct ieee80211_channel *chan = request->channels[i];
795
796                         if (chan->flags & (IEEE80211_CHAN_RADAR |
797                                            IEEE80211_CHAN_PASSIVE_SCAN))
798                                 continue;
799
800                         chanspecs[i] = channel_to_chanspec(chan);
801                         brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
802                                   num_nodfs, chan->hw_value, chanspecs[i]);
803                         num_nodfs++;
804                 }
805                 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
806                                       action, P2PAPI_BSSCFG_DEVICE);
807         }
808 exit:
809         if (err)
810                 brcmf_err("error (%d)\n", err);
811         return err;
812 }
813
814 /**
815  * brcmf_p2p_scan_prep() - prepare scan based on request.
816  *
817  * @wiphy: wiphy device.
818  * @request: scan request from cfg80211.
819  * @vif: vif on which scan request is to be executed.
820  *
821  * Prepare the scan appropriately for type of scan requested. Overrides the
822  * escan .run() callback for peer-to-peer scanning.
823  */
824 int brcmf_p2p_scan_prep(struct wiphy *wiphy,
825                         struct cfg80211_scan_request *request,
826                         struct brcmf_cfg80211_vif *vif)
827 {
828         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
829         struct brcmf_p2p_info *p2p = &cfg->p2p;
830         int err = 0;
831
832         if (brcmf_p2p_scan_is_p2p_request(request)) {
833                 /* find my listen channel */
834                 err = cfg80211_get_p2p_attr(request->ie, request->ie_len,
835                                       IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
836                                       &p2p->listen_channel, 1);
837                 if (err < 0)
838                         return err;
839
840                 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
841                 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
842
843                 err = brcmf_p2p_enable_discovery(p2p);
844                 if (err)
845                         return err;
846
847                 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
848
849                 /* override .run_escan() callback. */
850                 cfg->escan_info.run = brcmf_p2p_run_escan;
851         }
852         err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
853                                     request->ie, request->ie_len);
854         return err;
855 }
856
857
858 /**
859  * brcmf_p2p_discover_listen() - set firmware to discover listen state.
860  *
861  * @p2p: p2p device.
862  * @freq: center frequency for discover listen.
863  * #@duration: time in ms to stay on channel.
864  *
865  */
866 static s32
867 brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p,
868                           struct ieee80211_channel *channel, u32 duration)
869 {
870         struct brcmf_cfg80211_vif *vif;
871         s32 err = 0;
872         u16 chanspec;
873
874         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
875         if (!vif) {
876                 brcmf_err("Discovery is not set, so we have nothing to do\n");
877                 err = -EPERM;
878                 goto exit;
879         }
880
881         if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
882                 brcmf_err("Previous LISTEN is not completed yet\n");
883                 /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
884                 goto exit;
885         }
886
887         chanspec = channel_to_chanspec(channel);
888         err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
889                                            chanspec, (u16)duration);
890         if (!err) {
891                 set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
892                 p2p->remain_on_channel_cookie++;
893         }
894 exit:
895         return err;
896 }
897
898
899 /**
900  * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
901  *
902  * @wiphy: wiphy device.
903  * @channel: channel to stay on.
904  * @duration: time in ms to remain on channel.
905  *
906  */
907 int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
908                                 struct ieee80211_channel *channel,
909                                 unsigned int duration, u64 *cookie)
910 {
911         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
912         struct brcmf_p2p_info *p2p = &cfg->p2p;
913         s32 err;
914
915         brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n",
916                   ieee80211_frequency_to_channel(channel->center_freq),
917                   duration);
918
919         err = brcmf_p2p_enable_discovery(p2p);
920         if (err)
921                 goto exit;
922         err = brcmf_p2p_discover_listen(p2p, channel, duration);
923         if (err)
924                 goto exit;
925
926         memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
927         *cookie = p2p->remain_on_channel_cookie;
928         cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
929
930 exit:
931         return err;
932 }
933
934
935 /**
936  * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
937  *
938  * @ifp: interfac control.
939  * @e: event message. Not used, to make it usable for fweh event dispatcher.
940  * @data: payload of message. Not used.
941  *
942  */
943 int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
944                                      const struct brcmf_event_msg *e,
945                                      void *data)
946 {
947         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
948         struct brcmf_p2p_info *p2p = &cfg->p2p;
949
950         brcmf_dbg(TRACE, "Enter\n");
951         if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
952                                &p2p->status))
953                 cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
954                                                    p2p->remain_on_channel_cookie,
955                                                    &p2p->remain_on_channel,
956                                                    GFP_KERNEL);
957         return 0;
958 }
959
960
961 /**
962  * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
963  *
964  * @ifp: interfac control.
965  *
966  */
967 void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
968 {
969         if (!ifp)
970                 return;
971         brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
972         brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
973 }
974
975
976 /**
977  * brcmf_p2p_notify_action_frame_rx() - received action frame.
978  *
979  * @ifp: interfac control.
980  * @e: event message. Not used, to make it usable for fweh event dispatcher.
981  * @data: payload of message, containing action frame data.
982  *
983  */
984 int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
985                                      const struct brcmf_event_msg *e,
986                                      void *data)
987 {
988         struct wireless_dev *wdev;
989         u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
990         struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
991         u16 chanspec = be16_to_cpu(rxframe->chanspec);
992         struct ieee80211_mgmt *mgmt_frame;
993         s32 err;
994         s32 freq;
995         u16 mgmt_type;
996
997         /* Check if wpa_supplicant has registered for this frame */
998         brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
999         mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1000         if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1001                 return 0;
1002
1003         brcmf_p2p_print_actframe(false, (u8 *)(rxframe + 1), mgmt_frame_len);
1004
1005         mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1006                              mgmt_frame_len, GFP_KERNEL);
1007         if (!mgmt_frame) {
1008                 brcmf_err("No memory available for action frame\n");
1009                 return -ENOMEM;
1010         }
1011         memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
1012         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1013                                      ETH_ALEN);
1014         if (err < 0)
1015                 brcmf_err("BRCMF_C_GET_BSSID error %d\n", err);
1016         memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1017         mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
1018         memcpy(&mgmt_frame->u, (u8 *)(rxframe + 1), mgmt_frame_len);
1019         mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1020
1021         freq = ieee80211_channel_to_frequency(CHSPEC_CHANNEL(chanspec),
1022                                               CHSPEC_IS2G(chanspec) ?
1023                                               IEEE80211_BAND_2GHZ :
1024                                               IEEE80211_BAND_5GHZ);
1025         wdev = ifp->ndev->ieee80211_ptr;
1026         cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len,
1027                          GFP_ATOMIC);
1028
1029         kfree(mgmt_frame);
1030         return 0;
1031 }
1032
1033
1034 /**
1035  * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1036  *
1037  * @ifp: interfac control.
1038  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1039  * @data: not used.
1040  *
1041  */
1042 int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1043                                         const struct brcmf_event_msg *e,
1044                                         void *data)
1045 {
1046         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1047         struct brcmf_p2p_info *p2p = &cfg->p2p;
1048
1049         brcmf_dbg(INFO, "Enter: status %d\n", e->status);
1050
1051         if (e->status == BRCMF_E_STATUS_SUCCESS)
1052                 set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1053         else
1054                 set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1055         /* for now complete the receiver process here !! */
1056         complete(&p2p->send_af_done);
1057
1058         return 0;
1059 }
1060
1061
1062 /**
1063  * brcmf_p2p_tx_action_frame() - send action frame over fil.
1064  *
1065  * @p2p: p2p info struct for vif.
1066  * @af_params: action frame data/info.
1067  *
1068  * Send an action frame immediately without doing channel synchronization.
1069  *
1070  * This function waits for a completion event before returning.
1071  * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1072  * frame is transmitted.
1073  */
1074 static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1075                                      struct brcmf_fil_af_params_le *af_params)
1076 {
1077         struct brcmf_cfg80211_vif *vif;
1078         s32 err = 0;
1079         s32 timeout = 0;
1080
1081         brcmf_dbg(TRACE, "Enter\n");
1082
1083         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1084         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1085
1086         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1087         err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1088                                         sizeof(*af_params));
1089         if (err) {
1090                 brcmf_err(" sending action frame has failed\n");
1091                 goto exit;
1092         }
1093
1094         timeout = wait_for_completion_timeout(&p2p->send_af_done,
1095                                         msecs_to_jiffies(P2P_AF_MAX_WAIT_TIME));
1096
1097         if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1098                 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1099         } else {
1100                 err = -EIO;
1101                 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1102         }
1103         /* clear status bit for action tx */
1104         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1105         clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1106
1107 exit:
1108         return err;
1109 }
1110
1111
1112 /**
1113  * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1114  *
1115  * @cfg: driver private data for cfg80211 interface.
1116  * @af_params: action frame data/info.
1117  * @config_af_params: configuration data for action frame.
1118  *
1119  * routine which transmits ation frame public type.
1120  */
1121 static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1122                                struct brcmf_fil_af_params_le *af_params,
1123                                struct brcmf_config_af_params *config_af_params)
1124 {
1125         struct brcmf_p2p_info *p2p = &cfg->p2p;
1126         struct brcmf_fil_action_frame_le *action_frame;
1127         struct brcmf_p2p_pub_act_frame *act_frm;
1128         s32 err = 0;
1129
1130         action_frame = &af_params->action_frame;
1131         act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1132
1133         switch (act_frm->subtype) {
1134         case P2P_PAF_GON_REQ:
1135                 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1136                 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1137                 config_af_params->mpc_onoff = 0;
1138                 p2p->next_af_subtype = act_frm->subtype + 1;
1139                 /* increase dwell time to wait for RESP frame */
1140                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1141                 break;
1142         case P2P_PAF_GON_RSP:
1143                 p2p->next_af_subtype = act_frm->subtype + 1;
1144                 /* increase dwell time to wait for CONF frame */
1145                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1146                 break;
1147         case P2P_PAF_GON_CONF:
1148                 /* If we reached till GO Neg confirmation reset the filter */
1149                 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1150                 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1151                 /* turn on mpc again if go nego is done */
1152                 config_af_params->mpc_onoff = 1;
1153                 /* minimize dwell time */
1154                 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1155                 break;
1156         case P2P_PAF_INVITE_REQ:
1157                 p2p->next_af_subtype = act_frm->subtype + 1;
1158                 /* increase dwell time */
1159                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1160                 break;
1161         case P2P_PAF_INVITE_RSP:
1162                 /* minimize dwell time */
1163                 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1164                 break;
1165         case P2P_PAF_DEVDIS_REQ:
1166                 p2p->next_af_subtype = act_frm->subtype + 1;
1167                 /* maximize dwell time to wait for RESP frame */
1168                 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1169                 break;
1170         case P2P_PAF_DEVDIS_RSP:
1171                 /* minimize dwell time */
1172                 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1173                 break;
1174         case P2P_PAF_PROVDIS_REQ:
1175                 config_af_params->mpc_onoff = 0;
1176                 p2p->next_af_subtype = act_frm->subtype + 1;
1177                 /* increase dwell time to wait for RESP frame */
1178                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1179                 break;
1180         case P2P_PAF_PROVDIS_RSP:
1181                 /* wpa_supplicant send go nego req right after prov disc */
1182                 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1183                 /* increase dwell time to MED level */
1184                 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1185                 break;
1186         default:
1187                 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1188                           act_frm->subtype);
1189                 err = -EINVAL;
1190         }
1191         return err;
1192 }
1193
1194 /**
1195  * brcmf_p2p_send_action_frame() - send action frame .
1196  *
1197  * @cfg: driver private data for cfg80211 interface.
1198  * @ndev: net device to transmit on.
1199  * @af_params: configuration data for action frame.
1200  */
1201 bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1202                                  struct net_device *ndev,
1203                                  struct brcmf_fil_af_params_le *af_params)
1204 {
1205         struct brcmf_p2p_info *p2p = &cfg->p2p;
1206         struct brcmf_fil_action_frame_le *action_frame;
1207         struct brcmf_config_af_params config_af_params;
1208         u16 action_frame_len;
1209         bool ack = false;
1210         u8 category;
1211         u8 action;
1212         s32 tx_retry;
1213
1214         action_frame = &af_params->action_frame;
1215         action_frame_len = le16_to_cpu(action_frame->len);
1216
1217         brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1218
1219         /* Add the default dwell time. Dwell time to stay off-channel */
1220         /* to wait for a response action frame after transmitting an  */
1221         /* GO Negotiation action frame                                */
1222         af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1223
1224         category = action_frame->data[DOT11_ACTION_CAT_OFF];
1225         action = action_frame->data[DOT11_ACTION_ACT_OFF];
1226
1227         /* initialize variables */
1228         p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
1229
1230         /* config parameters */
1231         config_af_params.max_tx_retry = P2P_AF_TX_MAX_RETRY;
1232         config_af_params.mpc_onoff = -1;
1233
1234         if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1235                 /* p2p public action frame process */
1236                 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1237                         /* Just send unknown subtype frame with */
1238                         /* default parameters.                  */
1239                         brcmf_err("P2P Public action frame, unknown subtype.\n");
1240                 }
1241         } else if (brcmf_p2p_is_gas_action(action_frame->data,
1242                                            action_frame_len)) {
1243                 /* service discovery process */
1244                 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1245                     action == P2PSD_ACTION_ID_GAS_CREQ) {
1246                         /* save next af suptype to cancel */
1247                         /* remaining dwell time           */
1248                         p2p->next_af_subtype = action + 1;
1249
1250                         af_params->dwell_time =
1251                                 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1252                 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1253                            action == P2PSD_ACTION_ID_GAS_CRESP) {
1254                         /* configure service discovery response frame */
1255                         af_params->dwell_time =
1256                                 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1257                 } else {
1258                         brcmf_err("Unknown action type: %d\n", action);
1259                         goto exit;
1260                 }
1261         } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1262                                            action_frame_len)) {
1263                 /* do not configure anything. it will be */
1264                 /* sent with a default configuration     */
1265         } else {
1266                 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1267                           category, action);
1268                 return false;
1269         }
1270
1271         /* if scan is ongoing, abort current scan. */
1272         if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1273                 brcmf_abort_scanning(cfg);
1274
1275         /* To make sure to send successfully action frame, turn off mpc */
1276         if (config_af_params.mpc_onoff == 0)
1277                 brcmf_set_mpc(ndev, 0);
1278
1279         /* if failed, retry it. tx_retry_max value is configure by .... */
1280         tx_retry = 0;
1281         while ((ack == false) && (tx_retry < config_af_params.max_tx_retry)) {
1282                 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1283                 tx_retry++;
1284         }
1285         if (ack == false)
1286                 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
1287
1288 exit:
1289         /* if all done, turn mpc on again */
1290         if (config_af_params.mpc_onoff == 1)
1291                 brcmf_set_mpc(ndev, 1);
1292
1293         return ack;
1294 }
1295
1296
1297 /**
1298  * brcmf_p2p_attach() - attach for P2P.
1299  *
1300  * @cfg: driver private data for cfg80211 interface.
1301  */
1302 s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg)
1303 {
1304         struct brcmf_if *pri_ifp;
1305         struct brcmf_if *p2p_ifp;
1306         struct brcmf_cfg80211_vif *p2p_vif;
1307         struct brcmf_p2p_info *p2p;
1308         struct brcmf_pub *drvr;
1309         s32 bssidx;
1310         s32 err = 0;
1311
1312         p2p = &cfg->p2p;
1313         p2p->cfg = cfg;
1314
1315         drvr = cfg->pub;
1316
1317         pri_ifp = drvr->iflist[0];
1318         p2p_ifp = drvr->iflist[1];
1319
1320         p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
1321
1322         if (p2p_ifp) {
1323                 p2p_vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_STATION,
1324                                           false);
1325                 if (IS_ERR(p2p_vif)) {
1326                         brcmf_err("could not create discovery vif\n");
1327                         err = -ENOMEM;
1328                         goto exit;
1329                 }
1330
1331                 p2p_vif->ifp = p2p_ifp;
1332                 p2p_ifp->vif = p2p_vif;
1333                 p2p_vif->wdev.netdev = p2p_ifp->ndev;
1334                 p2p_ifp->ndev->ieee80211_ptr = &p2p_vif->wdev;
1335                 SET_NETDEV_DEV(p2p_ifp->ndev, wiphy_dev(cfg->wiphy));
1336
1337                 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
1338
1339                 brcmf_p2p_generate_bss_mac(p2p);
1340                 brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
1341
1342                 /* Initialize P2P Discovery in the firmware */
1343                 err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
1344                 if (err < 0) {
1345                         brcmf_err("set p2p_disc error\n");
1346                         brcmf_free_vif(p2p_vif);
1347                         goto exit;
1348                 }
1349                 /* obtain bsscfg index for P2P discovery */
1350                 err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
1351                 if (err < 0) {
1352                         brcmf_err("retrieving discover bsscfg index failed\n");
1353                         brcmf_free_vif(p2p_vif);
1354                         goto exit;
1355                 }
1356                 /* Verify that firmware uses same bssidx as driver !! */
1357                 if (p2p_ifp->bssidx != bssidx) {
1358                         brcmf_err("Incorrect bssidx=%d, compared to p2p_ifp->bssidx=%d\n",
1359                                   bssidx, p2p_ifp->bssidx);
1360                         brcmf_free_vif(p2p_vif);
1361                         goto exit;
1362                 }
1363
1364                 init_completion(&p2p->send_af_done);
1365         }
1366 exit:
1367         return err;
1368 }
1369
1370
1371 /**
1372  * brcmf_p2p_detach() - detach P2P.
1373  *
1374  * @p2p: P2P specific data.
1375  */
1376 void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
1377 {
1378         struct brcmf_cfg80211_vif *vif;
1379
1380         vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1381         if (vif != NULL) {
1382                 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
1383                 brcmf_p2p_deinit_discovery(p2p);
1384                 /* remove discovery interface */
1385                 brcmf_free_vif(vif);
1386                 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
1387         }
1388         /* just set it all to zero */
1389         memset(p2p, 0, sizeof(*p2p));
1390 }
1391
1392 /**
1393  * brcmf_p2p_get_current_chanspec() - Get current operation channel.
1394  *
1395  * @p2p: P2P specific data.
1396  * @chanspec: chanspec to be returned.
1397  */
1398 static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
1399                                            u16 *chanspec)
1400 {
1401         struct brcmf_if *ifp;
1402         struct brcmf_fil_chan_info_le ci;
1403         s32 err;
1404
1405         ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1406
1407         *chanspec = 11 & WL_CHANSPEC_CHAN_MASK;
1408
1409         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_CHANNEL, &ci, sizeof(ci));
1410         if (!err) {
1411                 *chanspec = le32_to_cpu(ci.hw_channel) & WL_CHANSPEC_CHAN_MASK;
1412                 if (*chanspec < CH_MAX_2G_CHANNEL)
1413                         *chanspec |= WL_CHANSPEC_BAND_2G;
1414                 else
1415                         *chanspec |= WL_CHANSPEC_BAND_5G;
1416         }
1417         *chanspec |= WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
1418 }
1419
1420 /**
1421  * Change a P2P Role.
1422  * Parameters:
1423  * @mac: MAC address of the BSS to change a role
1424  * Returns 0 if success.
1425  */
1426 int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
1427                        enum brcmf_fil_p2p_if_types if_type)
1428 {
1429         struct brcmf_p2p_info *p2p = &cfg->p2p;
1430         struct brcmf_cfg80211_vif *vif;
1431         struct brcmf_fil_p2p_if_le if_request;
1432         s32 err;
1433         u16 chanspec;
1434
1435         brcmf_dbg(TRACE, "Enter\n");
1436
1437         vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1438         if (!vif) {
1439                 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
1440                 return -EPERM;
1441         }
1442         brcmf_notify_escan_complete(cfg, vif->ifp->ndev, true, true);
1443         vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
1444         if (!vif) {
1445                 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
1446                 return -EPERM;
1447         }
1448         brcmf_set_mpc(vif->ifp->ndev, 0);
1449
1450         /* In concurrency case, STA may be already associated in a particular */
1451         /* channel. so retrieve the current channel of primary interface and  */
1452         /* then start the virtual interface on that.                          */
1453         brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1454
1455         if_request.type = cpu_to_le16((u16)if_type);
1456         if_request.chspec = cpu_to_le16(chanspec);
1457         memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
1458
1459         brcmf_cfg80211_arm_vif_event(cfg, vif);
1460         err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
1461                                        sizeof(if_request));
1462         if (err) {
1463                 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
1464                 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1465                 return err;
1466         }
1467         err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_CHANGE,
1468                                                     msecs_to_jiffies(1500));
1469         brcmf_cfg80211_arm_vif_event(cfg, NULL);
1470         if (!err)  {
1471                 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
1472                 return -EIO;
1473         }
1474
1475         err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
1476                                     BRCMF_SCB_TIMEOUT_VALUE);
1477
1478         return err;
1479 }
1480
1481 static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
1482                                     struct brcmf_if *ifp, u8 ea[ETH_ALEN],
1483                                     enum brcmf_fil_p2p_if_types iftype)
1484 {
1485         struct brcmf_fil_p2p_if_le if_request;
1486         int err;
1487         u16 chanspec;
1488
1489         /* we need a default channel */
1490         brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1491
1492         /* fill the firmware request */
1493         memcpy(if_request.addr, ea, ETH_ALEN);
1494         if_request.type = cpu_to_le16((u16)iftype);
1495         if_request.chspec = cpu_to_le16(chanspec);
1496
1497         err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
1498                                        sizeof(if_request));
1499         if (err)
1500                 return err;
1501
1502         return err;
1503 }
1504
1505 static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
1506 {
1507         struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
1508         struct net_device *pri_ndev = cfg_to_ndev(cfg);
1509         struct brcmf_if *ifp = netdev_priv(pri_ndev);
1510         u8 *addr = vif->wdev.netdev->dev_addr;
1511
1512         return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
1513 }
1514
1515 static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
1516 {
1517         struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
1518         struct net_device *pri_ndev = cfg_to_ndev(cfg);
1519         struct brcmf_if *ifp = netdev_priv(pri_ndev);
1520         u8 *addr = vif->wdev.netdev->dev_addr;
1521
1522         return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
1523 }
1524
1525 /**
1526  * brcmf_p2p_add_vif() - create a new P2P virtual interface.
1527  *
1528  * @wiphy: wiphy device of new interface.
1529  * @name: name of the new interface.
1530  * @type: nl80211 interface type.
1531  * @flags: TBD
1532  * @params: TBD
1533  */
1534 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
1535                                        enum nl80211_iftype type, u32 *flags,
1536                                        struct vif_params *params)
1537 {
1538         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1539         struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
1540         struct brcmf_cfg80211_vif *vif;
1541         enum brcmf_fil_p2p_if_types iftype;
1542         enum wl_mode mode;
1543         int err;
1544
1545         if (brcmf_cfg80211_vif_event_armed(cfg))
1546                 return ERR_PTR(-EBUSY);
1547
1548         brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
1549
1550         switch (type) {
1551         case NL80211_IFTYPE_P2P_CLIENT:
1552                 iftype = BRCMF_FIL_P2P_IF_CLIENT;
1553                 mode = WL_MODE_BSS;
1554                 break;
1555         case NL80211_IFTYPE_P2P_GO:
1556                 iftype = BRCMF_FIL_P2P_IF_GO;
1557                 mode = WL_MODE_AP;
1558                 break;
1559         default:
1560                 return ERR_PTR(-EOPNOTSUPP);
1561         }
1562
1563         vif = brcmf_alloc_vif(cfg, type, false);
1564         if (IS_ERR(vif))
1565                 return (struct wireless_dev *)vif;
1566         brcmf_cfg80211_arm_vif_event(cfg, vif);
1567
1568         err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
1569                                        iftype);
1570         if (err) {
1571                 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1572                 goto fail;
1573         }
1574
1575         /* wait for firmware event */
1576         err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
1577                                                     msecs_to_jiffies(1500));
1578         brcmf_cfg80211_arm_vif_event(cfg, NULL);
1579         if (!err) {
1580                 brcmf_err("timeout occurred\n");
1581                 err = -EIO;
1582                 goto fail;
1583         }
1584
1585         /* interface created in firmware */
1586         ifp = vif->ifp;
1587         if (!ifp) {
1588                 brcmf_err("no if pointer provided\n");
1589                 err = -ENOENT;
1590                 goto fail;
1591         }
1592
1593         strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
1594         brcmf_cfg80211_vif_complete(cfg);
1595         cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
1596         /* Disable firmware roaming for P2P interface  */
1597         brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
1598         if (iftype == BRCMF_FIL_P2P_IF_GO) {
1599                 /* set station timeout for p2p */
1600                 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
1601                                       BRCMF_SCB_TIMEOUT_VALUE);
1602         }
1603         return &ifp->vif->wdev;
1604
1605 fail:
1606         brcmf_free_vif(vif);
1607         return ERR_PTR(err);
1608 }
1609
1610 /**
1611  * brcmf_p2p_del_vif() - delete a P2P virtual interface.
1612  *
1613  * @wiphy: wiphy device of interface.
1614  * @wdev: wireless device of interface.
1615  *
1616  * TODO: not yet supported.
1617  */
1618 int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
1619 {
1620         struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
1621         struct brcmf_cfg80211_vif *vif;
1622         unsigned long jiffie_timeout = msecs_to_jiffies(1500);
1623         bool wait_for_disable = false;
1624         int err;
1625
1626         brcmf_dbg(TRACE, "delete P2P vif\n");
1627         vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
1628
1629         switch (vif->wdev.iftype) {
1630         case NL80211_IFTYPE_P2P_CLIENT:
1631                 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
1632                         wait_for_disable = true;
1633                 break;
1634
1635         case NL80211_IFTYPE_P2P_GO:
1636                 if (!brcmf_p2p_disable_p2p_if(vif))
1637                         wait_for_disable = true;
1638                 break;
1639
1640         case NL80211_IFTYPE_P2P_DEVICE:
1641         default:
1642                 return -ENOTSUPP;
1643                 break;
1644         }
1645
1646         if (wait_for_disable)
1647                 wait_for_completion_timeout(&cfg->vif_disabled,
1648                                             msecs_to_jiffies(500));
1649
1650         brcmf_vif_clear_mgmt_ies(vif);
1651
1652         brcmf_cfg80211_arm_vif_event(cfg, vif);
1653         err = brcmf_p2p_release_p2p_if(vif);
1654         if (!err) {
1655                 /* wait for firmware event */
1656                 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_DEL,
1657                                                             jiffie_timeout);
1658                 if (!err)
1659                         err = -EIO;
1660                 else
1661                         err = 0;
1662         }
1663         brcmf_cfg80211_arm_vif_event(cfg, NULL);
1664         brcmf_free_vif(vif);
1665         cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
1666
1667         return err;
1668 }