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