1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl12xx
5 * Copyright (C) 2012 Texas Instruments. All rights reserved.
8 #include <linux/ieee80211.h>
10 #include "../wlcore/debug.h"
11 #include "../wlcore/tx.h"
13 static int wl1271_get_scan_channels(struct wl1271
*wl
,
14 struct cfg80211_scan_request
*req
,
15 struct basic_scan_channel_params
*channels
,
16 enum nl80211_band band
, bool passive
)
18 struct conf_scan_settings
*c
= &wl
->conf
.scan
;
23 i
< req
->n_channels
&& j
< WL1271_SCAN_MAX_CHANNELS
;
25 flags
= req
->channels
[i
]->flags
;
27 if (!test_bit(i
, wl
->scan
.scanned_ch
) &&
28 !(flags
& IEEE80211_CHAN_DISABLED
) &&
29 (req
->channels
[i
]->band
== band
) &&
31 * In passive scans, we scan all remaining
32 * channels, even if not marked as such.
33 * In active scans, we only scan channels not
36 (passive
|| !(flags
& IEEE80211_CHAN_NO_IR
))) {
37 wl1271_debug(DEBUG_SCAN
, "band %d, center_freq %d ",
38 req
->channels
[i
]->band
,
39 req
->channels
[i
]->center_freq
);
40 wl1271_debug(DEBUG_SCAN
, "hw_value %d, flags %X",
41 req
->channels
[i
]->hw_value
,
42 req
->channels
[i
]->flags
);
43 wl1271_debug(DEBUG_SCAN
,
44 "max_antenna_gain %d, max_power %d",
45 req
->channels
[i
]->max_antenna_gain
,
46 req
->channels
[i
]->max_power
);
47 wl1271_debug(DEBUG_SCAN
, "beacon_found %d",
48 req
->channels
[i
]->beacon_found
);
51 channels
[j
].min_duration
=
52 cpu_to_le32(c
->min_dwell_time_active
);
53 channels
[j
].max_duration
=
54 cpu_to_le32(c
->max_dwell_time_active
);
56 channels
[j
].min_duration
=
57 cpu_to_le32(c
->dwell_time_passive
);
58 channels
[j
].max_duration
=
59 cpu_to_le32(c
->dwell_time_passive
);
61 channels
[j
].early_termination
= 0;
62 channels
[j
].tx_power_att
= req
->channels
[i
]->max_power
;
63 channels
[j
].channel
= req
->channels
[i
]->hw_value
;
65 memset(&channels
[j
].bssid_lsb
, 0xff, 4);
66 memset(&channels
[j
].bssid_msb
, 0xff, 2);
68 /* Mark the channels we already used */
69 set_bit(i
, wl
->scan
.scanned_ch
);
78 #define WL1271_NOTHING_TO_SCAN 1
80 static int wl1271_scan_send(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
81 enum nl80211_band band
,
82 bool passive
, u32 basic_rate
)
84 struct ieee80211_vif
*vif
= wl12xx_wlvif_to_vif(wlvif
);
85 struct wl1271_cmd_scan
*cmd
;
86 struct wl1271_cmd_trigger_scan_to
*trigger
;
90 /* skip active scans if we don't have SSIDs */
91 if (!passive
&& wl
->scan
.req
->n_ssids
== 0)
92 return WL1271_NOTHING_TO_SCAN
;
94 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
95 trigger
= kzalloc(sizeof(*trigger
), GFP_KERNEL
);
96 if (!cmd
|| !trigger
) {
101 if (wl
->conf
.scan
.split_scan_timeout
)
102 scan_options
|= WL1271_SCAN_OPT_SPLIT_SCAN
;
105 scan_options
|= WL1271_SCAN_OPT_PASSIVE
;
107 /* scan on the dev role if the regular one is not started */
108 if (wlcore_is_p2p_mgmt(wlvif
))
109 cmd
->params
.role_id
= wlvif
->dev_role_id
;
111 cmd
->params
.role_id
= wlvif
->role_id
;
113 if (WARN_ON(cmd
->params
.role_id
== WL12XX_INVALID_ROLE_ID
)) {
118 cmd
->params
.scan_options
= cpu_to_le16(scan_options
);
120 cmd
->params
.n_ch
= wl1271_get_scan_channels(wl
, wl
->scan
.req
,
123 if (cmd
->params
.n_ch
== 0) {
124 ret
= WL1271_NOTHING_TO_SCAN
;
128 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
129 cmd
->params
.n_probe_reqs
= wl
->conf
.scan
.num_probe_reqs
;
130 cmd
->params
.tid_trigger
= CONF_TX_AC_ANY_TID
;
131 cmd
->params
.scan_tag
= WL1271_SCAN_DEFAULT_TAG
;
133 if (band
== NL80211_BAND_2GHZ
)
134 cmd
->params
.band
= WL1271_SCAN_BAND_2_4_GHZ
;
136 cmd
->params
.band
= WL1271_SCAN_BAND_5_GHZ
;
138 if (wl
->scan
.ssid_len
) {
139 cmd
->params
.ssid_len
= wl
->scan
.ssid_len
;
140 memcpy(cmd
->params
.ssid
, wl
->scan
.ssid
, wl
->scan
.ssid_len
);
143 memcpy(cmd
->addr
, vif
->addr
, ETH_ALEN
);
145 ret
= wl12xx_cmd_build_probe_req(wl
, wlvif
,
146 cmd
->params
.role_id
, band
,
147 wl
->scan
.ssid
, wl
->scan
.ssid_len
,
149 wl
->scan
.req
->ie_len
, NULL
, 0, false);
151 wl1271_error("PROBE request template failed");
155 trigger
->timeout
= cpu_to_le32(wl
->conf
.scan
.split_scan_timeout
);
156 ret
= wl1271_cmd_send(wl
, CMD_TRIGGER_SCAN_TO
, trigger
,
157 sizeof(*trigger
), 0);
159 wl1271_error("trigger scan to failed for hw scan");
163 wl1271_dump(DEBUG_SCAN
, "SCAN: ", cmd
, sizeof(*cmd
));
165 ret
= wl1271_cmd_send(wl
, CMD_SCAN
, cmd
, sizeof(*cmd
), 0);
167 wl1271_error("SCAN failed");
177 int wl12xx_scan_stop(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
179 struct wl1271_cmd_header
*cmd
= NULL
;
182 if (WARN_ON(wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
))
185 wl1271_debug(DEBUG_CMD
, "cmd scan stop");
187 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
193 ret
= wl1271_cmd_send(wl
, CMD_STOP_SCAN
, cmd
,
196 wl1271_error("cmd stop_scan failed");
204 void wl1271_scan_stm(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
207 enum nl80211_band band
;
210 switch (wl
->scan
.state
) {
211 case WL1271_SCAN_STATE_IDLE
:
214 case WL1271_SCAN_STATE_2GHZ_ACTIVE
:
215 band
= NL80211_BAND_2GHZ
;
216 mask
= wlvif
->bitrate_masks
[band
];
217 if (wl
->scan
.req
->no_cck
) {
218 mask
&= ~CONF_TX_CCK_RATES
;
220 mask
= CONF_TX_RATE_MASK_BASIC_P2P
;
222 rate
= wl1271_tx_min_rate_get(wl
, mask
);
223 ret
= wl1271_scan_send(wl
, wlvif
, band
, false, rate
);
224 if (ret
== WL1271_NOTHING_TO_SCAN
) {
225 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_PASSIVE
;
226 wl1271_scan_stm(wl
, wlvif
);
231 case WL1271_SCAN_STATE_2GHZ_PASSIVE
:
232 band
= NL80211_BAND_2GHZ
;
233 mask
= wlvif
->bitrate_masks
[band
];
234 if (wl
->scan
.req
->no_cck
) {
235 mask
&= ~CONF_TX_CCK_RATES
;
237 mask
= CONF_TX_RATE_MASK_BASIC_P2P
;
239 rate
= wl1271_tx_min_rate_get(wl
, mask
);
240 ret
= wl1271_scan_send(wl
, wlvif
, band
, true, rate
);
241 if (ret
== WL1271_NOTHING_TO_SCAN
) {
243 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_ACTIVE
;
245 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
246 wl1271_scan_stm(wl
, wlvif
);
251 case WL1271_SCAN_STATE_5GHZ_ACTIVE
:
252 band
= NL80211_BAND_5GHZ
;
253 rate
= wl1271_tx_min_rate_get(wl
, wlvif
->bitrate_masks
[band
]);
254 ret
= wl1271_scan_send(wl
, wlvif
, band
, false, rate
);
255 if (ret
== WL1271_NOTHING_TO_SCAN
) {
256 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_PASSIVE
;
257 wl1271_scan_stm(wl
, wlvif
);
262 case WL1271_SCAN_STATE_5GHZ_PASSIVE
:
263 band
= NL80211_BAND_5GHZ
;
264 rate
= wl1271_tx_min_rate_get(wl
, wlvif
->bitrate_masks
[band
]);
265 ret
= wl1271_scan_send(wl
, wlvif
, band
, true, rate
);
266 if (ret
== WL1271_NOTHING_TO_SCAN
) {
267 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
268 wl1271_scan_stm(wl
, wlvif
);
273 case WL1271_SCAN_STATE_DONE
:
274 wl
->scan
.failed
= false;
275 cancel_delayed_work(&wl
->scan_complete_work
);
276 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
277 msecs_to_jiffies(0));
281 wl1271_error("invalid scan state");
286 cancel_delayed_work(&wl
->scan_complete_work
);
287 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
288 msecs_to_jiffies(0));
292 static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config
*cmd
,
293 struct wlcore_scan_channels
*cmd_channels
)
295 memcpy(cmd
->passive
, cmd_channels
->passive
, sizeof(cmd
->passive
));
296 memcpy(cmd
->active
, cmd_channels
->active
, sizeof(cmd
->active
));
297 cmd
->dfs
= cmd_channels
->dfs
;
298 cmd
->n_pactive_ch
= cmd_channels
->passive_active
;
300 memcpy(cmd
->channels_2
, cmd_channels
->channels_2
,
301 sizeof(cmd
->channels_2
));
302 memcpy(cmd
->channels_5
, cmd_channels
->channels_5
,
303 sizeof(cmd
->channels_5
));
304 /* channels_4 are not supported, so no need to copy them */
307 int wl1271_scan_sched_scan_config(struct wl1271
*wl
,
308 struct wl12xx_vif
*wlvif
,
309 struct cfg80211_sched_scan_request
*req
,
310 struct ieee80211_scan_ies
*ies
)
312 struct wl1271_cmd_sched_scan_config
*cfg
= NULL
;
313 struct wlcore_scan_channels
*cfg_channels
= NULL
;
314 struct conf_sched_scan_settings
*c
= &wl
->conf
.sched_scan
;
316 bool force_passive
= !req
->n_ssids
;
318 wl1271_debug(DEBUG_CMD
, "cmd sched_scan scan config");
320 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
324 cfg
->role_id
= wlvif
->role_id
;
325 cfg
->rssi_threshold
= c
->rssi_threshold
;
326 cfg
->snr_threshold
= c
->snr_threshold
;
327 cfg
->n_probe_reqs
= c
->num_probe_reqs
;
328 /* cycles set to 0 it means infinite (until manually stopped) */
330 /* report APs when at least 1 is found */
331 cfg
->report_after
= 1;
332 /* don't stop scanning automatically when something is found */
334 cfg
->tag
= WL1271_SCAN_DEFAULT_TAG
;
335 /* don't filter on BSS type */
336 cfg
->bss_type
= SCAN_BSS_TYPE_ANY
;
337 /* currently NL80211 supports only a single interval */
338 for (i
= 0; i
< SCAN_MAX_CYCLE_INTERVALS
; i
++)
339 cfg
->intervals
[i
] = cpu_to_le32(req
->scan_plans
[0].interval
*
343 ret
= wlcore_scan_sched_scan_ssid_list(wl
, wlvif
, req
);
347 cfg
->filter_type
= ret
;
349 wl1271_debug(DEBUG_SCAN
, "filter_type = %d", cfg
->filter_type
);
351 cfg_channels
= kzalloc(sizeof(*cfg_channels
), GFP_KERNEL
);
357 if (!wlcore_set_scan_chan_params(wl
, cfg_channels
, req
->channels
,
358 req
->n_channels
, req
->n_ssids
,
359 SCAN_TYPE_PERIODIC
)) {
360 wl1271_error("scan channel list is empty");
364 wl12xx_adjust_channels(cfg
, cfg_channels
);
366 if (!force_passive
&& cfg
->active
[0]) {
367 u8 band
= NL80211_BAND_2GHZ
;
368 ret
= wl12xx_cmd_build_probe_req(wl
, wlvif
,
369 wlvif
->role_id
, band
,
371 req
->ssids
[0].ssid_len
,
378 wl1271_error("2.4GHz PROBE request template failed");
383 if (!force_passive
&& cfg
->active
[1]) {
384 u8 band
= NL80211_BAND_5GHZ
;
385 ret
= wl12xx_cmd_build_probe_req(wl
, wlvif
,
386 wlvif
->role_id
, band
,
388 req
->ssids
[0].ssid_len
,
395 wl1271_error("5GHz PROBE request template failed");
400 wl1271_dump(DEBUG_SCAN
, "SCAN_CFG: ", cfg
, sizeof(*cfg
));
402 ret
= wl1271_cmd_send(wl
, CMD_CONNECTION_SCAN_CFG
, cfg
,
405 wl1271_error("SCAN configuration failed");
414 int wl1271_scan_sched_scan_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
416 struct wl1271_cmd_sched_scan_start
*start
;
419 wl1271_debug(DEBUG_CMD
, "cmd periodic scan start");
421 if (wlvif
->bss_type
!= BSS_TYPE_STA_BSS
)
424 if ((wl
->quirks
& WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN
) &&
425 test_bit(WLVIF_FLAG_IN_USE
, &wlvif
->flags
))
428 start
= kzalloc(sizeof(*start
), GFP_KERNEL
);
432 start
->role_id
= wlvif
->role_id
;
433 start
->tag
= WL1271_SCAN_DEFAULT_TAG
;
435 ret
= wl1271_cmd_send(wl
, CMD_START_PERIODIC_SCAN
, start
,
438 wl1271_error("failed to send scan start command");
447 int wl12xx_sched_scan_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
448 struct cfg80211_sched_scan_request
*req
,
449 struct ieee80211_scan_ies
*ies
)
453 ret
= wl1271_scan_sched_scan_config(wl
, wlvif
, req
, ies
);
457 return wl1271_scan_sched_scan_start(wl
, wlvif
);
460 void wl12xx_scan_sched_scan_stop(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
462 struct wl1271_cmd_sched_scan_stop
*stop
;
465 wl1271_debug(DEBUG_CMD
, "cmd periodic scan stop");
467 /* FIXME: what to do if alloc'ing to stop fails? */
468 stop
= kzalloc(sizeof(*stop
), GFP_KERNEL
);
470 wl1271_error("failed to alloc memory to send sched scan stop");
474 stop
->role_id
= wlvif
->role_id
;
475 stop
->tag
= WL1271_SCAN_DEFAULT_TAG
;
477 ret
= wl1271_cmd_send(wl
, CMD_STOP_PERIODIC_SCAN
, stop
,
480 wl1271_error("failed to send sched scan stop command");
488 int wl12xx_scan_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
489 struct cfg80211_scan_request
*req
)
491 wl1271_scan_stm(wl
, wlvif
);
495 void wl12xx_scan_completed(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
497 wl1271_scan_stm(wl
, wlvif
);