blob: 488033b2a39b70844b6d0d3362da0b7a52c46c00 [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"
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030031
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030032void wl1271_scan_complete_work(struct work_struct *work)
33{
Juuso Oikarinen78abd322010-09-21 06:23:32 +020034 struct delayed_work *dwork;
35 struct wl1271 *wl;
Eliad Peller251c1772011-08-14 13:17:17 +030036 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030037 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020038
39 dwork = container_of(work, struct delayed_work, work);
40 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030041
42 wl1271_debug(DEBUG_SCAN, "Scanning complete");
43
44 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020045
Arik Nemtsov24225b32011-03-01 12:27:26 +020046 if (wl->state == WL1271_STATE_OFF)
47 goto out;
48
49 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
50 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020051
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030052 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020053 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020054 wl->scan.req = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020055
Eliad Peller251c1772011-08-14 13:17:17 +030056 ret = wl1271_ps_elp_wakeup(wl);
57 if (ret < 0)
58 goto out;
59
Arik Nemtsov24225b32011-03-01 12:27:26 +020060 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030061 /* restore hardware connection monitoring template */
62 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030063 }
64
65 /* return to ROC if needed */
66 is_sta = (wl->bss_type == BSS_TYPE_STA_BSS);
67 is_ibss = (wl->bss_type == BSS_TYPE_IBSS);
Eliad Pellere0b38262011-08-28 15:11:55 +030068 if (((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
69 (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) &&
70 !test_bit(wl->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030071 /* restore remain on channel */
72 wl12xx_cmd_role_start_dev(wl);
73 wl12xx_roc(wl, wl->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020074 }
Eliad Peller251c1772011-08-14 13:17:17 +030075 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020076
Juuso Oikarinen78abd322010-09-21 06:23:32 +020077 if (wl->scan.failed) {
78 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030079 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020080 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020081
Eliad Peller251c1772011-08-14 13:17:17 +030082 ieee80211_scan_completed(wl->hw, false);
83
Arik Nemtsov24225b32011-03-01 12:27:26 +020084out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020085 mutex_unlock(&wl->mutex);
86
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030087}
88
89
Luciano Coelho08688d62010-07-08 17:50:07 +030090static int wl1271_get_scan_channels(struct wl1271 *wl,
91 struct cfg80211_scan_request *req,
92 struct basic_scan_channel_params *channels,
93 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030094{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +020095 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +030096 int i, j;
97 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030098
Luciano Coelho08688d62010-07-08 17:50:07 +030099 for (i = 0, j = 0;
100 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
101 i++) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300102
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) &&
107 ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
108 (req->channels[i]->band == band)) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300109
Luciano Coelho08688d62010-07-08 17:50:07 +0300110 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
111 req->channels[i]->band,
112 req->channels[i]->center_freq);
113 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
114 req->channels[i]->hw_value,
115 req->channels[i]->flags);
116 wl1271_debug(DEBUG_SCAN,
117 "max_antenna_gain %d, max_power %d",
118 req->channels[i]->max_antenna_gain,
119 req->channels[i]->max_power);
120 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
121 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300122
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200123 if (!passive) {
124 channels[j].min_duration =
125 cpu_to_le32(c->min_dwell_time_active);
126 channels[j].max_duration =
127 cpu_to_le32(c->max_dwell_time_active);
128 } else {
129 channels[j].min_duration =
130 cpu_to_le32(c->min_dwell_time_passive);
131 channels[j].max_duration =
132 cpu_to_le32(c->max_dwell_time_passive);
133 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300134 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300135 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300136 channels[j].channel = req->channels[i]->hw_value;
137
138 memset(&channels[j].bssid_lsb, 0xff, 4);
139 memset(&channels[j].bssid_msb, 0xff, 2);
140
141 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200142 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300143
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300144 j++;
145 }
146 }
147
Luciano Coelho08688d62010-07-08 17:50:07 +0300148 return j;
149}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300150
Luciano Coelho08688d62010-07-08 17:50:07 +0300151#define WL1271_NOTHING_TO_SCAN 1
152
153static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
154 bool passive, u32 basic_rate)
155{
156 struct wl1271_cmd_scan *cmd;
157 struct wl1271_cmd_trigger_scan_to *trigger;
158 int ret;
159 u16 scan_options = 0;
160
161 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
162 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
163 if (!cmd || !trigger) {
164 ret = -ENOMEM;
165 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300166 }
167
Luciano Coelho4f35c022010-08-04 04:36:32 +0300168 /* No SSIDs means that we have a forced passive scan */
169 if (passive || wl->scan.req->n_ssids == 0)
Luciano Coelho08688d62010-07-08 17:50:07 +0300170 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300171
Eliad Pellera4e02f32011-08-14 13:17:10 +0300172 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
173 ret = -EINVAL;
174 goto out;
175 }
176 cmd->params.role_id = wl->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300177 cmd->params.scan_options = cpu_to_le16(scan_options);
178
179 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
180 cmd->channels,
181 band, passive);
182 if (cmd->params.n_ch == 0) {
183 ret = WL1271_NOTHING_TO_SCAN;
184 goto out;
185 }
186
187 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200188 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300189 cmd->params.tx_rate = cpu_to_le32(basic_rate);
190 cmd->params.tid_trigger = 0;
191 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
192
193 if (band == IEEE80211_BAND_2GHZ)
194 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
195 else
196 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
197
198 if (wl->scan.ssid_len && wl->scan.ssid) {
199 cmd->params.ssid_len = wl->scan.ssid_len;
200 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
201 }
202
Eliad Pellera4e02f32011-08-14 13:17:10 +0300203 memcpy(cmd->addr, wl->mac_addr, ETH_ALEN);
204
Luciano Coelho08688d62010-07-08 17:50:07 +0300205 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
206 wl->scan.req->ie, wl->scan.req->ie_len,
207 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300208 if (ret < 0) {
209 wl1271_error("PROBE request template failed");
210 goto out;
211 }
212
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300213 /* disable the timeout */
214 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300215 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
216 sizeof(*trigger), 0);
217 if (ret < 0) {
218 wl1271_error("trigger scan to failed for hw scan");
219 goto out;
220 }
221
Luciano Coelho08688d62010-07-08 17:50:07 +0300222 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300223
Luciano Coelho08688d62010-07-08 17:50:07 +0300224 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300225 if (ret < 0) {
226 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300227 goto out;
228 }
229
230out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300231 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300232 kfree(trigger);
233 return ret;
234}
235
Luciano Coelho08688d62010-07-08 17:50:07 +0300236void wl1271_scan_stm(struct wl1271 *wl)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300237{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200238 int ret = 0;
Luciano Coelho08688d62010-07-08 17:50:07 +0300239
240 switch (wl->scan.state) {
241 case WL1271_SCAN_STATE_IDLE:
242 break;
243
244 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
245 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
246 wl->conf.tx.basic_rate);
247 if (ret == WL1271_NOTHING_TO_SCAN) {
248 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
249 wl1271_scan_stm(wl);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300250 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300251
252 break;
253
254 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
255 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
256 wl->conf.tx.basic_rate);
257 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200258 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300259 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
260 else
261 wl->scan.state = WL1271_SCAN_STATE_DONE;
262 wl1271_scan_stm(wl);
263 }
264
265 break;
266
267 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
268 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
269 wl->conf.tx.basic_rate_5);
270 if (ret == WL1271_NOTHING_TO_SCAN) {
271 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
272 wl1271_scan_stm(wl);
273 }
274
275 break;
276
277 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
278 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
279 wl->conf.tx.basic_rate_5);
280 if (ret == WL1271_NOTHING_TO_SCAN) {
281 wl->scan.state = WL1271_SCAN_STATE_DONE;
282 wl1271_scan_stm(wl);
283 }
284
285 break;
286
287 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200288 wl->scan.failed = false;
289 cancel_delayed_work(&wl->scan_complete_work);
290 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
291 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300292 break;
293
294 default:
295 wl1271_error("invalid scan state");
296 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300297 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200298
299 if (ret < 0) {
300 cancel_delayed_work(&wl->scan_complete_work);
301 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
302 msecs_to_jiffies(0));
303 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300304}
305
306int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
307 struct cfg80211_scan_request *req)
308{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200309 /*
310 * cfg80211 should guarantee that we don't get more channels
311 * than what we have registered.
312 */
313 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
314
Luciano Coelho08688d62010-07-08 17:50:07 +0300315 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
316 return -EBUSY;
317
318 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
319
320 if (ssid_len && ssid) {
321 wl->scan.ssid_len = ssid_len;
322 memcpy(wl->scan.ssid, ssid, ssid_len);
323 } else {
324 wl->scan.ssid_len = 0;
325 }
326
327 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200328 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300329
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200330 /* we assume failure so that timeout scenarios are handled correctly */
331 wl->scan.failed = true;
332 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
333 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
334
Luciano Coelho08688d62010-07-08 17:50:07 +0300335 wl1271_scan_stm(wl);
336
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300337 return 0;
338}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300339
Eliad Peller2aa01592011-06-27 13:06:44 +0300340int wl1271_scan_stop(struct wl1271 *wl)
341{
342 struct wl1271_cmd_header *cmd = NULL;
343 int ret = 0;
344
345 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
346 return -EINVAL;
347
348 wl1271_debug(DEBUG_CMD, "cmd scan stop");
349
350 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
351 if (!cmd) {
352 ret = -ENOMEM;
353 goto out;
354 }
355
356 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
357 sizeof(*cmd), 0);
358 if (ret < 0) {
359 wl1271_error("cmd stop_scan failed");
360 goto out;
361 }
362out:
363 kfree(cmd);
364 return ret;
365}
366
Luciano Coelho95feadc2011-05-10 14:38:59 +0300367static int
368wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
369 struct cfg80211_sched_scan_request *req,
370 struct conn_scan_ch_params *channels,
371 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300372 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300373{
374 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
375 int i, j;
376 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300377 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300378
379 for (i = 0, j = start;
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300380 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300381 i++) {
382 flags = req->channels[i]->flags;
383
Luciano Coelho66870b12011-05-27 15:34:48 +0300384 if (force_passive)
385 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
386
Luciano Coelho2497a242011-05-27 15:34:46 +0300387 if ((req->channels[i]->band == band) &&
388 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300389 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300390 /* if radar is set, we ignore the passive flag */
391 (radar ||
392 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300393 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
394 req->channels[i]->band,
395 req->channels[i]->center_freq);
396 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
397 req->channels[i]->hw_value,
398 req->channels[i]->flags);
399 wl1271_debug(DEBUG_SCAN, "max_power %d",
400 req->channels[i]->max_power);
401
Luciano Coelho50a66d72011-05-27 15:34:47 +0300402 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300403 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300404 channels[j].passive_duration =
405 cpu_to_le16(c->dwell_time_dfs);
406 }
407 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300408 channels[j].passive_duration =
409 cpu_to_le16(c->dwell_time_passive);
410 } else {
411 channels[j].min_duration =
412 cpu_to_le16(c->min_dwell_time_active);
413 channels[j].max_duration =
414 cpu_to_le16(c->max_dwell_time_active);
415 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300416 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300417 channels[j].channel = req->channels[i]->hw_value;
418
419 j++;
420 }
421 }
422
423 return j - start;
424}
425
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300426static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300427wl1271_scan_sched_scan_channels(struct wl1271 *wl,
428 struct cfg80211_sched_scan_request *req,
429 struct wl1271_cmd_sched_scan_config *cfg)
430{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300431 cfg->passive[0] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300432 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300433 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300434 false, true, 0,
435 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300436 cfg->active[0] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300437 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300438 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300439 false, false,
440 cfg->passive[0],
441 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300442 cfg->passive[1] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300443 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300444 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300445 false, true, 0,
446 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300447 cfg->dfs =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300448 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300449 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300450 true, true,
451 cfg->passive[1],
452 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300453 cfg->active[1] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300454 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300455 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300456 false, false,
457 cfg->passive[1] + cfg->dfs,
458 MAX_CHANNELS_5GHZ);
459 /* 802.11j channels are not supported yet */
460 cfg->passive[2] = 0;
461 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300462
Luciano Coelho95feadc2011-05-10 14:38:59 +0300463 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
464 cfg->active[0], cfg->passive[0]);
465 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
466 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300467 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300468
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300469 return cfg->passive[0] || cfg->active[0] ||
470 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
471 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300472}
473
Luciano Coelhofb553772011-09-02 14:28:21 +0300474/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300475static int
476wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
477 struct cfg80211_sched_scan_request *req)
478{
479 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300480 struct cfg80211_match_set *sets = req->match_sets;
481 struct cfg80211_ssid *ssids = req->ssids;
482 int ret = 0, type, i, j;
Luciano Coelhof9520792011-08-23 18:34:44 +0300483
484 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
485
Luciano Coelhofb553772011-09-02 14:28:21 +0300486 /* No filter, no ssids or only bcast ssid */
487 if (!req->n_match_sets &&
488 (!req->n_ssids ||
489 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
490 type = SCAN_SSID_FILTER_ANY;
491 goto out;
492 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300493
Luciano Coelhofb553772011-09-02 14:28:21 +0300494 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
495 if (!cmd) {
496 ret = -ENOMEM;
497 goto out;
498 }
499
500 if (!req->n_match_sets) {
501 /* No filter, with ssids */
502 type = SCAN_SSID_FILTER_DISABLED;
503
504 for (i = 0; i < req->n_ssids; i++) {
505 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
506 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
507 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
508 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
509 ssids[i].ssid_len);
510 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300511 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300512 } else {
513 type = SCAN_SSID_FILTER_LIST;
514
515 /* Add all SSIDs from the filters */
516 for (i = 0; i < req->n_match_sets; i++) {
517 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
518 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
519 memcpy(cmd->ssids[cmd->n_ssids].ssid,
520 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
521 cmd->n_ssids++;
522 }
523 if ((req->n_ssids > 1) ||
524 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
525 /*
526 * Mark all the SSIDs passed in the SSID list as HIDDEN,
527 * so they're used in probe requests.
528 */
529 for (i = 0; i < req->n_ssids; i++) {
530 for (j = 0; j < cmd->n_ssids; j++)
531 if (!memcmp(req->ssids[i].ssid,
532 cmd->ssids[j].ssid,
533 req->ssids[i].ssid_len)) {
534 cmd->ssids[j].type =
535 SCAN_SSID_TYPE_HIDDEN;
536 break;
537 }
538 /* Fail if SSID isn't present in the filters */
539 if (j == req->n_ssids) {
540 ret = -EINVAL;
541 goto out_free;
542 }
543 }
544 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300545 }
546
547 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
548
549 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
550 sizeof(*cmd), 0);
551 if (ret < 0) {
552 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300553 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300554 }
555
Luciano Coelhofb553772011-09-02 14:28:21 +0300556out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300557 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300558out:
559 if (ret < 0)
560 return ret;
561 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300562}
563
Luciano Coelho95feadc2011-05-10 14:38:59 +0300564int wl1271_scan_sched_scan_config(struct wl1271 *wl,
565 struct cfg80211_sched_scan_request *req,
566 struct ieee80211_sched_scan_ies *ies)
567{
568 struct wl1271_cmd_sched_scan_config *cfg = NULL;
569 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300570 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300571 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300572
573 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
574
575 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
576 if (!cfg)
577 return -ENOMEM;
578
579 cfg->rssi_threshold = c->rssi_threshold;
580 cfg->snr_threshold = c->snr_threshold;
581 cfg->n_probe_reqs = c->num_probe_reqs;
582 /* cycles set to 0 it means infinite (until manually stopped) */
583 cfg->cycles = 0;
584 /* report APs when at least 1 is found */
585 cfg->report_after = 1;
586 /* don't stop scanning automatically when something is found */
587 cfg->terminate = 0;
588 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
589 /* don't filter on BSS type */
590 cfg->bss_type = SCAN_BSS_TYPE_ANY;
591 /* currently NL80211 supports only a single interval */
592 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
593 cfg->intervals[i] = cpu_to_le32(req->interval);
594
Luciano Coelhof9520792011-08-23 18:34:44 +0300595 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300596 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
597 if (ret < 0)
598 goto out;
599
600 cfg->filter_type = ret;
601
602 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300603
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300604 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300605 wl1271_error("scan channel list is empty");
606 ret = -EINVAL;
607 goto out;
608 }
609
Luciano Coelho66870b12011-05-27 15:34:48 +0300610 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300611 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
612 req->ssids[0].ssid_len,
613 ies->ie[IEEE80211_BAND_2GHZ],
614 ies->len[IEEE80211_BAND_2GHZ],
615 IEEE80211_BAND_2GHZ);
616 if (ret < 0) {
617 wl1271_error("2.4GHz PROBE request template failed");
618 goto out;
619 }
620 }
621
Luciano Coelho66870b12011-05-27 15:34:48 +0300622 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300623 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
624 req->ssids[0].ssid_len,
625 ies->ie[IEEE80211_BAND_5GHZ],
626 ies->len[IEEE80211_BAND_5GHZ],
627 IEEE80211_BAND_5GHZ);
628 if (ret < 0) {
629 wl1271_error("5GHz PROBE request template failed");
630 goto out;
631 }
632 }
633
634 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
635
636 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
637 sizeof(*cfg), 0);
638 if (ret < 0) {
639 wl1271_error("SCAN configuration failed");
640 goto out;
641 }
642out:
643 kfree(cfg);
644 return ret;
645}
646
647int wl1271_scan_sched_scan_start(struct wl1271 *wl)
648{
649 struct wl1271_cmd_sched_scan_start *start;
650 int ret = 0;
651
652 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
653
Luciano Coelho683c0022011-05-12 17:07:55 +0300654 if (wl->bss_type != BSS_TYPE_STA_BSS)
655 return -EOPNOTSUPP;
656
657 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
658 return -EBUSY;
659
Luciano Coelho95feadc2011-05-10 14:38:59 +0300660 start = kzalloc(sizeof(*start), GFP_KERNEL);
661 if (!start)
662 return -ENOMEM;
663
664 start->tag = WL1271_SCAN_DEFAULT_TAG;
665
666 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
667 sizeof(*start), 0);
668 if (ret < 0) {
669 wl1271_error("failed to send scan start command");
670 goto out_free;
671 }
672
673out_free:
674 kfree(start);
675 return ret;
676}
677
678void wl1271_scan_sched_scan_results(struct wl1271 *wl)
679{
680 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
681
682 ieee80211_sched_scan_results(wl->hw);
683}
684
685void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
686{
687 struct wl1271_cmd_sched_scan_stop *stop;
688 int ret = 0;
689
690 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
691
692 /* FIXME: what to do if alloc'ing to stop fails? */
693 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
694 if (!stop) {
695 wl1271_error("failed to alloc memory to send sched scan stop");
696 return;
697 }
698
699 stop->tag = WL1271_SCAN_DEFAULT_TAG;
700
701 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
702 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300703 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300704 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300705 goto out_free;
706 }
707 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300708
Luciano Coelho33c2c062011-05-10 14:46:02 +0300709out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300710 kfree(stop);
711}