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