p2p.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2014-2017 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, "p2p_discovery_timer_fn\n");
  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, "p2p_search: channel %d\n", P2P_DMG_SOCIAL_CHANNEL);
  69. lockdep_assert_held(&wil->mutex);
  70. if (p2p->discovery_started) {
  71. wil_err(wil, "search failed. discovery already ongoing\n");
  72. rc = -EBUSY;
  73. goto out;
  74. }
  75. rc = wmi_p2p_cfg(wil, P2P_DMG_SOCIAL_CHANNEL, P2P_DEFAULT_BI);
  76. if (rc) {
  77. wil_err(wil, "wmi_p2p_cfg failed\n");
  78. goto out;
  79. }
  80. rc = wmi_set_ssid(wil, strlen(P2P_WILDCARD_SSID), P2P_WILDCARD_SSID);
  81. if (rc) {
  82. wil_err(wil, "wmi_set_ssid failed\n");
  83. goto out_stop;
  84. }
  85. /* Set application IE to probe request and probe response */
  86. rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ,
  87. request->ie_len, request->ie);
  88. if (rc) {
  89. wil_err(wil, "wmi_set_ie(WMI_FRAME_PROBE_REQ) failed\n");
  90. goto out_stop;
  91. }
  92. /* supplicant doesn't provide Probe Response IEs. As a workaround -
  93. * re-use Probe Request IEs
  94. */
  95. rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP,
  96. request->ie_len, request->ie);
  97. if (rc) {
  98. wil_err(wil, "wmi_set_ie(WMI_FRAME_PROBE_RESP) failed\n");
  99. goto out_stop;
  100. }
  101. rc = wmi_start_search(wil);
  102. if (rc) {
  103. wil_err(wil, "wmi_start_search failed\n");
  104. goto out_stop;
  105. }
  106. p2p->discovery_started = 1;
  107. INIT_WORK(&p2p->discovery_expired_work, wil_p2p_search_expired);
  108. mod_timer(&p2p->discovery_timer,
  109. jiffies + msecs_to_jiffies(P2P_SEARCH_DURATION_MS));
  110. out_stop:
  111. if (rc)
  112. wmi_stop_discovery(wil);
  113. out:
  114. return rc;
  115. }
  116. int wil_p2p_listen(struct wil6210_priv *wil, struct wireless_dev *wdev,
  117. unsigned int duration, struct ieee80211_channel *chan,
  118. u64 *cookie)
  119. {
  120. struct wil_p2p_info *p2p = &wil->p2p;
  121. int rc;
  122. if (!chan)
  123. return -EINVAL;
  124. wil_dbg_misc(wil, "p2p_listen: duration %d\n", duration);
  125. mutex_lock(&wil->mutex);
  126. if (p2p->discovery_started) {
  127. wil_err(wil, "discovery already ongoing\n");
  128. rc = -EBUSY;
  129. goto out;
  130. }
  131. memcpy(&p2p->listen_chan, chan, sizeof(*chan));
  132. *cookie = ++p2p->cookie;
  133. p2p->listen_duration = duration;
  134. mutex_lock(&wil->p2p_wdev_mutex);
  135. if (wil->scan_request) {
  136. wil_dbg_misc(wil, "Delaying p2p listen until scan done\n");
  137. p2p->pending_listen_wdev = wdev;
  138. p2p->discovery_started = 1;
  139. rc = 0;
  140. mutex_unlock(&wil->p2p_wdev_mutex);
  141. goto out;
  142. }
  143. mutex_unlock(&wil->p2p_wdev_mutex);
  144. rc = wil_p2p_start_listen(wil);
  145. if (rc)
  146. goto out;
  147. p2p->discovery_started = 1;
  148. wil->radio_wdev = wdev;
  149. cfg80211_ready_on_channel(wdev, *cookie, chan, duration,
  150. GFP_KERNEL);
  151. out:
  152. mutex_unlock(&wil->mutex);
  153. return rc;
  154. }
  155. u8 wil_p2p_stop_discovery(struct wil6210_priv *wil)
  156. {
  157. struct wil_p2p_info *p2p = &wil->p2p;
  158. u8 started = p2p->discovery_started;
  159. if (p2p->discovery_started) {
  160. if (p2p->pending_listen_wdev) {
  161. /* discovery not really started, only pending */
  162. p2p->pending_listen_wdev = NULL;
  163. } else {
  164. del_timer_sync(&p2p->discovery_timer);
  165. wmi_stop_discovery(wil);
  166. }
  167. p2p->discovery_started = 0;
  168. }
  169. return started;
  170. }
  171. int wil_p2p_cancel_listen(struct wil6210_priv *wil, u64 cookie)
  172. {
  173. struct wil_p2p_info *p2p = &wil->p2p;
  174. u8 started;
  175. mutex_lock(&wil->mutex);
  176. if (cookie != p2p->cookie) {
  177. wil_info(wil, "Cookie mismatch: 0x%016llx vs. 0x%016llx\n",
  178. p2p->cookie, cookie);
  179. mutex_unlock(&wil->mutex);
  180. return -ENOENT;
  181. }
  182. started = wil_p2p_stop_discovery(wil);
  183. mutex_unlock(&wil->mutex);
  184. if (!started) {
  185. wil_err(wil, "listen not started\n");
  186. return -ENOENT;
  187. }
  188. mutex_lock(&wil->p2p_wdev_mutex);
  189. cfg80211_remain_on_channel_expired(wil->radio_wdev,
  190. p2p->cookie,
  191. &p2p->listen_chan,
  192. GFP_KERNEL);
  193. wil->radio_wdev = wil->wdev;
  194. mutex_unlock(&wil->p2p_wdev_mutex);
  195. return 0;
  196. }
  197. void wil_p2p_listen_expired(struct work_struct *work)
  198. {
  199. struct wil_p2p_info *p2p = container_of(work,
  200. struct wil_p2p_info, discovery_expired_work);
  201. struct wil6210_priv *wil = container_of(p2p,
  202. struct wil6210_priv, p2p);
  203. u8 started;
  204. wil_dbg_misc(wil, "p2p_listen_expired\n");
  205. mutex_lock(&wil->mutex);
  206. started = wil_p2p_stop_discovery(wil);
  207. mutex_unlock(&wil->mutex);
  208. if (started) {
  209. mutex_lock(&wil->p2p_wdev_mutex);
  210. cfg80211_remain_on_channel_expired(wil->radio_wdev,
  211. p2p->cookie,
  212. &p2p->listen_chan,
  213. GFP_KERNEL);
  214. wil->radio_wdev = wil->wdev;
  215. mutex_unlock(&wil->p2p_wdev_mutex);
  216. }
  217. }
  218. void wil_p2p_search_expired(struct work_struct *work)
  219. {
  220. struct wil_p2p_info *p2p = container_of(work,
  221. struct wil_p2p_info, discovery_expired_work);
  222. struct wil6210_priv *wil = container_of(p2p,
  223. struct wil6210_priv, p2p);
  224. u8 started;
  225. wil_dbg_misc(wil, "p2p_search_expired\n");
  226. mutex_lock(&wil->mutex);
  227. started = wil_p2p_stop_discovery(wil);
  228. mutex_unlock(&wil->mutex);
  229. if (started) {
  230. struct cfg80211_scan_info info = {
  231. .aborted = false,
  232. };
  233. mutex_lock(&wil->p2p_wdev_mutex);
  234. if (wil->scan_request) {
  235. cfg80211_scan_done(wil->scan_request, &info);
  236. wil->scan_request = NULL;
  237. wil->radio_wdev = wil->wdev;
  238. }
  239. mutex_unlock(&wil->p2p_wdev_mutex);
  240. }
  241. }
  242. void wil_p2p_delayed_listen_work(struct work_struct *work)
  243. {
  244. struct wil_p2p_info *p2p = container_of(work,
  245. struct wil_p2p_info, delayed_listen_work);
  246. struct wil6210_priv *wil = container_of(p2p,
  247. struct wil6210_priv, p2p);
  248. int rc;
  249. mutex_lock(&wil->mutex);
  250. wil_dbg_misc(wil, "Checking delayed p2p listen\n");
  251. if (!p2p->discovery_started || !p2p->pending_listen_wdev)
  252. goto out;
  253. mutex_lock(&wil->p2p_wdev_mutex);
  254. if (wil->scan_request) {
  255. /* another scan started, wait again... */
  256. mutex_unlock(&wil->p2p_wdev_mutex);
  257. goto out;
  258. }
  259. mutex_unlock(&wil->p2p_wdev_mutex);
  260. rc = wil_p2p_start_listen(wil);
  261. mutex_lock(&wil->p2p_wdev_mutex);
  262. if (rc) {
  263. cfg80211_remain_on_channel_expired(p2p->pending_listen_wdev,
  264. p2p->cookie,
  265. &p2p->listen_chan,
  266. GFP_KERNEL);
  267. wil->radio_wdev = wil->wdev;
  268. } else {
  269. cfg80211_ready_on_channel(p2p->pending_listen_wdev, p2p->cookie,
  270. &p2p->listen_chan,
  271. p2p->listen_duration, GFP_KERNEL);
  272. wil->radio_wdev = p2p->pending_listen_wdev;
  273. }
  274. p2p->pending_listen_wdev = NULL;
  275. mutex_unlock(&wil->p2p_wdev_mutex);
  276. out:
  277. mutex_unlock(&wil->mutex);
  278. }
  279. void wil_p2p_stop_radio_operations(struct wil6210_priv *wil)
  280. {
  281. struct wil_p2p_info *p2p = &wil->p2p;
  282. struct cfg80211_scan_info info = {
  283. .aborted = true,
  284. };
  285. lockdep_assert_held(&wil->mutex);
  286. lockdep_assert_held(&wil->p2p_wdev_mutex);
  287. if (wil->radio_wdev != wil->p2p_wdev)
  288. goto out;
  289. if (!p2p->discovery_started) {
  290. /* Regular scan on the p2p device */
  291. if (wil->scan_request &&
  292. wil->scan_request->wdev == wil->p2p_wdev)
  293. wil_abort_scan(wil, true);
  294. goto out;
  295. }
  296. /* Search or listen on p2p device */
  297. mutex_unlock(&wil->p2p_wdev_mutex);
  298. wil_p2p_stop_discovery(wil);
  299. mutex_lock(&wil->p2p_wdev_mutex);
  300. if (wil->scan_request) {
  301. /* search */
  302. cfg80211_scan_done(wil->scan_request, &info);
  303. wil->scan_request = NULL;
  304. } else {
  305. /* listen */
  306. cfg80211_remain_on_channel_expired(wil->radio_wdev,
  307. p2p->cookie,
  308. &p2p->listen_chan,
  309. GFP_KERNEL);
  310. }
  311. out:
  312. wil->radio_wdev = wil->wdev;
  313. }