p2p.c 22 KB

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