scan.c 12 KB

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