scan.c 15 KB

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