brcmfmac: P2P action frame tx.
[GitHub/mt8127/android_kernel_alcatel_ttab.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 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
304 {
305 struct brcmf_p2p_pub_act_frame *pact_frm;
306 struct brcmf_p2p_action_frame *act_frm;
307 struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
308
309 if (!frame || frame_len <= 2)
310 return;
311
312 if (brcmf_p2p_is_pub_action(frame, frame_len)) {
313 pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
314 switch (pact_frm->subtype) {
315 case P2P_PAF_GON_REQ:
316 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
317 (tx) ? "TX" : "RX");
318 break;
319 case P2P_PAF_GON_RSP:
320 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
321 (tx) ? "TX" : "RX");
322 break;
323 case P2P_PAF_GON_CONF:
324 brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
325 (tx) ? "TX" : "RX");
326 break;
327 case P2P_PAF_INVITE_REQ:
328 brcmf_dbg(TRACE, "%s P2P Invitation Request Frame\n",
329 (tx) ? "TX" : "RX");
330 break;
331 case P2P_PAF_INVITE_RSP:
332 brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
333 (tx) ? "TX" : "RX");
334 break;
335 case P2P_PAF_DEVDIS_REQ:
336 brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
337 (tx) ? "TX" : "RX");
338 break;
339 case P2P_PAF_DEVDIS_RSP:
340 brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
341 (tx) ? "TX" : "RX");
342 break;
343 case P2P_PAF_PROVDIS_REQ:
344 brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
345 (tx) ? "TX" : "RX");
346 break;
347 case P2P_PAF_PROVDIS_RSP:
348 brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
349 (tx) ? "TX" : "RX");
350 break;
351 default:
352 brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
353 (tx) ? "TX" : "RX");
354 break;
355 }
356 } else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
357 act_frm = (struct brcmf_p2p_action_frame *)frame;
358 switch (act_frm->subtype) {
359 case P2P_AF_NOTICE_OF_ABSENCE:
360 brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
361 (tx) ? "TX" : "RX");
362 break;
363 case P2P_AF_PRESENCE_REQ:
364 brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
365 (tx) ? "TX" : "RX");
366 break;
367 case P2P_AF_PRESENCE_RSP:
368 brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
369 (tx) ? "TX" : "RX");
370 break;
371 case P2P_AF_GO_DISC_REQ:
372 brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
373 (tx) ? "TX" : "RX");
374 break;
375 default:
376 brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
377 (tx) ? "TX" : "RX");
378 }
379
380 } else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
381 sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
382 switch (sd_act_frm->action) {
383 case P2PSD_ACTION_ID_GAS_IREQ:
384 brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
385 (tx) ? "TX" : "RX");
386 break;
387 case P2PSD_ACTION_ID_GAS_IRESP:
388 brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
389 (tx) ? "TX" : "RX");
390 break;
391 case P2PSD_ACTION_ID_GAS_CREQ:
392 brcmf_dbg(TRACE, "%s P2P GAS Comback Request\n",
393 (tx) ? "TX" : "RX");
394 break;
395 case P2PSD_ACTION_ID_GAS_CRESP:
396 brcmf_dbg(TRACE, "%s P2P GAS Comback Response\n",
397 (tx) ? "TX" : "RX");
398 break;
399 default:
400 brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
401 (tx) ? "TX" : "RX");
402 break;
403 }
404 }
405 }
406
407 /**
408 * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
409 *
410 * @p2p: P2P specific data.
411 */
412 static int brcmf_p2p_set_firmware(struct brcmf_p2p_info *p2p)
413 {
414 struct net_device *ndev = cfg_to_ndev(p2p->cfg);
415 u8 null_eth_addr[] = { 0, 0, 0, 0, 0, 0 };
416 s32 ret = 0;
417
418 brcmf_fil_iovar_int_set(netdev_priv(ndev), "apsta", 1);
419
420 /* In case of COB type, firmware has default mac address
421 * After Initializing firmware, we have to set current mac address to
422 * firmware for P2P device address
423 */
424 ret = brcmf_fil_iovar_data_set(netdev_priv(ndev), "p2p_da_override",
425 null_eth_addr, sizeof(null_eth_addr));
426 if (ret)
427 brcmf_err("failed to update device address ret %d\n", ret);
428
429 return ret;
430 }
431
432 /**
433 * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
434 *
435 * @p2p: P2P specific data.
436 *
437 * P2P needs mac addresses for P2P device and interface. These are
438 * derived from the primary net device, ie. the permanent ethernet
439 * address of the device.
440 */
441 static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p)
442 {
443 /* Generate the P2P Device Address. This consists of the device's
444 * primary MAC address with the locally administered bit set.
445 */
446 memcpy(p2p->dev_addr, p2p->cfg->pub->mac, ETH_ALEN);
447 p2p->dev_addr[0] |= 0x02;
448
449 /* Generate the P2P Interface Address. If the discovery and connection
450 * BSSCFGs need to simultaneously co-exist, then this address must be
451 * different from the P2P Device Address, but also locally administered.
452 */
453 memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
454 p2p->int_addr[4] ^= 0x80;
455 }
456
457 /**
458 * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
459 *
460 * @request: the scan request as received from cfg80211.
461 *
462 * returns true if one of the ssids in the request matches the
463 * P2P wildcard ssid; otherwise returns false.
464 */
465 static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
466 {
467 struct cfg80211_ssid *ssids = request->ssids;
468 int i;
469
470 for (i = 0; i < request->n_ssids; i++) {
471 if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
472 continue;
473
474 brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
475 if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
476 BRCMF_P2P_WILDCARD_SSID_LEN))
477 return true;
478 }
479 return false;
480 }
481
482 /**
483 * brcmf_p2p_set_discover_state - set discover state in firmware.
484 *
485 * @ifp: low-level interface object.
486 * @state: discover state to set.
487 * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
488 * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
489 */
490 static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
491 u16 chanspec, u16 listen_ms)
492 {
493 struct brcmf_p2p_disc_st_le discover_state;
494 s32 ret = 0;
495 brcmf_dbg(TRACE, "enter\n");
496
497 discover_state.state = state;
498 discover_state.chspec = cpu_to_le16(chanspec);
499 discover_state.dwell = cpu_to_le16(listen_ms);
500 ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
501 sizeof(discover_state));
502 return ret;
503 }
504
505 /**
506 * brcmf_p2p_init_discovery() - enable discovery in the firmware.
507 *
508 * @p2p: P2P specific data.
509 *
510 * Configures the firmware to allow P2P peer discovery. Creates the
511 * virtual interface and consequently the P2P device for it.
512 */
513 static s32 brcmf_p2p_init_discovery(struct brcmf_p2p_info *p2p)
514 {
515 struct net_device *ndev = cfg_to_ndev(p2p->cfg);
516 struct brcmf_cfg80211_vif *vif;
517 struct brcmf_if *ifp;
518 struct p2p_bss *bss_dev;
519 s32 index;
520 s32 ret;
521
522 brcmf_dbg(TRACE, "enter\n");
523
524 bss_dev = &p2p->bss_idx[P2PAPI_BSSCFG_DEVICE];
525 if (bss_dev->vif != NULL) {
526 brcmf_dbg(INFO, "do nothing, already initialized\n");
527 return 0;
528 }
529
530 /* Enable P2P Discovery in the firmware */
531 ret = brcmf_fil_iovar_int_set(netdev_priv(ndev), "p2p_disc", 1);
532 if (ret < 0) {
533 brcmf_err("set discover error\n");
534 return ret;
535 }
536
537 /* obtain bsscfg index for P2P discovery */
538 ret = brcmf_fil_iovar_int_get(netdev_priv(ndev), "p2p_dev", &index);
539 if (ret < 0) {
540 brcmf_err("retrieving discover bsscfg index failed\n");
541 return ret;
542 }
543
544 /*
545 * need brcmf_if for setting the discovery state.
546 */
547 ifp = kzalloc(sizeof(*vif->ifp), GFP_KERNEL);
548 if (!ifp) {
549 brcmf_err("could not create discovery if\n");
550 return -ENOMEM;
551 }
552
553 /* set required fields */
554 ifp->drvr = p2p->cfg->pub;
555 ifp->ifidx = 0;
556 ifp->bssidx = index;
557
558 /* Set the initial discovery state to SCAN */
559 ret = brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
560
561 if (ret != 0) {
562 brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
563 (void)brcmf_fil_iovar_int_set(netdev_priv(ndev), "p2p_disc", 0);
564 kfree(ifp);
565 return ret;
566 }
567
568 /* create a vif for it */
569 vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE, false);
570 if (IS_ERR(vif)) {
571 brcmf_err("could not create discovery vif\n");
572 kfree(ifp);
573 return PTR_ERR(vif);
574 }
575
576 vif->ifp = ifp;
577 ifp->vif = vif;
578 bss_dev->vif = vif;
579
580 return 0;
581 }
582
583 /**
584 * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
585 *
586 * @p2p: P2P specific data.
587 *
588 * Resets the discovery state and disables it in firmware. The virtual
589 * interface and P2P device are freed.
590 */
591 static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
592 {
593 struct net_device *ndev = cfg_to_ndev(p2p->cfg);
594 struct brcmf_if *ifp;
595 struct p2p_bss *bss_dev;
596 brcmf_dbg(TRACE, "enter\n");
597
598 bss_dev = &p2p->bss_idx[P2PAPI_BSSCFG_DEVICE];
599 ifp = bss_dev->vif->ifp;
600
601 /* Set the discovery state to SCAN */
602 (void)brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
603
604 /* Disable P2P discovery in the firmware */
605 (void)brcmf_fil_iovar_int_set(netdev_priv(ndev), "p2p_disc", 0);
606
607 /* remove discovery interface */
608 brcmf_free_vif(bss_dev->vif);
609 bss_dev->vif = NULL;
610 kfree(ifp);
611
612 return 0;
613 }
614
615 /**
616 * brcmf_p2p_enable_discovery() - initialize and configure discovery.
617 *
618 * @p2p: P2P specific data.
619 *
620 * Initializes the discovery device and configure the virtual interface.
621 */
622 static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
623 {
624 struct brcmf_cfg80211_vif *vif;
625 s32 ret = 0;
626
627 brcmf_dbg(TRACE, "enter\n");
628 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
629 if (vif) {
630 brcmf_dbg(INFO, "DISCOVERY init already done\n");
631 goto exit;
632 }
633
634 ret = brcmf_p2p_init_discovery(p2p);
635 if (ret < 0) {
636 brcmf_err("init discovery error %d\n", ret);
637 goto exit;
638 }
639
640 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
641
642 /*
643 * Set wsec to any non-zero value in the discovery bsscfg
644 * to ensure our P2P probe responses have the privacy bit
645 * set in the 802.11 WPA IE. Some peer devices may not
646 * initiate WPS with us if this bit is not set.
647 */
648 ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
649 if (ret < 0)
650 brcmf_err("wsec error %d\n", ret);
651
652 exit:
653 return ret;
654 }
655
656 /**
657 * brcmf_p2p_escan() - initiate a P2P scan.
658 *
659 * @p2p: P2P specific data.
660 * @num_chans: number of channels to scan.
661 * @chanspecs: channel parameters for @num_chans channels.
662 * @search_state: P2P discover state to use.
663 * @action: scan action to pass to firmware.
664 * @bss_type: type of P2P bss.
665 */
666 static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
667 u16 chanspecs[], s32 search_state, u16 action,
668 enum p2p_bss_type bss_type)
669 {
670 s32 ret = 0;
671 s32 memsize = offsetof(struct brcmf_p2p_scan_le,
672 eparams.params_le.channel_list);
673 s32 nprobes;
674 s32 active;
675 u32 i;
676 u8 *memblk;
677 struct brcmf_cfg80211_vif *vif;
678 struct brcmf_p2p_scan_le *p2p_params;
679 struct brcmf_scan_params_le *sparams;
680 struct brcmf_ssid ssid;
681
682 memsize += num_chans * sizeof(__le16);
683 memblk = kzalloc(memsize, GFP_KERNEL);
684 if (!memblk)
685 return -ENOMEM;
686
687 vif = p2p->bss_idx[bss_type].vif;
688 if (vif == NULL) {
689 brcmf_err("no vif for bss type %d\n", bss_type);
690 ret = -EINVAL;
691 goto exit;
692 }
693
694 switch (search_state) {
695 case WL_P2P_DISC_ST_SEARCH:
696 /*
697 * If we in SEARCH STATE, we don't need to set SSID explictly
698 * because dongle use P2P WILDCARD internally by default
699 */
700 /* use null ssid */
701 ssid.SSID_len = 0;
702 memset(ssid.SSID, 0, sizeof(ssid.SSID));
703 break;
704 case WL_P2P_DISC_ST_SCAN:
705 /*
706 * wpa_supplicant has p2p_find command with type social or
707 * progressive. For progressive, we need to set the ssid to
708 * P2P WILDCARD because we just do broadcast scan unless
709 * setting SSID.
710 */
711 ssid.SSID_len = BRCMF_P2P_WILDCARD_SSID_LEN;
712 memcpy(ssid.SSID, BRCMF_P2P_WILDCARD_SSID, ssid.SSID_len);
713 break;
714 default:
715 brcmf_err(" invalid search state %d\n", search_state);
716 ret = -EINVAL;
717 goto exit;
718 }
719
720 brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
721
722 /*
723 * set p2p scan parameters.
724 */
725 p2p_params = (struct brcmf_p2p_scan_le *)memblk;
726 p2p_params->type = 'E';
727
728 /* determine the scan engine parameters */
729 sparams = &p2p_params->eparams.params_le;
730 sparams->bss_type = DOT11_BSSTYPE_ANY;
731 if (p2p->cfg->active_scan)
732 sparams->scan_type = 0;
733 else
734 sparams->scan_type = 1;
735
736 memset(&sparams->bssid, 0xFF, ETH_ALEN);
737 if (ssid.SSID_len)
738 memcpy(sparams->ssid_le.SSID, ssid.SSID, ssid.SSID_len);
739 sparams->ssid_le.SSID_len = cpu_to_le32(ssid.SSID_len);
740 sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
741
742 /*
743 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
744 * supported by the supplicant.
745 */
746 if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
747 active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
748 else if (num_chans == AF_PEER_SEARCH_CNT)
749 active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
750 else if (wl_get_vif_state_all(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
751 active = -1;
752 else
753 active = P2PAPI_SCAN_DWELL_TIME_MS;
754
755 /* Override scan params to find a peer for a connection */
756 if (num_chans == 1) {
757 active = WL_SCAN_CONNECT_DWELL_TIME_MS;
758 /* WAR to sync with presence period of VSDB GO.
759 * send probe request more frequently
760 */
761 nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
762 } else {
763 nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
764 }
765
766 if (nprobes <= 0)
767 nprobes = 1;
768
769 brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
770 sparams->active_time = cpu_to_le32(active);
771 sparams->nprobes = cpu_to_le32(nprobes);
772 sparams->passive_time = cpu_to_le32(-1);
773 sparams->channel_num = cpu_to_le32(num_chans &
774 BRCMF_SCAN_PARAMS_COUNT_MASK);
775 for (i = 0; i < num_chans; i++)
776 sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
777
778 /* set the escan specific parameters */
779 p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
780 p2p_params->eparams.action = cpu_to_le16(action);
781 p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
782 /* perform p2p scan on primary device */
783 ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
784 if (!ret)
785 set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
786 exit:
787 kfree(memblk);
788 return ret;
789 }
790
791 /**
792 * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
793 *
794 * @cfg: driver private data for cfg80211 interface.
795 * @ndev: net device for which scan is requested.
796 * @request: scan request from cfg80211.
797 * @action: scan action.
798 *
799 * Determines the P2P discovery state based to scan request parameters and
800 * validates the channels in the request.
801 */
802 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
803 struct net_device *ndev,
804 struct cfg80211_scan_request *request,
805 u16 action)
806 {
807 struct brcmf_p2p_info *p2p = &cfg->p2p;
808 s32 err = 0;
809 s32 search_state = WL_P2P_DISC_ST_SCAN;
810 struct brcmf_cfg80211_vif *vif;
811 struct net_device *dev = NULL;
812 int i, num_nodfs = 0;
813 u16 *chanspecs;
814
815 brcmf_dbg(TRACE, "enter\n");
816
817 if (!request) {
818 err = -EINVAL;
819 goto exit;
820 }
821
822 if (request->n_channels) {
823 chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
824 GFP_KERNEL);
825 if (!chanspecs) {
826 err = -ENOMEM;
827 goto exit;
828 }
829 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
830 if (vif)
831 dev = vif->wdev.netdev;
832 if (request->n_channels == 3 &&
833 request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
834 request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
835 request->channels[2]->hw_value == SOCIAL_CHAN_3) {
836 /* SOCIAL CHANNELS 1, 6, 11 */
837 search_state = WL_P2P_DISC_ST_SEARCH;
838 brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
839 } else if (dev != NULL && vif->mode == WL_MODE_AP) {
840 /* If you are already a GO, then do SEARCH only */
841 brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
842 search_state = WL_P2P_DISC_ST_SEARCH;
843 } else {
844 brcmf_dbg(INFO, "P2P SCAN STATE START\n");
845 }
846
847 /*
848 * no P2P scanning on passive or DFS channels.
849 */
850 for (i = 0; i < request->n_channels; i++) {
851 struct ieee80211_channel *chan = request->channels[i];
852
853 if (chan->flags & (IEEE80211_CHAN_RADAR |
854 IEEE80211_CHAN_PASSIVE_SCAN))
855 continue;
856
857 chanspecs[i] = channel_to_chanspec(chan);
858 brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
859 num_nodfs, chan->hw_value, chanspecs[i]);
860 num_nodfs++;
861 }
862 err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
863 action, P2PAPI_BSSCFG_DEVICE);
864 }
865 exit:
866 if (err)
867 brcmf_err("error (%d)\n", err);
868 return err;
869 }
870
871 /**
872 * brcmf_p2p_scan_prep() - prepare scan based on request.
873 *
874 * @wiphy: wiphy device.
875 * @request: scan request from cfg80211.
876 * @vif: vif on which scan request is to be executed.
877 *
878 * Prepare the scan appropriately for type of scan requested. Overrides the
879 * escan .run() callback for peer-to-peer scanning.
880 */
881 int brcmf_p2p_scan_prep(struct wiphy *wiphy,
882 struct cfg80211_scan_request *request,
883 struct brcmf_cfg80211_vif *vif)
884 {
885 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
886 struct brcmf_p2p_info *p2p = &cfg->p2p;
887 int err = 0;
888
889 if (brcmf_p2p_scan_is_p2p_request(request)) {
890 /* find my listen channel */
891 err = cfg80211_get_p2p_attr(request->ie, request->ie_len,
892 IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
893 &p2p->listen_channel, 1);
894 if (err < 0)
895 return err;
896
897 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
898 brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
899
900 err = brcmf_p2p_enable_discovery(p2p);
901 if (err)
902 return err;
903
904 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
905
906 /* override .run_escan() callback. */
907 cfg->escan_info.run = brcmf_p2p_run_escan;
908 }
909 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
910 request->ie, request->ie_len);
911 return err;
912 }
913
914
915 /**
916 * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
917 *
918 * @wiphy: wiphy device.
919 * @channel: channel to stay on.
920 * @duration: time in ms to remain on channel.
921 *
922 */
923 int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
924 struct ieee80211_channel *channel,
925 unsigned int duration, u64 *cookie)
926 {
927 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
928 struct brcmf_p2p_info *p2p = &cfg->p2p;
929 struct brcmf_cfg80211_vif *vif;
930 s32 err;
931 u16 chanspec;
932
933 brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n",
934 ieee80211_frequency_to_channel(channel->center_freq),
935 duration);
936
937 *cookie = 0;
938 err = brcmf_p2p_enable_discovery(p2p);
939 if (err)
940 goto exit;
941
942 chanspec = channel_to_chanspec(channel);
943 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
944 err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
945 chanspec, (u16)duration);
946 if (err)
947 goto exit;
948
949 memcpy(&p2p->remain_on_channel, channel,
950 sizeof(p2p->remain_on_channel));
951
952 set_bit(BRCMF_P2P_STATUS_REMAIN_ON_CHANNEL, &p2p->status);
953
954 exit:
955 cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
956 return err;
957 }
958
959
960 /**
961 * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
962 *
963 * @ifp: interfac control.
964 * @e: event message. Not used, to make it usable for fweh event dispatcher.
965 * @data: payload of message. Not used.
966 *
967 */
968 int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
969 const struct brcmf_event_msg *e,
970 void *data)
971 {
972 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
973 struct brcmf_p2p_info *p2p = &cfg->p2p;
974
975 brcmf_dbg(TRACE, "Enter\n");
976 if (test_and_clear_bit(BRCMF_P2P_STATUS_REMAIN_ON_CHANNEL,
977 &p2p->status))
978 cfg80211_remain_on_channel_expired(&ifp->vif->wdev, 0,
979 &p2p->remain_on_channel,
980 GFP_KERNEL);
981 return 0;
982 }
983
984
985 /**
986 * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
987 *
988 * @ifp: interfac control.
989 *
990 */
991 void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
992 {
993 if (!ifp)
994 return;
995 brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
996 brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
997 }
998
999
1000 /**
1001 * brcmf_p2p_notify_action_frame_rx() - received action frame.
1002 *
1003 * @ifp: interfac control.
1004 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1005 * @data: payload of message, containing action frame data.
1006 *
1007 */
1008 int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1009 const struct brcmf_event_msg *e,
1010 void *data)
1011 {
1012 struct wireless_dev *wdev;
1013 u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1014 struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1015 u16 chanspec = be16_to_cpu(rxframe->chanspec);
1016 struct ieee80211_mgmt *mgmt_frame;
1017 s32 err;
1018 s32 freq;
1019 u16 mgmt_type;
1020
1021 /* Check if wpa_supplicant has registered for this frame */
1022 brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1023 mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1024 if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1025 return 0;
1026
1027 brcmf_p2p_print_actframe(false, (u8 *)(rxframe + 1), mgmt_frame_len);
1028
1029 mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1030 mgmt_frame_len, GFP_KERNEL);
1031 if (!mgmt_frame) {
1032 brcmf_err("No memory available for action frame\n");
1033 return -ENOMEM;
1034 }
1035 memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
1036 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1037 ETH_ALEN);
1038 if (err < 0)
1039 brcmf_err("BRCMF_C_GET_BSSID error %d\n", err);
1040 memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1041 mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
1042 memcpy(&mgmt_frame->u, (u8 *)(rxframe + 1), mgmt_frame_len);
1043 mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1044
1045 freq = ieee80211_channel_to_frequency(CHSPEC_CHANNEL(chanspec),
1046 CHSPEC_IS2G(chanspec) ?
1047 IEEE80211_BAND_2GHZ :
1048 IEEE80211_BAND_5GHZ);
1049 wdev = ifp->ndev->ieee80211_ptr;
1050 cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len,
1051 GFP_ATOMIC);
1052
1053 kfree(mgmt_frame);
1054 return 0;
1055 }
1056
1057
1058 /**
1059 * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1060 *
1061 * @ifp: interfac control.
1062 * @e: event message. Not used, to make it usable for fweh event dispatcher.
1063 * @data: not used.
1064 *
1065 */
1066 int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1067 const struct brcmf_event_msg *e,
1068 void *data)
1069 {
1070 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1071 struct brcmf_p2p_info *p2p = &cfg->p2p;
1072
1073 brcmf_dbg(INFO, "Enter: status %d\n", e->status);
1074
1075 if (e->status == BRCMF_E_STATUS_SUCCESS)
1076 set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1077 else
1078 set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1079 /* for now complete the receiver process here !! */
1080 complete(&p2p->send_af_done);
1081
1082 return 0;
1083 }
1084
1085
1086 /**
1087 * brcmf_p2p_tx_action_frame() - send action frame over fil.
1088 *
1089 * @p2p: p2p info struct for vif.
1090 * @af_params: action frame data/info.
1091 *
1092 * Send an action frame immediately without doing channel synchronization.
1093 *
1094 * This function waits for a completion event before returning.
1095 * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1096 * frame is transmitted.
1097 */
1098 static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1099 struct brcmf_fil_af_params_le *af_params)
1100 {
1101 struct brcmf_cfg80211_vif *vif;
1102 s32 err = 0;
1103 s32 timeout = 0;
1104
1105 brcmf_dbg(TRACE, "Enter\n");
1106
1107 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1108 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1109
1110 vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1111 err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1112 sizeof(*af_params));
1113 if (err) {
1114 brcmf_err(" sending action frame has failed\n");
1115 goto exit;
1116 }
1117
1118 timeout = wait_for_completion_timeout(&p2p->send_af_done,
1119 msecs_to_jiffies(P2P_AF_MAX_WAIT_TIME));
1120
1121 if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1122 brcmf_dbg(TRACE, "TX action frame operation is success\n");
1123 } else {
1124 err = -EIO;
1125 brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1126 }
1127 /* clear status bit for action tx */
1128 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1129 clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1130
1131 exit:
1132 return err;
1133 }
1134
1135
1136 /**
1137 * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1138 *
1139 * @cfg: driver private data for cfg80211 interface.
1140 * @af_params: action frame data/info.
1141 * @config_af_params: configuration data for action frame.
1142 *
1143 * routine which transmits ation frame public type.
1144 */
1145 static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1146 struct brcmf_fil_af_params_le *af_params,
1147 struct brcmf_config_af_params *config_af_params)
1148 {
1149 struct brcmf_p2p_info *p2p = &cfg->p2p;
1150 struct brcmf_fil_action_frame_le *action_frame;
1151 struct brcmf_p2p_pub_act_frame *act_frm;
1152 s32 err = 0;
1153
1154 action_frame = &af_params->action_frame;
1155 act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1156
1157 switch (act_frm->subtype) {
1158 case P2P_PAF_GON_REQ:
1159 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1160 set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1161 config_af_params->mpc_onoff = 0;
1162 p2p->next_af_subtype = act_frm->subtype + 1;
1163 /* increase dwell time to wait for RESP frame */
1164 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1165 break;
1166 case P2P_PAF_GON_RSP:
1167 p2p->next_af_subtype = act_frm->subtype + 1;
1168 /* increase dwell time to wait for CONF frame */
1169 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1170 break;
1171 case P2P_PAF_GON_CONF:
1172 /* If we reached till GO Neg confirmation reset the filter */
1173 brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1174 clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1175 /* turn on mpc again if go nego is done */
1176 config_af_params->mpc_onoff = 1;
1177 /* minimize dwell time */
1178 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1179 break;
1180 case P2P_PAF_INVITE_REQ:
1181 p2p->next_af_subtype = act_frm->subtype + 1;
1182 /* increase dwell time */
1183 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1184 break;
1185 case P2P_PAF_INVITE_RSP:
1186 /* minimize dwell time */
1187 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1188 break;
1189 case P2P_PAF_DEVDIS_REQ:
1190 p2p->next_af_subtype = act_frm->subtype + 1;
1191 /* maximize dwell time to wait for RESP frame */
1192 af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1193 break;
1194 case P2P_PAF_DEVDIS_RSP:
1195 /* minimize dwell time */
1196 af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1197 break;
1198 case P2P_PAF_PROVDIS_REQ:
1199 config_af_params->mpc_onoff = 0;
1200 p2p->next_af_subtype = act_frm->subtype + 1;
1201 /* increase dwell time to wait for RESP frame */
1202 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1203 break;
1204 case P2P_PAF_PROVDIS_RSP:
1205 /* wpa_supplicant send go nego req right after prov disc */
1206 p2p->next_af_subtype = P2P_PAF_GON_REQ;
1207 /* increase dwell time to MED level */
1208 af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1209 break;
1210 default:
1211 brcmf_err("Unknown p2p pub act frame subtype: %d\n",
1212 act_frm->subtype);
1213 err = -EINVAL;
1214 }
1215 return err;
1216 }
1217
1218 /**
1219 * brcmf_p2p_send_action_frame() - send action frame .
1220 *
1221 * @cfg: driver private data for cfg80211 interface.
1222 * @ndev: net device to transmit on.
1223 * @af_params: configuration data for action frame.
1224 */
1225 bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1226 struct net_device *ndev,
1227 struct brcmf_fil_af_params_le *af_params)
1228 {
1229 struct brcmf_p2p_info *p2p = &cfg->p2p;
1230 struct brcmf_fil_action_frame_le *action_frame;
1231 struct brcmf_config_af_params config_af_params;
1232 u16 action_frame_len;
1233 bool ack = false;
1234 u8 category;
1235 u8 action;
1236 s32 tx_retry;
1237
1238 action_frame = &af_params->action_frame;
1239 action_frame_len = le16_to_cpu(action_frame->len);
1240
1241 brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1242
1243 /* Add the default dwell time. Dwell time to stay off-channel */
1244 /* to wait for a response action frame after transmitting an */
1245 /* GO Negotiation action frame */
1246 af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1247
1248 category = action_frame->data[DOT11_ACTION_CAT_OFF];
1249 action = action_frame->data[DOT11_ACTION_ACT_OFF];
1250
1251 /* initialize variables */
1252 p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
1253
1254 /* config parameters */
1255 config_af_params.max_tx_retry = P2P_AF_TX_MAX_RETRY;
1256 config_af_params.mpc_onoff = -1;
1257
1258 if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1259 /* p2p public action frame process */
1260 if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1261 /* Just send unknown subtype frame with */
1262 /* default parameters. */
1263 brcmf_err("P2P Public action frame, unknown subtype.\n");
1264 }
1265 } else if (brcmf_p2p_is_gas_action(action_frame->data,
1266 action_frame_len)) {
1267 /* service discovery process */
1268 if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1269 action == P2PSD_ACTION_ID_GAS_CREQ) {
1270 /* save next af suptype to cancel */
1271 /* remaining dwell time */
1272 p2p->next_af_subtype = action + 1;
1273
1274 af_params->dwell_time =
1275 cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1276 } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1277 action == P2PSD_ACTION_ID_GAS_CRESP) {
1278 /* configure service discovery response frame */
1279 af_params->dwell_time =
1280 cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1281 } else {
1282 brcmf_err("Unknown action type: %d\n", action);
1283 goto exit;
1284 }
1285 } else if (brcmf_p2p_is_p2p_action(action_frame->data,
1286 action_frame_len)) {
1287 /* do not configure anything. it will be */
1288 /* sent with a default configuration */
1289 } else {
1290 brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
1291 category, action);
1292 return false;
1293 }
1294
1295 /* if scan is ongoing, abort current scan. */
1296 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1297 brcmf_abort_scanning(cfg);
1298
1299 /* To make sure to send successfully action frame, turn off mpc */
1300 if (config_af_params.mpc_onoff == 0)
1301 brcmf_set_mpc(ndev, 0);
1302
1303 /* if failed, retry it. tx_retry_max value is configure by .... */
1304 tx_retry = 0;
1305 while ((ack == false) && (tx_retry < config_af_params.max_tx_retry)) {
1306 ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1307 tx_retry++;
1308 }
1309 if (ack == false)
1310 brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
1311
1312 exit:
1313 /* if all done, turn mpc on again */
1314 if (config_af_params.mpc_onoff == 1)
1315 brcmf_set_mpc(ndev, 1);
1316
1317 return ack;
1318 }
1319
1320
1321 /**
1322 * brcmf_p2p_attach() - attach for P2P.
1323 *
1324 * @cfg: driver private data for cfg80211 interface.
1325 */
1326 void brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg,
1327 struct brcmf_cfg80211_vif *vif)
1328 {
1329 struct brcmf_p2p_info *p2p;
1330
1331 p2p = &cfg->p2p;
1332
1333 p2p->cfg = cfg;
1334 p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = vif;
1335 brcmf_p2p_generate_bss_mac(p2p);
1336 brcmf_p2p_set_firmware(p2p);
1337 init_completion(&p2p->send_af_done);
1338 }
1339
1340 /**
1341 * brcmf_p2p_detach() - detach P2P.
1342 *
1343 * @p2p: P2P specific data.
1344 */
1345 void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
1346 {
1347 if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif != NULL) {
1348 brcmf_p2p_cancel_remain_on_channel(
1349 p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp);
1350 brcmf_p2p_deinit_discovery(p2p);
1351 }
1352 /* just set it all to zero */
1353 memset(p2p, 0, sizeof(*p2p));
1354 }
1355
1356 /**
1357 * brcmf_p2p_get_current_chanspec() - Get current operation channel.
1358 *
1359 * @p2p: P2P specific data.
1360 * @chanspec: chanspec to be returned.
1361 */
1362 static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
1363 u16 *chanspec)
1364 {
1365 struct brcmf_if *ifp;
1366 struct brcmf_fil_chan_info_le ci;
1367 s32 err;
1368
1369 ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1370
1371 *chanspec = 11 & WL_CHANSPEC_CHAN_MASK;
1372
1373 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_CHANNEL, &ci, sizeof(ci));
1374 if (!err) {
1375 *chanspec = le32_to_cpu(ci.hw_channel) & WL_CHANSPEC_CHAN_MASK;
1376 if (*chanspec < CH_MAX_2G_CHANNEL)
1377 *chanspec |= WL_CHANSPEC_BAND_2G;
1378 else
1379 *chanspec |= WL_CHANSPEC_BAND_5G;
1380 }
1381 *chanspec |= WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
1382 }
1383
1384 /**
1385 * Change a P2P Role.
1386 * Parameters:
1387 * @mac: MAC address of the BSS to change a role
1388 * Returns 0 if success.
1389 */
1390 int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
1391 enum brcmf_fil_p2p_if_types if_type)
1392 {
1393 struct brcmf_p2p_info *p2p = &cfg->p2p;
1394 struct brcmf_cfg80211_vif *vif;
1395 struct brcmf_fil_p2p_if_le if_request;
1396 s32 err;
1397 u16 chanspec;
1398
1399 brcmf_dbg(TRACE, "Enter\n");
1400
1401 vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1402 if (!vif) {
1403 brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
1404 return -EPERM;
1405 }
1406 brcmf_notify_escan_complete(cfg, vif->ifp->ndev, true, true);
1407 vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
1408 if (!vif) {
1409 brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
1410 return -EPERM;
1411 }
1412 brcmf_set_mpc(vif->ifp->ndev, 0);
1413
1414 /* In concurrency case, STA may be already associated in a particular */
1415 /* channel. so retrieve the current channel of primary interface and */
1416 /* then start the virtual interface on that. */
1417 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1418
1419 if_request.type = cpu_to_le16((u16)if_type);
1420 if_request.chspec = cpu_to_le16(chanspec);
1421 memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
1422
1423 brcmf_cfg80211_arm_vif_event(cfg, vif);
1424 err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
1425 sizeof(if_request));
1426 if (err) {
1427 brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
1428 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1429 return err;
1430 }
1431 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_CHANGE,
1432 msecs_to_jiffies(1500));
1433 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1434 if (!err) {
1435 brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
1436 return -EIO;
1437 }
1438
1439 err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
1440 BRCMF_SCB_TIMEOUT_VALUE);
1441
1442 return err;
1443 }
1444
1445 static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
1446 struct brcmf_if *ifp, u8 ea[ETH_ALEN],
1447 enum brcmf_fil_p2p_if_types iftype)
1448 {
1449 struct brcmf_fil_p2p_if_le if_request;
1450 int err;
1451 u16 chanspec;
1452
1453 /* we need a default channel */
1454 brcmf_p2p_get_current_chanspec(p2p, &chanspec);
1455
1456 /* fill the firmware request */
1457 memcpy(if_request.addr, ea, ETH_ALEN);
1458 if_request.type = cpu_to_le16((u16)iftype);
1459 if_request.chspec = cpu_to_le16(chanspec);
1460
1461 err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
1462 sizeof(if_request));
1463 if (err)
1464 return err;
1465
1466 return err;
1467 }
1468
1469 static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
1470 {
1471 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
1472 struct net_device *pri_ndev = cfg_to_ndev(cfg);
1473 struct brcmf_if *ifp = netdev_priv(pri_ndev);
1474 u8 *addr = vif->wdev.netdev->dev_addr;
1475
1476 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
1477 }
1478
1479 static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
1480 {
1481 struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
1482 struct net_device *pri_ndev = cfg_to_ndev(cfg);
1483 struct brcmf_if *ifp = netdev_priv(pri_ndev);
1484 u8 *addr = vif->wdev.netdev->dev_addr;
1485
1486 return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
1487 }
1488
1489 /**
1490 * brcmf_p2p_add_vif() - create a new P2P virtual interface.
1491 *
1492 * @wiphy: wiphy device of new interface.
1493 * @name: name of the new interface.
1494 * @type: nl80211 interface type.
1495 * @flags: TBD
1496 * @params: TBD
1497 */
1498 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
1499 enum nl80211_iftype type, u32 *flags,
1500 struct vif_params *params)
1501 {
1502 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1503 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
1504 struct brcmf_cfg80211_vif *vif;
1505 enum brcmf_fil_p2p_if_types iftype;
1506 enum wl_mode mode;
1507 int err;
1508
1509 if (brcmf_cfg80211_vif_event_armed(cfg))
1510 return ERR_PTR(-EBUSY);
1511
1512 brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
1513
1514 switch (type) {
1515 case NL80211_IFTYPE_P2P_CLIENT:
1516 iftype = BRCMF_FIL_P2P_IF_CLIENT;
1517 mode = WL_MODE_BSS;
1518 break;
1519 case NL80211_IFTYPE_P2P_GO:
1520 iftype = BRCMF_FIL_P2P_IF_GO;
1521 mode = WL_MODE_AP;
1522 break;
1523 default:
1524 return ERR_PTR(-EOPNOTSUPP);
1525 }
1526
1527 vif = brcmf_alloc_vif(cfg, type, false);
1528 if (IS_ERR(vif))
1529 return (struct wireless_dev *)vif;
1530 brcmf_cfg80211_arm_vif_event(cfg, vif);
1531
1532 err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
1533 iftype);
1534 if (err) {
1535 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1536 goto fail;
1537 }
1538
1539 /* wait for firmware event */
1540 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
1541 msecs_to_jiffies(1500));
1542 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1543 if (!err) {
1544 brcmf_err("timeout occurred\n");
1545 err = -EIO;
1546 goto fail;
1547 }
1548
1549 /* interface created in firmware */
1550 ifp = vif->ifp;
1551 if (!ifp) {
1552 brcmf_err("no if pointer provided\n");
1553 err = -ENOENT;
1554 goto fail;
1555 }
1556
1557 strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
1558 brcmf_cfg80211_vif_complete(cfg);
1559 cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
1560 /* Disable firmware roaming for P2P interface */
1561 brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
1562 if (iftype == BRCMF_FIL_P2P_IF_GO) {
1563 /* set station timeout for p2p */
1564 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
1565 BRCMF_SCB_TIMEOUT_VALUE);
1566 }
1567 return &ifp->vif->wdev;
1568
1569 fail:
1570 brcmf_free_vif(vif);
1571 return ERR_PTR(err);
1572 }
1573
1574 /**
1575 * brcmf_p2p_del_vif() - delete a P2P virtual interface.
1576 *
1577 * @wiphy: wiphy device of interface.
1578 * @wdev: wireless device of interface.
1579 *
1580 * TODO: not yet supported.
1581 */
1582 int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
1583 {
1584 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
1585 struct brcmf_cfg80211_vif *vif;
1586 unsigned long jiffie_timeout = msecs_to_jiffies(1500);
1587 bool wait_for_disable = false;
1588 int err;
1589
1590 brcmf_dbg(TRACE, "delete P2P vif\n");
1591 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
1592
1593 switch (vif->wdev.iftype) {
1594 case NL80211_IFTYPE_P2P_CLIENT:
1595 if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
1596 wait_for_disable = true;
1597 break;
1598
1599 case NL80211_IFTYPE_P2P_GO:
1600 if (!brcmf_p2p_disable_p2p_if(vif))
1601 wait_for_disable = true;
1602 break;
1603
1604 case NL80211_IFTYPE_P2P_DEVICE:
1605 default:
1606 return -ENOTSUPP;
1607 break;
1608 }
1609
1610 if (wait_for_disable)
1611 wait_for_completion_timeout(&cfg->vif_disabled,
1612 msecs_to_jiffies(500));
1613
1614 brcmf_vif_clear_mgmt_ies(vif);
1615
1616 brcmf_cfg80211_arm_vif_event(cfg, vif);
1617 err = brcmf_p2p_release_p2p_if(vif);
1618 if (!err) {
1619 /* wait for firmware event */
1620 err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_DEL,
1621 jiffie_timeout);
1622 if (!err)
1623 err = -EIO;
1624 else
1625 err = 0;
1626 }
1627 brcmf_cfg80211_arm_vif_event(cfg, NULL);
1628 brcmf_free_vif(vif);
1629 cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
1630
1631 return err;
1632 }