scan.c 8.8 KB

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