scan.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of wl18xx
  4. *
  5. * Copyright (C) 2012 Texas Instruments. All rights reserved.
  6. */
  7. #include <linux/ieee80211.h>
  8. #include "scan.h"
  9. #include "../wlcore/debug.h"
  10. static void wl18xx_adjust_channels(struct wl18xx_cmd_scan_params *cmd,
  11. struct wlcore_scan_channels *cmd_channels)
  12. {
  13. memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive));
  14. memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active));
  15. cmd->dfs = cmd_channels->dfs;
  16. cmd->passive_active = cmd_channels->passive_active;
  17. memcpy(cmd->channels_2, cmd_channels->channels_2,
  18. sizeof(cmd->channels_2));
  19. memcpy(cmd->channels_5, cmd_channels->channels_5,
  20. sizeof(cmd->channels_5));
  21. /* channels_4 are not supported, so no need to copy them */
  22. }
  23. static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  24. struct cfg80211_scan_request *req)
  25. {
  26. struct wl18xx_cmd_scan_params *cmd;
  27. struct wlcore_scan_channels *cmd_channels = NULL;
  28. int ret;
  29. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  30. if (!cmd) {
  31. ret = -ENOMEM;
  32. goto out;
  33. }
  34. /* scan on the dev role if the regular one is not started */
  35. if (wlcore_is_p2p_mgmt(wlvif))
  36. cmd->role_id = wlvif->dev_role_id;
  37. else
  38. cmd->role_id = wlvif->role_id;
  39. if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
  40. ret = -EINVAL;
  41. goto out;
  42. }
  43. cmd->scan_type = SCAN_TYPE_SEARCH;
  44. cmd->rssi_threshold = -127;
  45. cmd->snr_threshold = 0;
  46. cmd->bss_type = SCAN_BSS_TYPE_ANY;
  47. cmd->ssid_from_list = 0;
  48. cmd->filter = 0;
  49. cmd->add_broadcast = 0;
  50. cmd->urgency = 0;
  51. cmd->protect = 0;
  52. cmd->n_probe_reqs = wl->conf.scan.num_probe_reqs;
  53. cmd->terminate_after = 0;
  54. /* configure channels */
  55. WARN_ON(req->n_ssids > 1);
  56. cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
  57. if (!cmd_channels) {
  58. ret = -ENOMEM;
  59. goto out;
  60. }
  61. wlcore_set_scan_chan_params(wl, cmd_channels, req->channels,
  62. req->n_channels, req->n_ssids,
  63. SCAN_TYPE_SEARCH);
  64. wl18xx_adjust_channels(cmd, cmd_channels);
  65. /*
  66. * all the cycles params (except total cycles) should
  67. * remain 0 for normal scan
  68. */
  69. cmd->total_cycles = 1;
  70. if (req->no_cck)
  71. cmd->rate = WL18XX_SCAN_RATE_6;
  72. cmd->tag = WL1271_SCAN_DEFAULT_TAG;
  73. if (req->n_ssids) {
  74. cmd->ssid_len = req->ssids[0].ssid_len;
  75. memcpy(cmd->ssid, req->ssids[0].ssid, cmd->ssid_len);
  76. }
  77. /* TODO: per-band ies? */
  78. if (cmd->active[0]) {
  79. u8 band = NL80211_BAND_2GHZ;
  80. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  81. cmd->role_id, band,
  82. req->ssids ? req->ssids[0].ssid : NULL,
  83. req->ssids ? req->ssids[0].ssid_len : 0,
  84. req->ie,
  85. req->ie_len,
  86. NULL,
  87. 0,
  88. false);
  89. if (ret < 0) {
  90. wl1271_error("2.4GHz PROBE request template failed");
  91. goto out;
  92. }
  93. }
  94. if (cmd->active[1] || cmd->dfs) {
  95. u8 band = NL80211_BAND_5GHZ;
  96. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  97. cmd->role_id, band,
  98. req->ssids ? req->ssids[0].ssid : NULL,
  99. req->ssids ? req->ssids[0].ssid_len : 0,
  100. req->ie,
  101. req->ie_len,
  102. NULL,
  103. 0,
  104. false);
  105. if (ret < 0) {
  106. wl1271_error("5GHz PROBE request template failed");
  107. goto out;
  108. }
  109. }
  110. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  111. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  112. if (ret < 0) {
  113. wl1271_error("SCAN failed");
  114. goto out;
  115. }
  116. out:
  117. kfree(cmd_channels);
  118. kfree(cmd);
  119. return ret;
  120. }
  121. void wl18xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  122. {
  123. wl->scan.failed = false;
  124. cancel_delayed_work(&wl->scan_complete_work);
  125. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  126. msecs_to_jiffies(0));
  127. }
  128. static
  129. int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
  130. struct wl12xx_vif *wlvif,
  131. struct cfg80211_sched_scan_request *req,
  132. struct ieee80211_scan_ies *ies)
  133. {
  134. struct wl18xx_cmd_scan_params *cmd;
  135. struct wlcore_scan_channels *cmd_channels = NULL;
  136. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  137. int ret;
  138. int filter_type;
  139. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  140. filter_type = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req);
  141. if (filter_type < 0)
  142. return filter_type;
  143. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  144. if (!cmd) {
  145. ret = -ENOMEM;
  146. goto out;
  147. }
  148. cmd->role_id = wlvif->role_id;
  149. if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. cmd->scan_type = SCAN_TYPE_PERIODIC;
  154. cmd->rssi_threshold = c->rssi_threshold;
  155. cmd->snr_threshold = c->snr_threshold;
  156. /* don't filter on BSS type */
  157. cmd->bss_type = SCAN_BSS_TYPE_ANY;
  158. cmd->ssid_from_list = 1;
  159. if (filter_type == SCAN_SSID_FILTER_LIST)
  160. cmd->filter = 1;
  161. cmd->add_broadcast = 0;
  162. cmd->urgency = 0;
  163. cmd->protect = 0;
  164. cmd->n_probe_reqs = c->num_probe_reqs;
  165. /* don't stop scanning automatically when something is found */
  166. cmd->terminate_after = 0;
  167. cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
  168. if (!cmd_channels) {
  169. ret = -ENOMEM;
  170. goto out;
  171. }
  172. /* configure channels */
  173. wlcore_set_scan_chan_params(wl, cmd_channels, req->channels,
  174. req->n_channels, req->n_ssids,
  175. SCAN_TYPE_PERIODIC);
  176. wl18xx_adjust_channels(cmd, cmd_channels);
  177. if (c->num_short_intervals && c->long_interval &&
  178. c->long_interval > req->scan_plans[0].interval * MSEC_PER_SEC) {
  179. cmd->short_cycles_msec =
  180. cpu_to_le16(req->scan_plans[0].interval * MSEC_PER_SEC);
  181. cmd->long_cycles_msec = cpu_to_le16(c->long_interval);
  182. cmd->short_cycles_count = c->num_short_intervals;
  183. } else {
  184. cmd->short_cycles_msec = 0;
  185. cmd->long_cycles_msec =
  186. cpu_to_le16(req->scan_plans[0].interval * MSEC_PER_SEC);
  187. cmd->short_cycles_count = 0;
  188. }
  189. wl1271_debug(DEBUG_SCAN, "short_interval: %d, long_interval: %d, num_short: %d",
  190. le16_to_cpu(cmd->short_cycles_msec),
  191. le16_to_cpu(cmd->long_cycles_msec),
  192. cmd->short_cycles_count);
  193. cmd->total_cycles = 0;
  194. cmd->tag = WL1271_SCAN_DEFAULT_TAG;
  195. /* create a PERIODIC_SCAN_REPORT_EVENT whenever we've got a match */
  196. cmd->report_threshold = 1;
  197. cmd->terminate_on_report = 0;
  198. if (cmd->active[0]) {
  199. u8 band = NL80211_BAND_2GHZ;
  200. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  201. cmd->role_id, band,
  202. req->ssids ? req->ssids[0].ssid : NULL,
  203. req->ssids ? req->ssids[0].ssid_len : 0,
  204. ies->ies[band],
  205. ies->len[band],
  206. ies->common_ies,
  207. ies->common_ie_len,
  208. true);
  209. if (ret < 0) {
  210. wl1271_error("2.4GHz PROBE request template failed");
  211. goto out;
  212. }
  213. }
  214. if (cmd->active[1] || cmd->dfs) {
  215. u8 band = NL80211_BAND_5GHZ;
  216. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  217. cmd->role_id, band,
  218. req->ssids ? req->ssids[0].ssid : NULL,
  219. req->ssids ? req->ssids[0].ssid_len : 0,
  220. ies->ies[band],
  221. ies->len[band],
  222. ies->common_ies,
  223. ies->common_ie_len,
  224. true);
  225. if (ret < 0) {
  226. wl1271_error("5GHz PROBE request template failed");
  227. goto out;
  228. }
  229. }
  230. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  231. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  232. if (ret < 0) {
  233. wl1271_error("SCAN failed");
  234. goto out;
  235. }
  236. out:
  237. kfree(cmd_channels);
  238. kfree(cmd);
  239. return ret;
  240. }
  241. int wl18xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  242. struct cfg80211_sched_scan_request *req,
  243. struct ieee80211_scan_ies *ies)
  244. {
  245. return wl18xx_scan_sched_scan_config(wl, wlvif, req, ies);
  246. }
  247. static int __wl18xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  248. u8 scan_type)
  249. {
  250. struct wl18xx_cmd_scan_stop *stop;
  251. int ret;
  252. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  253. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  254. if (!stop) {
  255. wl1271_error("failed to alloc memory to send sched scan stop");
  256. return -ENOMEM;
  257. }
  258. stop->role_id = wlvif->role_id;
  259. stop->scan_type = scan_type;
  260. ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, stop, sizeof(*stop), 0);
  261. if (ret < 0) {
  262. wl1271_error("failed to send sched scan stop command");
  263. goto out_free;
  264. }
  265. out_free:
  266. kfree(stop);
  267. return ret;
  268. }
  269. void wl18xx_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  270. {
  271. __wl18xx_scan_stop(wl, wlvif, SCAN_TYPE_PERIODIC);
  272. }
  273. int wl18xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  274. struct cfg80211_scan_request *req)
  275. {
  276. return wl18xx_scan_send(wl, wlvif, req);
  277. }
  278. int wl18xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  279. {
  280. return __wl18xx_scan_stop(wl, wlvif, SCAN_TYPE_SEARCH);
  281. }