scan.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 ieee80211_vif *vif;
  36. struct wl12xx_vif *wlvif;
  37. int ret;
  38. dwork = container_of(work, struct delayed_work, work);
  39. wl = container_of(dwork, struct wl1271, scan_complete_work);
  40. wl1271_debug(DEBUG_SCAN, "Scanning complete");
  41. mutex_lock(&wl->mutex);
  42. if (wl->state == WL1271_STATE_OFF)
  43. goto out;
  44. if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
  45. goto out;
  46. vif = wl->scan_vif;
  47. wlvif = wl12xx_vif_to_data(vif);
  48. /*
  49. * Rearm the tx watchdog just before idling scan. This
  50. * prevents just-finished scans from triggering the watchdog
  51. */
  52. wl12xx_rearm_tx_watchdog_locked(wl);
  53. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  54. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  55. wl->scan.req = NULL;
  56. wl->scan_vif = NULL;
  57. ret = wl1271_ps_elp_wakeup(wl);
  58. if (ret < 0)
  59. goto out;
  60. if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
  61. /* restore hardware connection monitoring template */
  62. wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
  63. }
  64. wl1271_ps_elp_sleep(wl);
  65. if (wl->scan.failed) {
  66. wl1271_info("Scan completed due to error.");
  67. wl12xx_queue_recovery_work(wl);
  68. }
  69. ieee80211_scan_completed(wl->hw, false);
  70. out:
  71. mutex_unlock(&wl->mutex);
  72. }
  73. static int wl1271_get_scan_channels(struct wl1271 *wl,
  74. struct cfg80211_scan_request *req,
  75. struct basic_scan_channel_params *channels,
  76. enum ieee80211_band band, bool passive)
  77. {
  78. struct conf_scan_settings *c = &wl->conf.scan;
  79. int i, j;
  80. u32 flags;
  81. for (i = 0, j = 0;
  82. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  83. i++) {
  84. flags = req->channels[i]->flags;
  85. if (!test_bit(i, wl->scan.scanned_ch) &&
  86. !(flags & IEEE80211_CHAN_DISABLED) &&
  87. (req->channels[i]->band == band) &&
  88. /*
  89. * In passive scans, we scan all remaining
  90. * channels, even if not marked as such.
  91. * In active scans, we only scan channels not
  92. * marked as passive.
  93. */
  94. (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
  95. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  96. req->channels[i]->band,
  97. req->channels[i]->center_freq);
  98. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  99. req->channels[i]->hw_value,
  100. req->channels[i]->flags);
  101. wl1271_debug(DEBUG_SCAN,
  102. "max_antenna_gain %d, max_power %d",
  103. req->channels[i]->max_antenna_gain,
  104. req->channels[i]->max_power);
  105. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  106. req->channels[i]->beacon_found);
  107. if (!passive) {
  108. channels[j].min_duration =
  109. cpu_to_le32(c->min_dwell_time_active);
  110. channels[j].max_duration =
  111. cpu_to_le32(c->max_dwell_time_active);
  112. } else {
  113. channels[j].min_duration =
  114. cpu_to_le32(c->min_dwell_time_passive);
  115. channels[j].max_duration =
  116. cpu_to_le32(c->max_dwell_time_passive);
  117. }
  118. channels[j].early_termination = 0;
  119. channels[j].tx_power_att = req->channels[i]->max_power;
  120. channels[j].channel = req->channels[i]->hw_value;
  121. memset(&channels[j].bssid_lsb, 0xff, 4);
  122. memset(&channels[j].bssid_msb, 0xff, 2);
  123. /* Mark the channels we already used */
  124. set_bit(i, wl->scan.scanned_ch);
  125. j++;
  126. }
  127. }
  128. return j;
  129. }
  130. #define WL1271_NOTHING_TO_SCAN 1
  131. static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
  132. enum ieee80211_band band,
  133. bool passive, u32 basic_rate)
  134. {
  135. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  136. struct wl1271_cmd_scan *cmd;
  137. struct wl1271_cmd_trigger_scan_to *trigger;
  138. int ret;
  139. u16 scan_options = 0;
  140. /* skip active scans if we don't have SSIDs */
  141. if (!passive && wl->scan.req->n_ssids == 0)
  142. return WL1271_NOTHING_TO_SCAN;
  143. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  144. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  145. if (!cmd || !trigger) {
  146. ret = -ENOMEM;
  147. goto out;
  148. }
  149. if (wl->conf.scan.split_scan_timeout)
  150. scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;
  151. if (passive)
  152. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  153. if (wlvif->bss_type == BSS_TYPE_AP_BSS ||
  154. test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
  155. cmd->params.role_id = wlvif->role_id;
  156. else
  157. cmd->params.role_id = wlvif->dev_role_id;
  158. if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
  159. ret = -EINVAL;
  160. goto out;
  161. }
  162. cmd->params.scan_options = cpu_to_le16(scan_options);
  163. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  164. cmd->channels,
  165. band, passive);
  166. if (cmd->params.n_ch == 0) {
  167. ret = WL1271_NOTHING_TO_SCAN;
  168. goto out;
  169. }
  170. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  171. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  172. cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
  173. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  174. if (band == IEEE80211_BAND_2GHZ)
  175. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  176. else
  177. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  178. if (wl->scan.ssid_len && wl->scan.ssid) {
  179. cmd->params.ssid_len = wl->scan.ssid_len;
  180. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  181. }
  182. memcpy(cmd->addr, vif->addr, ETH_ALEN);
  183. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  184. cmd->params.role_id, band,
  185. wl->scan.ssid, wl->scan.ssid_len,
  186. wl->scan.req->ie,
  187. wl->scan.req->ie_len, false);
  188. if (ret < 0) {
  189. wl1271_error("PROBE request template failed");
  190. goto out;
  191. }
  192. trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
  193. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  194. sizeof(*trigger), 0);
  195. if (ret < 0) {
  196. wl1271_error("trigger scan to failed for hw scan");
  197. goto out;
  198. }
  199. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  200. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  201. if (ret < 0) {
  202. wl1271_error("SCAN failed");
  203. goto out;
  204. }
  205. out:
  206. kfree(cmd);
  207. kfree(trigger);
  208. return ret;
  209. }
  210. void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
  211. {
  212. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  213. int ret = 0;
  214. enum ieee80211_band band;
  215. u32 rate, mask;
  216. switch (wl->scan.state) {
  217. case WL1271_SCAN_STATE_IDLE:
  218. break;
  219. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  220. band = IEEE80211_BAND_2GHZ;
  221. mask = wlvif->bitrate_masks[band];
  222. if (wl->scan.req->no_cck) {
  223. mask &= ~CONF_TX_CCK_RATES;
  224. if (!mask)
  225. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  226. }
  227. rate = wl1271_tx_min_rate_get(wl, mask);
  228. ret = wl1271_scan_send(wl, vif, band, false, rate);
  229. if (ret == WL1271_NOTHING_TO_SCAN) {
  230. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  231. wl1271_scan_stm(wl, vif);
  232. }
  233. break;
  234. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  235. band = IEEE80211_BAND_2GHZ;
  236. mask = wlvif->bitrate_masks[band];
  237. if (wl->scan.req->no_cck) {
  238. mask &= ~CONF_TX_CCK_RATES;
  239. if (!mask)
  240. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  241. }
  242. rate = wl1271_tx_min_rate_get(wl, mask);
  243. ret = wl1271_scan_send(wl, vif, band, true, rate);
  244. if (ret == WL1271_NOTHING_TO_SCAN) {
  245. if (wl->enable_11a)
  246. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  247. else
  248. wl->scan.state = WL1271_SCAN_STATE_DONE;
  249. wl1271_scan_stm(wl, vif);
  250. }
  251. break;
  252. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  253. band = IEEE80211_BAND_5GHZ;
  254. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  255. ret = wl1271_scan_send(wl, vif, band, false, rate);
  256. if (ret == WL1271_NOTHING_TO_SCAN) {
  257. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  258. wl1271_scan_stm(wl, vif);
  259. }
  260. break;
  261. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  262. band = IEEE80211_BAND_5GHZ;
  263. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  264. ret = wl1271_scan_send(wl, vif, band, true, rate);
  265. if (ret == WL1271_NOTHING_TO_SCAN) {
  266. wl->scan.state = WL1271_SCAN_STATE_DONE;
  267. wl1271_scan_stm(wl, vif);
  268. }
  269. break;
  270. case WL1271_SCAN_STATE_DONE:
  271. wl->scan.failed = false;
  272. cancel_delayed_work(&wl->scan_complete_work);
  273. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  274. msecs_to_jiffies(0));
  275. break;
  276. default:
  277. wl1271_error("invalid scan state");
  278. break;
  279. }
  280. if (ret < 0) {
  281. cancel_delayed_work(&wl->scan_complete_work);
  282. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  283. msecs_to_jiffies(0));
  284. }
  285. }
  286. int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
  287. const u8 *ssid, size_t ssid_len,
  288. struct cfg80211_scan_request *req)
  289. {
  290. /*
  291. * cfg80211 should guarantee that we don't get more channels
  292. * than what we have registered.
  293. */
  294. BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
  295. if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
  296. return -EBUSY;
  297. wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
  298. if (ssid_len && ssid) {
  299. wl->scan.ssid_len = ssid_len;
  300. memcpy(wl->scan.ssid, ssid, ssid_len);
  301. } else {
  302. wl->scan.ssid_len = 0;
  303. }
  304. wl->scan_vif = vif;
  305. wl->scan.req = req;
  306. memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
  307. /* we assume failure so that timeout scenarios are handled correctly */
  308. wl->scan.failed = true;
  309. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  310. msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
  311. wl1271_scan_stm(wl, vif);
  312. return 0;
  313. }
  314. int wl1271_scan_stop(struct wl1271 *wl)
  315. {
  316. struct wl1271_cmd_header *cmd = NULL;
  317. int ret = 0;
  318. if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
  319. return -EINVAL;
  320. wl1271_debug(DEBUG_CMD, "cmd scan stop");
  321. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  322. if (!cmd) {
  323. ret = -ENOMEM;
  324. goto out;
  325. }
  326. ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
  327. sizeof(*cmd), 0);
  328. if (ret < 0) {
  329. wl1271_error("cmd stop_scan failed");
  330. goto out;
  331. }
  332. out:
  333. kfree(cmd);
  334. return ret;
  335. }
  336. static int
  337. wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
  338. struct cfg80211_sched_scan_request *req,
  339. struct conn_scan_ch_params *channels,
  340. u32 band, bool radar, bool passive,
  341. int start, int max_channels,
  342. u8 *n_pactive_ch)
  343. {
  344. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  345. int i, j;
  346. u32 flags;
  347. bool force_passive = !req->n_ssids;
  348. u32 min_dwell_time_active, max_dwell_time_active, delta_per_probe;
  349. u32 dwell_time_passive, dwell_time_dfs;
  350. if (band == IEEE80211_BAND_5GHZ)
  351. delta_per_probe = c->dwell_time_delta_per_probe_5;
  352. else
  353. delta_per_probe = c->dwell_time_delta_per_probe;
  354. min_dwell_time_active = c->base_dwell_time +
  355. req->n_ssids * c->num_probe_reqs * delta_per_probe;
  356. max_dwell_time_active = min_dwell_time_active + c->max_dwell_time_delta;
  357. min_dwell_time_active = DIV_ROUND_UP(min_dwell_time_active, 1000);
  358. max_dwell_time_active = DIV_ROUND_UP(max_dwell_time_active, 1000);
  359. dwell_time_passive = DIV_ROUND_UP(c->dwell_time_passive, 1000);
  360. dwell_time_dfs = DIV_ROUND_UP(c->dwell_time_dfs, 1000);
  361. for (i = 0, j = start;
  362. i < req->n_channels && j < max_channels;
  363. i++) {
  364. flags = req->channels[i]->flags;
  365. if (force_passive)
  366. flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  367. if ((req->channels[i]->band == band) &&
  368. !(flags & IEEE80211_CHAN_DISABLED) &&
  369. (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
  370. /* if radar is set, we ignore the passive flag */
  371. (radar ||
  372. !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
  373. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  374. req->channels[i]->band,
  375. req->channels[i]->center_freq);
  376. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  377. req->channels[i]->hw_value,
  378. req->channels[i]->flags);
  379. wl1271_debug(DEBUG_SCAN, "max_power %d",
  380. req->channels[i]->max_power);
  381. wl1271_debug(DEBUG_SCAN, "min_dwell_time %d max dwell time %d",
  382. min_dwell_time_active,
  383. max_dwell_time_active);
  384. if (flags & IEEE80211_CHAN_RADAR) {
  385. channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
  386. channels[j].passive_duration =
  387. cpu_to_le16(dwell_time_dfs);
  388. } else {
  389. channels[j].passive_duration =
  390. cpu_to_le16(dwell_time_passive);
  391. }
  392. channels[j].min_duration =
  393. cpu_to_le16(min_dwell_time_active);
  394. channels[j].max_duration =
  395. cpu_to_le16(max_dwell_time_active);
  396. channels[j].tx_power_att = req->channels[i]->max_power;
  397. channels[j].channel = req->channels[i]->hw_value;
  398. if ((band == IEEE80211_BAND_2GHZ) &&
  399. (channels[j].channel >= 12) &&
  400. (channels[j].channel <= 14) &&
  401. (flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
  402. !force_passive) {
  403. /* pactive channels treated as DFS */
  404. channels[j].flags = SCAN_CHANNEL_FLAGS_DFS;
  405. /*
  406. * n_pactive_ch is counted down from the end of
  407. * the passive channel list
  408. */
  409. (*n_pactive_ch)++;
  410. wl1271_debug(DEBUG_SCAN, "n_pactive_ch = %d",
  411. *n_pactive_ch);
  412. }
  413. j++;
  414. }
  415. }
  416. return j - start;
  417. }
  418. static bool
  419. wl1271_scan_sched_scan_channels(struct wl1271 *wl,
  420. struct cfg80211_sched_scan_request *req,
  421. struct wl1271_cmd_sched_scan_config *cfg)
  422. {
  423. u8 n_pactive_ch = 0;
  424. cfg->passive[0] =
  425. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
  426. IEEE80211_BAND_2GHZ,
  427. false, true, 0,
  428. MAX_CHANNELS_2GHZ,
  429. &n_pactive_ch);
  430. cfg->active[0] =
  431. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
  432. IEEE80211_BAND_2GHZ,
  433. false, false,
  434. cfg->passive[0],
  435. MAX_CHANNELS_2GHZ,
  436. &n_pactive_ch);
  437. cfg->passive[1] =
  438. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
  439. IEEE80211_BAND_5GHZ,
  440. false, true, 0,
  441. MAX_CHANNELS_5GHZ,
  442. &n_pactive_ch);
  443. cfg->dfs =
  444. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
  445. IEEE80211_BAND_5GHZ,
  446. true, true,
  447. cfg->passive[1],
  448. MAX_CHANNELS_5GHZ,
  449. &n_pactive_ch);
  450. cfg->active[1] =
  451. wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
  452. IEEE80211_BAND_5GHZ,
  453. false, false,
  454. cfg->passive[1] + cfg->dfs,
  455. MAX_CHANNELS_5GHZ,
  456. &n_pactive_ch);
  457. /* 802.11j channels are not supported yet */
  458. cfg->passive[2] = 0;
  459. cfg->active[2] = 0;
  460. cfg->n_pactive_ch = n_pactive_ch;
  461. wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
  462. cfg->active[0], cfg->passive[0]);
  463. wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
  464. cfg->active[1], cfg->passive[1]);
  465. wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
  466. return cfg->passive[0] || cfg->active[0] ||
  467. cfg->passive[1] || cfg->active[1] || cfg->dfs ||
  468. cfg->passive[2] || cfg->active[2];
  469. }
  470. /* Returns the scan type to be used or a negative value on error */
  471. static int
  472. wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
  473. struct wl12xx_vif *wlvif,
  474. struct cfg80211_sched_scan_request *req)
  475. {
  476. struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
  477. struct cfg80211_match_set *sets = req->match_sets;
  478. struct cfg80211_ssid *ssids = req->ssids;
  479. int ret = 0, type, i, j, n_match_ssids = 0;
  480. wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
  481. /* count the match sets that contain SSIDs */
  482. for (i = 0; i < req->n_match_sets; i++)
  483. if (sets[i].ssid.ssid_len > 0)
  484. n_match_ssids++;
  485. /* No filter, no ssids or only bcast ssid */
  486. if (!n_match_ssids &&
  487. (!req->n_ssids ||
  488. (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
  489. type = SCAN_SSID_FILTER_ANY;
  490. goto out;
  491. }
  492. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  493. if (!cmd) {
  494. ret = -ENOMEM;
  495. goto out;
  496. }
  497. cmd->role_id = wlvif->dev_role_id;
  498. if (!n_match_ssids) {
  499. /* No filter, with ssids */
  500. type = SCAN_SSID_FILTER_DISABLED;
  501. for (i = 0; i < req->n_ssids; i++) {
  502. cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
  503. SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
  504. cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
  505. memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
  506. ssids[i].ssid_len);
  507. cmd->n_ssids++;
  508. }
  509. } else {
  510. type = SCAN_SSID_FILTER_LIST;
  511. /* Add all SSIDs from the filters */
  512. for (i = 0; i < req->n_match_sets; i++) {
  513. /* ignore sets without SSIDs */
  514. if (!sets[i].ssid.ssid_len)
  515. continue;
  516. cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
  517. cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
  518. memcpy(cmd->ssids[cmd->n_ssids].ssid,
  519. sets[i].ssid.ssid, sets[i].ssid.ssid_len);
  520. cmd->n_ssids++;
  521. }
  522. if ((req->n_ssids > 1) ||
  523. (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
  524. /*
  525. * Mark all the SSIDs passed in the SSID list as HIDDEN,
  526. * so they're used in probe requests.
  527. */
  528. for (i = 0; i < req->n_ssids; i++) {
  529. if (!req->ssids[i].ssid_len)
  530. continue;
  531. for (j = 0; j < cmd->n_ssids; j++)
  532. if ((req->ssids[i].ssid_len ==
  533. cmd->ssids[j].len) &&
  534. !memcmp(req->ssids[i].ssid,
  535. cmd->ssids[j].ssid,
  536. req->ssids[i].ssid_len)) {
  537. cmd->ssids[j].type =
  538. SCAN_SSID_TYPE_HIDDEN;
  539. break;
  540. }
  541. /* Fail if SSID isn't present in the filters */
  542. if (j == cmd->n_ssids) {
  543. ret = -EINVAL;
  544. goto out_free;
  545. }
  546. }
  547. }
  548. }
  549. wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
  550. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
  551. sizeof(*cmd), 0);
  552. if (ret < 0) {
  553. wl1271_error("cmd sched scan ssid list failed");
  554. goto out_free;
  555. }
  556. out_free:
  557. kfree(cmd);
  558. out:
  559. if (ret < 0)
  560. return ret;
  561. return type;
  562. }
  563. int wl1271_scan_sched_scan_config(struct wl1271 *wl,
  564. struct wl12xx_vif *wlvif,
  565. struct cfg80211_sched_scan_request *req,
  566. struct ieee80211_sched_scan_ies *ies)
  567. {
  568. struct wl1271_cmd_sched_scan_config *cfg = NULL;
  569. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  570. int i, ret;
  571. bool force_passive = !req->n_ssids;
  572. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  573. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  574. if (!cfg)
  575. return -ENOMEM;
  576. cfg->role_id = wlvif->dev_role_id;
  577. cfg->rssi_threshold = c->rssi_threshold;
  578. cfg->snr_threshold = c->snr_threshold;
  579. cfg->n_probe_reqs = c->num_probe_reqs;
  580. /* cycles set to 0 it means infinite (until manually stopped) */
  581. cfg->cycles = 0;
  582. /* report APs when at least 1 is found */
  583. cfg->report_after = 1;
  584. /* don't stop scanning automatically when something is found */
  585. cfg->terminate = 0;
  586. cfg->tag = WL1271_SCAN_DEFAULT_TAG;
  587. /* don't filter on BSS type */
  588. cfg->bss_type = SCAN_BSS_TYPE_ANY;
  589. /* currently NL80211 supports only a single interval */
  590. for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
  591. cfg->intervals[i] = cpu_to_le32(req->interval);
  592. cfg->ssid_len = 0;
  593. ret = wl12xx_scan_sched_scan_ssid_list(wl, wlvif, req);
  594. if (ret < 0)
  595. goto out;
  596. cfg->filter_type = ret;
  597. wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
  598. if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
  599. wl1271_error("scan channel list is empty");
  600. ret = -EINVAL;
  601. goto out;
  602. }
  603. if (!force_passive && cfg->active[0]) {
  604. u8 band = IEEE80211_BAND_2GHZ;
  605. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  606. wlvif->dev_role_id, band,
  607. req->ssids[0].ssid,
  608. req->ssids[0].ssid_len,
  609. ies->ie[band],
  610. ies->len[band], true);
  611. if (ret < 0) {
  612. wl1271_error("2.4GHz PROBE request template failed");
  613. goto out;
  614. }
  615. }
  616. if (!force_passive && cfg->active[1]) {
  617. u8 band = IEEE80211_BAND_5GHZ;
  618. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  619. wlvif->dev_role_id, band,
  620. req->ssids[0].ssid,
  621. req->ssids[0].ssid_len,
  622. ies->ie[band],
  623. ies->len[band], true);
  624. if (ret < 0) {
  625. wl1271_error("5GHz PROBE request template failed");
  626. goto out;
  627. }
  628. }
  629. wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
  630. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
  631. sizeof(*cfg), 0);
  632. if (ret < 0) {
  633. wl1271_error("SCAN configuration failed");
  634. goto out;
  635. }
  636. out:
  637. kfree(cfg);
  638. return ret;
  639. }
  640. int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  641. {
  642. struct wl1271_cmd_sched_scan_start *start;
  643. int ret = 0;
  644. wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
  645. if (wlvif->bss_type != BSS_TYPE_STA_BSS)
  646. return -EOPNOTSUPP;
  647. if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
  648. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  649. return -EBUSY;
  650. start = kzalloc(sizeof(*start), GFP_KERNEL);
  651. if (!start)
  652. return -ENOMEM;
  653. start->role_id = wlvif->dev_role_id;
  654. start->tag = WL1271_SCAN_DEFAULT_TAG;
  655. ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
  656. sizeof(*start), 0);
  657. if (ret < 0) {
  658. wl1271_error("failed to send scan start command");
  659. goto out_free;
  660. }
  661. out_free:
  662. kfree(start);
  663. return ret;
  664. }
  665. void wl1271_scan_sched_scan_results(struct wl1271 *wl)
  666. {
  667. wl1271_debug(DEBUG_SCAN, "got periodic scan results");
  668. ieee80211_sched_scan_results(wl->hw);
  669. }
  670. void wl1271_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  671. {
  672. struct wl1271_cmd_sched_scan_stop *stop;
  673. int ret = 0;
  674. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  675. /* FIXME: what to do if alloc'ing to stop fails? */
  676. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  677. if (!stop) {
  678. wl1271_error("failed to alloc memory to send sched scan stop");
  679. return;
  680. }
  681. stop->role_id = wlvif->dev_role_id;
  682. stop->tag = WL1271_SCAN_DEFAULT_TAG;
  683. ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
  684. sizeof(*stop), 0);
  685. if (ret < 0) {
  686. wl1271_error("failed to send sched scan stop command");
  687. goto out_free;
  688. }
  689. out_free:
  690. kfree(stop);
  691. }