blob: fc29c671cf3b34db3fc695fbd70209720a3c52e8 [file] [log] [blame]
Luciano Coelho34dd2aa2010-07-08 17:50:06 +03001/*
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
Shahar Levi00d20102010-11-08 11:20:10 +000026#include "wl12xx.h"
27#include "cmd.h"
28#include "scan.h"
29#include "acx.h"
Arik Nemtsov24225b32011-03-01 12:27:26 +020030#include "ps.h"
Eliad Pelleraf7fbb22011-09-19 13:51:42 +030031#include "tx.h"
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030032
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030033void wl1271_scan_complete_work(struct work_struct *work)
34{
Juuso Oikarinen78abd322010-09-21 06:23:32 +020035 struct delayed_work *dwork;
36 struct wl1271 *wl;
Eliad Peller251c1772011-08-14 13:17:17 +030037 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030038 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020039
40 dwork = container_of(work, struct delayed_work, work);
41 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030042
43 wl1271_debug(DEBUG_SCAN, "Scanning complete");
44
45 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020046
Arik Nemtsov24225b32011-03-01 12:27:26 +020047 if (wl->state == WL1271_STATE_OFF)
48 goto out;
49
50 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
51 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020052
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030053 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020054 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020055 wl->scan.req = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020056
Eliad Peller251c1772011-08-14 13:17:17 +030057 ret = wl1271_ps_elp_wakeup(wl);
58 if (ret < 0)
59 goto out;
60
Arik Nemtsov24225b32011-03-01 12:27:26 +020061 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030062 /* restore hardware connection monitoring template */
63 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030064 }
65
66 /* return to ROC if needed */
67 is_sta = (wl->bss_type == BSS_TYPE_STA_BSS);
68 is_ibss = (wl->bss_type == BSS_TYPE_IBSS);
Eliad Pellere0b38262011-08-28 15:11:55 +030069 if (((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
70 (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) &&
71 !test_bit(wl->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030072 /* restore remain on channel */
73 wl12xx_cmd_role_start_dev(wl);
74 wl12xx_roc(wl, wl->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020075 }
Eliad Peller251c1772011-08-14 13:17:17 +030076 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020077
Juuso Oikarinen78abd322010-09-21 06:23:32 +020078 if (wl->scan.failed) {
79 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9ae2011-06-06 14:57:05 +030080 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020081 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020082
Eliad Peller251c1772011-08-14 13:17:17 +030083 ieee80211_scan_completed(wl->hw, false);
84
Arik Nemtsov24225b32011-03-01 12:27:26 +020085out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020086 mutex_unlock(&wl->mutex);
87
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030088}
89
90
Luciano Coelho08688d62010-07-08 17:50:07 +030091static int wl1271_get_scan_channels(struct wl1271 *wl,
92 struct cfg80211_scan_request *req,
93 struct basic_scan_channel_params *channels,
94 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030095{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +020096 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +030097 int i, j;
98 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030099
Luciano Coelho08688d62010-07-08 17:50:07 +0300100 for (i = 0, j = 0;
101 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
102 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300103 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300104
Luciano Coelho4a31c112011-03-21 23:16:14 +0200105 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300106 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300107 (req->channels[i]->band == band) &&
108 /*
109 * In passive scans, we scan all remaining
110 * channels, even if not marked as such.
111 * In active scans, we only scan channels not
112 * marked as passive.
113 */
114 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300115 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
116 req->channels[i]->band,
117 req->channels[i]->center_freq);
118 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
119 req->channels[i]->hw_value,
120 req->channels[i]->flags);
121 wl1271_debug(DEBUG_SCAN,
122 "max_antenna_gain %d, max_power %d",
123 req->channels[i]->max_antenna_gain,
124 req->channels[i]->max_power);
125 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
126 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300127
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200128 if (!passive) {
129 channels[j].min_duration =
130 cpu_to_le32(c->min_dwell_time_active);
131 channels[j].max_duration =
132 cpu_to_le32(c->max_dwell_time_active);
133 } else {
134 channels[j].min_duration =
135 cpu_to_le32(c->min_dwell_time_passive);
136 channels[j].max_duration =
137 cpu_to_le32(c->max_dwell_time_passive);
138 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300139 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300140 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300141 channels[j].channel = req->channels[i]->hw_value;
142
143 memset(&channels[j].bssid_lsb, 0xff, 4);
144 memset(&channels[j].bssid_msb, 0xff, 2);
145
146 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200147 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300148
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300149 j++;
150 }
151 }
152
Luciano Coelho08688d62010-07-08 17:50:07 +0300153 return j;
154}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300155
Luciano Coelho08688d62010-07-08 17:50:07 +0300156#define WL1271_NOTHING_TO_SCAN 1
157
158static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
159 bool passive, u32 basic_rate)
160{
161 struct wl1271_cmd_scan *cmd;
162 struct wl1271_cmd_trigger_scan_to *trigger;
163 int ret;
164 u16 scan_options = 0;
165
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300166 /* skip active scans if we don't have SSIDs */
167 if (!passive && wl->scan.req->n_ssids == 0)
168 return WL1271_NOTHING_TO_SCAN;
169
Luciano Coelho08688d62010-07-08 17:50:07 +0300170 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
171 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
172 if (!cmd || !trigger) {
173 ret = -ENOMEM;
174 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300175 }
176
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300177 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300178 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300179
Eliad Pellera4e02f32011-08-14 13:17:10 +0300180 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
181 ret = -EINVAL;
182 goto out;
183 }
184 cmd->params.role_id = wl->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300185 cmd->params.scan_options = cpu_to_le16(scan_options);
186
187 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
188 cmd->channels,
189 band, passive);
190 if (cmd->params.n_ch == 0) {
191 ret = WL1271_NOTHING_TO_SCAN;
192 goto out;
193 }
194
195 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200196 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300197 cmd->params.tx_rate = cpu_to_le32(basic_rate);
198 cmd->params.tid_trigger = 0;
199 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
200
201 if (band == IEEE80211_BAND_2GHZ)
202 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
203 else
204 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
205
206 if (wl->scan.ssid_len && wl->scan.ssid) {
207 cmd->params.ssid_len = wl->scan.ssid_len;
208 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
209 }
210
Eliad Pellera4e02f32011-08-14 13:17:10 +0300211 memcpy(cmd->addr, wl->mac_addr, ETH_ALEN);
212
Luciano Coelho08688d62010-07-08 17:50:07 +0300213 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
214 wl->scan.req->ie, wl->scan.req->ie_len,
215 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300216 if (ret < 0) {
217 wl1271_error("PROBE request template failed");
218 goto out;
219 }
220
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300221 /* disable the timeout */
222 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300223 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
224 sizeof(*trigger), 0);
225 if (ret < 0) {
226 wl1271_error("trigger scan to failed for hw scan");
227 goto out;
228 }
229
Luciano Coelho08688d62010-07-08 17:50:07 +0300230 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300231
Luciano Coelho08688d62010-07-08 17:50:07 +0300232 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300233 if (ret < 0) {
234 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300235 goto out;
236 }
237
238out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300239 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300240 kfree(trigger);
241 return ret;
242}
243
Luciano Coelho08688d62010-07-08 17:50:07 +0300244void wl1271_scan_stm(struct wl1271 *wl)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300245{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200246 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300247 enum ieee80211_band band;
248 u32 rate;
Luciano Coelho08688d62010-07-08 17:50:07 +0300249
250 switch (wl->scan.state) {
251 case WL1271_SCAN_STATE_IDLE:
252 break;
253
254 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300255 band = IEEE80211_BAND_2GHZ;
256 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
257 ret = wl1271_scan_send(wl, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300258 if (ret == WL1271_NOTHING_TO_SCAN) {
259 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
260 wl1271_scan_stm(wl);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300261 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300262
263 break;
264
265 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300266 band = IEEE80211_BAND_2GHZ;
267 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
268 ret = wl1271_scan_send(wl, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300269 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200270 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300271 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
272 else
273 wl->scan.state = WL1271_SCAN_STATE_DONE;
274 wl1271_scan_stm(wl);
275 }
276
277 break;
278
279 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300280 band = IEEE80211_BAND_5GHZ;
281 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
282 ret = wl1271_scan_send(wl, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300283 if (ret == WL1271_NOTHING_TO_SCAN) {
284 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
285 wl1271_scan_stm(wl);
286 }
287
288 break;
289
290 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300291 band = IEEE80211_BAND_5GHZ;
292 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
293 ret = wl1271_scan_send(wl, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300294 if (ret == WL1271_NOTHING_TO_SCAN) {
295 wl->scan.state = WL1271_SCAN_STATE_DONE;
296 wl1271_scan_stm(wl);
297 }
298
299 break;
300
301 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200302 wl->scan.failed = false;
303 cancel_delayed_work(&wl->scan_complete_work);
304 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
305 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300306 break;
307
308 default:
309 wl1271_error("invalid scan state");
310 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300311 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200312
313 if (ret < 0) {
314 cancel_delayed_work(&wl->scan_complete_work);
315 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
316 msecs_to_jiffies(0));
317 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300318}
319
320int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
321 struct cfg80211_scan_request *req)
322{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200323 /*
324 * cfg80211 should guarantee that we don't get more channels
325 * than what we have registered.
326 */
327 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
328
Luciano Coelho08688d62010-07-08 17:50:07 +0300329 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
330 return -EBUSY;
331
332 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
333
334 if (ssid_len && ssid) {
335 wl->scan.ssid_len = ssid_len;
336 memcpy(wl->scan.ssid, ssid, ssid_len);
337 } else {
338 wl->scan.ssid_len = 0;
339 }
340
341 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200342 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300343
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200344 /* we assume failure so that timeout scenarios are handled correctly */
345 wl->scan.failed = true;
346 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
347 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
348
Luciano Coelho08688d62010-07-08 17:50:07 +0300349 wl1271_scan_stm(wl);
350
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300351 return 0;
352}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300353
Eliad Peller2aa01592011-06-27 13:06:44 +0300354int wl1271_scan_stop(struct wl1271 *wl)
355{
356 struct wl1271_cmd_header *cmd = NULL;
357 int ret = 0;
358
359 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
360 return -EINVAL;
361
362 wl1271_debug(DEBUG_CMD, "cmd scan stop");
363
364 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
365 if (!cmd) {
366 ret = -ENOMEM;
367 goto out;
368 }
369
370 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
371 sizeof(*cmd), 0);
372 if (ret < 0) {
373 wl1271_error("cmd stop_scan failed");
374 goto out;
375 }
376out:
377 kfree(cmd);
378 return ret;
379}
380
Luciano Coelho95feadc2011-05-10 14:38:59 +0300381static int
382wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
383 struct cfg80211_sched_scan_request *req,
384 struct conn_scan_ch_params *channels,
385 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300386 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300387{
388 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
389 int i, j;
390 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300391 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300392
393 for (i = 0, j = start;
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300394 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300395 i++) {
396 flags = req->channels[i]->flags;
397
Luciano Coelho66870b12011-05-27 15:34:48 +0300398 if (force_passive)
399 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
400
Luciano Coelho2497a242011-05-27 15:34:46 +0300401 if ((req->channels[i]->band == band) &&
402 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300403 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300404 /* if radar is set, we ignore the passive flag */
405 (radar ||
406 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300407 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
408 req->channels[i]->band,
409 req->channels[i]->center_freq);
410 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
411 req->channels[i]->hw_value,
412 req->channels[i]->flags);
413 wl1271_debug(DEBUG_SCAN, "max_power %d",
414 req->channels[i]->max_power);
415
Luciano Coelho50a66d72011-05-27 15:34:47 +0300416 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300417 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300418 channels[j].passive_duration =
419 cpu_to_le16(c->dwell_time_dfs);
420 }
421 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300422 channels[j].passive_duration =
423 cpu_to_le16(c->dwell_time_passive);
424 } else {
425 channels[j].min_duration =
426 cpu_to_le16(c->min_dwell_time_active);
427 channels[j].max_duration =
428 cpu_to_le16(c->max_dwell_time_active);
429 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300430 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300431 channels[j].channel = req->channels[i]->hw_value;
432
433 j++;
434 }
435 }
436
437 return j - start;
438}
439
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300440static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300441wl1271_scan_sched_scan_channels(struct wl1271 *wl,
442 struct cfg80211_sched_scan_request *req,
443 struct wl1271_cmd_sched_scan_config *cfg)
444{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300445 cfg->passive[0] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300446 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300447 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300448 false, true, 0,
449 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300450 cfg->active[0] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300451 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300452 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300453 false, false,
454 cfg->passive[0],
455 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300456 cfg->passive[1] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300457 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300458 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300459 false, true, 0,
460 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300461 cfg->dfs =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300462 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300463 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300464 true, true,
465 cfg->passive[1],
466 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300467 cfg->active[1] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300468 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300469 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300470 false, false,
471 cfg->passive[1] + cfg->dfs,
472 MAX_CHANNELS_5GHZ);
473 /* 802.11j channels are not supported yet */
474 cfg->passive[2] = 0;
475 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300476
Luciano Coelho95feadc2011-05-10 14:38:59 +0300477 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
478 cfg->active[0], cfg->passive[0]);
479 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
480 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300481 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300482
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300483 return cfg->passive[0] || cfg->active[0] ||
484 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
485 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300486}
487
Luciano Coelhofb553772011-09-02 14:28:21 +0300488/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300489static int
490wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
491 struct cfg80211_sched_scan_request *req)
492{
493 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300494 struct cfg80211_match_set *sets = req->match_sets;
495 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300496 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300497
498 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
499
Luciano Coelho20a33e52011-09-02 14:28:23 +0300500 /* count the match sets that contain SSIDs */
501 for (i = 0; i < req->n_match_sets; i++)
502 if (sets[i].ssid.ssid_len > 0)
503 n_match_ssids++;
504
Luciano Coelhofb553772011-09-02 14:28:21 +0300505 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300506 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300507 (!req->n_ssids ||
508 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
509 type = SCAN_SSID_FILTER_ANY;
510 goto out;
511 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300512
Luciano Coelhofb553772011-09-02 14:28:21 +0300513 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
514 if (!cmd) {
515 ret = -ENOMEM;
516 goto out;
517 }
518
Luciano Coelho20a33e52011-09-02 14:28:23 +0300519 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300520 /* No filter, with ssids */
521 type = SCAN_SSID_FILTER_DISABLED;
522
523 for (i = 0; i < req->n_ssids; i++) {
524 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
525 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
526 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
527 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
528 ssids[i].ssid_len);
529 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300530 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300531 } else {
532 type = SCAN_SSID_FILTER_LIST;
533
534 /* Add all SSIDs from the filters */
535 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300536 /* ignore sets without SSIDs */
537 if (!sets[i].ssid.ssid_len)
538 continue;
539
Luciano Coelhofb553772011-09-02 14:28:21 +0300540 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
541 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
542 memcpy(cmd->ssids[cmd->n_ssids].ssid,
543 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
544 cmd->n_ssids++;
545 }
546 if ((req->n_ssids > 1) ||
547 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
548 /*
549 * Mark all the SSIDs passed in the SSID list as HIDDEN,
550 * so they're used in probe requests.
551 */
552 for (i = 0; i < req->n_ssids; i++) {
553 for (j = 0; j < cmd->n_ssids; j++)
554 if (!memcmp(req->ssids[i].ssid,
555 cmd->ssids[j].ssid,
556 req->ssids[i].ssid_len)) {
557 cmd->ssids[j].type =
558 SCAN_SSID_TYPE_HIDDEN;
559 break;
560 }
561 /* Fail if SSID isn't present in the filters */
Eyal Shapiracc438fc2011-11-08 15:54:46 +0200562 if (j == cmd->n_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300563 ret = -EINVAL;
564 goto out_free;
565 }
566 }
567 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300568 }
569
570 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
571
572 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
573 sizeof(*cmd), 0);
574 if (ret < 0) {
575 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300576 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300577 }
578
Luciano Coelhofb553772011-09-02 14:28:21 +0300579out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300580 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300581out:
582 if (ret < 0)
583 return ret;
584 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300585}
586
Luciano Coelho95feadc2011-05-10 14:38:59 +0300587int wl1271_scan_sched_scan_config(struct wl1271 *wl,
588 struct cfg80211_sched_scan_request *req,
589 struct ieee80211_sched_scan_ies *ies)
590{
591 struct wl1271_cmd_sched_scan_config *cfg = NULL;
592 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300593 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300594 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300595
596 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
597
598 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
599 if (!cfg)
600 return -ENOMEM;
601
602 cfg->rssi_threshold = c->rssi_threshold;
603 cfg->snr_threshold = c->snr_threshold;
604 cfg->n_probe_reqs = c->num_probe_reqs;
605 /* cycles set to 0 it means infinite (until manually stopped) */
606 cfg->cycles = 0;
607 /* report APs when at least 1 is found */
608 cfg->report_after = 1;
609 /* don't stop scanning automatically when something is found */
610 cfg->terminate = 0;
611 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
612 /* don't filter on BSS type */
613 cfg->bss_type = SCAN_BSS_TYPE_ANY;
614 /* currently NL80211 supports only a single interval */
615 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
616 cfg->intervals[i] = cpu_to_le32(req->interval);
617
Luciano Coelhof9520792011-08-23 18:34:44 +0300618 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300619 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
620 if (ret < 0)
621 goto out;
622
623 cfg->filter_type = ret;
624
625 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300626
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300627 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300628 wl1271_error("scan channel list is empty");
629 ret = -EINVAL;
630 goto out;
631 }
632
Luciano Coelho66870b12011-05-27 15:34:48 +0300633 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300634 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
635 req->ssids[0].ssid_len,
636 ies->ie[IEEE80211_BAND_2GHZ],
637 ies->len[IEEE80211_BAND_2GHZ],
638 IEEE80211_BAND_2GHZ);
639 if (ret < 0) {
640 wl1271_error("2.4GHz PROBE request template failed");
641 goto out;
642 }
643 }
644
Luciano Coelho66870b12011-05-27 15:34:48 +0300645 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300646 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
647 req->ssids[0].ssid_len,
648 ies->ie[IEEE80211_BAND_5GHZ],
649 ies->len[IEEE80211_BAND_5GHZ],
650 IEEE80211_BAND_5GHZ);
651 if (ret < 0) {
652 wl1271_error("5GHz PROBE request template failed");
653 goto out;
654 }
655 }
656
657 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
658
659 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
660 sizeof(*cfg), 0);
661 if (ret < 0) {
662 wl1271_error("SCAN configuration failed");
663 goto out;
664 }
665out:
666 kfree(cfg);
667 return ret;
668}
669
670int wl1271_scan_sched_scan_start(struct wl1271 *wl)
671{
672 struct wl1271_cmd_sched_scan_start *start;
673 int ret = 0;
674
675 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
676
Luciano Coelho683c0022011-05-12 17:07:55 +0300677 if (wl->bss_type != BSS_TYPE_STA_BSS)
678 return -EOPNOTSUPP;
679
680 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
681 return -EBUSY;
682
Luciano Coelho95feadc2011-05-10 14:38:59 +0300683 start = kzalloc(sizeof(*start), GFP_KERNEL);
684 if (!start)
685 return -ENOMEM;
686
687 start->tag = WL1271_SCAN_DEFAULT_TAG;
688
689 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
690 sizeof(*start), 0);
691 if (ret < 0) {
692 wl1271_error("failed to send scan start command");
693 goto out_free;
694 }
695
696out_free:
697 kfree(start);
698 return ret;
699}
700
701void wl1271_scan_sched_scan_results(struct wl1271 *wl)
702{
703 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
704
705 ieee80211_sched_scan_results(wl->hw);
706}
707
708void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
709{
710 struct wl1271_cmd_sched_scan_stop *stop;
711 int ret = 0;
712
713 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
714
715 /* FIXME: what to do if alloc'ing to stop fails? */
716 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
717 if (!stop) {
718 wl1271_error("failed to alloc memory to send sched scan stop");
719 return;
720 }
721
722 stop->tag = WL1271_SCAN_DEFAULT_TAG;
723
724 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
725 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300726 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300727 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300728 goto out_free;
729 }
730 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300731
Luciano Coelho33c2c062011-05-10 14:46:02 +0300732out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300733 kfree(stop);
734}