scan.c 12 KB

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