2 * This file is part of wl12xx
4 * Copyright (C) 2012 Texas Instruments. All rights reserved.
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.
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.
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
22 #include <linux/ieee80211.h>
24 #include "../wlcore/debug.h"
25 #include "../wlcore/tx.h"
27 static int wl1271_get_scan_channels(struct wl1271
*wl
,
28 struct cfg80211_scan_request
*req
,
29 struct basic_scan_channel_params
*channels
,
30 enum nl80211_band band
, bool passive
)
32 struct conf_scan_settings
*c
= &wl
->conf
.scan
;
37 i
< req
->n_channels
&& j
< WL1271_SCAN_MAX_CHANNELS
;
39 flags
= req
->channels
[i
]->flags
;
41 if (!test_bit(i
, wl
->scan
.scanned_ch
) &&
42 !(flags
& IEEE80211_CHAN_DISABLED
) &&
43 (req
->channels
[i
]->band
== band
) &&
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
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
);
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
);
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
);
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
;
79 memset(&channels
[j
].bssid_lsb
, 0xff, 4);
80 memset(&channels
[j
].bssid_msb
, 0xff, 2);
82 /* Mark the channels we already used */
83 set_bit(i
, wl
->scan
.scanned_ch
);
92 #define WL1271_NOTHING_TO_SCAN 1
94 static int wl1271_scan_send(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
95 enum nl80211_band band
,
96 bool passive
, u32 basic_rate
)
98 struct ieee80211_vif
*vif
= wl12xx_wlvif_to_vif(wlvif
);
99 struct wl1271_cmd_scan
*cmd
;
100 struct wl1271_cmd_trigger_scan_to
*trigger
;
102 u16 scan_options
= 0;
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
;
108 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
109 trigger
= kzalloc(sizeof(*trigger
), GFP_KERNEL
);
110 if (!cmd
|| !trigger
) {
115 if (wl
->conf
.scan
.split_scan_timeout
)
116 scan_options
|= WL1271_SCAN_OPT_SPLIT_SCAN
;
119 scan_options
|= WL1271_SCAN_OPT_PASSIVE
;
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
;
125 cmd
->params
.role_id
= wlvif
->role_id
;
127 if (WARN_ON(cmd
->params
.role_id
== WL12XX_INVALID_ROLE_ID
)) {
132 cmd
->params
.scan_options
= cpu_to_le16(scan_options
);
134 cmd
->params
.n_ch
= wl1271_get_scan_channels(wl
, wl
->scan
.req
,
137 if (cmd
->params
.n_ch
== 0) {
138 ret
= WL1271_NOTHING_TO_SCAN
;
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
;
147 if (band
== NL80211_BAND_2GHZ
)
148 cmd
->params
.band
= WL1271_SCAN_BAND_2_4_GHZ
;
150 cmd
->params
.band
= WL1271_SCAN_BAND_5_GHZ
;
152 if (wl
->scan
.ssid_len
) {
153 cmd
->params
.ssid_len
= wl
->scan
.ssid_len
;
154 memcpy(cmd
->params
.ssid
, wl
->scan
.ssid
, wl
->scan
.ssid_len
);
157 memcpy(cmd
->addr
, vif
->addr
, ETH_ALEN
);
159 ret
= wl12xx_cmd_build_probe_req(wl
, wlvif
,
160 cmd
->params
.role_id
, band
,
161 wl
->scan
.ssid
, wl
->scan
.ssid_len
,
163 wl
->scan
.req
->ie_len
, NULL
, 0, false);
165 wl1271_error("PROBE request template failed");
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);
173 wl1271_error("trigger scan to failed for hw scan");
177 wl1271_dump(DEBUG_SCAN
, "SCAN: ", cmd
, sizeof(*cmd
));
179 ret
= wl1271_cmd_send(wl
, CMD_SCAN
, cmd
, sizeof(*cmd
), 0);
181 wl1271_error("SCAN failed");
191 int wl12xx_scan_stop(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
193 struct wl1271_cmd_header
*cmd
= NULL
;
196 if (WARN_ON(wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
))
199 wl1271_debug(DEBUG_CMD
, "cmd scan stop");
201 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
207 ret
= wl1271_cmd_send(wl
, CMD_STOP_SCAN
, cmd
,
210 wl1271_error("cmd stop_scan failed");
218 void wl1271_scan_stm(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
221 enum nl80211_band band
;
224 switch (wl
->scan
.state
) {
225 case WL1271_SCAN_STATE_IDLE
:
228 case WL1271_SCAN_STATE_2GHZ_ACTIVE
:
229 band
= NL80211_BAND_2GHZ
;
230 mask
= wlvif
->bitrate_masks
[band
];
231 if (wl
->scan
.req
->no_cck
) {
232 mask
&= ~CONF_TX_CCK_RATES
;
234 mask
= CONF_TX_RATE_MASK_BASIC_P2P
;
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
);
245 case WL1271_SCAN_STATE_2GHZ_PASSIVE
:
246 band
= NL80211_BAND_2GHZ
;
247 mask
= wlvif
->bitrate_masks
[band
];
248 if (wl
->scan
.req
->no_cck
) {
249 mask
&= ~CONF_TX_CCK_RATES
;
251 mask
= CONF_TX_RATE_MASK_BASIC_P2P
;
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
) {
257 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_ACTIVE
;
259 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
260 wl1271_scan_stm(wl
, wlvif
);
265 case WL1271_SCAN_STATE_5GHZ_ACTIVE
:
266 band
= NL80211_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
);
276 case WL1271_SCAN_STATE_5GHZ_PASSIVE
:
277 band
= NL80211_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
);
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));
295 wl1271_error("invalid scan state");
300 cancel_delayed_work(&wl
->scan_complete_work
);
301 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
302 msecs_to_jiffies(0));
306 static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config
*cmd
,
307 struct wlcore_scan_channels
*cmd_channels
)
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
;
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 */
321 int 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
)
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
;
330 bool force_passive
= !req
->n_ssids
;
332 wl1271_debug(DEBUG_CMD
, "cmd sched_scan scan config");
334 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
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) */
344 /* report APs when at least 1 is found */
345 cfg
->report_after
= 1;
346 /* don't stop scanning automatically when something is found */
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
*
357 ret
= wlcore_scan_sched_scan_ssid_list(wl
, wlvif
, req
);
361 cfg
->filter_type
= ret
;
363 wl1271_debug(DEBUG_SCAN
, "filter_type = %d", cfg
->filter_type
);
365 cfg_channels
= kzalloc(sizeof(*cfg_channels
), GFP_KERNEL
);
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");
378 wl12xx_adjust_channels(cfg
, cfg_channels
);
380 if (!force_passive
&& cfg
->active
[0]) {
381 u8 band
= NL80211_BAND_2GHZ
;
382 ret
= wl12xx_cmd_build_probe_req(wl
, wlvif
,
383 wlvif
->role_id
, band
,
385 req
->ssids
[0].ssid_len
,
392 wl1271_error("2.4GHz PROBE request template failed");
397 if (!force_passive
&& cfg
->active
[1]) {
398 u8 band
= NL80211_BAND_5GHZ
;
399 ret
= wl12xx_cmd_build_probe_req(wl
, wlvif
,
400 wlvif
->role_id
, band
,
402 req
->ssids
[0].ssid_len
,
409 wl1271_error("5GHz PROBE request template failed");
414 wl1271_dump(DEBUG_SCAN
, "SCAN_CFG: ", cfg
, sizeof(*cfg
));
416 ret
= wl1271_cmd_send(wl
, CMD_CONNECTION_SCAN_CFG
, cfg
,
419 wl1271_error("SCAN configuration failed");
428 int wl1271_scan_sched_scan_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
430 struct wl1271_cmd_sched_scan_start
*start
;
433 wl1271_debug(DEBUG_CMD
, "cmd periodic scan start");
435 if (wlvif
->bss_type
!= BSS_TYPE_STA_BSS
)
438 if ((wl
->quirks
& WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN
) &&
439 test_bit(WLVIF_FLAG_IN_USE
, &wlvif
->flags
))
442 start
= kzalloc(sizeof(*start
), GFP_KERNEL
);
446 start
->role_id
= wlvif
->role_id
;
447 start
->tag
= WL1271_SCAN_DEFAULT_TAG
;
449 ret
= wl1271_cmd_send(wl
, CMD_START_PERIODIC_SCAN
, start
,
452 wl1271_error("failed to send scan start command");
461 int wl12xx_sched_scan_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
462 struct cfg80211_sched_scan_request
*req
,
463 struct ieee80211_scan_ies
*ies
)
467 ret
= wl1271_scan_sched_scan_config(wl
, wlvif
, req
, ies
);
471 return wl1271_scan_sched_scan_start(wl
, wlvif
);
474 void wl12xx_scan_sched_scan_stop(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
476 struct wl1271_cmd_sched_scan_stop
*stop
;
479 wl1271_debug(DEBUG_CMD
, "cmd periodic scan stop");
481 /* FIXME: what to do if alloc'ing to stop fails? */
482 stop
= kzalloc(sizeof(*stop
), GFP_KERNEL
);
484 wl1271_error("failed to alloc memory to send sched scan stop");
488 stop
->role_id
= wlvif
->role_id
;
489 stop
->tag
= WL1271_SCAN_DEFAULT_TAG
;
491 ret
= wl1271_cmd_send(wl
, CMD_STOP_PERIODIC_SCAN
, stop
,
494 wl1271_error("failed to send sched scan stop command");
502 int wl12xx_scan_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
503 struct cfg80211_scan_request
*req
)
505 wl1271_scan_stm(wl
, wlvif
);
509 void wl12xx_scan_completed(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
)
511 wl1271_scan_stm(wl
, wlvif
);