1/*
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	cmd->params.role_id = wlvif->role_id;
122
123	if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
124		ret = -EINVAL;
125		goto out;
126	}
127
128	cmd->params.scan_options = cpu_to_le16(scan_options);
129
130	cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
131						    cmd->channels,
132						    band, passive);
133	if (cmd->params.n_ch == 0) {
134		ret = WL1271_NOTHING_TO_SCAN;
135		goto out;
136	}
137
138	cmd->params.tx_rate = cpu_to_le32(basic_rate);
139	cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
140	cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
141	cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
142
143	if (band == IEEE80211_BAND_2GHZ)
144		cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
145	else
146		cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
147
148	if (wl->scan.ssid_len && wl->scan.ssid) {
149		cmd->params.ssid_len = wl->scan.ssid_len;
150		memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
151	}
152
153	memcpy(cmd->addr, vif->addr, ETH_ALEN);
154
155	ret = wl12xx_cmd_build_probe_req(wl, wlvif,
156					 cmd->params.role_id, band,
157					 wl->scan.ssid, wl->scan.ssid_len,
158					 wl->scan.req->ie,
159					 wl->scan.req->ie_len, NULL, 0, false);
160	if (ret < 0) {
161		wl1271_error("PROBE request template failed");
162		goto out;
163	}
164
165	trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
166	ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
167			      sizeof(*trigger), 0);
168	if (ret < 0) {
169		wl1271_error("trigger scan to failed for hw scan");
170		goto out;
171	}
172
173	wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
174
175	ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
176	if (ret < 0) {
177		wl1271_error("SCAN failed");
178		goto out;
179	}
180
181out:
182	kfree(cmd);
183	kfree(trigger);
184	return ret;
185}
186
187int wl12xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
188{
189	struct wl1271_cmd_header *cmd = NULL;
190	int ret = 0;
191
192	if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
193		return -EINVAL;
194
195	wl1271_debug(DEBUG_CMD, "cmd scan stop");
196
197	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
198	if (!cmd) {
199		ret = -ENOMEM;
200		goto out;
201	}
202
203	ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
204			      sizeof(*cmd), 0);
205	if (ret < 0) {
206		wl1271_error("cmd stop_scan failed");
207		goto out;
208	}
209out:
210	kfree(cmd);
211	return ret;
212}
213
214void wl1271_scan_stm(struct wl1271 *wl, struct wl12xx_vif *wlvif)
215{
216	int ret = 0;
217	enum ieee80211_band band;
218	u32 rate, mask;
219
220	switch (wl->scan.state) {
221	case WL1271_SCAN_STATE_IDLE:
222		break;
223
224	case WL1271_SCAN_STATE_2GHZ_ACTIVE:
225		band = IEEE80211_BAND_2GHZ;
226		mask = wlvif->bitrate_masks[band];
227		if (wl->scan.req->no_cck) {
228			mask &= ~CONF_TX_CCK_RATES;
229			if (!mask)
230				mask = CONF_TX_RATE_MASK_BASIC_P2P;
231		}
232		rate = wl1271_tx_min_rate_get(wl, mask);
233		ret = wl1271_scan_send(wl, wlvif, band, false, rate);
234		if (ret == WL1271_NOTHING_TO_SCAN) {
235			wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
236			wl1271_scan_stm(wl, wlvif);
237		}
238
239		break;
240
241	case WL1271_SCAN_STATE_2GHZ_PASSIVE:
242		band = IEEE80211_BAND_2GHZ;
243		mask = wlvif->bitrate_masks[band];
244		if (wl->scan.req->no_cck) {
245			mask &= ~CONF_TX_CCK_RATES;
246			if (!mask)
247				mask = CONF_TX_RATE_MASK_BASIC_P2P;
248		}
249		rate = wl1271_tx_min_rate_get(wl, mask);
250		ret = wl1271_scan_send(wl, wlvif, band, true, rate);
251		if (ret == WL1271_NOTHING_TO_SCAN) {
252			if (wl->enable_11a)
253				wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
254			else
255				wl->scan.state = WL1271_SCAN_STATE_DONE;
256			wl1271_scan_stm(wl, wlvif);
257		}
258
259		break;
260
261	case WL1271_SCAN_STATE_5GHZ_ACTIVE:
262		band = IEEE80211_BAND_5GHZ;
263		rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
264		ret = wl1271_scan_send(wl, wlvif, band, false, rate);
265		if (ret == WL1271_NOTHING_TO_SCAN) {
266			wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
267			wl1271_scan_stm(wl, wlvif);
268		}
269
270		break;
271
272	case WL1271_SCAN_STATE_5GHZ_PASSIVE:
273		band = IEEE80211_BAND_5GHZ;
274		rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
275		ret = wl1271_scan_send(wl, wlvif, band, true, rate);
276		if (ret == WL1271_NOTHING_TO_SCAN) {
277			wl->scan.state = WL1271_SCAN_STATE_DONE;
278			wl1271_scan_stm(wl, wlvif);
279		}
280
281		break;
282
283	case WL1271_SCAN_STATE_DONE:
284		wl->scan.failed = false;
285		cancel_delayed_work(&wl->scan_complete_work);
286		ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
287					     msecs_to_jiffies(0));
288		break;
289
290	default:
291		wl1271_error("invalid scan state");
292		break;
293	}
294
295	if (ret < 0) {
296		cancel_delayed_work(&wl->scan_complete_work);
297		ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
298					     msecs_to_jiffies(0));
299	}
300}
301
302static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config *cmd,
303				   struct wlcore_scan_channels *cmd_channels)
304{
305	memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive));
306	memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active));
307	cmd->dfs = cmd_channels->dfs;
308	cmd->n_pactive_ch = cmd_channels->passive_active;
309
310	memcpy(cmd->channels_2, cmd_channels->channels_2,
311	       sizeof(cmd->channels_2));
312	memcpy(cmd->channels_5, cmd_channels->channels_5,
313	       sizeof(cmd->channels_5));
314	/* channels_4 are not supported, so no need to copy them */
315}
316
317int wl1271_scan_sched_scan_config(struct wl1271 *wl,
318				  struct wl12xx_vif *wlvif,
319				  struct cfg80211_sched_scan_request *req,
320				  struct ieee80211_scan_ies *ies)
321{
322	struct wl1271_cmd_sched_scan_config *cfg = NULL;
323	struct wlcore_scan_channels *cfg_channels = NULL;
324	struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
325	int i, ret;
326	bool force_passive = !req->n_ssids;
327
328	wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
329
330	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
331	if (!cfg)
332		return -ENOMEM;
333
334	cfg->role_id = wlvif->role_id;
335	cfg->rssi_threshold = c->rssi_threshold;
336	cfg->snr_threshold  = c->snr_threshold;
337	cfg->n_probe_reqs = c->num_probe_reqs;
338	/* cycles set to 0 it means infinite (until manually stopped) */
339	cfg->cycles = 0;
340	/* report APs when at least 1 is found */
341	cfg->report_after = 1;
342	/* don't stop scanning automatically when something is found */
343	cfg->terminate = 0;
344	cfg->tag = WL1271_SCAN_DEFAULT_TAG;
345	/* don't filter on BSS type */
346	cfg->bss_type = SCAN_BSS_TYPE_ANY;
347	/* currently NL80211 supports only a single interval */
348	for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
349		cfg->intervals[i] = cpu_to_le32(req->interval);
350
351	cfg->ssid_len = 0;
352	ret = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req);
353	if (ret < 0)
354		goto out;
355
356	cfg->filter_type = ret;
357
358	wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
359
360	cfg_channels = kzalloc(sizeof(*cfg_channels), GFP_KERNEL);
361	if (!cfg_channels) {
362		ret = -ENOMEM;
363		goto out;
364	}
365
366	if (!wlcore_set_scan_chan_params(wl, cfg_channels, req->channels,
367					 req->n_channels, req->n_ssids,
368					 SCAN_TYPE_PERIODIC)) {
369		wl1271_error("scan channel list is empty");
370		ret = -EINVAL;
371		goto out;
372	}
373	wl12xx_adjust_channels(cfg, cfg_channels);
374
375	if (!force_passive && cfg->active[0]) {
376		u8 band = IEEE80211_BAND_2GHZ;
377		ret = wl12xx_cmd_build_probe_req(wl, wlvif,
378						 wlvif->role_id, band,
379						 req->ssids[0].ssid,
380						 req->ssids[0].ssid_len,
381						 ies->ies[band],
382						 ies->len[band],
383						 ies->common_ies,
384						 ies->common_ie_len,
385						 true);
386		if (ret < 0) {
387			wl1271_error("2.4GHz PROBE request template failed");
388			goto out;
389		}
390	}
391
392	if (!force_passive && cfg->active[1]) {
393		u8 band = IEEE80211_BAND_5GHZ;
394		ret = wl12xx_cmd_build_probe_req(wl, wlvif,
395						 wlvif->role_id, band,
396						 req->ssids[0].ssid,
397						 req->ssids[0].ssid_len,
398						 ies->ies[band],
399						 ies->len[band],
400						 ies->common_ies,
401						 ies->common_ie_len,
402						 true);
403		if (ret < 0) {
404			wl1271_error("5GHz PROBE request template failed");
405			goto out;
406		}
407	}
408
409	wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
410
411	ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
412			      sizeof(*cfg), 0);
413	if (ret < 0) {
414		wl1271_error("SCAN configuration failed");
415		goto out;
416	}
417out:
418	kfree(cfg_channels);
419	kfree(cfg);
420	return ret;
421}
422
423int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
424{
425	struct wl1271_cmd_sched_scan_start *start;
426	int ret = 0;
427
428	wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
429
430	if (wlvif->bss_type != BSS_TYPE_STA_BSS)
431		return -EOPNOTSUPP;
432
433	if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
434	    test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
435		return -EBUSY;
436
437	start = kzalloc(sizeof(*start), GFP_KERNEL);
438	if (!start)
439		return -ENOMEM;
440
441	start->role_id = wlvif->role_id;
442	start->tag = WL1271_SCAN_DEFAULT_TAG;
443
444	ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
445			      sizeof(*start), 0);
446	if (ret < 0) {
447		wl1271_error("failed to send scan start command");
448		goto out_free;
449	}
450
451out_free:
452	kfree(start);
453	return ret;
454}
455
456int wl12xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif  *wlvif,
457			    struct cfg80211_sched_scan_request *req,
458			    struct ieee80211_scan_ies *ies)
459{
460	int ret;
461
462	ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
463	if (ret < 0)
464		return ret;
465
466	return wl1271_scan_sched_scan_start(wl, wlvif);
467}
468
469void wl12xx_scan_sched_scan_stop(struct wl1271 *wl,  struct wl12xx_vif *wlvif)
470{
471	struct wl1271_cmd_sched_scan_stop *stop;
472	int ret = 0;
473
474	wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
475
476	/* FIXME: what to do if alloc'ing to stop fails? */
477	stop = kzalloc(sizeof(*stop), GFP_KERNEL);
478	if (!stop) {
479		wl1271_error("failed to alloc memory to send sched scan stop");
480		return;
481	}
482
483	stop->role_id = wlvif->role_id;
484	stop->tag = WL1271_SCAN_DEFAULT_TAG;
485
486	ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
487			      sizeof(*stop), 0);
488	if (ret < 0) {
489		wl1271_error("failed to send sched scan stop command");
490		goto out_free;
491	}
492
493out_free:
494	kfree(stop);
495}
496
497int wl12xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
498		      struct cfg80211_scan_request *req)
499{
500	wl1271_scan_stm(wl, wlvif);
501	return 0;
502}
503
504void wl12xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif)
505{
506	wl1271_scan_stm(wl, wlvif);
507}
508