scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * This file is part of wl12xx
  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. #include "../wlcore/tx.h"
  25. static int wl1271_get_scan_channels(struct wl1271 *wl,
  26. struct cfg80211_scan_request *req,
  27. struct basic_scan_channel_params *channels,
  28. enum nl80211_band band, bool passive)
  29. {
  30. struct conf_scan_settings *c = &wl->conf.scan;
  31. int i, j;
  32. u32 flags;
  33. for (i = 0, j = 0;
  34. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  35. i++) {
  36. flags = req->channels[i]->flags;
  37. if (!test_bit(i, wl->scan.scanned_ch) &&
  38. !(flags & IEEE80211_CHAN_DISABLED) &&
  39. (req->channels[i]->band == band) &&
  40. /*
  41. * In passive scans, we scan all remaining
  42. * channels, even if not marked as such.
  43. * In active scans, we only scan channels not
  44. * marked as passive.
  45. */
  46. (passive || !(flags & IEEE80211_CHAN_NO_IR))) {
  47. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  48. req->channels[i]->band,
  49. req->channels[i]->center_freq);
  50. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  51. req->channels[i]->hw_value,
  52. req->channels[i]->flags);
  53. wl1271_debug(DEBUG_SCAN,
  54. "max_antenna_gain %d, max_power %d",
  55. req->channels[i]->max_antenna_gain,
  56. req->channels[i]->max_power);
  57. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  58. req->channels[i]->beacon_found);
  59. if (!passive) {
  60. channels[j].min_duration =
  61. cpu_to_le32(c->min_dwell_time_active);
  62. channels[j].max_duration =
  63. cpu_to_le32(c->max_dwell_time_active);
  64. } else {
  65. channels[j].min_duration =
  66. cpu_to_le32(c->dwell_time_passive);
  67. channels[j].max_duration =
  68. cpu_to_le32(c->dwell_time_passive);
  69. }
  70. channels[j].early_termination = 0;
  71. channels[j].tx_power_att = req->channels[i]->max_power;
  72. channels[j].channel = req->channels[i]->hw_value;
  73. memset(&channels[j].bssid_lsb, 0xff, 4);
  74. memset(&channels[j].bssid_msb, 0xff, 2);
  75. /* Mark the channels we already used */
  76. set_bit(i, wl->scan.scanned_ch);
  77. j++;
  78. }
  79. }
  80. return j;
  81. }
  82. #define WL1271_NOTHING_TO_SCAN 1
  83. static int wl1271_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  84. enum nl80211_band band,
  85. bool passive, u32 basic_rate)
  86. {
  87. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  88. struct wl1271_cmd_scan *cmd;
  89. struct wl1271_cmd_trigger_scan_to *trigger;
  90. int ret;
  91. u16 scan_options = 0;
  92. /* skip active scans if we don't have SSIDs */
  93. if (!passive && wl->scan.req->n_ssids == 0)
  94. return WL1271_NOTHING_TO_SCAN;
  95. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  96. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  97. if (!cmd || !trigger) {
  98. ret = -ENOMEM;
  99. goto out;
  100. }
  101. if (wl->conf.scan.split_scan_timeout)
  102. scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;
  103. if (passive)
  104. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  105. /* scan on the dev role if the regular one is not started */
  106. if (wlcore_is_p2p_mgmt(wlvif))
  107. cmd->params.role_id = wlvif->dev_role_id;
  108. else
  109. cmd->params.role_id = wlvif->role_id;
  110. if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
  111. ret = -EINVAL;
  112. goto out;
  113. }
  114. cmd->params.scan_options = cpu_to_le16(scan_options);
  115. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  116. cmd->channels,
  117. band, passive);
  118. if (cmd->params.n_ch == 0) {
  119. ret = WL1271_NOTHING_TO_SCAN;
  120. goto out;
  121. }
  122. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  123. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  124. cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
  125. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  126. if (band == NL80211_BAND_2GHZ)
  127. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  128. else
  129. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  130. if (wl->scan.ssid_len) {
  131. cmd->params.ssid_len = wl->scan.ssid_len;
  132. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  133. }
  134. memcpy(cmd->addr, vif->addr, ETH_ALEN);
  135. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  136. cmd->params.role_id, band,
  137. wl->scan.ssid, wl->scan.ssid_len,
  138. wl->scan.req->ie,
  139. wl->scan.req->ie_len, NULL, 0, false);
  140. if (ret < 0) {
  141. wl1271_error("PROBE request template failed");
  142. goto out;
  143. }
  144. trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
  145. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  146. sizeof(*trigger), 0);
  147. if (ret < 0) {
  148. wl1271_error("trigger scan to failed for hw scan");
  149. goto out;
  150. }
  151. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  152. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  153. if (ret < 0) {
  154. wl1271_error("SCAN failed");
  155. goto out;
  156. }
  157. out:
  158. kfree(cmd);
  159. kfree(trigger);
  160. return ret;
  161. }
  162. int wl12xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  163. {
  164. struct wl1271_cmd_header *cmd = NULL;
  165. int ret = 0;
  166. if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
  167. return -EINVAL;
  168. wl1271_debug(DEBUG_CMD, "cmd scan stop");
  169. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  170. if (!cmd) {
  171. ret = -ENOMEM;
  172. goto out;
  173. }
  174. ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
  175. sizeof(*cmd), 0);
  176. if (ret < 0) {
  177. wl1271_error("cmd stop_scan failed");
  178. goto out;
  179. }
  180. out:
  181. kfree(cmd);
  182. return ret;
  183. }
  184. void wl1271_scan_stm(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  185. {
  186. int ret = 0;
  187. enum nl80211_band band;
  188. u32 rate, mask;
  189. switch (wl->scan.state) {
  190. case WL1271_SCAN_STATE_IDLE:
  191. break;
  192. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  193. band = NL80211_BAND_2GHZ;
  194. mask = wlvif->bitrate_masks[band];
  195. if (wl->scan.req->no_cck) {
  196. mask &= ~CONF_TX_CCK_RATES;
  197. if (!mask)
  198. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  199. }
  200. rate = wl1271_tx_min_rate_get(wl, mask);
  201. ret = wl1271_scan_send(wl, wlvif, band, false, rate);
  202. if (ret == WL1271_NOTHING_TO_SCAN) {
  203. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  204. wl1271_scan_stm(wl, wlvif);
  205. }
  206. break;
  207. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  208. band = NL80211_BAND_2GHZ;
  209. mask = wlvif->bitrate_masks[band];
  210. if (wl->scan.req->no_cck) {
  211. mask &= ~CONF_TX_CCK_RATES;
  212. if (!mask)
  213. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  214. }
  215. rate = wl1271_tx_min_rate_get(wl, mask);
  216. ret = wl1271_scan_send(wl, wlvif, band, true, rate);
  217. if (ret == WL1271_NOTHING_TO_SCAN) {
  218. if (wl->enable_11a)
  219. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  220. else
  221. wl->scan.state = WL1271_SCAN_STATE_DONE;
  222. wl1271_scan_stm(wl, wlvif);
  223. }
  224. break;
  225. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  226. band = NL80211_BAND_5GHZ;
  227. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  228. ret = wl1271_scan_send(wl, wlvif, band, false, rate);
  229. if (ret == WL1271_NOTHING_TO_SCAN) {
  230. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  231. wl1271_scan_stm(wl, wlvif);
  232. }
  233. break;
  234. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  235. band = NL80211_BAND_5GHZ;
  236. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  237. ret = wl1271_scan_send(wl, wlvif, band, true, rate);
  238. if (ret == WL1271_NOTHING_TO_SCAN) {
  239. wl->scan.state = WL1271_SCAN_STATE_DONE;
  240. wl1271_scan_stm(wl, wlvif);
  241. }
  242. break;
  243. case WL1271_SCAN_STATE_DONE:
  244. wl->scan.failed = false;
  245. cancel_delayed_work(&wl->scan_complete_work);
  246. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  247. msecs_to_jiffies(0));
  248. break;
  249. default:
  250. wl1271_error("invalid scan state");
  251. break;
  252. }
  253. if (ret < 0) {
  254. cancel_delayed_work(&wl->scan_complete_work);
  255. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  256. msecs_to_jiffies(0));
  257. }
  258. }
  259. static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config *cmd,
  260. struct wlcore_scan_channels *cmd_channels)
  261. {
  262. memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive));
  263. memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active));
  264. cmd->dfs = cmd_channels->dfs;
  265. cmd->n_pactive_ch = cmd_channels->passive_active;
  266. memcpy(cmd->channels_2, cmd_channels->channels_2,
  267. sizeof(cmd->channels_2));
  268. memcpy(cmd->channels_5, cmd_channels->channels_5,
  269. sizeof(cmd->channels_5));
  270. /* channels_4 are not supported, so no need to copy them */
  271. }
  272. int wl1271_scan_sched_scan_config(struct wl1271 *wl,
  273. struct wl12xx_vif *wlvif,
  274. struct cfg80211_sched_scan_request *req,
  275. struct ieee80211_scan_ies *ies)
  276. {
  277. struct wl1271_cmd_sched_scan_config *cfg = NULL;
  278. struct wlcore_scan_channels *cfg_channels = NULL;
  279. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  280. int i, ret;
  281. bool force_passive = !req->n_ssids;
  282. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  283. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  284. if (!cfg)
  285. return -ENOMEM;
  286. cfg->role_id = wlvif->role_id;
  287. cfg->rssi_threshold = c->rssi_threshold;
  288. cfg->snr_threshold = c->snr_threshold;
  289. cfg->n_probe_reqs = c->num_probe_reqs;
  290. /* cycles set to 0 it means infinite (until manually stopped) */
  291. cfg->cycles = 0;
  292. /* report APs when at least 1 is found */
  293. cfg->report_after = 1;
  294. /* don't stop scanning automatically when something is found */
  295. cfg->terminate = 0;
  296. cfg->tag = WL1271_SCAN_DEFAULT_TAG;
  297. /* don't filter on BSS type */
  298. cfg->bss_type = SCAN_BSS_TYPE_ANY;
  299. /* currently NL80211 supports only a single interval */
  300. for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
  301. cfg->intervals[i] = cpu_to_le32(req->scan_plans[0].interval *
  302. MSEC_PER_SEC);
  303. cfg->ssid_len = 0;
  304. ret = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req);
  305. if (ret < 0)
  306. goto out;
  307. cfg->filter_type = ret;
  308. wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
  309. cfg_channels = kzalloc(sizeof(*cfg_channels), GFP_KERNEL);
  310. if (!cfg_channels) {
  311. ret = -ENOMEM;
  312. goto out;
  313. }
  314. if (!wlcore_set_scan_chan_params(wl, cfg_channels, req->channels,
  315. req->n_channels, req->n_ssids,
  316. SCAN_TYPE_PERIODIC)) {
  317. wl1271_error("scan channel list is empty");
  318. ret = -EINVAL;
  319. goto out;
  320. }
  321. wl12xx_adjust_channels(cfg, cfg_channels);
  322. if (!force_passive && cfg->active[0]) {
  323. u8 band = NL80211_BAND_2GHZ;
  324. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  325. wlvif->role_id, band,
  326. req->ssids[0].ssid,
  327. req->ssids[0].ssid_len,
  328. ies->ies[band],
  329. ies->len[band],
  330. ies->common_ies,
  331. ies->common_ie_len,
  332. true);
  333. if (ret < 0) {
  334. wl1271_error("2.4GHz PROBE request template failed");
  335. goto out;
  336. }
  337. }
  338. if (!force_passive && cfg->active[1]) {
  339. u8 band = NL80211_BAND_5GHZ;
  340. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  341. wlvif->role_id, band,
  342. req->ssids[0].ssid,
  343. req->ssids[0].ssid_len,
  344. ies->ies[band],
  345. ies->len[band],
  346. ies->common_ies,
  347. ies->common_ie_len,
  348. true);
  349. if (ret < 0) {
  350. wl1271_error("5GHz PROBE request template failed");
  351. goto out;
  352. }
  353. }
  354. wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
  355. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
  356. sizeof(*cfg), 0);
  357. if (ret < 0) {
  358. wl1271_error("SCAN configuration failed");
  359. goto out;
  360. }
  361. out:
  362. kfree(cfg_channels);
  363. kfree(cfg);
  364. return ret;
  365. }
  366. int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  367. {
  368. struct wl1271_cmd_sched_scan_start *start;
  369. int ret = 0;
  370. wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
  371. if (wlvif->bss_type != BSS_TYPE_STA_BSS)
  372. return -EOPNOTSUPP;
  373. if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
  374. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  375. return -EBUSY;
  376. start = kzalloc(sizeof(*start), GFP_KERNEL);
  377. if (!start)
  378. return -ENOMEM;
  379. start->role_id = wlvif->role_id;
  380. start->tag = WL1271_SCAN_DEFAULT_TAG;
  381. ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
  382. sizeof(*start), 0);
  383. if (ret < 0) {
  384. wl1271_error("failed to send scan start command");
  385. goto out_free;
  386. }
  387. out_free:
  388. kfree(start);
  389. return ret;
  390. }
  391. int wl12xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  392. struct cfg80211_sched_scan_request *req,
  393. struct ieee80211_scan_ies *ies)
  394. {
  395. int ret;
  396. ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
  397. if (ret < 0)
  398. return ret;
  399. return wl1271_scan_sched_scan_start(wl, wlvif);
  400. }
  401. void wl12xx_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  402. {
  403. struct wl1271_cmd_sched_scan_stop *stop;
  404. int ret = 0;
  405. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  406. /* FIXME: what to do if alloc'ing to stop fails? */
  407. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  408. if (!stop) {
  409. wl1271_error("failed to alloc memory to send sched scan stop");
  410. return;
  411. }
  412. stop->role_id = wlvif->role_id;
  413. stop->tag = WL1271_SCAN_DEFAULT_TAG;
  414. ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
  415. sizeof(*stop), 0);
  416. if (ret < 0) {
  417. wl1271_error("failed to send sched scan stop command");
  418. goto out_free;
  419. }
  420. out_free:
  421. kfree(stop);
  422. }
  423. int wl12xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  424. struct cfg80211_scan_request *req)
  425. {
  426. wl1271_scan_stm(wl, wlvif);
  427. return 0;
  428. }
  429. void wl12xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  430. {
  431. wl1271_scan_stm(wl, wlvif);
  432. }