scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 ieee80211_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_PASSIVE_SCAN))) {
  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 ieee80211_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. cmd->params.role_id = wlvif->role_id;
  106. if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
  107. ret = -EINVAL;
  108. goto out;
  109. }
  110. cmd->params.scan_options = cpu_to_le16(scan_options);
  111. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  112. cmd->channels,
  113. band, passive);
  114. if (cmd->params.n_ch == 0) {
  115. ret = WL1271_NOTHING_TO_SCAN;
  116. goto out;
  117. }
  118. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  119. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  120. cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
  121. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  122. if (band == IEEE80211_BAND_2GHZ)
  123. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  124. else
  125. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  126. if (wl->scan.ssid_len && wl->scan.ssid) {
  127. cmd->params.ssid_len = wl->scan.ssid_len;
  128. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  129. }
  130. memcpy(cmd->addr, vif->addr, ETH_ALEN);
  131. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  132. cmd->params.role_id, band,
  133. wl->scan.ssid, wl->scan.ssid_len,
  134. wl->scan.req->ie,
  135. wl->scan.req->ie_len, false);
  136. if (ret < 0) {
  137. wl1271_error("PROBE request template failed");
  138. goto out;
  139. }
  140. trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
  141. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  142. sizeof(*trigger), 0);
  143. if (ret < 0) {
  144. wl1271_error("trigger scan to failed for hw scan");
  145. goto out;
  146. }
  147. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  148. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  149. if (ret < 0) {
  150. wl1271_error("SCAN failed");
  151. goto out;
  152. }
  153. out:
  154. kfree(cmd);
  155. kfree(trigger);
  156. return ret;
  157. }
  158. int wl12xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  159. {
  160. struct wl1271_cmd_header *cmd = NULL;
  161. int ret = 0;
  162. if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
  163. return -EINVAL;
  164. wl1271_debug(DEBUG_CMD, "cmd scan stop");
  165. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  166. if (!cmd) {
  167. ret = -ENOMEM;
  168. goto out;
  169. }
  170. ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
  171. sizeof(*cmd), 0);
  172. if (ret < 0) {
  173. wl1271_error("cmd stop_scan failed");
  174. goto out;
  175. }
  176. out:
  177. kfree(cmd);
  178. return ret;
  179. }
  180. void wl1271_scan_stm(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  181. {
  182. int ret = 0;
  183. enum ieee80211_band band;
  184. u32 rate, mask;
  185. switch (wl->scan.state) {
  186. case WL1271_SCAN_STATE_IDLE:
  187. break;
  188. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  189. band = IEEE80211_BAND_2GHZ;
  190. mask = wlvif->bitrate_masks[band];
  191. if (wl->scan.req->no_cck) {
  192. mask &= ~CONF_TX_CCK_RATES;
  193. if (!mask)
  194. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  195. }
  196. rate = wl1271_tx_min_rate_get(wl, mask);
  197. ret = wl1271_scan_send(wl, wlvif, band, false, rate);
  198. if (ret == WL1271_NOTHING_TO_SCAN) {
  199. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  200. wl1271_scan_stm(wl, wlvif);
  201. }
  202. break;
  203. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  204. band = IEEE80211_BAND_2GHZ;
  205. mask = wlvif->bitrate_masks[band];
  206. if (wl->scan.req->no_cck) {
  207. mask &= ~CONF_TX_CCK_RATES;
  208. if (!mask)
  209. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  210. }
  211. rate = wl1271_tx_min_rate_get(wl, mask);
  212. ret = wl1271_scan_send(wl, wlvif, band, true, rate);
  213. if (ret == WL1271_NOTHING_TO_SCAN) {
  214. if (wl->enable_11a)
  215. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  216. else
  217. wl->scan.state = WL1271_SCAN_STATE_DONE;
  218. wl1271_scan_stm(wl, wlvif);
  219. }
  220. break;
  221. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  222. band = IEEE80211_BAND_5GHZ;
  223. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  224. ret = wl1271_scan_send(wl, wlvif, band, false, rate);
  225. if (ret == WL1271_NOTHING_TO_SCAN) {
  226. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  227. wl1271_scan_stm(wl, wlvif);
  228. }
  229. break;
  230. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  231. band = IEEE80211_BAND_5GHZ;
  232. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  233. ret = wl1271_scan_send(wl, wlvif, band, true, rate);
  234. if (ret == WL1271_NOTHING_TO_SCAN) {
  235. wl->scan.state = WL1271_SCAN_STATE_DONE;
  236. wl1271_scan_stm(wl, wlvif);
  237. }
  238. break;
  239. case WL1271_SCAN_STATE_DONE:
  240. wl->scan.failed = false;
  241. cancel_delayed_work(&wl->scan_complete_work);
  242. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  243. msecs_to_jiffies(0));
  244. break;
  245. default:
  246. wl1271_error("invalid scan state");
  247. break;
  248. }
  249. if (ret < 0) {
  250. cancel_delayed_work(&wl->scan_complete_work);
  251. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  252. msecs_to_jiffies(0));
  253. }
  254. }
  255. static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config *cmd,
  256. struct wlcore_scan_channels *cmd_channels)
  257. {
  258. memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive));
  259. memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active));
  260. cmd->dfs = cmd_channels->dfs;
  261. cmd->n_pactive_ch = cmd_channels->passive_active;
  262. memcpy(cmd->channels_2, cmd_channels->channels_2,
  263. sizeof(cmd->channels_2));
  264. memcpy(cmd->channels_5, cmd_channels->channels_5,
  265. sizeof(cmd->channels_2));
  266. /* channels_4 are not supported, so no need to copy them */
  267. }
  268. int wl1271_scan_sched_scan_config(struct wl1271 *wl,
  269. struct wl12xx_vif *wlvif,
  270. struct cfg80211_sched_scan_request *req,
  271. struct ieee80211_sched_scan_ies *ies)
  272. {
  273. struct wl1271_cmd_sched_scan_config *cfg = NULL;
  274. struct wlcore_scan_channels *cfg_channels = NULL;
  275. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  276. int i, ret;
  277. bool force_passive = !req->n_ssids;
  278. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  279. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  280. if (!cfg)
  281. return -ENOMEM;
  282. cfg->role_id = wlvif->role_id;
  283. cfg->rssi_threshold = c->rssi_threshold;
  284. cfg->snr_threshold = c->snr_threshold;
  285. cfg->n_probe_reqs = c->num_probe_reqs;
  286. /* cycles set to 0 it means infinite (until manually stopped) */
  287. cfg->cycles = 0;
  288. /* report APs when at least 1 is found */
  289. cfg->report_after = 1;
  290. /* don't stop scanning automatically when something is found */
  291. cfg->terminate = 0;
  292. cfg->tag = WL1271_SCAN_DEFAULT_TAG;
  293. /* don't filter on BSS type */
  294. cfg->bss_type = SCAN_BSS_TYPE_ANY;
  295. /* currently NL80211 supports only a single interval */
  296. for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
  297. cfg->intervals[i] = cpu_to_le32(req->interval);
  298. cfg->ssid_len = 0;
  299. ret = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req);
  300. if (ret < 0)
  301. goto out;
  302. cfg->filter_type = ret;
  303. wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
  304. cfg_channels = kzalloc(sizeof(*cfg_channels), GFP_KERNEL);
  305. if (!cfg_channels) {
  306. ret = -ENOMEM;
  307. goto out;
  308. }
  309. if (!wlcore_set_scan_chan_params(wl, cfg_channels, req->channels,
  310. req->n_channels, req->n_ssids,
  311. SCAN_TYPE_PERIODIC)) {
  312. wl1271_error("scan channel list is empty");
  313. ret = -EINVAL;
  314. goto out;
  315. }
  316. wl12xx_adjust_channels(cfg, cfg_channels);
  317. if (!force_passive && cfg->active[0]) {
  318. u8 band = IEEE80211_BAND_2GHZ;
  319. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  320. wlvif->role_id, band,
  321. req->ssids[0].ssid,
  322. req->ssids[0].ssid_len,
  323. ies->ie[band],
  324. ies->len[band], true);
  325. if (ret < 0) {
  326. wl1271_error("2.4GHz PROBE request template failed");
  327. goto out;
  328. }
  329. }
  330. if (!force_passive && cfg->active[1]) {
  331. u8 band = IEEE80211_BAND_5GHZ;
  332. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  333. wlvif->role_id, band,
  334. req->ssids[0].ssid,
  335. req->ssids[0].ssid_len,
  336. ies->ie[band],
  337. ies->len[band], true);
  338. if (ret < 0) {
  339. wl1271_error("5GHz PROBE request template failed");
  340. goto out;
  341. }
  342. }
  343. wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
  344. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
  345. sizeof(*cfg), 0);
  346. if (ret < 0) {
  347. wl1271_error("SCAN configuration failed");
  348. goto out;
  349. }
  350. out:
  351. kfree(cfg_channels);
  352. kfree(cfg);
  353. return ret;
  354. }
  355. int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  356. {
  357. struct wl1271_cmd_sched_scan_start *start;
  358. int ret = 0;
  359. wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
  360. if (wlvif->bss_type != BSS_TYPE_STA_BSS)
  361. return -EOPNOTSUPP;
  362. if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
  363. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  364. return -EBUSY;
  365. start = kzalloc(sizeof(*start), GFP_KERNEL);
  366. if (!start)
  367. return -ENOMEM;
  368. start->role_id = wlvif->role_id;
  369. start->tag = WL1271_SCAN_DEFAULT_TAG;
  370. ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
  371. sizeof(*start), 0);
  372. if (ret < 0) {
  373. wl1271_error("failed to send scan start command");
  374. goto out_free;
  375. }
  376. out_free:
  377. kfree(start);
  378. return ret;
  379. }
  380. int wl12xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  381. struct cfg80211_sched_scan_request *req,
  382. struct ieee80211_sched_scan_ies *ies)
  383. {
  384. int ret;
  385. ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
  386. if (ret < 0)
  387. return ret;
  388. return wl1271_scan_sched_scan_start(wl, wlvif);
  389. }
  390. void wl12xx_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  391. {
  392. struct wl1271_cmd_sched_scan_stop *stop;
  393. int ret = 0;
  394. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  395. /* FIXME: what to do if alloc'ing to stop fails? */
  396. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  397. if (!stop) {
  398. wl1271_error("failed to alloc memory to send sched scan stop");
  399. return;
  400. }
  401. stop->role_id = wlvif->role_id;
  402. stop->tag = WL1271_SCAN_DEFAULT_TAG;
  403. ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
  404. sizeof(*stop), 0);
  405. if (ret < 0) {
  406. wl1271_error("failed to send sched scan stop command");
  407. goto out_free;
  408. }
  409. out_free:
  410. kfree(stop);
  411. }
  412. int wl12xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  413. struct cfg80211_scan_request *req)
  414. {
  415. wl1271_scan_stm(wl, wlvif);
  416. return 0;
  417. }
  418. void wl12xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  419. {
  420. wl1271_scan_stm(wl, wlvif);
  421. }