p2p.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 2014-2016 Qualcomm Atheros, Inc.
  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
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "wil6210.h"
  17. #include "wmi.h"
  18. #define P2P_WILDCARD_SSID "DIRECT-"
  19. #define P2P_DMG_SOCIAL_CHANNEL 2
  20. #define P2P_SEARCH_DURATION_MS 500
  21. #define P2P_DEFAULT_BI 100
  22. static int wil_p2p_start_listen(struct wil6210_priv *wil)
  23. {
  24. struct wil_p2p_info *p2p = &wil->p2p;
  25. u8 channel = p2p->listen_chan.hw_value;
  26. int rc;
  27. lockdep_assert_held(&wil->mutex);
  28. rc = wmi_p2p_cfg(wil, channel, P2P_DEFAULT_BI);
  29. if (rc) {
  30. wil_err(wil, "wmi_p2p_cfg failed\n");
  31. goto out;
  32. }
  33. rc = wmi_set_ssid(wil, strlen(P2P_WILDCARD_SSID), P2P_WILDCARD_SSID);
  34. if (rc) {
  35. wil_err(wil, "wmi_set_ssid failed\n");
  36. goto out_stop;
  37. }
  38. rc = wmi_start_listen(wil);
  39. if (rc) {
  40. wil_err(wil, "wmi_start_listen failed\n");
  41. goto out_stop;
  42. }
  43. INIT_WORK(&p2p->discovery_expired_work, wil_p2p_listen_expired);
  44. mod_timer(&p2p->discovery_timer,
  45. jiffies + msecs_to_jiffies(p2p->listen_duration));
  46. out_stop:
  47. if (rc)
  48. wmi_stop_discovery(wil);
  49. out:
  50. return rc;
  51. }
  52. bool wil_p2p_is_social_scan(struct cfg80211_scan_request *request)
  53. {
  54. return (request->n_channels == 1) &&
  55. (request->channels[0]->hw_value == P2P_DMG_SOCIAL_CHANNEL);
  56. }
  57. void wil_p2p_discovery_timer_fn(ulong x)
  58. {
  59. struct wil6210_priv *wil = (void *)x;
  60. wil_dbg_misc(wil, "%s\n", __func__);
  61. schedule_work(&wil->p2p.discovery_expired_work);
  62. }
  63. int wil_p2p_search(struct wil6210_priv *wil,
  64. struct cfg80211_scan_request *request)
  65. {
  66. int rc;
  67. struct wil_p2p_info *p2p = &wil->p2p;
  68. wil_dbg_misc(wil, "%s: channel %d\n",
  69. __func__, P2P_DMG_SOCIAL_CHANNEL);
  70. lockdep_assert_held(&wil->mutex);
  71. if (p2p->discovery_started) {
  72. wil_err(wil, "%s: search failed. discovery already ongoing\n",
  73. __func__);
  74. rc = -EBUSY;
  75. goto out;
  76. }
  77. rc = wmi_p2p_cfg(wil, P2P_DMG_SOCIAL_CHANNEL, P2P_DEFAULT_BI);
  78. if (rc) {
  79. wil_err(wil, "%s: wmi_p2p_cfg failed\n", __func__);
  80. goto out;
  81. }
  82. rc = wmi_set_ssid(wil, strlen(P2P_WILDCARD_SSID), P2P_WILDCARD_SSID);
  83. if (rc) {
  84. wil_err(wil, "%s: wmi_set_ssid failed\n", __func__);
  85. goto out_stop;
  86. }
  87. /* Set application IE to probe request and probe response */
  88. rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ,
  89. request->ie_len, request->ie);
  90. if (rc) {
  91. wil_err(wil, "%s: wmi_set_ie(WMI_FRAME_PROBE_REQ) failed\n",
  92. __func__);
  93. goto out_stop;
  94. }
  95. /* supplicant doesn't provide Probe Response IEs. As a workaround -
  96. * re-use Probe Request IEs
  97. */
  98. rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP,
  99. request->ie_len, request->ie);
  100. if (rc) {
  101. wil_err(wil, "%s: wmi_set_ie(WMI_FRAME_PROBE_RESP) failed\n",
  102. __func__);
  103. goto out_stop;
  104. }
  105. rc = wmi_start_search(wil);
  106. if (rc) {
  107. wil_err(wil, "%s: wmi_start_search failed\n", __func__);
  108. goto out_stop;
  109. }
  110. p2p->discovery_started = 1;
  111. INIT_WORK(&p2p->discovery_expired_work, wil_p2p_search_expired);
  112. mod_timer(&p2p->discovery_timer,
  113. jiffies + msecs_to_jiffies(P2P_SEARCH_DURATION_MS));
  114. out_stop:
  115. if (rc)
  116. wmi_stop_discovery(wil);
  117. out:
  118. return rc;
  119. }
  120. int wil_p2p_listen(struct wil6210_priv *wil, struct wireless_dev *wdev,
  121. unsigned int duration, struct ieee80211_channel *chan,
  122. u64 *cookie)
  123. {
  124. struct wil_p2p_info *p2p = &wil->p2p;
  125. int rc;
  126. if (!chan)
  127. return -EINVAL;
  128. wil_dbg_misc(wil, "%s: duration %d\n", __func__, duration);
  129. mutex_lock(&wil->mutex);
  130. if (p2p->discovery_started) {
  131. wil_err(wil, "%s: discovery already ongoing\n", __func__);
  132. rc = -EBUSY;
  133. goto out;
  134. }
  135. memcpy(&p2p->listen_chan, chan, sizeof(*chan));
  136. *cookie = ++p2p->cookie;
  137. p2p->listen_duration = duration;
  138. mutex_lock(&wil->p2p_wdev_mutex);
  139. if (wil->scan_request) {
  140. wil_dbg_misc(wil, "Delaying p2p listen until scan done\n");
  141. p2p->pending_listen_wdev = wdev;
  142. p2p->discovery_started = 1;
  143. rc = 0;
  144. mutex_unlock(&wil->p2p_wdev_mutex);
  145. goto out;
  146. }
  147. mutex_unlock(&wil->p2p_wdev_mutex);
  148. rc = wil_p2p_start_listen(wil);
  149. if (rc)
  150. goto out;
  151. p2p->discovery_started = 1;
  152. wil->radio_wdev = wdev;
  153. cfg80211_ready_on_channel(wdev, *cookie, chan, duration,
  154. GFP_KERNEL);
  155. out:
  156. mutex_unlock(&wil->mutex);
  157. return rc;
  158. }
  159. u8 wil_p2p_stop_discovery(struct wil6210_priv *wil)
  160. {
  161. struct wil_p2p_info *p2p = &wil->p2p;
  162. u8 started = p2p->discovery_started;
  163. if (p2p->discovery_started) {
  164. if (p2p->pending_listen_wdev) {
  165. /* discovery not really started, only pending */
  166. p2p->pending_listen_wdev = NULL;
  167. } else {
  168. del_timer_sync(&p2p->discovery_timer);
  169. wmi_stop_discovery(wil);
  170. }
  171. p2p->discovery_started = 0;
  172. }
  173. return started;
  174. }
  175. int wil_p2p_cancel_listen(struct wil6210_priv *wil, u64 cookie)
  176. {
  177. struct wil_p2p_info *p2p = &wil->p2p;
  178. u8 started;
  179. mutex_lock(&wil->mutex);
  180. if (cookie != p2p->cookie) {
  181. wil_info(wil, "%s: Cookie mismatch: 0x%016llx vs. 0x%016llx\n",
  182. __func__, p2p->cookie, cookie);
  183. mutex_unlock(&wil->mutex);
  184. return -ENOENT;
  185. }
  186. started = wil_p2p_stop_discovery(wil);
  187. mutex_unlock(&wil->mutex);
  188. if (!started) {
  189. wil_err(wil, "%s: listen not started\n", __func__);
  190. return -ENOENT;
  191. }
  192. mutex_lock(&wil->p2p_wdev_mutex);
  193. cfg80211_remain_on_channel_expired(wil->radio_wdev,
  194. p2p->cookie,
  195. &p2p->listen_chan,
  196. GFP_KERNEL);
  197. wil->radio_wdev = wil->wdev;
  198. mutex_unlock(&wil->p2p_wdev_mutex);
  199. return 0;
  200. }
  201. void wil_p2p_listen_expired(struct work_struct *work)
  202. {
  203. struct wil_p2p_info *p2p = container_of(work,
  204. struct wil_p2p_info, discovery_expired_work);
  205. struct wil6210_priv *wil = container_of(p2p,
  206. struct wil6210_priv, p2p);
  207. u8 started;
  208. wil_dbg_misc(wil, "%s()\n", __func__);
  209. mutex_lock(&wil->mutex);
  210. started = wil_p2p_stop_discovery(wil);
  211. mutex_unlock(&wil->mutex);
  212. if (started) {
  213. mutex_lock(&wil->p2p_wdev_mutex);
  214. cfg80211_remain_on_channel_expired(wil->radio_wdev,
  215. p2p->cookie,
  216. &p2p->listen_chan,
  217. GFP_KERNEL);
  218. wil->radio_wdev = wil->wdev;
  219. mutex_unlock(&wil->p2p_wdev_mutex);
  220. }
  221. }
  222. void wil_p2p_search_expired(struct work_struct *work)
  223. {
  224. struct wil_p2p_info *p2p = container_of(work,
  225. struct wil_p2p_info, discovery_expired_work);
  226. struct wil6210_priv *wil = container_of(p2p,
  227. struct wil6210_priv, p2p);
  228. u8 started;
  229. wil_dbg_misc(wil, "%s()\n", __func__);
  230. mutex_lock(&wil->mutex);
  231. started = wil_p2p_stop_discovery(wil);
  232. mutex_unlock(&wil->mutex);
  233. if (started) {
  234. struct cfg80211_scan_info info = {
  235. .aborted = false,
  236. };
  237. mutex_lock(&wil->p2p_wdev_mutex);
  238. if (wil->scan_request) {
  239. cfg80211_scan_done(wil->scan_request, &info);
  240. wil->scan_request = NULL;
  241. wil->radio_wdev = wil->wdev;
  242. }
  243. mutex_unlock(&wil->p2p_wdev_mutex);
  244. }
  245. }
  246. void wil_p2p_delayed_listen_work(struct work_struct *work)
  247. {
  248. struct wil_p2p_info *p2p = container_of(work,
  249. struct wil_p2p_info, delayed_listen_work);
  250. struct wil6210_priv *wil = container_of(p2p,
  251. struct wil6210_priv, p2p);
  252. int rc;
  253. mutex_lock(&wil->mutex);
  254. wil_dbg_misc(wil, "Checking delayed p2p listen\n");
  255. if (!p2p->discovery_started || !p2p->pending_listen_wdev)
  256. goto out;
  257. mutex_lock(&wil->p2p_wdev_mutex);
  258. if (wil->scan_request) {
  259. /* another scan started, wait again... */
  260. mutex_unlock(&wil->p2p_wdev_mutex);
  261. goto out;
  262. }
  263. mutex_unlock(&wil->p2p_wdev_mutex);
  264. rc = wil_p2p_start_listen(wil);
  265. mutex_lock(&wil->p2p_wdev_mutex);
  266. if (rc) {
  267. cfg80211_remain_on_channel_expired(p2p->pending_listen_wdev,
  268. p2p->cookie,
  269. &p2p->listen_chan,
  270. GFP_KERNEL);
  271. wil->radio_wdev = wil->wdev;
  272. } else {
  273. cfg80211_ready_on_channel(p2p->pending_listen_wdev, p2p->cookie,
  274. &p2p->listen_chan,
  275. p2p->listen_duration, GFP_KERNEL);
  276. wil->radio_wdev = p2p->pending_listen_wdev;
  277. }
  278. p2p->pending_listen_wdev = NULL;
  279. mutex_unlock(&wil->p2p_wdev_mutex);
  280. out:
  281. mutex_unlock(&wil->mutex);
  282. }
  283. void wil_p2p_stop_radio_operations(struct wil6210_priv *wil)
  284. {
  285. struct wil_p2p_info *p2p = &wil->p2p;
  286. struct cfg80211_scan_info info = {
  287. .aborted = true,
  288. };
  289. lockdep_assert_held(&wil->mutex);
  290. lockdep_assert_held(&wil->p2p_wdev_mutex);
  291. if (wil->radio_wdev != wil->p2p_wdev)
  292. goto out;
  293. if (!p2p->discovery_started) {
  294. /* Regular scan on the p2p device */
  295. if (wil->scan_request &&
  296. wil->scan_request->wdev == wil->p2p_wdev)
  297. wil_abort_scan(wil, true);
  298. goto out;
  299. }
  300. /* Search or listen on p2p device */
  301. mutex_unlock(&wil->p2p_wdev_mutex);
  302. wil_p2p_stop_discovery(wil);
  303. mutex_lock(&wil->p2p_wdev_mutex);
  304. if (wil->scan_request) {
  305. /* search */
  306. cfg80211_scan_done(wil->scan_request, &info);
  307. wil->scan_request = NULL;
  308. } else {
  309. /* listen */
  310. cfg80211_remain_on_channel_expired(wil->radio_wdev,
  311. p2p->cookie,
  312. &p2p->listen_chan,
  313. GFP_KERNEL);
  314. }
  315. out:
  316. wil->radio_wdev = wil->wdev;
  317. }