scan.c 11 KB

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