p2p.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. #include <brcmu_wifi.h>
  20. #include <brcmu_utils.h>
  21. #include <defs.h>
  22. #include <dhd.h>
  23. #include <dhd_dbg.h>
  24. #include "fwil.h"
  25. #include "fwil_types.h"
  26. #include "p2p.h"
  27. #include "wl_cfg80211.h"
  28. /* parameters used for p2p escan */
  29. #define P2PAPI_SCAN_NPROBES 1
  30. #define P2PAPI_SCAN_DWELL_TIME_MS 80
  31. #define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
  32. #define P2PAPI_SCAN_HOME_TIME_MS 60
  33. #define P2PAPI_SCAN_NPROBS_TIME_MS 30
  34. #define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
  35. #define WL_SCAN_CONNECT_DWELL_TIME_MS 200
  36. #define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
  37. #define BRCMF_P2P_WILDCARD_SSID "DIRECT-"
  38. #define BRCMF_P2P_WILDCARD_SSID_LEN (sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
  39. #define SOCIAL_CHAN_1 1
  40. #define SOCIAL_CHAN_2 6
  41. #define SOCIAL_CHAN_3 11
  42. #define SOCIAL_CHAN_CNT 3
  43. #define AF_PEER_SEARCH_CNT 2
  44. #define BRCMF_SCB_TIMEOUT_VALUE 20
  45. /**
  46. * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
  47. *
  48. * @state: requested discovery state (see enum brcmf_p2p_disc_state).
  49. * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
  50. * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
  51. */
  52. struct brcmf_p2p_disc_st_le {
  53. u8 state;
  54. __le16 chspec;
  55. __le16 dwell;
  56. };
  57. /**
  58. * enum brcmf_p2p_disc_state - P2P discovery state values
  59. *
  60. * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
  61. * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
  62. * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
  63. */
  64. enum brcmf_p2p_disc_state {
  65. WL_P2P_DISC_ST_SCAN,
  66. WL_P2P_DISC_ST_LISTEN,
  67. WL_P2P_DISC_ST_SEARCH
  68. };
  69. /**
  70. * struct brcmf_p2p_scan_le - P2P specific scan request.
  71. *
  72. * @type: type of scan method requested (values: 'E' or 'S').
  73. * @reserved: reserved (ignored).
  74. * @eparams: parameters used for type 'E'.
  75. * @sparams: parameters used for type 'S'.
  76. */
  77. struct brcmf_p2p_scan_le {
  78. u8 type;
  79. u8 reserved[3];
  80. union {
  81. struct brcmf_escan_params_le eparams;
  82. struct brcmf_scan_params_le sparams;
  83. };
  84. };
  85. /**
  86. * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
  87. *
  88. * @p2p: P2P specific data.
  89. */
  90. static int brcmf_p2p_set_firmware(struct brcmf_p2p_info *p2p)
  91. {
  92. struct net_device *ndev = cfg_to_ndev(p2p->cfg);
  93. u8 null_eth_addr[] = { 0, 0, 0, 0, 0, 0 };
  94. s32 ret = 0;
  95. brcmf_fil_iovar_int_set(netdev_priv(ndev), "apsta", 1);
  96. /* In case of COB type, firmware has default mac address
  97. * After Initializing firmware, we have to set current mac address to
  98. * firmware for P2P device address
  99. */
  100. ret = brcmf_fil_iovar_data_set(netdev_priv(ndev), "p2p_da_override",
  101. null_eth_addr, sizeof(null_eth_addr));
  102. if (ret)
  103. brcmf_err("failed to update device address ret %d\n", ret);
  104. return ret;
  105. }
  106. /**
  107. * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
  108. *
  109. * @p2p: P2P specific data.
  110. *
  111. * P2P needs mac addresses for P2P device and interface. These are
  112. * derived from the primary net device, ie. the permanent ethernet
  113. * address of the device.
  114. */
  115. static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p)
  116. {
  117. /* Generate the P2P Device Address. This consists of the device's
  118. * primary MAC address with the locally administered bit set.
  119. */
  120. memcpy(p2p->dev_addr, p2p->cfg->pub->mac, ETH_ALEN);
  121. p2p->dev_addr[0] |= 0x02;
  122. /* Generate the P2P Interface Address. If the discovery and connection
  123. * BSSCFGs need to simultaneously co-exist, then this address must be
  124. * different from the P2P Device Address, but also locally administered.
  125. */
  126. memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
  127. p2p->int_addr[4] ^= 0x80;
  128. }
  129. /**
  130. * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
  131. *
  132. * @request: the scan request as received from cfg80211.
  133. *
  134. * returns true if one of the ssids in the request matches the
  135. * P2P wildcard ssid; otherwise returns false.
  136. */
  137. static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
  138. {
  139. struct cfg80211_ssid *ssids = request->ssids;
  140. int i;
  141. for (i = 0; i < request->n_ssids; i++) {
  142. if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
  143. continue;
  144. brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
  145. if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
  146. BRCMF_P2P_WILDCARD_SSID_LEN))
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * brcmf_p2p_set_discover_state - set discover state in firmware.
  153. *
  154. * @ifp: low-level interface object.
  155. * @state: discover state to set.
  156. * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
  157. * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
  158. */
  159. static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
  160. u16 chanspec, u16 listen_ms)
  161. {
  162. struct brcmf_p2p_disc_st_le discover_state;
  163. s32 ret = 0;
  164. brcmf_dbg(TRACE, "enter\n");
  165. discover_state.state = state;
  166. discover_state.chspec = cpu_to_le16(chanspec);
  167. discover_state.dwell = cpu_to_le16(listen_ms);
  168. ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
  169. sizeof(discover_state));
  170. return ret;
  171. }
  172. /**
  173. * brcmf_p2p_init_discovery() - enable discovery in the firmware.
  174. *
  175. * @p2p: P2P specific data.
  176. *
  177. * Configures the firmware to allow P2P peer discovery. Creates the
  178. * virtual interface and consequently the P2P device for it.
  179. */
  180. static s32 brcmf_p2p_init_discovery(struct brcmf_p2p_info *p2p)
  181. {
  182. struct net_device *ndev = cfg_to_ndev(p2p->cfg);
  183. struct brcmf_cfg80211_vif *vif;
  184. struct brcmf_if *ifp;
  185. struct p2p_bss *bss_dev;
  186. s32 index;
  187. s32 ret;
  188. brcmf_dbg(TRACE, "enter\n");
  189. bss_dev = &p2p->bss_idx[P2PAPI_BSSCFG_DEVICE];
  190. if (bss_dev->vif != NULL) {
  191. brcmf_dbg(INFO, "do nothing, already initialized\n");
  192. return 0;
  193. }
  194. /* Enable P2P Discovery in the firmware */
  195. ret = brcmf_fil_iovar_int_set(netdev_priv(ndev), "p2p_disc", 1);
  196. if (ret < 0) {
  197. brcmf_err("set discover error\n");
  198. return ret;
  199. }
  200. /* obtain bsscfg index for P2P discovery */
  201. ret = brcmf_fil_iovar_int_get(netdev_priv(ndev), "p2p_dev", &index);
  202. if (ret < 0) {
  203. brcmf_err("retrieving discover bsscfg index failed\n");
  204. return ret;
  205. }
  206. /*
  207. * need brcmf_if for setting the discovery state.
  208. */
  209. ifp = kzalloc(sizeof(*vif->ifp), GFP_KERNEL);
  210. if (!ifp) {
  211. brcmf_err("could not create discovery if\n");
  212. return -ENOMEM;
  213. }
  214. /* set required fields */
  215. ifp->drvr = p2p->cfg->pub;
  216. ifp->ifidx = 0;
  217. ifp->bssidx = index;
  218. /* Set the initial discovery state to SCAN */
  219. ret = brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
  220. if (ret != 0) {
  221. brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
  222. (void)brcmf_fil_iovar_int_set(netdev_priv(ndev), "p2p_disc", 0);
  223. kfree(ifp);
  224. return ret;
  225. }
  226. /* create a vif for it */
  227. vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE, false);
  228. if (IS_ERR(vif)) {
  229. brcmf_err("could not create discovery vif\n");
  230. kfree(ifp);
  231. return PTR_ERR(vif);
  232. }
  233. vif->ifp = ifp;
  234. ifp->vif = vif;
  235. bss_dev->vif = vif;
  236. return 0;
  237. }
  238. /**
  239. * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
  240. *
  241. * @p2p: P2P specific data.
  242. *
  243. * Resets the discovery state and disables it in firmware. The virtual
  244. * interface and P2P device are freed.
  245. */
  246. static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
  247. {
  248. struct net_device *ndev = cfg_to_ndev(p2p->cfg);
  249. struct brcmf_if *ifp;
  250. struct p2p_bss *bss_dev;
  251. brcmf_dbg(TRACE, "enter\n");
  252. bss_dev = &p2p->bss_idx[P2PAPI_BSSCFG_DEVICE];
  253. ifp = bss_dev->vif->ifp;
  254. /* Set the discovery state to SCAN */
  255. (void)brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
  256. /* Disable P2P discovery in the firmware */
  257. (void)brcmf_fil_iovar_int_set(netdev_priv(ndev), "p2p_disc", 0);
  258. /* remove discovery interface */
  259. brcmf_free_vif(bss_dev->vif);
  260. bss_dev->vif = NULL;
  261. kfree(ifp);
  262. return 0;
  263. }
  264. /**
  265. * brcmf_p2p_enable_discovery() - initialize and configure discovery.
  266. *
  267. * @p2p: P2P specific data.
  268. * @ie: buffer containing information elements.
  269. * @ie_len: length of @ie buffer.
  270. *
  271. * Initializes the discovery device and configure the virtual interface.
  272. */
  273. static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
  274. {
  275. struct brcmf_cfg80211_vif *vif;
  276. s32 ret = 0;
  277. brcmf_dbg(TRACE, "enter\n");
  278. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  279. if (vif) {
  280. brcmf_dbg(INFO, "DISCOVERY init already done\n");
  281. goto exit;
  282. }
  283. ret = brcmf_p2p_init_discovery(p2p);
  284. if (ret < 0) {
  285. brcmf_err("init discovery error %d\n", ret);
  286. goto exit;
  287. }
  288. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  289. /*
  290. * Set wsec to any non-zero value in the discovery bsscfg
  291. * to ensure our P2P probe responses have the privacy bit
  292. * set in the 802.11 WPA IE. Some peer devices may not
  293. * initiate WPS with us if this bit is not set.
  294. */
  295. ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
  296. if (ret < 0)
  297. brcmf_err("wsec error %d\n", ret);
  298. exit:
  299. return ret;
  300. }
  301. /**
  302. * brcmf_p2p_escan() - initiate a P2P scan.
  303. *
  304. * @p2p: P2P specific data.
  305. * @num_chans: number of channels to scan.
  306. * @chanspecs: channel parameters for @num_chans channels.
  307. * @search_state: P2P discover state to use.
  308. * @action: scan action to pass to firmware.
  309. * @bss_type: type of P2P bss.
  310. */
  311. static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
  312. u16 chanspecs[], s32 search_state, u16 action,
  313. enum p2p_bss_type bss_type)
  314. {
  315. s32 ret = 0;
  316. s32 memsize = offsetof(struct brcmf_p2p_scan_le,
  317. eparams.params_le.channel_list);
  318. s32 nprobes;
  319. s32 active;
  320. u32 i;
  321. u8 *memblk;
  322. struct brcmf_cfg80211_vif *vif;
  323. struct brcmf_p2p_scan_le *p2p_params;
  324. struct brcmf_scan_params_le *sparams;
  325. struct brcmf_ssid ssid;
  326. memsize += num_chans * sizeof(__le16);
  327. memblk = kzalloc(memsize, GFP_KERNEL);
  328. if (!memblk)
  329. return -ENOMEM;
  330. vif = p2p->bss_idx[bss_type].vif;
  331. if (vif == NULL) {
  332. brcmf_err("no vif for bss type %d\n", bss_type);
  333. ret = -EINVAL;
  334. goto exit;
  335. }
  336. switch (search_state) {
  337. case WL_P2P_DISC_ST_SEARCH:
  338. /*
  339. * If we in SEARCH STATE, we don't need to set SSID explictly
  340. * because dongle use P2P WILDCARD internally by default
  341. */
  342. /* use null ssid */
  343. ssid.SSID_len = 0;
  344. memset(ssid.SSID, 0, sizeof(ssid.SSID));
  345. break;
  346. case WL_P2P_DISC_ST_SCAN:
  347. /*
  348. * wpa_supplicant has p2p_find command with type social or
  349. * progressive. For progressive, we need to set the ssid to
  350. * P2P WILDCARD because we just do broadcast scan unless
  351. * setting SSID.
  352. */
  353. ssid.SSID_len = BRCMF_P2P_WILDCARD_SSID_LEN;
  354. memcpy(ssid.SSID, BRCMF_P2P_WILDCARD_SSID, ssid.SSID_len);
  355. break;
  356. default:
  357. brcmf_err(" invalid search state %d\n", search_state);
  358. ret = -EINVAL;
  359. goto exit;
  360. }
  361. brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
  362. /*
  363. * set p2p scan parameters.
  364. */
  365. p2p_params = (struct brcmf_p2p_scan_le *)memblk;
  366. p2p_params->type = 'E';
  367. /* determine the scan engine parameters */
  368. sparams = &p2p_params->eparams.params_le;
  369. sparams->bss_type = DOT11_BSSTYPE_ANY;
  370. if (p2p->cfg->active_scan)
  371. sparams->scan_type = 0;
  372. else
  373. sparams->scan_type = 1;
  374. memset(&sparams->bssid, 0xFF, ETH_ALEN);
  375. if (ssid.SSID_len)
  376. memcpy(sparams->ssid_le.SSID, ssid.SSID, ssid.SSID_len);
  377. sparams->ssid_le.SSID_len = cpu_to_le32(ssid.SSID_len);
  378. sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
  379. /*
  380. * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
  381. * supported by the supplicant.
  382. */
  383. if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
  384. active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
  385. else if (num_chans == AF_PEER_SEARCH_CNT)
  386. active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
  387. else if (wl_get_vif_state_all(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
  388. active = -1;
  389. else
  390. active = P2PAPI_SCAN_DWELL_TIME_MS;
  391. /* Override scan params to find a peer for a connection */
  392. if (num_chans == 1) {
  393. active = WL_SCAN_CONNECT_DWELL_TIME_MS;
  394. /* XXX WAR to sync with presence period of VSDB GO.
  395. * send probe request more frequently
  396. */
  397. nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
  398. } else {
  399. nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
  400. }
  401. if (nprobes <= 0)
  402. nprobes = 1;
  403. brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
  404. sparams->active_time = cpu_to_le32(active);
  405. sparams->nprobes = cpu_to_le32(nprobes);
  406. sparams->passive_time = cpu_to_le32(-1);
  407. sparams->channel_num = cpu_to_le32(num_chans &
  408. BRCMF_SCAN_PARAMS_COUNT_MASK);
  409. for (i = 0; i < num_chans; i++)
  410. sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
  411. /* set the escan specific parameters */
  412. p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
  413. p2p_params->eparams.action = cpu_to_le16(action);
  414. p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
  415. /* perform p2p scan on primary device */
  416. ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
  417. if (!ret)
  418. set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
  419. exit:
  420. kfree(memblk);
  421. return ret;
  422. }
  423. /**
  424. * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
  425. *
  426. * @cfg: driver private data for cfg80211 interface.
  427. * @ndev: net device for which scan is requested.
  428. * @request: scan request from cfg80211.
  429. * @action: scan action.
  430. *
  431. * Determines the P2P discovery state based to scan request parameters and
  432. * validates the channels in the request.
  433. */
  434. static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
  435. struct net_device *ndev,
  436. struct cfg80211_scan_request *request,
  437. u16 action)
  438. {
  439. struct brcmf_p2p_info *p2p = &cfg->p2p;
  440. s32 err = 0;
  441. s32 search_state = WL_P2P_DISC_ST_SCAN;
  442. struct brcmf_cfg80211_vif *vif;
  443. struct net_device *dev = NULL;
  444. int i, num_nodfs = 0;
  445. u16 *chanspecs;
  446. brcmf_dbg(TRACE, "enter\n");
  447. if (!request) {
  448. err = -EINVAL;
  449. goto exit;
  450. }
  451. if (request->n_channels) {
  452. chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
  453. GFP_KERNEL);
  454. if (!chanspecs) {
  455. err = -ENOMEM;
  456. goto exit;
  457. }
  458. vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
  459. if (vif)
  460. dev = vif->wdev.netdev;
  461. if (request->n_channels == 3 &&
  462. request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
  463. request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
  464. request->channels[2]->hw_value == SOCIAL_CHAN_3) {
  465. /* SOCIAL CHANNELS 1, 6, 11 */
  466. search_state = WL_P2P_DISC_ST_SEARCH;
  467. brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
  468. } else if (dev != NULL && vif->mode == WL_MODE_AP) {
  469. /* If you are already a GO, then do SEARCH only */
  470. brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
  471. search_state = WL_P2P_DISC_ST_SEARCH;
  472. } else {
  473. brcmf_dbg(INFO, "P2P SCAN STATE START\n");
  474. }
  475. /*
  476. * no P2P scanning on passive or DFS channels.
  477. */
  478. for (i = 0; i < request->n_channels; i++) {
  479. struct ieee80211_channel *chan = request->channels[i];
  480. if (chan->flags & (IEEE80211_CHAN_RADAR |
  481. IEEE80211_CHAN_PASSIVE_SCAN))
  482. continue;
  483. chanspecs[i] = channel_to_chanspec(chan);
  484. brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
  485. num_nodfs, chan->hw_value, chanspecs[i]);
  486. num_nodfs++;
  487. }
  488. err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
  489. action, P2PAPI_BSSCFG_DEVICE);
  490. }
  491. exit:
  492. if (err)
  493. brcmf_err("error (%d)\n", err);
  494. return err;
  495. }
  496. /**
  497. * brcmf_p2p_scan_prep() - prepare scan based on request.
  498. *
  499. * @wiphy: wiphy device.
  500. * @request: scan request from cfg80211.
  501. * @vif: vif on which scan request is to be executed.
  502. *
  503. * Prepare the scan appropriately for type of scan requested. Overrides the
  504. * escan .run() callback for peer-to-peer scanning.
  505. */
  506. int brcmf_p2p_scan_prep(struct wiphy *wiphy,
  507. struct cfg80211_scan_request *request,
  508. struct brcmf_cfg80211_vif *vif)
  509. {
  510. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  511. struct brcmf_p2p_info *p2p = &cfg->p2p;
  512. int err = 0;
  513. if (brcmf_p2p_scan_is_p2p_request(request)) {
  514. /* find my listen channel */
  515. err = cfg80211_get_p2p_attr(request->ie, request->ie_len,
  516. IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
  517. &p2p->listen_channel, 1);
  518. if (err < 0)
  519. return err;
  520. clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  521. brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
  522. err = brcmf_p2p_enable_discovery(p2p);
  523. if (err)
  524. return err;
  525. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  526. /* override .run_escan() callback. */
  527. cfg->escan_info.run = brcmf_p2p_run_escan;
  528. }
  529. err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
  530. request->ie, request->ie_len);
  531. return err;
  532. }
  533. /**
  534. * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
  535. *
  536. * @wiphy: wiphy device.
  537. * @channel: channel to stay on.
  538. * @duration: time in ms to remain on channel.
  539. *
  540. */
  541. int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
  542. struct ieee80211_channel *channel,
  543. unsigned int duration, u64 *cookie)
  544. {
  545. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  546. struct brcmf_p2p_info *p2p = &cfg->p2p;
  547. struct brcmf_cfg80211_vif *vif;
  548. s32 err;
  549. u16 chanspec;
  550. brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n",
  551. ieee80211_frequency_to_channel(channel->center_freq),
  552. duration);
  553. *cookie = 0;
  554. err = brcmf_p2p_enable_discovery(p2p);
  555. if (err)
  556. goto exit;
  557. chanspec = channel_to_chanspec(channel);
  558. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  559. err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
  560. chanspec, (u16)duration);
  561. if (err)
  562. goto exit;
  563. memcpy(&p2p->remain_on_channel, channel,
  564. sizeof(p2p->remain_on_channel));
  565. set_bit(BRCMF_P2P_STATUS_REMAIN_ON_CHANNEL, &p2p->status);
  566. exit:
  567. cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
  568. return err;
  569. }
  570. /**
  571. * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
  572. *
  573. * @ifp: interfac control.
  574. * @e: event message. Not used, to make it usable for fweh event dispatcher.
  575. * @data: payload of message. Not used.
  576. *
  577. */
  578. int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
  579. const struct brcmf_event_msg *e,
  580. void *data)
  581. {
  582. struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
  583. struct brcmf_p2p_info *p2p = &cfg->p2p;
  584. brcmf_dbg(TRACE, "Enter\n");
  585. if (test_and_clear_bit(BRCMF_P2P_STATUS_REMAIN_ON_CHANNEL,
  586. &p2p->status))
  587. cfg80211_remain_on_channel_expired(&ifp->vif->wdev, 0,
  588. &p2p->remain_on_channel,
  589. GFP_KERNEL);
  590. return 0;
  591. }
  592. /**
  593. * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
  594. *
  595. * @ifp: interfac control.
  596. *
  597. */
  598. void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
  599. {
  600. if (!ifp)
  601. return;
  602. brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
  603. brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
  604. }
  605. /**
  606. * brcmf_p2p_attach() - attach for P2P.
  607. *
  608. * @cfg: driver private data for cfg80211 interface.
  609. */
  610. void brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg)
  611. {
  612. struct brcmf_p2p_info *p2p;
  613. p2p = &cfg->p2p;
  614. p2p->cfg = cfg;
  615. brcmf_p2p_set_firmware(p2p);
  616. brcmf_p2p_generate_bss_mac(p2p);
  617. }
  618. /**
  619. * brcmf_p2p_detach() - detach P2P.
  620. *
  621. * @p2p: P2P specific data.
  622. */
  623. void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
  624. {
  625. if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif != NULL) {
  626. brcmf_p2p_cancel_remain_on_channel(
  627. p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp);
  628. brcmf_p2p_deinit_discovery(p2p);
  629. }
  630. /* just set it all to zero */
  631. memset(p2p, 0, sizeof(*p2p));
  632. }
  633. static int brcmf_p2p_request_p2p_if(struct brcmf_if *ifp, u8 ea[ETH_ALEN],
  634. enum brcmf_fil_p2p_if_types iftype)
  635. {
  636. struct brcmf_fil_p2p_if_le if_request;
  637. struct brcmf_fil_chan_info_le ci;
  638. u16 chanspec = 11 & WL_CHANSPEC_CHAN_MASK;
  639. int err;
  640. /* we need a default channel */
  641. err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_CHANNEL, &ci, sizeof(ci));
  642. if (!err) {
  643. chanspec = le32_to_cpu(ci.hw_channel) & WL_CHANSPEC_CHAN_MASK;
  644. if (chanspec < CH_MAX_2G_CHANNEL)
  645. chanspec |= WL_CHANSPEC_BAND_2G;
  646. else
  647. chanspec |= WL_CHANSPEC_BAND_5G;
  648. }
  649. chanspec |= WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
  650. /* fill the firmware request */
  651. memcpy(if_request.addr, ea, ETH_ALEN);
  652. if_request.type = iftype;
  653. if_request.chspec = cpu_to_le16(chanspec);
  654. err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
  655. sizeof(if_request));
  656. if (err)
  657. return err;
  658. if (iftype == BRCMF_FIL_P2P_IF_GO) {
  659. /* set station timeout for p2p */
  660. err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
  661. BRCMF_SCB_TIMEOUT_VALUE);
  662. }
  663. return err;
  664. }
  665. static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
  666. {
  667. struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
  668. struct net_device *pri_ndev = cfg_to_ndev(cfg);
  669. struct brcmf_if *ifp = netdev_priv(pri_ndev);
  670. u8 *addr = vif->wdev.netdev->dev_addr;
  671. return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
  672. }
  673. static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
  674. {
  675. struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
  676. struct net_device *pri_ndev = cfg_to_ndev(cfg);
  677. struct brcmf_if *ifp = netdev_priv(pri_ndev);
  678. u8 *addr = vif->wdev.netdev->dev_addr;
  679. return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
  680. }
  681. /**
  682. * brcmf_p2p_add_vif() - create a new P2P virtual interface.
  683. *
  684. * @wiphy: wiphy device of new interface.
  685. * @name: name of the new interface.
  686. * @type: nl80211 interface type.
  687. * @flags: TBD
  688. * @params: TBD
  689. */
  690. struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
  691. enum nl80211_iftype type, u32 *flags,
  692. struct vif_params *params)
  693. {
  694. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  695. struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
  696. struct brcmf_cfg80211_vif *vif;
  697. enum brcmf_fil_p2p_if_types iftype;
  698. enum wl_mode mode;
  699. int err;
  700. if (brcmf_cfg80211_vif_event_armed(cfg))
  701. return ERR_PTR(-EBUSY);
  702. brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
  703. switch (type) {
  704. case NL80211_IFTYPE_P2P_CLIENT:
  705. iftype = BRCMF_FIL_P2P_IF_CLIENT;
  706. mode = WL_MODE_BSS;
  707. break;
  708. case NL80211_IFTYPE_P2P_GO:
  709. iftype = BRCMF_FIL_P2P_IF_GO;
  710. mode = WL_MODE_AP;
  711. break;
  712. default:
  713. return ERR_PTR(-EOPNOTSUPP);
  714. }
  715. vif = brcmf_alloc_vif(cfg, type, false);
  716. brcmf_cfg80211_arm_vif_event(cfg, vif);
  717. err = brcmf_p2p_request_p2p_if(ifp, cfg->p2p.int_addr, iftype);
  718. if (err)
  719. goto fail;
  720. /* wait for firmware event */
  721. err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
  722. msecs_to_jiffies(1500));
  723. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  724. if (!err) {
  725. brcmf_err("timeout occurred\n");
  726. err = -EIO;
  727. goto fail;
  728. }
  729. /* interface created in firmware */
  730. ifp = vif->ifp;
  731. if (!ifp) {
  732. brcmf_err("no if pointer provided\n");
  733. err = -ENOENT;
  734. }
  735. strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
  736. brcmf_cfg80211_vif_complete(cfg);
  737. return &ifp->vif->wdev;
  738. fail:
  739. brcmf_free_vif(vif);
  740. return ERR_PTR(err);
  741. }
  742. /**
  743. * brcmf_p2p_del_vif() - delete a P2P virtual interface.
  744. *
  745. * @wiphy: wiphy device of interface.
  746. * @wdev: wireless device of interface.
  747. *
  748. * TODO: not yet supported.
  749. */
  750. int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
  751. {
  752. struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
  753. struct brcmf_cfg80211_vif *vif;
  754. unsigned long jiffie_timeout = msecs_to_jiffies(1500);
  755. bool wait_for_disable = false;
  756. int err;
  757. brcmf_dbg(TRACE, "delete P2P vif\n");
  758. vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
  759. switch (vif->wdev.iftype) {
  760. case NL80211_IFTYPE_P2P_CLIENT:
  761. if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
  762. wait_for_disable = true;
  763. break;
  764. case NL80211_IFTYPE_P2P_GO:
  765. if (!brcmf_p2p_disable_p2p_if(vif))
  766. wait_for_disable = true;
  767. break;
  768. case NL80211_IFTYPE_P2P_DEVICE:
  769. default:
  770. return -ENOTSUPP;
  771. break;
  772. }
  773. if (wait_for_disable)
  774. wait_for_completion_timeout(&cfg->vif_disabled, 500);
  775. brcmf_vif_clear_mgmt_ies(vif);
  776. brcmf_cfg80211_arm_vif_event(cfg, vif);
  777. err = brcmf_p2p_release_p2p_if(vif);
  778. if (!err)
  779. /* wait for firmware event */
  780. err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_DEL,
  781. jiffie_timeout);
  782. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  783. brcmf_free_vif(vif);
  784. return err;
  785. }