p2p.c 26 KB

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