blob: ebed13af9852beac98f26392c3a98e8ab57ff01f [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * This file is part of wl12xx
3 *
4 * Copyright (C) 2012 Texas Instruments. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/ieee80211.h>
23#include "scan.h"
24#include "../wlcore/debug.h"
25#include "../wlcore/tx.h"
26
27static int wl1271_get_scan_channels(struct wl1271 *wl,
28 struct cfg80211_scan_request *req,
29 struct basic_scan_channel_params *channels,
30 enum ieee80211_band band, bool passive)
31{
32 struct conf_scan_settings *c = &wl->conf.scan;
33 int i, j;
34 u32 flags;
35
36 for (i = 0, j = 0;
37 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
38 i++) {
39 flags = req->channels[i]->flags;
40
41 if (!test_bit(i, wl->scan.scanned_ch) &&
42 !(flags & IEEE80211_CHAN_DISABLED) &&
43 (req->channels[i]->band == band) &&
44 /*
45 * In passive scans, we scan all remaining
46 * channels, even if not marked as such.
47 * In active scans, we only scan channels not
48 * marked as passive.
49 */
50 (passive || !(flags & IEEE80211_CHAN_NO_IR))) {
51 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
52 req->channels[i]->band,
53 req->channels[i]->center_freq);
54 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
55 req->channels[i]->hw_value,
56 req->channels[i]->flags);
57 wl1271_debug(DEBUG_SCAN,
58 "max_antenna_gain %d, max_power %d",
59 req->channels[i]->max_antenna_gain,
60 req->channels[i]->max_power);
61 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
62 req->channels[i]->beacon_found);
63
64 if (!passive) {
65 channels[j].min_duration =
66 cpu_to_le32(c->min_dwell_time_active);
67 channels[j].max_duration =
68 cpu_to_le32(c->max_dwell_time_active);
69 } else {
70 channels[j].min_duration =
71 cpu_to_le32(c->dwell_time_passive);
72 channels[j].max_duration =
73 cpu_to_le32(c->dwell_time_passive);
74 }
75 channels[j].early_termination = 0;
76 channels[j].tx_power_att = req->channels[i]->max_power;
77 channels[j].channel = req->channels[i]->hw_value;
78
79 memset(&channels[j].bssid_lsb, 0xff, 4);
80 memset(&channels[j].bssid_msb, 0xff, 2);
81
82 /* Mark the channels we already used */
83 set_bit(i, wl->scan.scanned_ch);
84
85 j++;
86 }
87 }
88
89 return j;
90}
91
92#define WL1271_NOTHING_TO_SCAN 1
93
94static int wl1271_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
95 enum ieee80211_band band,
96 bool passive, u32 basic_rate)
97{
98 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
99 struct wl1271_cmd_scan *cmd;
100 struct wl1271_cmd_trigger_scan_to *trigger;
101 int ret;
102 u16 scan_options = 0;
103
104 /* skip active scans if we don't have SSIDs */
105 if (!passive && wl->scan.req->n_ssids == 0)
106 return WL1271_NOTHING_TO_SCAN;
107
108 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
109 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
110 if (!cmd || !trigger) {
111 ret = -ENOMEM;
112 goto out;
113 }
114
115 if (wl->conf.scan.split_scan_timeout)
116 scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;
117
118 if (passive)
119 scan_options |= WL1271_SCAN_OPT_PASSIVE;
120
121 /* scan on the dev role if the regular one is not started */
122 if (wlcore_is_p2p_mgmt(wlvif))
123 cmd->params.role_id = wlvif->dev_role_id;
124 else
125 cmd->params.role_id = wlvif->role_id;
126
127 if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
128 ret = -EINVAL;
129 goto out;
130 }
131
132 cmd->params.scan_options = cpu_to_le16(scan_options);
133
134 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
135 cmd->channels,
136 band, passive);
137 if (cmd->params.n_ch == 0) {
138 ret = WL1271_NOTHING_TO_SCAN;
139 goto out;
140 }
141
142 cmd->params.tx_rate = cpu_to_le32(basic_rate);
143 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
144 cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
145 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
146
147 if (band == IEEE80211_BAND_2GHZ)
148 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
149 else
150 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
151
152 if (wl->scan.ssid_len && wl->scan.ssid) {
153 cmd->params.ssid_len = wl->scan.ssid_len;
154 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
155 }
156
157 memcpy(cmd->addr, vif->addr, ETH_ALEN);
158
159 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
160 cmd->params.role_id, band,
161 wl->scan.ssid, wl->scan.ssid_len,
162 wl->scan.req->ie,
163 wl->scan.req->ie_len, NULL, 0, false);
164 if (ret < 0) {
165 wl1271_error("PROBE request template failed");
166 goto out;
167 }
168
169 trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
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
177 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
178
179 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
180 if (ret < 0) {
181 wl1271_error("SCAN failed");
182 goto out;
183 }
184
185out:
186 kfree(cmd);
187 kfree(trigger);
188 return ret;
189}
190
191int wl12xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
192{
193 struct wl1271_cmd_header *cmd = NULL;
194 int ret = 0;
195
196 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
197 return -EINVAL;
198
199 wl1271_debug(DEBUG_CMD, "cmd scan stop");
200
201 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
202 if (!cmd) {
203 ret = -ENOMEM;
204 goto out;
205 }
206
207 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
208 sizeof(*cmd), 0);
209 if (ret < 0) {
210 wl1271_error("cmd stop_scan failed");
211 goto out;
212 }
213out:
214 kfree(cmd);
215 return ret;
216}
217
218void wl1271_scan_stm(struct wl1271 *wl, struct wl12xx_vif *wlvif)
219{
220 int ret = 0;
221 enum ieee80211_band band;
222 u32 rate, mask;
223
224 switch (wl->scan.state) {
225 case WL1271_SCAN_STATE_IDLE:
226 break;
227
228 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
229 band = IEEE80211_BAND_2GHZ;
230 mask = wlvif->bitrate_masks[band];
231 if (wl->scan.req->no_cck) {
232 mask &= ~CONF_TX_CCK_RATES;
233 if (!mask)
234 mask = CONF_TX_RATE_MASK_BASIC_P2P;
235 }
236 rate = wl1271_tx_min_rate_get(wl, mask);
237 ret = wl1271_scan_send(wl, wlvif, band, false, rate);
238 if (ret == WL1271_NOTHING_TO_SCAN) {
239 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
240 wl1271_scan_stm(wl, wlvif);
241 }
242
243 break;
244
245 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
246 band = IEEE80211_BAND_2GHZ;
247 mask = wlvif->bitrate_masks[band];
248 if (wl->scan.req->no_cck) {
249 mask &= ~CONF_TX_CCK_RATES;
250 if (!mask)
251 mask = CONF_TX_RATE_MASK_BASIC_P2P;
252 }
253 rate = wl1271_tx_min_rate_get(wl, mask);
254 ret = wl1271_scan_send(wl, wlvif, band, true, rate);
255 if (ret == WL1271_NOTHING_TO_SCAN) {
256 if (wl->enable_11a)
257 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
258 else
259 wl->scan.state = WL1271_SCAN_STATE_DONE;
260 wl1271_scan_stm(wl, wlvif);
261 }
262
263 break;
264
265 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
266 band = IEEE80211_BAND_5GHZ;
267 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
268 ret = wl1271_scan_send(wl, wlvif, band, false, rate);
269 if (ret == WL1271_NOTHING_TO_SCAN) {
270 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
271 wl1271_scan_stm(wl, wlvif);
272 }
273
274 break;
275
276 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
277 band = IEEE80211_BAND_5GHZ;
278 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
279 ret = wl1271_scan_send(wl, wlvif, band, true, rate);
280 if (ret == WL1271_NOTHING_TO_SCAN) {
281 wl->scan.state = WL1271_SCAN_STATE_DONE;
282 wl1271_scan_stm(wl, wlvif);
283 }
284
285 break;
286
287 case WL1271_SCAN_STATE_DONE:
288 wl->scan.failed = false;
289 cancel_delayed_work(&wl->scan_complete_work);
290 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
291 msecs_to_jiffies(0));
292 break;
293
294 default:
295 wl1271_error("invalid scan state");
296 break;
297 }
298
299 if (ret < 0) {
300 cancel_delayed_work(&wl->scan_complete_work);
301 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
302 msecs_to_jiffies(0));
303 }
304}
305
306static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config *cmd,
307 struct wlcore_scan_channels *cmd_channels)
308{
309 memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive));
310 memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active));
311 cmd->dfs = cmd_channels->dfs;
312 cmd->n_pactive_ch = cmd_channels->passive_active;
313
314 memcpy(cmd->channels_2, cmd_channels->channels_2,
315 sizeof(cmd->channels_2));
316 memcpy(cmd->channels_5, cmd_channels->channels_5,
317 sizeof(cmd->channels_5));
318 /* channels_4 are not supported, so no need to copy them */
319}
320
321int wl1271_scan_sched_scan_config(struct wl1271 *wl,
322 struct wl12xx_vif *wlvif,
323 struct cfg80211_sched_scan_request *req,
324 struct ieee80211_scan_ies *ies)
325{
326 struct wl1271_cmd_sched_scan_config *cfg = NULL;
327 struct wlcore_scan_channels *cfg_channels = NULL;
328 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
329 int i, ret;
330 bool force_passive = !req->n_ssids;
331
332 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
333
334 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
335 if (!cfg)
336 return -ENOMEM;
337
338 cfg->role_id = wlvif->role_id;
339 cfg->rssi_threshold = c->rssi_threshold;
340 cfg->snr_threshold = c->snr_threshold;
341 cfg->n_probe_reqs = c->num_probe_reqs;
342 /* cycles set to 0 it means infinite (until manually stopped) */
343 cfg->cycles = 0;
344 /* report APs when at least 1 is found */
345 cfg->report_after = 1;
346 /* don't stop scanning automatically when something is found */
347 cfg->terminate = 0;
348 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
349 /* don't filter on BSS type */
350 cfg->bss_type = SCAN_BSS_TYPE_ANY;
351 /* currently NL80211 supports only a single interval */
352 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
353 cfg->intervals[i] = cpu_to_le32(req->scan_plans[0].interval *
354 MSEC_PER_SEC);
355
356 cfg->ssid_len = 0;
357 ret = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req);
358 if (ret < 0)
359 goto out;
360
361 cfg->filter_type = ret;
362
363 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
364
365 cfg_channels = kzalloc(sizeof(*cfg_channels), GFP_KERNEL);
366 if (!cfg_channels) {
367 ret = -ENOMEM;
368 goto out;
369 }
370
371 if (!wlcore_set_scan_chan_params(wl, cfg_channels, req->channels,
372 req->n_channels, req->n_ssids,
373 SCAN_TYPE_PERIODIC)) {
374 wl1271_error("scan channel list is empty");
375 ret = -EINVAL;
376 goto out;
377 }
378 wl12xx_adjust_channels(cfg, cfg_channels);
379
380 if (!force_passive && cfg->active[0]) {
381 u8 band = IEEE80211_BAND_2GHZ;
382 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
383 wlvif->role_id, band,
384 req->ssids[0].ssid,
385 req->ssids[0].ssid_len,
386 ies->ies[band],
387 ies->len[band],
388 ies->common_ies,
389 ies->common_ie_len,
390 true);
391 if (ret < 0) {
392 wl1271_error("2.4GHz PROBE request template failed");
393 goto out;
394 }
395 }
396
397 if (!force_passive && cfg->active[1]) {
398 u8 band = IEEE80211_BAND_5GHZ;
399 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
400 wlvif->role_id, band,
401 req->ssids[0].ssid,
402 req->ssids[0].ssid_len,
403 ies->ies[band],
404 ies->len[band],
405 ies->common_ies,
406 ies->common_ie_len,
407 true);
408 if (ret < 0) {
409 wl1271_error("5GHz PROBE request template failed");
410 goto out;
411 }
412 }
413
414 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
415
416 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
417 sizeof(*cfg), 0);
418 if (ret < 0) {
419 wl1271_error("SCAN configuration failed");
420 goto out;
421 }
422out:
423 kfree(cfg_channels);
424 kfree(cfg);
425 return ret;
426}
427
428int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
429{
430 struct wl1271_cmd_sched_scan_start *start;
431 int ret = 0;
432
433 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
434
435 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
436 return -EOPNOTSUPP;
437
438 if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
439 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
440 return -EBUSY;
441
442 start = kzalloc(sizeof(*start), GFP_KERNEL);
443 if (!start)
444 return -ENOMEM;
445
446 start->role_id = wlvif->role_id;
447 start->tag = WL1271_SCAN_DEFAULT_TAG;
448
449 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
450 sizeof(*start), 0);
451 if (ret < 0) {
452 wl1271_error("failed to send scan start command");
453 goto out_free;
454 }
455
456out_free:
457 kfree(start);
458 return ret;
459}
460
461int wl12xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
462 struct cfg80211_sched_scan_request *req,
463 struct ieee80211_scan_ies *ies)
464{
465 int ret;
466
467 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
468 if (ret < 0)
469 return ret;
470
471 return wl1271_scan_sched_scan_start(wl, wlvif);
472}
473
474void wl12xx_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
475{
476 struct wl1271_cmd_sched_scan_stop *stop;
477 int ret = 0;
478
479 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
480
481 /* FIXME: what to do if alloc'ing to stop fails? */
482 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
483 if (!stop) {
484 wl1271_error("failed to alloc memory to send sched scan stop");
485 return;
486 }
487
488 stop->role_id = wlvif->role_id;
489 stop->tag = WL1271_SCAN_DEFAULT_TAG;
490
491 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
492 sizeof(*stop), 0);
493 if (ret < 0) {
494 wl1271_error("failed to send sched scan stop command");
495 goto out_free;
496 }
497
498out_free:
499 kfree(stop);
500}
501
502int wl12xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
503 struct cfg80211_scan_request *req)
504{
505 wl1271_scan_stm(wl, wlvif);
506 return 0;
507}
508
509void wl12xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif)
510{
511 wl1271_scan_stm(wl, wlvif);
512}