scan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. void wl1271_scan_complete_work(struct work_struct *work)
  30. {
  31. struct delayed_work *dwork;
  32. struct wl1271 *wl;
  33. dwork = container_of(work, struct delayed_work, work);
  34. wl = container_of(dwork, struct wl1271, scan_complete_work);
  35. wl1271_debug(DEBUG_SCAN, "Scanning complete");
  36. mutex_lock(&wl->mutex);
  37. if (wl->state == WL1271_STATE_OFF)
  38. goto out;
  39. if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
  40. goto out;
  41. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  42. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  43. wl->scan.req = NULL;
  44. ieee80211_scan_completed(wl->hw, false);
  45. /* restore hardware connection monitoring template */
  46. if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
  47. if (wl1271_ps_elp_wakeup(wl) == 0) {
  48. wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
  49. wl1271_ps_elp_sleep(wl);
  50. }
  51. }
  52. if (wl->scan.failed) {
  53. wl1271_info("Scan completed due to error.");
  54. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  55. }
  56. out:
  57. mutex_unlock(&wl->mutex);
  58. }
  59. static int wl1271_get_scan_channels(struct wl1271 *wl,
  60. struct cfg80211_scan_request *req,
  61. struct basic_scan_channel_params *channels,
  62. enum ieee80211_band band, bool passive)
  63. {
  64. struct conf_scan_settings *c = &wl->conf.scan;
  65. int i, j;
  66. u32 flags;
  67. for (i = 0, j = 0;
  68. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  69. i++) {
  70. flags = req->channels[i]->flags;
  71. if (!test_bit(i, wl->scan.scanned_ch) &&
  72. !(flags & IEEE80211_CHAN_DISABLED) &&
  73. (req->channels[i]->band == band) &&
  74. /*
  75. * In passive scans, we scan all remaining
  76. * channels, even if not marked as such.
  77. * In active scans, we only scan channels not
  78. * marked as passive.
  79. */
  80. (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
  81. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  82. req->channels[i]->band,
  83. req->channels[i]->center_freq);
  84. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  85. req->channels[i]->hw_value,
  86. req->channels[i]->flags);
  87. wl1271_debug(DEBUG_SCAN,
  88. "max_antenna_gain %d, max_power %d",
  89. req->channels[i]->max_antenna_gain,
  90. req->channels[i]->max_power);
  91. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  92. req->channels[i]->beacon_found);
  93. if (!passive) {
  94. channels[j].min_duration =
  95. cpu_to_le32(c->min_dwell_time_active);
  96. channels[j].max_duration =
  97. cpu_to_le32(c->max_dwell_time_active);
  98. } else {
  99. channels[j].min_duration =
  100. cpu_to_le32(c->min_dwell_time_passive);
  101. channels[j].max_duration =
  102. cpu_to_le32(c->max_dwell_time_passive);
  103. }
  104. channels[j].early_termination = 0;
  105. channels[j].tx_power_att = req->channels[i]->max_power;
  106. channels[j].channel = req->channels[i]->hw_value;
  107. memset(&channels[j].bssid_lsb, 0xff, 4);
  108. memset(&channels[j].bssid_msb, 0xff, 2);
  109. /* Mark the channels we already used */
  110. set_bit(i, wl->scan.scanned_ch);
  111. j++;
  112. }
  113. }
  114. return j;
  115. }
  116. #define WL1271_NOTHING_TO_SCAN 1
  117. static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
  118. bool passive, u32 basic_rate)
  119. {
  120. struct wl1271_cmd_scan *cmd;
  121. struct wl1271_cmd_trigger_scan_to *trigger;
  122. int ret;
  123. u16 scan_options = 0;
  124. /* skip active scans if we don't have SSIDs */
  125. if (!passive && wl->scan.req->n_ssids == 0)
  126. return WL1271_NOTHING_TO_SCAN;
  127. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  128. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  129. if (!cmd || !trigger) {
  130. ret = -ENOMEM;
  131. goto out;
  132. }
  133. /* We always use high priority scans */
  134. scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
  135. if (passive)
  136. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  137. cmd->params.scan_options = cpu_to_le16(scan_options);
  138. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  139. cmd->channels,
  140. band, passive);
  141. if (cmd->params.n_ch == 0) {
  142. ret = WL1271_NOTHING_TO_SCAN;
  143. goto out;
  144. }
  145. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  146. cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  147. cmd->params.rx_filter_options =
  148. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  149. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  150. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  151. cmd->params.tid_trigger = 0;
  152. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  153. if (band == IEEE80211_BAND_2GHZ)
  154. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  155. else
  156. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  157. if (wl->scan.ssid_len && wl->scan.ssid) {
  158. cmd->params.ssid_len = wl->scan.ssid_len;
  159. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  160. }
  161. ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
  162. wl->scan.req->ie, wl->scan.req->ie_len,
  163. band);
  164. if (ret < 0) {
  165. wl1271_error("PROBE request template failed");
  166. goto out;
  167. }
  168. /* disable the timeout */
  169. trigger->timeout = 0;
  170. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  171. sizeof(*trigger), 0);
  172. if (ret < 0) {
  173. wl1271_error("trigger scan to failed for hw scan");
  174. goto out;
  175. }
  176. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  177. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  178. if (ret < 0) {
  179. wl1271_error("SCAN failed");
  180. goto out;
  181. }
  182. out:
  183. kfree(cmd);
  184. kfree(trigger);
  185. return ret;
  186. }
  187. void wl1271_scan_stm(struct wl1271 *wl)
  188. {
  189. int ret = 0;
  190. switch (wl->scan.state) {
  191. case WL1271_SCAN_STATE_IDLE:
  192. break;
  193. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  194. ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
  195. wl->conf.tx.basic_rate);
  196. if (ret == WL1271_NOTHING_TO_SCAN) {
  197. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  198. wl1271_scan_stm(wl);
  199. }
  200. break;
  201. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  202. ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
  203. wl->conf.tx.basic_rate);
  204. if (ret == WL1271_NOTHING_TO_SCAN) {
  205. if (wl->enable_11a)
  206. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  207. else
  208. wl->scan.state = WL1271_SCAN_STATE_DONE;
  209. wl1271_scan_stm(wl);
  210. }
  211. break;
  212. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  213. ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
  214. wl->conf.tx.basic_rate_5);
  215. if (ret == WL1271_NOTHING_TO_SCAN) {
  216. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  217. wl1271_scan_stm(wl);
  218. }
  219. break;
  220. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  221. ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
  222. wl->conf.tx.basic_rate_5);
  223. if (ret == WL1271_NOTHING_TO_SCAN) {
  224. wl->scan.state = WL1271_SCAN_STATE_DONE;
  225. wl1271_scan_stm(wl);
  226. }
  227. break;
  228. case WL1271_SCAN_STATE_DONE:
  229. wl->scan.failed = false;
  230. cancel_delayed_work(&wl->scan_complete_work);
  231. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  232. msecs_to_jiffies(0));
  233. break;
  234. default:
  235. wl1271_error("invalid scan state");
  236. break;
  237. }
  238. if (ret < 0) {
  239. cancel_delayed_work(&wl->scan_complete_work);
  240. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  241. msecs_to_jiffies(0));
  242. }
  243. }
  244. int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
  245. struct cfg80211_scan_request *req)
  246. {
  247. /*
  248. * cfg80211 should guarantee that we don't get more channels
  249. * than what we have registered.
  250. */
  251. BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
  252. if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
  253. return -EBUSY;
  254. wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
  255. if (ssid_len && ssid) {
  256. wl->scan.ssid_len = ssid_len;
  257. memcpy(wl->scan.ssid, ssid, ssid_len);
  258. } else {
  259. wl->scan.ssid_len = 0;
  260. }
  261. wl->scan.req = req;
  262. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  263. /* we assume failure so that timeout scenarios are handled correctly */
  264. wl->scan.failed = true;
  265. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  266. msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
  267. wl1271_scan_stm(wl);
  268. return 0;
  269. }
  270. static int
  271. wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
  272. struct cfg80211_sched_scan_request *req,
  273. struct conn_scan_ch_params *channels,
  274. u32 band, bool radar, bool passive,
  275. int start)
  276. {
  277. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  278. int i, j;
  279. u32 flags;
  280. bool force_passive = !req->n_ssids;
  281. for (i = 0, j = start;
  282. i < req->n_channels && j < MAX_CHANNELS_ALL_BANDS;
  283. i++) {
  284. flags = req->channels[i]->flags;
  285. if (force_passive)
  286. flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  287. if ((req->channels[i]->band == band) &&
  288. !(flags & IEEE80211_CHAN_DISABLED) &&
  289. (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
  290. /* if radar is set, we ignore the passive flag */
  291. (radar ||
  292. !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
  293. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  294. req->channels[i]->band,
  295. req->channels[i]->center_freq);
  296. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  297. req->channels[i]->hw_value,
  298. req->channels[i]->flags);
  299. wl1271_debug(DEBUG_SCAN, "max_power %d",
  300. req->channels[i]->max_power);
  301. if (flags & IEEE80211_CHAN_RADAR) {
  302. channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
  303. channels[j].passive_duration =
  304. cpu_to_le16(c->dwell_time_dfs);
  305. }
  306. else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  307. channels[j].passive_duration =
  308. cpu_to_le16(c->dwell_time_passive);
  309. } else {
  310. channels[j].min_duration =
  311. cpu_to_le16(c->min_dwell_time_active);
  312. channels[j].max_duration =
  313. cpu_to_le16(c->max_dwell_time_active);
  314. }
  315. channels[j].tx_power_att = req->channels[i]->max_power;
  316. channels[j].channel = req->channels[i]->hw_value;
  317. j++;
  318. }
  319. }
  320. return j - start;
  321. }
  322. static int
  323. wl1271_scan_sched_scan_channels(struct wl1271 *wl,
  324. struct cfg80211_sched_scan_request *req,
  325. struct wl1271_cmd_sched_scan_config *cfg)
  326. {
  327. int idx = 0;
  328. cfg->passive[0] =
  329. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels,
  330. IEEE80211_BAND_2GHZ,
  331. false, true, idx);
  332. idx += cfg->passive[0];
  333. cfg->active[0] =
  334. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels,
  335. IEEE80211_BAND_2GHZ,
  336. false, false, idx);
  337. /*
  338. * 5GHz channels always start at position 14, not immediately
  339. * after the last 2.4GHz channel
  340. */
  341. idx = 14;
  342. cfg->passive[1] =
  343. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels,
  344. IEEE80211_BAND_5GHZ,
  345. false, true, idx);
  346. idx += cfg->passive[1];
  347. cfg->dfs =
  348. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels,
  349. IEEE80211_BAND_5GHZ,
  350. true, true, idx);
  351. idx += cfg->dfs;
  352. cfg->active[1] =
  353. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels,
  354. IEEE80211_BAND_5GHZ,
  355. false, false, idx);
  356. idx += cfg->active[1];
  357. wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
  358. cfg->active[0], cfg->passive[0]);
  359. wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
  360. cfg->active[1], cfg->passive[1]);
  361. wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
  362. return idx;
  363. }
  364. int wl1271_scan_sched_scan_config(struct wl1271 *wl,
  365. struct cfg80211_sched_scan_request *req,
  366. struct ieee80211_sched_scan_ies *ies)
  367. {
  368. struct wl1271_cmd_sched_scan_config *cfg = NULL;
  369. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  370. int i, total_channels, ret;
  371. bool force_passive = !req->n_ssids;
  372. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  373. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  374. if (!cfg)
  375. return -ENOMEM;
  376. cfg->rssi_threshold = c->rssi_threshold;
  377. cfg->snr_threshold = c->snr_threshold;
  378. cfg->n_probe_reqs = c->num_probe_reqs;
  379. /* cycles set to 0 it means infinite (until manually stopped) */
  380. cfg->cycles = 0;
  381. /* report APs when at least 1 is found */
  382. cfg->report_after = 1;
  383. /* don't stop scanning automatically when something is found */
  384. cfg->terminate = 0;
  385. cfg->tag = WL1271_SCAN_DEFAULT_TAG;
  386. /* don't filter on BSS type */
  387. cfg->bss_type = SCAN_BSS_TYPE_ANY;
  388. /* currently NL80211 supports only a single interval */
  389. for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
  390. cfg->intervals[i] = cpu_to_le32(req->interval);
  391. if (!force_passive && req->ssids[0].ssid_len && req->ssids[0].ssid) {
  392. cfg->filter_type = SCAN_SSID_FILTER_SPECIFIC;
  393. cfg->ssid_len = req->ssids[0].ssid_len;
  394. memcpy(cfg->ssid, req->ssids[0].ssid,
  395. req->ssids[0].ssid_len);
  396. } else {
  397. cfg->filter_type = SCAN_SSID_FILTER_ANY;
  398. cfg->ssid_len = 0;
  399. }
  400. total_channels = wl1271_scan_sched_scan_channels(wl, req, cfg);
  401. if (total_channels == 0) {
  402. wl1271_error("scan channel list is empty");
  403. ret = -EINVAL;
  404. goto out;
  405. }
  406. if (!force_passive && cfg->active[0]) {
  407. ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
  408. req->ssids[0].ssid_len,
  409. ies->ie[IEEE80211_BAND_2GHZ],
  410. ies->len[IEEE80211_BAND_2GHZ],
  411. IEEE80211_BAND_2GHZ);
  412. if (ret < 0) {
  413. wl1271_error("2.4GHz PROBE request template failed");
  414. goto out;
  415. }
  416. }
  417. if (!force_passive && cfg->active[1]) {
  418. ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
  419. req->ssids[0].ssid_len,
  420. ies->ie[IEEE80211_BAND_5GHZ],
  421. ies->len[IEEE80211_BAND_5GHZ],
  422. IEEE80211_BAND_5GHZ);
  423. if (ret < 0) {
  424. wl1271_error("5GHz PROBE request template failed");
  425. goto out;
  426. }
  427. }
  428. wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
  429. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
  430. sizeof(*cfg), 0);
  431. if (ret < 0) {
  432. wl1271_error("SCAN configuration failed");
  433. goto out;
  434. }
  435. out:
  436. kfree(cfg);
  437. return ret;
  438. }
  439. int wl1271_scan_sched_scan_start(struct wl1271 *wl)
  440. {
  441. struct wl1271_cmd_sched_scan_start *start;
  442. int ret = 0;
  443. wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
  444. if (wl->bss_type != BSS_TYPE_STA_BSS)
  445. return -EOPNOTSUPP;
  446. if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
  447. return -EBUSY;
  448. start = kzalloc(sizeof(*start), GFP_KERNEL);
  449. if (!start)
  450. return -ENOMEM;
  451. start->tag = WL1271_SCAN_DEFAULT_TAG;
  452. ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
  453. sizeof(*start), 0);
  454. if (ret < 0) {
  455. wl1271_error("failed to send scan start command");
  456. goto out_free;
  457. }
  458. out_free:
  459. kfree(start);
  460. return ret;
  461. }
  462. void wl1271_scan_sched_scan_results(struct wl1271 *wl)
  463. {
  464. wl1271_debug(DEBUG_SCAN, "got periodic scan results");
  465. ieee80211_sched_scan_results(wl->hw);
  466. }
  467. void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
  468. {
  469. struct wl1271_cmd_sched_scan_stop *stop;
  470. int ret = 0;
  471. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  472. /* FIXME: what to do if alloc'ing to stop fails? */
  473. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  474. if (!stop) {
  475. wl1271_error("failed to alloc memory to send sched scan stop");
  476. return;
  477. }
  478. stop->tag = WL1271_SCAN_DEFAULT_TAG;
  479. ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
  480. sizeof(*stop), 0);
  481. if (ret < 0) {
  482. wl1271_error("failed to send sched scan stop command");
  483. goto out_free;
  484. }
  485. wl->sched_scanning = false;
  486. out_free:
  487. kfree(stop);
  488. }