scan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/ieee80211.h>
  24. #include "wl12xx.h"
  25. #include "cmd.h"
  26. #include "scan.h"
  27. #include "acx.h"
  28. #include "ps.h"
  29. #include "tx.h"
  30. void wl1271_scan_complete_work(struct work_struct *work)
  31. {
  32. struct delayed_work *dwork;
  33. struct wl1271 *wl;
  34. struct wl12xx_vif *wlvif;
  35. int ret;
  36. bool is_sta, is_ibss;
  37. dwork = container_of(work, struct delayed_work, work);
  38. wl = container_of(dwork, struct wl1271, scan_complete_work);
  39. wl1271_debug(DEBUG_SCAN, "Scanning complete");
  40. mutex_lock(&wl->mutex);
  41. if (wl->state == WL1271_STATE_OFF)
  42. goto out;
  43. if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
  44. goto out;
  45. wlvif = wl12xx_vif_to_data(wl->scan_vif);
  46. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  47. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  48. wl->scan.req = NULL;
  49. wl->scan_vif = NULL;
  50. ret = wl1271_ps_elp_wakeup(wl);
  51. if (ret < 0)
  52. goto out;
  53. if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
  54. /* restore hardware connection monitoring template */
  55. wl1271_cmd_build_ap_probe_req(wl, wlvif->probereq);
  56. }
  57. /* return to ROC if needed */
  58. is_sta = (wlvif->bss_type == BSS_TYPE_STA_BSS);
  59. is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
  60. if (((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
  61. (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) &&
  62. !test_bit(wl->dev_role_id, wl->roc_map)) {
  63. /* restore remain on channel */
  64. wl12xx_cmd_role_start_dev(wl);
  65. wl12xx_roc(wl, wl->dev_role_id);
  66. }
  67. wl1271_ps_elp_sleep(wl);
  68. if (wl->scan.failed) {
  69. wl1271_info("Scan completed due to error.");
  70. wl12xx_queue_recovery_work(wl);
  71. }
  72. ieee80211_scan_completed(wl->hw, false);
  73. out:
  74. mutex_unlock(&wl->mutex);
  75. }
  76. static int wl1271_get_scan_channels(struct wl1271 *wl,
  77. struct cfg80211_scan_request *req,
  78. struct basic_scan_channel_params *channels,
  79. enum ieee80211_band band, bool passive)
  80. {
  81. struct conf_scan_settings *c = &wl->conf.scan;
  82. int i, j;
  83. u32 flags;
  84. for (i = 0, j = 0;
  85. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  86. i++) {
  87. flags = req->channels[i]->flags;
  88. if (!test_bit(i, wl->scan.scanned_ch) &&
  89. !(flags & IEEE80211_CHAN_DISABLED) &&
  90. (req->channels[i]->band == band) &&
  91. /*
  92. * In passive scans, we scan all remaining
  93. * channels, even if not marked as such.
  94. * In active scans, we only scan channels not
  95. * marked as passive.
  96. */
  97. (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
  98. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  99. req->channels[i]->band,
  100. req->channels[i]->center_freq);
  101. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  102. req->channels[i]->hw_value,
  103. req->channels[i]->flags);
  104. wl1271_debug(DEBUG_SCAN,
  105. "max_antenna_gain %d, max_power %d",
  106. req->channels[i]->max_antenna_gain,
  107. req->channels[i]->max_power);
  108. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  109. req->channels[i]->beacon_found);
  110. if (!passive) {
  111. channels[j].min_duration =
  112. cpu_to_le32(c->min_dwell_time_active);
  113. channels[j].max_duration =
  114. cpu_to_le32(c->max_dwell_time_active);
  115. } else {
  116. channels[j].min_duration =
  117. cpu_to_le32(c->min_dwell_time_passive);
  118. channels[j].max_duration =
  119. cpu_to_le32(c->max_dwell_time_passive);
  120. }
  121. channels[j].early_termination = 0;
  122. channels[j].tx_power_att = req->channels[i]->max_power;
  123. channels[j].channel = req->channels[i]->hw_value;
  124. memset(&channels[j].bssid_lsb, 0xff, 4);
  125. memset(&channels[j].bssid_msb, 0xff, 2);
  126. /* Mark the channels we already used */
  127. set_bit(i, wl->scan.scanned_ch);
  128. j++;
  129. }
  130. }
  131. return j;
  132. }
  133. #define WL1271_NOTHING_TO_SCAN 1
  134. static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
  135. enum ieee80211_band band,
  136. bool passive, u32 basic_rate)
  137. {
  138. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  139. struct wl1271_cmd_scan *cmd;
  140. struct wl1271_cmd_trigger_scan_to *trigger;
  141. int ret;
  142. u16 scan_options = 0;
  143. /* skip active scans if we don't have SSIDs */
  144. if (!passive && wl->scan.req->n_ssids == 0)
  145. return WL1271_NOTHING_TO_SCAN;
  146. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  147. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  148. if (!cmd || !trigger) {
  149. ret = -ENOMEM;
  150. goto out;
  151. }
  152. if (passive)
  153. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  154. if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID)) {
  155. ret = -EINVAL;
  156. goto out;
  157. }
  158. cmd->params.role_id = wlvif->role_id;
  159. cmd->params.scan_options = cpu_to_le16(scan_options);
  160. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  161. cmd->channels,
  162. band, passive);
  163. if (cmd->params.n_ch == 0) {
  164. ret = WL1271_NOTHING_TO_SCAN;
  165. goto out;
  166. }
  167. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  168. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  169. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  170. cmd->params.tid_trigger = 0;
  171. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  172. if (band == IEEE80211_BAND_2GHZ)
  173. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  174. else
  175. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  176. if (wl->scan.ssid_len && wl->scan.ssid) {
  177. cmd->params.ssid_len = wl->scan.ssid_len;
  178. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  179. }
  180. memcpy(cmd->addr, vif->addr, ETH_ALEN);
  181. ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
  182. wl->scan.req->ie, wl->scan.req->ie_len,
  183. band);
  184. if (ret < 0) {
  185. wl1271_error("PROBE request template failed");
  186. goto out;
  187. }
  188. /* disable the timeout */
  189. trigger->timeout = 0;
  190. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  191. sizeof(*trigger), 0);
  192. if (ret < 0) {
  193. wl1271_error("trigger scan to failed for hw scan");
  194. goto out;
  195. }
  196. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  197. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  198. if (ret < 0) {
  199. wl1271_error("SCAN failed");
  200. goto out;
  201. }
  202. out:
  203. kfree(cmd);
  204. kfree(trigger);
  205. return ret;
  206. }
  207. void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
  208. {
  209. int ret = 0;
  210. enum ieee80211_band band;
  211. u32 rate;
  212. switch (wl->scan.state) {
  213. case WL1271_SCAN_STATE_IDLE:
  214. break;
  215. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  216. band = IEEE80211_BAND_2GHZ;
  217. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
  218. ret = wl1271_scan_send(wl, vif, band, false, rate);
  219. if (ret == WL1271_NOTHING_TO_SCAN) {
  220. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  221. wl1271_scan_stm(wl, vif);
  222. }
  223. break;
  224. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  225. band = IEEE80211_BAND_2GHZ;
  226. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
  227. ret = wl1271_scan_send(wl, vif, band, true, rate);
  228. if (ret == WL1271_NOTHING_TO_SCAN) {
  229. if (wl->enable_11a)
  230. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  231. else
  232. wl->scan.state = WL1271_SCAN_STATE_DONE;
  233. wl1271_scan_stm(wl, vif);
  234. }
  235. break;
  236. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  237. band = IEEE80211_BAND_5GHZ;
  238. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
  239. ret = wl1271_scan_send(wl, vif, band, false, rate);
  240. if (ret == WL1271_NOTHING_TO_SCAN) {
  241. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  242. wl1271_scan_stm(wl, vif);
  243. }
  244. break;
  245. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  246. band = IEEE80211_BAND_5GHZ;
  247. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
  248. ret = wl1271_scan_send(wl, vif, band, true, rate);
  249. if (ret == WL1271_NOTHING_TO_SCAN) {
  250. wl->scan.state = WL1271_SCAN_STATE_DONE;
  251. wl1271_scan_stm(wl, vif);
  252. }
  253. break;
  254. case WL1271_SCAN_STATE_DONE:
  255. wl->scan.failed = false;
  256. cancel_delayed_work(&wl->scan_complete_work);
  257. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  258. msecs_to_jiffies(0));
  259. break;
  260. default:
  261. wl1271_error("invalid scan state");
  262. break;
  263. }
  264. if (ret < 0) {
  265. cancel_delayed_work(&wl->scan_complete_work);
  266. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  267. msecs_to_jiffies(0));
  268. }
  269. }
  270. int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
  271. const u8 *ssid, size_t ssid_len,
  272. struct cfg80211_scan_request *req)
  273. {
  274. /*
  275. * cfg80211 should guarantee that we don't get more channels
  276. * than what we have registered.
  277. */
  278. BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
  279. if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
  280. return -EBUSY;
  281. wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
  282. if (ssid_len && ssid) {
  283. wl->scan.ssid_len = ssid_len;
  284. memcpy(wl->scan.ssid, ssid, ssid_len);
  285. } else {
  286. wl->scan.ssid_len = 0;
  287. }
  288. wl->scan_vif = vif;
  289. wl->scan.req = req;
  290. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  291. /* we assume failure so that timeout scenarios are handled correctly */
  292. wl->scan.failed = true;
  293. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  294. msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
  295. wl1271_scan_stm(wl, vif);
  296. return 0;
  297. }
  298. int wl1271_scan_stop(struct wl1271 *wl)
  299. {
  300. struct wl1271_cmd_header *cmd = NULL;
  301. int ret = 0;
  302. if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
  303. return -EINVAL;
  304. wl1271_debug(DEBUG_CMD, "cmd scan stop");
  305. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  306. if (!cmd) {
  307. ret = -ENOMEM;
  308. goto out;
  309. }
  310. ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
  311. sizeof(*cmd), 0);
  312. if (ret < 0) {
  313. wl1271_error("cmd stop_scan failed");
  314. goto out;
  315. }
  316. out:
  317. kfree(cmd);
  318. return ret;
  319. }
  320. static int
  321. wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
  322. struct cfg80211_sched_scan_request *req,
  323. struct conn_scan_ch_params *channels,
  324. u32 band, bool radar, bool passive,
  325. int start, int max_channels)
  326. {
  327. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  328. int i, j;
  329. u32 flags;
  330. bool force_passive = !req->n_ssids;
  331. for (i = 0, j = start;
  332. i < req->n_channels && j < max_channels;
  333. i++) {
  334. flags = req->channels[i]->flags;
  335. if (force_passive)
  336. flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  337. if ((req->channels[i]->band == band) &&
  338. !(flags & IEEE80211_CHAN_DISABLED) &&
  339. (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
  340. /* if radar is set, we ignore the passive flag */
  341. (radar ||
  342. !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
  343. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  344. req->channels[i]->band,
  345. req->channels[i]->center_freq);
  346. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  347. req->channels[i]->hw_value,
  348. req->channels[i]->flags);
  349. wl1271_debug(DEBUG_SCAN, "max_power %d",
  350. req->channels[i]->max_power);
  351. if (flags & IEEE80211_CHAN_RADAR) {
  352. channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
  353. channels[j].passive_duration =
  354. cpu_to_le16(c->dwell_time_dfs);
  355. }
  356. else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  357. channels[j].passive_duration =
  358. cpu_to_le16(c->dwell_time_passive);
  359. } else {
  360. channels[j].min_duration =
  361. cpu_to_le16(c->min_dwell_time_active);
  362. channels[j].max_duration =
  363. cpu_to_le16(c->max_dwell_time_active);
  364. }
  365. channels[j].tx_power_att = req->channels[i]->max_power;
  366. channels[j].channel = req->channels[i]->hw_value;
  367. j++;
  368. }
  369. }
  370. return j - start;
  371. }
  372. static bool
  373. wl1271_scan_sched_scan_channels(struct wl1271 *wl,
  374. struct cfg80211_sched_scan_request *req,
  375. struct wl1271_cmd_sched_scan_config *cfg)
  376. {
  377. cfg->passive[0] =
  378. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
  379. IEEE80211_BAND_2GHZ,
  380. false, true, 0,
  381. MAX_CHANNELS_2GHZ);
  382. cfg->active[0] =
  383. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
  384. IEEE80211_BAND_2GHZ,
  385. false, false,
  386. cfg->passive[0],
  387. MAX_CHANNELS_2GHZ);
  388. cfg->passive[1] =
  389. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
  390. IEEE80211_BAND_5GHZ,
  391. false, true, 0,
  392. MAX_CHANNELS_5GHZ);
  393. cfg->dfs =
  394. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
  395. IEEE80211_BAND_5GHZ,
  396. true, true,
  397. cfg->passive[1],
  398. MAX_CHANNELS_5GHZ);
  399. cfg->active[1] =
  400. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
  401. IEEE80211_BAND_5GHZ,
  402. false, false,
  403. cfg->passive[1] + cfg->dfs,
  404. MAX_CHANNELS_5GHZ);
  405. /* 802.11j channels are not supported yet */
  406. cfg->passive[2] = 0;
  407. cfg->active[2] = 0;
  408. wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
  409. cfg->active[0], cfg->passive[0]);
  410. wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
  411. cfg->active[1], cfg->passive[1]);
  412. wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
  413. return cfg->passive[0] || cfg->active[0] ||
  414. cfg->passive[1] || cfg->active[1] || cfg->dfs ||
  415. cfg->passive[2] || cfg->active[2];
  416. }
  417. /* Returns the scan type to be used or a negative value on error */
  418. static int
  419. wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
  420. struct cfg80211_sched_scan_request *req)
  421. {
  422. struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
  423. struct cfg80211_match_set *sets = req->match_sets;
  424. struct cfg80211_ssid *ssids = req->ssids;
  425. int ret = 0, type, i, j, n_match_ssids = 0;
  426. wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
  427. /* count the match sets that contain SSIDs */
  428. for (i = 0; i < req->n_match_sets; i++)
  429. if (sets[i].ssid.ssid_len > 0)
  430. n_match_ssids++;
  431. /* No filter, no ssids or only bcast ssid */
  432. if (!n_match_ssids &&
  433. (!req->n_ssids ||
  434. (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
  435. type = SCAN_SSID_FILTER_ANY;
  436. goto out;
  437. }
  438. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  439. if (!cmd) {
  440. ret = -ENOMEM;
  441. goto out;
  442. }
  443. if (!n_match_ssids) {
  444. /* No filter, with ssids */
  445. type = SCAN_SSID_FILTER_DISABLED;
  446. for (i = 0; i < req->n_ssids; i++) {
  447. cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
  448. SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
  449. cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
  450. memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
  451. ssids[i].ssid_len);
  452. cmd->n_ssids++;
  453. }
  454. } else {
  455. type = SCAN_SSID_FILTER_LIST;
  456. /* Add all SSIDs from the filters */
  457. for (i = 0; i < req->n_match_sets; i++) {
  458. /* ignore sets without SSIDs */
  459. if (!sets[i].ssid.ssid_len)
  460. continue;
  461. cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
  462. cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
  463. memcpy(cmd->ssids[cmd->n_ssids].ssid,
  464. sets[i].ssid.ssid, sets[i].ssid.ssid_len);
  465. cmd->n_ssids++;
  466. }
  467. if ((req->n_ssids > 1) ||
  468. (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
  469. /*
  470. * Mark all the SSIDs passed in the SSID list as HIDDEN,
  471. * so they're used in probe requests.
  472. */
  473. for (i = 0; i < req->n_ssids; i++) {
  474. for (j = 0; j < cmd->n_ssids; j++)
  475. if (!memcmp(req->ssids[i].ssid,
  476. cmd->ssids[j].ssid,
  477. req->ssids[i].ssid_len)) {
  478. cmd->ssids[j].type =
  479. SCAN_SSID_TYPE_HIDDEN;
  480. break;
  481. }
  482. /* Fail if SSID isn't present in the filters */
  483. if (j == req->n_ssids) {
  484. ret = -EINVAL;
  485. goto out_free;
  486. }
  487. }
  488. }
  489. }
  490. wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
  491. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
  492. sizeof(*cmd), 0);
  493. if (ret < 0) {
  494. wl1271_error("cmd sched scan ssid list failed");
  495. goto out_free;
  496. }
  497. out_free:
  498. kfree(cmd);
  499. out:
  500. if (ret < 0)
  501. return ret;
  502. return type;
  503. }
  504. int wl1271_scan_sched_scan_config(struct wl1271 *wl,
  505. struct wl12xx_vif *wlvif,
  506. struct cfg80211_sched_scan_request *req,
  507. struct ieee80211_sched_scan_ies *ies)
  508. {
  509. struct wl1271_cmd_sched_scan_config *cfg = NULL;
  510. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  511. int i, ret;
  512. bool force_passive = !req->n_ssids;
  513. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  514. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  515. if (!cfg)
  516. return -ENOMEM;
  517. cfg->rssi_threshold = c->rssi_threshold;
  518. cfg->snr_threshold = c->snr_threshold;
  519. cfg->n_probe_reqs = c->num_probe_reqs;
  520. /* cycles set to 0 it means infinite (until manually stopped) */
  521. cfg->cycles = 0;
  522. /* report APs when at least 1 is found */
  523. cfg->report_after = 1;
  524. /* don't stop scanning automatically when something is found */
  525. cfg->terminate = 0;
  526. cfg->tag = WL1271_SCAN_DEFAULT_TAG;
  527. /* don't filter on BSS type */
  528. cfg->bss_type = SCAN_BSS_TYPE_ANY;
  529. /* currently NL80211 supports only a single interval */
  530. for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
  531. cfg->intervals[i] = cpu_to_le32(req->interval);
  532. cfg->ssid_len = 0;
  533. ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
  534. if (ret < 0)
  535. goto out;
  536. cfg->filter_type = ret;
  537. wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
  538. if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
  539. wl1271_error("scan channel list is empty");
  540. ret = -EINVAL;
  541. goto out;
  542. }
  543. if (!force_passive && cfg->active[0]) {
  544. ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
  545. req->ssids[0].ssid_len,
  546. ies->ie[IEEE80211_BAND_2GHZ],
  547. ies->len[IEEE80211_BAND_2GHZ],
  548. IEEE80211_BAND_2GHZ);
  549. if (ret < 0) {
  550. wl1271_error("2.4GHz PROBE request template failed");
  551. goto out;
  552. }
  553. }
  554. if (!force_passive && cfg->active[1]) {
  555. ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
  556. req->ssids[0].ssid_len,
  557. ies->ie[IEEE80211_BAND_5GHZ],
  558. ies->len[IEEE80211_BAND_5GHZ],
  559. IEEE80211_BAND_5GHZ);
  560. if (ret < 0) {
  561. wl1271_error("5GHz PROBE request template failed");
  562. goto out;
  563. }
  564. }
  565. wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
  566. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
  567. sizeof(*cfg), 0);
  568. if (ret < 0) {
  569. wl1271_error("SCAN configuration failed");
  570. goto out;
  571. }
  572. out:
  573. kfree(cfg);
  574. return ret;
  575. }
  576. int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  577. {
  578. struct wl1271_cmd_sched_scan_start *start;
  579. int ret = 0;
  580. wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
  581. if (wlvif->bss_type != BSS_TYPE_STA_BSS)
  582. return -EOPNOTSUPP;
  583. if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
  584. return -EBUSY;
  585. start = kzalloc(sizeof(*start), GFP_KERNEL);
  586. if (!start)
  587. return -ENOMEM;
  588. start->tag = WL1271_SCAN_DEFAULT_TAG;
  589. ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
  590. sizeof(*start), 0);
  591. if (ret < 0) {
  592. wl1271_error("failed to send scan start command");
  593. goto out_free;
  594. }
  595. out_free:
  596. kfree(start);
  597. return ret;
  598. }
  599. void wl1271_scan_sched_scan_results(struct wl1271 *wl)
  600. {
  601. wl1271_debug(DEBUG_SCAN, "got periodic scan results");
  602. ieee80211_sched_scan_results(wl->hw);
  603. }
  604. void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
  605. {
  606. struct wl1271_cmd_sched_scan_stop *stop;
  607. int ret = 0;
  608. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  609. /* FIXME: what to do if alloc'ing to stop fails? */
  610. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  611. if (!stop) {
  612. wl1271_error("failed to alloc memory to send sched scan stop");
  613. return;
  614. }
  615. stop->tag = WL1271_SCAN_DEFAULT_TAG;
  616. ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
  617. sizeof(*stop), 0);
  618. if (ret < 0) {
  619. wl1271_error("failed to send sched scan stop command");
  620. goto out_free;
  621. }
  622. wl->sched_scanning = false;
  623. out_free:
  624. kfree(stop);
  625. }