scan.c 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of wl1271
 *
 * Copyright (C) 2009-2010 Nokia Corporation
 *
 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/ieee80211.h>

26
#include "wlcore.h"
27
#include "debug.h"
S
Shahar Levi 已提交
28 29 30
#include "cmd.h"
#include "scan.h"
#include "acx.h"
31
#include "ps.h"
32
#include "tx.h"
33

34 35
void wl1271_scan_complete_work(struct work_struct *work)
{
36 37
	struct delayed_work *dwork;
	struct wl1271 *wl;
E
Eliad Peller 已提交
38
	struct ieee80211_vif *vif;
E
Eliad Peller 已提交
39
	struct wl12xx_vif *wlvif;
40
	int ret;
41 42 43

	dwork = container_of(work, struct delayed_work, work);
	wl = container_of(dwork, struct wl1271, scan_complete_work);
44 45 46 47

	wl1271_debug(DEBUG_SCAN, "Scanning complete");

	mutex_lock(&wl->mutex);
48

49 50 51 52 53
	if (wl->state == WL1271_STATE_OFF)
		goto out;

	if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
		goto out;
54

E
Eliad Peller 已提交
55 56
	vif = wl->scan_vif;
	wlvif = wl12xx_vif_to_data(vif);
E
Eliad Peller 已提交
57

A
Arik Nemtsov 已提交
58 59 60 61 62 63
	/*
	 * Rearm the tx watchdog just before idling scan. This
	 * prevents just-finished scans from triggering the watchdog
	 */
	wl12xx_rearm_tx_watchdog_locked(wl);

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 70 71 72
	ret = wl1271_ps_elp_wakeup(wl);
	if (ret < 0)
		goto out;

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);
E
Eliad Peller 已提交
76 77
	}

78
	wl1271_ps_elp_sleep(wl);
79

80 81
	if (wl->scan.failed) {
		wl1271_info("Scan completed due to error.");
82
		wl12xx_queue_recovery_work(wl);
83
	}
84

85 86
	ieee80211_scan_completed(wl->hw, false);

87
out:
88 89
	mutex_unlock(&wl->mutex);

90 91 92
}


L
Luciano Coelho 已提交
93 94 95 96
static int wl1271_get_scan_channels(struct wl1271 *wl,
				    struct cfg80211_scan_request *req,
				    struct basic_scan_channel_params *channels,
				    enum ieee80211_band band, bool passive)
97
{
J
Juuso Oikarinen 已提交
98
	struct conf_scan_settings *c = &wl->conf.scan;
L
Luciano Coelho 已提交
99 100
	int i, j;
	u32 flags;
101

L
Luciano Coelho 已提交
102 103 104 105
	for (i = 0, j = 0;
	     i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
	     i++) {
		flags = req->channels[i]->flags;
106

107
		if (!test_bit(i, wl->scan.scanned_ch) &&
L
Luciano Coelho 已提交
108
		    !(flags & IEEE80211_CHAN_DISABLED) &&
109 110 111 112 113 114 115 116
		    (req->channels[i]->band == band) &&
		    /*
		     * In passive scans, we scan all remaining
		     * channels, even if not marked as such.
		     * In active scans, we only scan channels not
		     * marked as passive.
		     */
		    (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
L
Luciano Coelho 已提交
117 118 119 120 121 122 123 124 125 126 127 128
			wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
				     req->channels[i]->band,
				     req->channels[i]->center_freq);
			wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
				     req->channels[i]->hw_value,
				     req->channels[i]->flags);
			wl1271_debug(DEBUG_SCAN,
				     "max_antenna_gain %d, max_power %d",
				     req->channels[i]->max_antenna_gain,
				     req->channels[i]->max_power);
			wl1271_debug(DEBUG_SCAN, "beacon_found %d",
				     req->channels[i]->beacon_found);
129

J
Juuso Oikarinen 已提交
130 131 132 133 134 135 136 137 138 139 140
			if (!passive) {
				channels[j].min_duration =
					cpu_to_le32(c->min_dwell_time_active);
				channels[j].max_duration =
					cpu_to_le32(c->max_dwell_time_active);
			} else {
				channels[j].min_duration =
					cpu_to_le32(c->min_dwell_time_passive);
				channels[j].max_duration =
					cpu_to_le32(c->max_dwell_time_passive);
			}
L
Luciano Coelho 已提交
141
			channels[j].early_termination = 0;
142
			channels[j].tx_power_att = req->channels[i]->max_power;
L
Luciano Coelho 已提交
143 144 145 146 147 148
			channels[j].channel = req->channels[i]->hw_value;

			memset(&channels[j].bssid_lsb, 0xff, 4);
			memset(&channels[j].bssid_msb, 0xff, 2);

			/* Mark the channels we already used */
149
			set_bit(i, wl->scan.scanned_ch);
L
Luciano Coelho 已提交
150

151 152 153 154
			j++;
		}
	}

L
Luciano Coelho 已提交
155 156
	return j;
}
157

L
Luciano Coelho 已提交
158 159
#define WL1271_NOTHING_TO_SCAN 1

160 161 162
static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
			    enum ieee80211_band band,
			    bool passive, u32 basic_rate)
L
Luciano Coelho 已提交
163
{
E
Eliad Peller 已提交
164
	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
L
Luciano Coelho 已提交
165 166 167 168 169
	struct wl1271_cmd_scan *cmd;
	struct wl1271_cmd_trigger_scan_to *trigger;
	int ret;
	u16 scan_options = 0;

170 171 172 173
	/* skip active scans if we don't have SSIDs */
	if (!passive && wl->scan.req->n_ssids == 0)
		return WL1271_NOTHING_TO_SCAN;

L
Luciano Coelho 已提交
174 175 176 177 178
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
	if (!cmd || !trigger) {
		ret = -ENOMEM;
		goto out;
179 180
	}

181 182 183
	if (wl->conf.scan.split_scan_timeout)
		scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;

184
	if (passive)
L
Luciano Coelho 已提交
185
		scan_options |= WL1271_SCAN_OPT_PASSIVE;
L
Luciano Coelho 已提交
186

E
Eliad Peller 已提交
187 188
	if (wlvif->bss_type == BSS_TYPE_AP_BSS ||
	    test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
E
Eliad Peller 已提交
189 190 191 192
		cmd->params.role_id = wlvif->role_id;
	else
		cmd->params.role_id = wlvif->dev_role_id;

E
Eliad Peller 已提交
193 194 195 196 197
	if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
		ret = -EINVAL;
		goto out;
	}

L
Luciano Coelho 已提交
198 199 200 201 202 203 204
	cmd->params.scan_options = cpu_to_le16(scan_options);

	cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
						    cmd->channels,
						    band, passive);
	if (cmd->params.n_ch == 0) {
		ret = WL1271_NOTHING_TO_SCAN;
205 206 207
		goto out;
	}

L
Luciano Coelho 已提交
208
	cmd->params.tx_rate = cpu_to_le32(basic_rate);
J
Juuso Oikarinen 已提交
209
	cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
210
	cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
L
Luciano Coelho 已提交
211 212 213 214 215 216 217 218 219 220 221 222
	cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;

	if (band == IEEE80211_BAND_2GHZ)
		cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
	else
		cmd->params.band = WL1271_SCAN_BAND_5_GHZ;

	if (wl->scan.ssid_len && wl->scan.ssid) {
		cmd->params.ssid_len = wl->scan.ssid_len;
		memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
	}

223
	memcpy(cmd->addr, vif->addr, ETH_ALEN);
E
Eliad Peller 已提交
224

225
	ret = wl12xx_cmd_build_probe_req(wl, wlvif,
E
Eliad Peller 已提交
226
					 cmd->params.role_id, band,
227 228
					 wl->scan.ssid, wl->scan.ssid_len,
					 wl->scan.req->ie,
229
					 wl->scan.req->ie_len, false);
L
Luciano Coelho 已提交
230 231
	if (ret < 0) {
		wl1271_error("PROBE request template failed");
232 233 234
		goto out;
	}

235
	trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
236 237 238 239 240 241 242
	ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
			      sizeof(*trigger), 0);
	if (ret < 0) {
		wl1271_error("trigger scan to failed for hw scan");
		goto out;
	}

L
Luciano Coelho 已提交
243
	wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
244

L
Luciano Coelho 已提交
245
	ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
246 247 248 249 250 251
	if (ret < 0) {
		wl1271_error("SCAN failed");
		goto out;
	}

out:
L
Luciano Coelho 已提交
252
	kfree(cmd);
253 254 255 256
	kfree(trigger);
	return ret;
}

257
void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
258
{
259
	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
260
	int ret = 0;
261
	enum ieee80211_band band;
262
	u32 rate, mask;
L
Luciano Coelho 已提交
263 264 265 266 267 268

	switch (wl->scan.state) {
	case WL1271_SCAN_STATE_IDLE:
		break;

	case WL1271_SCAN_STATE_2GHZ_ACTIVE:
269
		band = IEEE80211_BAND_2GHZ;
270 271 272 273 274 275 276
		mask = wlvif->bitrate_masks[band];
		if (wl->scan.req->no_cck) {
			mask &= ~CONF_TX_CCK_RATES;
			if (!mask)
				mask = CONF_TX_RATE_MASK_BASIC_P2P;
		}
		rate = wl1271_tx_min_rate_get(wl, mask);
277
		ret = wl1271_scan_send(wl, vif, band, false, rate);
L
Luciano Coelho 已提交
278 279
		if (ret == WL1271_NOTHING_TO_SCAN) {
			wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
280
			wl1271_scan_stm(wl, vif);
L
Luciano Coelho 已提交
281 282 283 284 285
		}

		break;

	case WL1271_SCAN_STATE_2GHZ_PASSIVE:
286
		band = IEEE80211_BAND_2GHZ;
287 288 289 290 291 292 293
		mask = wlvif->bitrate_masks[band];
		if (wl->scan.req->no_cck) {
			mask &= ~CONF_TX_CCK_RATES;
			if (!mask)
				mask = CONF_TX_RATE_MASK_BASIC_P2P;
		}
		rate = wl1271_tx_min_rate_get(wl, mask);
294
		ret = wl1271_scan_send(wl, vif, band, true, rate);
L
Luciano Coelho 已提交
295
		if (ret == WL1271_NOTHING_TO_SCAN) {
296
			if (wl->enable_11a)
L
Luciano Coelho 已提交
297 298 299
				wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
			else
				wl->scan.state = WL1271_SCAN_STATE_DONE;
300
			wl1271_scan_stm(wl, vif);
301
		}
L
Luciano Coelho 已提交
302 303 304 305

		break;

	case WL1271_SCAN_STATE_5GHZ_ACTIVE:
306
		band = IEEE80211_BAND_5GHZ;
307
		rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
308
		ret = wl1271_scan_send(wl, vif, band, false, rate);
L
Luciano Coelho 已提交
309 310
		if (ret == WL1271_NOTHING_TO_SCAN) {
			wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
311
			wl1271_scan_stm(wl, vif);
L
Luciano Coelho 已提交
312 313 314 315 316
		}

		break;

	case WL1271_SCAN_STATE_5GHZ_PASSIVE:
317
		band = IEEE80211_BAND_5GHZ;
318
		rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
319
		ret = wl1271_scan_send(wl, vif, band, true, rate);
L
Luciano Coelho 已提交
320 321
		if (ret == WL1271_NOTHING_TO_SCAN) {
			wl->scan.state = WL1271_SCAN_STATE_DONE;
322
			wl1271_scan_stm(wl, vif);
L
Luciano Coelho 已提交
323 324 325 326 327
		}

		break;

	case WL1271_SCAN_STATE_DONE:
328 329 330 331
		wl->scan.failed = false;
		cancel_delayed_work(&wl->scan_complete_work);
		ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
					     msecs_to_jiffies(0));
L
Luciano Coelho 已提交
332 333 334 335 336
		break;

	default:
		wl1271_error("invalid scan state");
		break;
337
	}
338 339 340 341 342 343

	if (ret < 0) {
		cancel_delayed_work(&wl->scan_complete_work);
		ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
					     msecs_to_jiffies(0));
	}
L
Luciano Coelho 已提交
344 345
}

346 347
int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
		const u8 *ssid, size_t ssid_len,
L
Luciano Coelho 已提交
348 349
		struct cfg80211_scan_request *req)
{
350 351 352 353 354 355
	/*
	 * cfg80211 should guarantee that we don't get more channels
	 * than what we have registered.
	 */
	BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);

L
Luciano Coelho 已提交
356 357 358 359 360 361 362 363 364 365 366 367
	if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
		return -EBUSY;

	wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;

	if (ssid_len && ssid) {
		wl->scan.ssid_len = ssid_len;
		memcpy(wl->scan.ssid, ssid, ssid_len);
	} else {
		wl->scan.ssid_len = 0;
	}

368
	wl->scan_vif = vif;
L
Luciano Coelho 已提交
369
	wl->scan.req = req;
370
	memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
L
Luciano Coelho 已提交
371

372 373 374 375 376
	/* we assume failure so that timeout scenarios are handled correctly */
	wl->scan.failed = true;
	ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
				     msecs_to_jiffies(WL1271_SCAN_TIMEOUT));

377
	wl1271_scan_stm(wl, vif);
L
Luciano Coelho 已提交
378

379 380
	return 0;
}
381

E
Eliad Peller 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
int wl1271_scan_stop(struct wl1271 *wl)
{
	struct wl1271_cmd_header *cmd = NULL;
	int ret = 0;

	if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
		return -EINVAL;

	wl1271_debug(DEBUG_CMD, "cmd scan stop");

	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd) {
		ret = -ENOMEM;
		goto out;
	}

	ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
			      sizeof(*cmd), 0);
	if (ret < 0) {
		wl1271_error("cmd stop_scan failed");
		goto out;
	}
out:
	kfree(cmd);
	return ret;
}

409 410 411 412 413
static int
wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
				    struct cfg80211_sched_scan_request *req,
				    struct conn_scan_ch_params *channels,
				    u32 band, bool radar, bool passive,
414 415
				    int start, int max_channels,
				    u8 *n_pactive_ch)
416 417 418 419
{
	struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
	int i, j;
	u32 flags;
420
	bool force_passive = !req->n_ssids;
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	u32 min_dwell_time_active, max_dwell_time_active, delta_per_probe;
	u32 dwell_time_passive, dwell_time_dfs;

	if (band == IEEE80211_BAND_5GHZ)
		delta_per_probe = c->dwell_time_delta_per_probe_5;
	else
		delta_per_probe = c->dwell_time_delta_per_probe;

	min_dwell_time_active = c->base_dwell_time +
		 req->n_ssids * c->num_probe_reqs * delta_per_probe;

	max_dwell_time_active = min_dwell_time_active + c->max_dwell_time_delta;

	min_dwell_time_active = DIV_ROUND_UP(min_dwell_time_active, 1000);
	max_dwell_time_active = DIV_ROUND_UP(max_dwell_time_active, 1000);
	dwell_time_passive = DIV_ROUND_UP(c->dwell_time_passive, 1000);
	dwell_time_dfs = DIV_ROUND_UP(c->dwell_time_dfs, 1000);
438 439

	for (i = 0, j = start;
440
	     i < req->n_channels && j < max_channels;
441 442 443
	     i++) {
		flags = req->channels[i]->flags;

444 445 446
		if (force_passive)
			flags |= IEEE80211_CHAN_PASSIVE_SCAN;

447 448
		if ((req->channels[i]->band == band) &&
		    !(flags & IEEE80211_CHAN_DISABLED) &&
449
		    (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
450 451 452
		    /* if radar is set, we ignore the passive flag */
		    (radar ||
		     !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
453 454 455 456 457 458 459 460
			wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
				     req->channels[i]->band,
				     req->channels[i]->center_freq);
			wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
				     req->channels[i]->hw_value,
				     req->channels[i]->flags);
			wl1271_debug(DEBUG_SCAN, "max_power %d",
				     req->channels[i]->max_power);
461 462 463
			wl1271_debug(DEBUG_SCAN, "min_dwell_time %d max dwell time %d",
				     min_dwell_time_active,
				     max_dwell_time_active);
464

465
			if (flags & IEEE80211_CHAN_RADAR) {
466
				channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
467

468
				channels[j].passive_duration =
469
					cpu_to_le16(dwell_time_dfs);
470
			} else {
471
				channels[j].passive_duration =
472
					cpu_to_le16(dwell_time_passive);
473
			}
474 475

			channels[j].min_duration =
476
				cpu_to_le16(min_dwell_time_active);
477
			channels[j].max_duration =
478
				cpu_to_le16(max_dwell_time_active);
479

480
			channels[j].tx_power_att = req->channels[i]->max_power;
481 482
			channels[j].channel = req->channels[i]->hw_value;

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
			if ((band == IEEE80211_BAND_2GHZ) &&
			    (channels[j].channel >= 12) &&
			    (channels[j].channel <= 14) &&
			    (flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
			    !force_passive) {
				/* pactive channels treated as DFS */
				channels[j].flags = SCAN_CHANNEL_FLAGS_DFS;

				/*
				 * n_pactive_ch is counted down from the end of
				 * the passive channel list
				 */
				(*n_pactive_ch)++;
				wl1271_debug(DEBUG_SCAN, "n_pactive_ch = %d",
					     *n_pactive_ch);
			}

500 501 502 503 504 505 506
			j++;
		}
	}

	return j - start;
}

507
static bool
508 509 510 511
wl1271_scan_sched_scan_channels(struct wl1271 *wl,
				struct cfg80211_sched_scan_request *req,
				struct wl1271_cmd_sched_scan_config *cfg)
{
512 513
	u8 n_pactive_ch = 0;

514
	cfg->passive[0] =
515
		wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
516
						    IEEE80211_BAND_2GHZ,
517
						    false, true, 0,
518 519
						    MAX_CHANNELS_2GHZ,
						    &n_pactive_ch);
520
	cfg->active[0] =
521
		wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
522
						    IEEE80211_BAND_2GHZ,
523 524
						    false, false,
						    cfg->passive[0],
525 526
						    MAX_CHANNELS_2GHZ,
						    &n_pactive_ch);
527
	cfg->passive[1] =
528
		wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
529
						    IEEE80211_BAND_5GHZ,
530
						    false, true, 0,
531 532
						    MAX_CHANNELS_5GHZ,
						    &n_pactive_ch);
533
	cfg->dfs =
534
		wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
535
						    IEEE80211_BAND_5GHZ,
536 537
						    true, true,
						    cfg->passive[1],
538 539
						    MAX_CHANNELS_5GHZ,
						    &n_pactive_ch);
540
	cfg->active[1] =
541
		wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
542
						    IEEE80211_BAND_5GHZ,
543 544
						    false, false,
						    cfg->passive[1] + cfg->dfs,
545 546
						    MAX_CHANNELS_5GHZ,
						    &n_pactive_ch);
547 548 549
	/* 802.11j channels are not supported yet */
	cfg->passive[2] = 0;
	cfg->active[2] = 0;
550

551 552
	cfg->n_pactive_ch = n_pactive_ch;

553 554 555 556
	wl1271_debug(DEBUG_SCAN, "    2.4GHz: active %d passive %d",
		     cfg->active[0], cfg->passive[0]);
	wl1271_debug(DEBUG_SCAN, "    5GHz: active %d passive %d",
		     cfg->active[1], cfg->passive[1]);
557
	wl1271_debug(DEBUG_SCAN, "    DFS: %d", cfg->dfs);
558

559 560 561
	return  cfg->passive[0] || cfg->active[0] ||
		cfg->passive[1] || cfg->active[1] || cfg->dfs ||
		cfg->passive[2] || cfg->active[2];
562 563
}

564
/* Returns the scan type to be used or a negative value on error */
565 566
static int
wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
567
				 struct wl12xx_vif *wlvif,
568 569 570
				 struct cfg80211_sched_scan_request *req)
{
	struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
571 572
	struct cfg80211_match_set *sets = req->match_sets;
	struct cfg80211_ssid *ssids = req->ssids;
573
	int ret = 0, type, i, j, n_match_ssids = 0;
574 575 576

	wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");

577 578 579 580 581
	/* count the match sets that contain SSIDs */
	for (i = 0; i < req->n_match_sets; i++)
		if (sets[i].ssid.ssid_len > 0)
			n_match_ssids++;

582
	/* No filter, no ssids or only bcast ssid */
583
	if (!n_match_ssids &&
584 585 586 587 588 589
	    (!req->n_ssids ||
	     (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
		type = SCAN_SSID_FILTER_ANY;
		goto out;
	}

590
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
591 592 593 594 595
	if (!cmd) {
		ret = -ENOMEM;
		goto out;
	}

596
	cmd->role_id = wlvif->dev_role_id;
597
	if (!n_match_ssids) {
598 599 600 601 602 603 604 605 606 607 608 609 610
		/* No filter, with ssids */
		type = SCAN_SSID_FILTER_DISABLED;

		for (i = 0; i < req->n_ssids; i++) {
			cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
				SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
			cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
			memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
			       ssids[i].ssid_len);
			cmd->n_ssids++;
		}
	} else {
		type = SCAN_SSID_FILTER_LIST;
611

612 613
		/* Add all SSIDs from the filters */
		for (i = 0; i < req->n_match_sets; i++) {
614 615 616 617
			/* ignore sets without SSIDs */
			if (!sets[i].ssid.ssid_len)
				continue;

618
			cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
619 620 621 622 623 624 625 626 627 628 629 630
			cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
			memcpy(cmd->ssids[cmd->n_ssids].ssid,
			       sets[i].ssid.ssid, sets[i].ssid.ssid_len);
			cmd->n_ssids++;
		}
		if ((req->n_ssids > 1) ||
		    (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
			/*
			 * Mark all the SSIDs passed in the SSID list as HIDDEN,
			 * so they're used in probe requests.
			 */
			for (i = 0; i < req->n_ssids; i++) {
631 632 633
				if (!req->ssids[i].ssid_len)
					continue;

634
				for (j = 0; j < cmd->n_ssids; j++)
635 636 637
					if ((req->ssids[i].ssid_len ==
					     req->ssids[j].ssid_len) &&
					    !memcmp(req->ssids[i].ssid,
638 639 640 641 642 643 644
						   cmd->ssids[j].ssid,
						   req->ssids[i].ssid_len)) {
						cmd->ssids[j].type =
							SCAN_SSID_TYPE_HIDDEN;
						break;
					}
				/* Fail if SSID isn't present in the filters */
645
				if (j == cmd->n_ssids) {
646 647 648 649
					ret = -EINVAL;
					goto out_free;
				}
			}
650
		}
651 652 653 654 655 656 657 658
	}

	wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));

	ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
			      sizeof(*cmd), 0);
	if (ret < 0) {
		wl1271_error("cmd sched scan ssid list failed");
659
		goto out_free;
660 661
	}

662
out_free:
663
	kfree(cmd);
664 665 666 667
out:
	if (ret < 0)
		return ret;
	return type;
668 669
}

670
int wl1271_scan_sched_scan_config(struct wl1271 *wl,
E
Eliad Peller 已提交
671
				  struct wl12xx_vif *wlvif,
672 673 674 675 676
				  struct cfg80211_sched_scan_request *req,
				  struct ieee80211_sched_scan_ies *ies)
{
	struct wl1271_cmd_sched_scan_config *cfg = NULL;
	struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
677
	int i, ret;
678
	bool force_passive = !req->n_ssids;
679 680 681 682 683 684 685

	wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");

	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
	if (!cfg)
		return -ENOMEM;

686
	cfg->role_id = wlvif->dev_role_id;
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	cfg->rssi_threshold = c->rssi_threshold;
	cfg->snr_threshold  = c->snr_threshold;
	cfg->n_probe_reqs = c->num_probe_reqs;
	/* cycles set to 0 it means infinite (until manually stopped) */
	cfg->cycles = 0;
	/* report APs when at least 1 is found */
	cfg->report_after = 1;
	/* don't stop scanning automatically when something is found */
	cfg->terminate = 0;
	cfg->tag = WL1271_SCAN_DEFAULT_TAG;
	/* don't filter on BSS type */
	cfg->bss_type = SCAN_BSS_TYPE_ANY;
	/* currently NL80211 supports only a single interval */
	for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
		cfg->intervals[i] = cpu_to_le32(req->interval);

703
	cfg->ssid_len = 0;
704
	ret = wl12xx_scan_sched_scan_ssid_list(wl, wlvif, req);
705 706 707 708 709 710
	if (ret < 0)
		goto out;

	cfg->filter_type = ret;

	wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
711

712
	if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
713 714 715 716 717
		wl1271_error("scan channel list is empty");
		ret = -EINVAL;
		goto out;
	}

718
	if (!force_passive && cfg->active[0]) {
719 720
		u8 band = IEEE80211_BAND_2GHZ;
		ret = wl12xx_cmd_build_probe_req(wl, wlvif,
E
Eliad Peller 已提交
721
						 wlvif->dev_role_id, band,
722
						 req->ssids[0].ssid,
723
						 req->ssids[0].ssid_len,
724
						 ies->ie[band],
725
						 ies->len[band], true);
726 727 728 729 730 731
		if (ret < 0) {
			wl1271_error("2.4GHz PROBE request template failed");
			goto out;
		}
	}

732
	if (!force_passive && cfg->active[1]) {
733 734
		u8 band = IEEE80211_BAND_5GHZ;
		ret = wl12xx_cmd_build_probe_req(wl, wlvif,
E
Eliad Peller 已提交
735
						 wlvif->dev_role_id, band,
736
						 req->ssids[0].ssid,
737
						 req->ssids[0].ssid_len,
738
						 ies->ie[band],
739
						 ies->len[band], true);
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
		if (ret < 0) {
			wl1271_error("5GHz PROBE request template failed");
			goto out;
		}
	}

	wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));

	ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
			      sizeof(*cfg), 0);
	if (ret < 0) {
		wl1271_error("SCAN configuration failed");
		goto out;
	}
out:
	kfree(cfg);
	return ret;
}

E
Eliad Peller 已提交
759
int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
760 761 762 763 764 765
{
	struct wl1271_cmd_sched_scan_start *start;
	int ret = 0;

	wl1271_debug(DEBUG_CMD, "cmd periodic scan start");

E
Eliad Peller 已提交
766
	if (wlvif->bss_type != BSS_TYPE_STA_BSS)
767 768
		return -EOPNOTSUPP;

769 770
	if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
	    test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
771 772
		return -EBUSY;

773 774 775 776
	start = kzalloc(sizeof(*start), GFP_KERNEL);
	if (!start)
		return -ENOMEM;

777
	start->role_id = wlvif->dev_role_id;
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	start->tag = WL1271_SCAN_DEFAULT_TAG;

	ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
			      sizeof(*start), 0);
	if (ret < 0) {
		wl1271_error("failed to send scan start command");
		goto out_free;
	}

out_free:
	kfree(start);
	return ret;
}

void wl1271_scan_sched_scan_results(struct wl1271 *wl)
{
	wl1271_debug(DEBUG_SCAN, "got periodic scan results");

	ieee80211_sched_scan_results(wl->hw);
}

799
void wl1271_scan_sched_scan_stop(struct wl1271 *wl,  struct wl12xx_vif *wlvif)
800 801 802 803 804 805 806 807 808 809 810 811 812
{
	struct wl1271_cmd_sched_scan_stop *stop;
	int ret = 0;

	wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");

	/* FIXME: what to do if alloc'ing to stop fails? */
	stop = kzalloc(sizeof(*stop), GFP_KERNEL);
	if (!stop) {
		wl1271_error("failed to alloc memory to send sched scan stop");
		return;
	}

813
	stop->role_id = wlvif->dev_role_id;
814 815 816 817
	stop->tag = WL1271_SCAN_DEFAULT_TAG;

	ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
			      sizeof(*stop), 0);
818
	if (ret < 0) {
819
		wl1271_error("failed to send sched scan stop command");
820 821
		goto out_free;
	}
822

823
out_free:
824 825
	kfree(stop);
}