scan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 "wlcore.h"
  25. #include "debug.h"
  26. #include "cmd.h"
  27. #include "scan.h"
  28. #include "acx.h"
  29. #include "ps.h"
  30. #include "tx.h"
  31. void wl1271_scan_complete_work(struct work_struct *work)
  32. {
  33. struct delayed_work *dwork;
  34. struct wl1271 *wl;
  35. struct wl12xx_vif *wlvif;
  36. struct cfg80211_scan_info info = {
  37. .aborted = false,
  38. };
  39. int ret;
  40. dwork = to_delayed_work(work);
  41. wl = container_of(dwork, struct wl1271, scan_complete_work);
  42. wl1271_debug(DEBUG_SCAN, "Scanning complete");
  43. mutex_lock(&wl->mutex);
  44. if (unlikely(wl->state != WLCORE_STATE_ON))
  45. goto out;
  46. if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
  47. goto out;
  48. wlvif = wl->scan_wlvif;
  49. /*
  50. * Rearm the tx watchdog just before idling scan. This
  51. * prevents just-finished scans from triggering the watchdog
  52. */
  53. wl12xx_rearm_tx_watchdog_locked(wl);
  54. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  55. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  56. wl->scan.req = NULL;
  57. wl->scan_wlvif = NULL;
  58. ret = wl1271_ps_elp_wakeup(wl);
  59. if (ret < 0)
  60. goto out;
  61. if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
  62. /* restore hardware connection monitoring template */
  63. wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
  64. }
  65. wl1271_ps_elp_sleep(wl);
  66. if (wl->scan.failed) {
  67. wl1271_info("Scan completed due to error.");
  68. wl12xx_queue_recovery_work(wl);
  69. }
  70. wlcore_cmd_regdomain_config_locked(wl);
  71. ieee80211_scan_completed(wl->hw, &info);
  72. out:
  73. mutex_unlock(&wl->mutex);
  74. }
  75. static void wlcore_started_vifs_iter(void *data, u8 *mac,
  76. struct ieee80211_vif *vif)
  77. {
  78. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  79. bool active = false;
  80. int *count = (int *)data;
  81. /*
  82. * count active interfaces according to interface type.
  83. * checking only bss_conf.idle is bad for some cases, e.g.
  84. * we don't want to count sta in p2p_find as active interface.
  85. */
  86. switch (wlvif->bss_type) {
  87. case BSS_TYPE_STA_BSS:
  88. if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
  89. active = true;
  90. break;
  91. case BSS_TYPE_AP_BSS:
  92. if (wlvif->wl->active_sta_count > 0)
  93. active = true;
  94. break;
  95. default:
  96. break;
  97. }
  98. if (active)
  99. (*count)++;
  100. }
  101. static int wlcore_count_started_vifs(struct wl1271 *wl)
  102. {
  103. int count = 0;
  104. ieee80211_iterate_active_interfaces_atomic(wl->hw,
  105. IEEE80211_IFACE_ITER_RESUME_ALL,
  106. wlcore_started_vifs_iter, &count);
  107. return count;
  108. }
  109. static int
  110. wlcore_scan_get_channels(struct wl1271 *wl,
  111. struct ieee80211_channel *req_channels[],
  112. u32 n_channels,
  113. u32 n_ssids,
  114. struct conn_scan_ch_params *channels,
  115. u32 band, bool radar, bool passive,
  116. int start, int max_channels,
  117. u8 *n_pactive_ch,
  118. int scan_type)
  119. {
  120. int i, j;
  121. u32 flags;
  122. bool force_passive = !n_ssids;
  123. u32 min_dwell_time_active, max_dwell_time_active;
  124. u32 dwell_time_passive, dwell_time_dfs;
  125. /* configure dwell times according to scan type */
  126. if (scan_type == SCAN_TYPE_SEARCH) {
  127. struct conf_scan_settings *c = &wl->conf.scan;
  128. bool active_vif_exists = !!wlcore_count_started_vifs(wl);
  129. min_dwell_time_active = active_vif_exists ?
  130. c->min_dwell_time_active :
  131. c->min_dwell_time_active_long;
  132. max_dwell_time_active = active_vif_exists ?
  133. c->max_dwell_time_active :
  134. c->max_dwell_time_active_long;
  135. dwell_time_passive = c->dwell_time_passive;
  136. dwell_time_dfs = c->dwell_time_dfs;
  137. } else {
  138. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  139. u32 delta_per_probe;
  140. if (band == NL80211_BAND_5GHZ)
  141. delta_per_probe = c->dwell_time_delta_per_probe_5;
  142. else
  143. delta_per_probe = c->dwell_time_delta_per_probe;
  144. min_dwell_time_active = c->base_dwell_time +
  145. n_ssids * c->num_probe_reqs * delta_per_probe;
  146. max_dwell_time_active = min_dwell_time_active +
  147. c->max_dwell_time_delta;
  148. dwell_time_passive = c->dwell_time_passive;
  149. dwell_time_dfs = c->dwell_time_dfs;
  150. }
  151. min_dwell_time_active = DIV_ROUND_UP(min_dwell_time_active, 1000);
  152. max_dwell_time_active = DIV_ROUND_UP(max_dwell_time_active, 1000);
  153. dwell_time_passive = DIV_ROUND_UP(dwell_time_passive, 1000);
  154. dwell_time_dfs = DIV_ROUND_UP(dwell_time_dfs, 1000);
  155. for (i = 0, j = start;
  156. i < n_channels && j < max_channels;
  157. i++) {
  158. flags = req_channels[i]->flags;
  159. if (force_passive)
  160. flags |= IEEE80211_CHAN_NO_IR;
  161. if ((req_channels[i]->band == band) &&
  162. !(flags & IEEE80211_CHAN_DISABLED) &&
  163. (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
  164. /* if radar is set, we ignore the passive flag */
  165. (radar ||
  166. !!(flags & IEEE80211_CHAN_NO_IR) == passive)) {
  167. if (flags & IEEE80211_CHAN_RADAR) {
  168. channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
  169. channels[j].passive_duration =
  170. cpu_to_le16(dwell_time_dfs);
  171. } else {
  172. channels[j].passive_duration =
  173. cpu_to_le16(dwell_time_passive);
  174. }
  175. channels[j].min_duration =
  176. cpu_to_le16(min_dwell_time_active);
  177. channels[j].max_duration =
  178. cpu_to_le16(max_dwell_time_active);
  179. channels[j].tx_power_att = req_channels[i]->max_power;
  180. channels[j].channel = req_channels[i]->hw_value;
  181. if (n_pactive_ch &&
  182. (band == NL80211_BAND_2GHZ) &&
  183. (channels[j].channel >= 12) &&
  184. (channels[j].channel <= 14) &&
  185. (flags & IEEE80211_CHAN_NO_IR) &&
  186. !force_passive) {
  187. /* pactive channels treated as DFS */
  188. channels[j].flags = SCAN_CHANNEL_FLAGS_DFS;
  189. /*
  190. * n_pactive_ch is counted down from the end of
  191. * the passive channel list
  192. */
  193. (*n_pactive_ch)++;
  194. wl1271_debug(DEBUG_SCAN, "n_pactive_ch = %d",
  195. *n_pactive_ch);
  196. }
  197. wl1271_debug(DEBUG_SCAN, "freq %d, ch. %d, flags 0x%x, power %d, min/max_dwell %d/%d%s%s",
  198. req_channels[i]->center_freq,
  199. req_channels[i]->hw_value,
  200. req_channels[i]->flags,
  201. req_channels[i]->max_power,
  202. min_dwell_time_active,
  203. max_dwell_time_active,
  204. flags & IEEE80211_CHAN_RADAR ?
  205. ", DFS" : "",
  206. flags & IEEE80211_CHAN_NO_IR ?
  207. ", NO-IR" : "");
  208. j++;
  209. }
  210. }
  211. return j - start;
  212. }
  213. bool
  214. wlcore_set_scan_chan_params(struct wl1271 *wl,
  215. struct wlcore_scan_channels *cfg,
  216. struct ieee80211_channel *channels[],
  217. u32 n_channels,
  218. u32 n_ssids,
  219. int scan_type)
  220. {
  221. u8 n_pactive_ch = 0;
  222. cfg->passive[0] =
  223. wlcore_scan_get_channels(wl,
  224. channels,
  225. n_channels,
  226. n_ssids,
  227. cfg->channels_2,
  228. NL80211_BAND_2GHZ,
  229. false, true, 0,
  230. MAX_CHANNELS_2GHZ,
  231. &n_pactive_ch,
  232. scan_type);
  233. cfg->active[0] =
  234. wlcore_scan_get_channels(wl,
  235. channels,
  236. n_channels,
  237. n_ssids,
  238. cfg->channels_2,
  239. NL80211_BAND_2GHZ,
  240. false, false,
  241. cfg->passive[0],
  242. MAX_CHANNELS_2GHZ,
  243. &n_pactive_ch,
  244. scan_type);
  245. cfg->passive[1] =
  246. wlcore_scan_get_channels(wl,
  247. channels,
  248. n_channels,
  249. n_ssids,
  250. cfg->channels_5,
  251. NL80211_BAND_5GHZ,
  252. false, true, 0,
  253. wl->max_channels_5,
  254. &n_pactive_ch,
  255. scan_type);
  256. cfg->dfs =
  257. wlcore_scan_get_channels(wl,
  258. channels,
  259. n_channels,
  260. n_ssids,
  261. cfg->channels_5,
  262. NL80211_BAND_5GHZ,
  263. true, true,
  264. cfg->passive[1],
  265. wl->max_channels_5,
  266. &n_pactive_ch,
  267. scan_type);
  268. cfg->active[1] =
  269. wlcore_scan_get_channels(wl,
  270. channels,
  271. n_channels,
  272. n_ssids,
  273. cfg->channels_5,
  274. NL80211_BAND_5GHZ,
  275. false, false,
  276. cfg->passive[1] + cfg->dfs,
  277. wl->max_channels_5,
  278. &n_pactive_ch,
  279. scan_type);
  280. /* 802.11j channels are not supported yet */
  281. cfg->passive[2] = 0;
  282. cfg->active[2] = 0;
  283. cfg->passive_active = n_pactive_ch;
  284. wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
  285. cfg->active[0], cfg->passive[0]);
  286. wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
  287. cfg->active[1], cfg->passive[1]);
  288. wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
  289. return cfg->passive[0] || cfg->active[0] ||
  290. cfg->passive[1] || cfg->active[1] || cfg->dfs ||
  291. cfg->passive[2] || cfg->active[2];
  292. }
  293. EXPORT_SYMBOL_GPL(wlcore_set_scan_chan_params);
  294. int wlcore_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
  295. const u8 *ssid, size_t ssid_len,
  296. struct cfg80211_scan_request *req)
  297. {
  298. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  299. /*
  300. * cfg80211 should guarantee that we don't get more channels
  301. * than what we have registered.
  302. */
  303. BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
  304. if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
  305. return -EBUSY;
  306. wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
  307. if (ssid_len && ssid) {
  308. wl->scan.ssid_len = ssid_len;
  309. memcpy(wl->scan.ssid, ssid, ssid_len);
  310. } else {
  311. wl->scan.ssid_len = 0;
  312. }
  313. wl->scan_wlvif = wlvif;
  314. wl->scan.req = req;
  315. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  316. /* we assume failure so that timeout scenarios are handled correctly */
  317. wl->scan.failed = true;
  318. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  319. msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
  320. wl->ops->scan_start(wl, wlvif, req);
  321. return 0;
  322. }
  323. /* Returns the scan type to be used or a negative value on error */
  324. int
  325. wlcore_scan_sched_scan_ssid_list(struct wl1271 *wl,
  326. struct wl12xx_vif *wlvif,
  327. struct cfg80211_sched_scan_request *req)
  328. {
  329. struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
  330. struct cfg80211_match_set *sets = req->match_sets;
  331. struct cfg80211_ssid *ssids = req->ssids;
  332. int ret = 0, type, i, j, n_match_ssids = 0;
  333. wl1271_debug((DEBUG_CMD | DEBUG_SCAN), "cmd sched scan ssid list");
  334. /* count the match sets that contain SSIDs */
  335. for (i = 0; i < req->n_match_sets; i++)
  336. if (sets[i].ssid.ssid_len > 0)
  337. n_match_ssids++;
  338. /* No filter, no ssids or only bcast ssid */
  339. if (!n_match_ssids &&
  340. (!req->n_ssids ||
  341. (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
  342. type = SCAN_SSID_FILTER_ANY;
  343. goto out;
  344. }
  345. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  346. if (!cmd) {
  347. ret = -ENOMEM;
  348. goto out;
  349. }
  350. cmd->role_id = wlvif->role_id;
  351. if (!n_match_ssids) {
  352. /* No filter, with ssids */
  353. type = SCAN_SSID_FILTER_DISABLED;
  354. for (i = 0; i < req->n_ssids; i++) {
  355. cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
  356. SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
  357. cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
  358. memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
  359. ssids[i].ssid_len);
  360. cmd->n_ssids++;
  361. }
  362. } else {
  363. type = SCAN_SSID_FILTER_LIST;
  364. /* Add all SSIDs from the filters */
  365. for (i = 0; i < req->n_match_sets; i++) {
  366. /* ignore sets without SSIDs */
  367. if (!sets[i].ssid.ssid_len)
  368. continue;
  369. cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
  370. cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
  371. memcpy(cmd->ssids[cmd->n_ssids].ssid,
  372. sets[i].ssid.ssid, sets[i].ssid.ssid_len);
  373. cmd->n_ssids++;
  374. }
  375. if ((req->n_ssids > 1) ||
  376. (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
  377. /*
  378. * Mark all the SSIDs passed in the SSID list as HIDDEN,
  379. * so they're used in probe requests.
  380. */
  381. for (i = 0; i < req->n_ssids; i++) {
  382. if (!req->ssids[i].ssid_len)
  383. continue;
  384. for (j = 0; j < cmd->n_ssids; j++)
  385. if ((req->ssids[i].ssid_len ==
  386. cmd->ssids[j].len) &&
  387. !memcmp(req->ssids[i].ssid,
  388. cmd->ssids[j].ssid,
  389. req->ssids[i].ssid_len)) {
  390. cmd->ssids[j].type =
  391. SCAN_SSID_TYPE_HIDDEN;
  392. break;
  393. }
  394. /* Fail if SSID isn't present in the filters */
  395. if (j == cmd->n_ssids) {
  396. ret = -EINVAL;
  397. goto out_free;
  398. }
  399. }
  400. }
  401. }
  402. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
  403. sizeof(*cmd), 0);
  404. if (ret < 0) {
  405. wl1271_error("cmd sched scan ssid list failed");
  406. goto out_free;
  407. }
  408. out_free:
  409. kfree(cmd);
  410. out:
  411. if (ret < 0)
  412. return ret;
  413. return type;
  414. }
  415. EXPORT_SYMBOL_GPL(wlcore_scan_sched_scan_ssid_list);
  416. void wlcore_scan_sched_scan_results(struct wl1271 *wl)
  417. {
  418. wl1271_debug(DEBUG_SCAN, "got periodic scan results");
  419. ieee80211_sched_scan_results(wl->hw);
  420. }
  421. EXPORT_SYMBOL_GPL(wlcore_scan_sched_scan_results);