blob: b2e2abd400455274a2d9fb5e572820bbf226f1d6 [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);
68 if ((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
69 (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) {
Eliad Peller251c1772011-08-14 13:17:17 +030070 /* restore remain on channel */
71 wl12xx_cmd_role_start_dev(wl);
72 wl12xx_roc(wl, wl->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020073 }
Eliad Peller251c1772011-08-14 13:17:17 +030074 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020075
Juuso Oikarinen78abd322010-09-21 06:23:32 +020076 if (wl->scan.failed) {
77 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030078 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020079 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020080
Eliad Peller251c1772011-08-14 13:17:17 +030081 ieee80211_scan_completed(wl->hw, false);
82
Arik Nemtsov24225b32011-03-01 12:27:26 +020083out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020084 mutex_unlock(&wl->mutex);
85
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030086}
87
88
Luciano Coelho08688d62010-07-08 17:50:07 +030089static int wl1271_get_scan_channels(struct wl1271 *wl,
90 struct cfg80211_scan_request *req,
91 struct basic_scan_channel_params *channels,
92 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030093{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +020094 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +030095 int i, j;
96 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030097
Luciano Coelho08688d62010-07-08 17:50:07 +030098 for (i = 0, j = 0;
99 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
100 i++) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300101
Luciano Coelho08688d62010-07-08 17:50:07 +0300102 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300103
Luciano Coelho4a31c112011-03-21 23:16:14 +0200104 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300105 !(flags & IEEE80211_CHAN_DISABLED) &&
106 ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
107 (req->channels[i]->band == band)) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300108
Luciano Coelho08688d62010-07-08 17:50:07 +0300109 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
110 req->channels[i]->band,
111 req->channels[i]->center_freq);
112 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
113 req->channels[i]->hw_value,
114 req->channels[i]->flags);
115 wl1271_debug(DEBUG_SCAN,
116 "max_antenna_gain %d, max_power %d",
117 req->channels[i]->max_antenna_gain,
118 req->channels[i]->max_power);
119 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
120 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300121
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200122 if (!passive) {
123 channels[j].min_duration =
124 cpu_to_le32(c->min_dwell_time_active);
125 channels[j].max_duration =
126 cpu_to_le32(c->max_dwell_time_active);
127 } else {
128 channels[j].min_duration =
129 cpu_to_le32(c->min_dwell_time_passive);
130 channels[j].max_duration =
131 cpu_to_le32(c->max_dwell_time_passive);
132 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300133 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300134 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300135 channels[j].channel = req->channels[i]->hw_value;
136
137 memset(&channels[j].bssid_lsb, 0xff, 4);
138 memset(&channels[j].bssid_msb, 0xff, 2);
139
140 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200141 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300142
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300143 j++;
144 }
145 }
146
Luciano Coelho08688d62010-07-08 17:50:07 +0300147 return j;
148}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300149
Luciano Coelho08688d62010-07-08 17:50:07 +0300150#define WL1271_NOTHING_TO_SCAN 1
151
152static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
153 bool passive, u32 basic_rate)
154{
155 struct wl1271_cmd_scan *cmd;
156 struct wl1271_cmd_trigger_scan_to *trigger;
157 int ret;
158 u16 scan_options = 0;
159
160 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
161 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
162 if (!cmd || !trigger) {
163 ret = -ENOMEM;
164 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300165 }
166
Luciano Coelho4f35c022010-08-04 04:36:32 +0300167 /* No SSIDs means that we have a forced passive scan */
168 if (passive || wl->scan.req->n_ssids == 0)
Luciano Coelho08688d62010-07-08 17:50:07 +0300169 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300170
Eliad Pellera4e02f32011-08-14 13:17:10 +0300171 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
172 ret = -EINVAL;
173 goto out;
174 }
175 cmd->params.role_id = wl->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300176 cmd->params.scan_options = cpu_to_le16(scan_options);
177
178 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
179 cmd->channels,
180 band, passive);
181 if (cmd->params.n_ch == 0) {
182 ret = WL1271_NOTHING_TO_SCAN;
183 goto out;
184 }
185
186 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200187 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300188 cmd->params.tx_rate = cpu_to_le32(basic_rate);
189 cmd->params.tid_trigger = 0;
190 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
191
192 if (band == IEEE80211_BAND_2GHZ)
193 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
194 else
195 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
196
197 if (wl->scan.ssid_len && wl->scan.ssid) {
198 cmd->params.ssid_len = wl->scan.ssid_len;
199 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
200 }
201
Eliad Pellera4e02f32011-08-14 13:17:10 +0300202 memcpy(cmd->addr, wl->mac_addr, ETH_ALEN);
203
Luciano Coelho08688d62010-07-08 17:50:07 +0300204 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
205 wl->scan.req->ie, wl->scan.req->ie_len,
206 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300207 if (ret < 0) {
208 wl1271_error("PROBE request template failed");
209 goto out;
210 }
211
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300212 /* disable the timeout */
213 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300214 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
215 sizeof(*trigger), 0);
216 if (ret < 0) {
217 wl1271_error("trigger scan to failed for hw scan");
218 goto out;
219 }
220
Luciano Coelho08688d62010-07-08 17:50:07 +0300221 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300222
Luciano Coelho08688d62010-07-08 17:50:07 +0300223 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300224 if (ret < 0) {
225 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300226 goto out;
227 }
228
229out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300230 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300231 kfree(trigger);
232 return ret;
233}
234
Luciano Coelho08688d62010-07-08 17:50:07 +0300235void wl1271_scan_stm(struct wl1271 *wl)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300236{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200237 int ret = 0;
Luciano Coelho08688d62010-07-08 17:50:07 +0300238
239 switch (wl->scan.state) {
240 case WL1271_SCAN_STATE_IDLE:
241 break;
242
243 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
244 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
245 wl->conf.tx.basic_rate);
246 if (ret == WL1271_NOTHING_TO_SCAN) {
247 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
248 wl1271_scan_stm(wl);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300249 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300250
251 break;
252
253 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
254 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
255 wl->conf.tx.basic_rate);
256 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200257 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300258 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
259 else
260 wl->scan.state = WL1271_SCAN_STATE_DONE;
261 wl1271_scan_stm(wl);
262 }
263
264 break;
265
266 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
267 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
268 wl->conf.tx.basic_rate_5);
269 if (ret == WL1271_NOTHING_TO_SCAN) {
270 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
271 wl1271_scan_stm(wl);
272 }
273
274 break;
275
276 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
277 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
278 wl->conf.tx.basic_rate_5);
279 if (ret == WL1271_NOTHING_TO_SCAN) {
280 wl->scan.state = WL1271_SCAN_STATE_DONE;
281 wl1271_scan_stm(wl);
282 }
283
284 break;
285
286 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200287 wl->scan.failed = false;
288 cancel_delayed_work(&wl->scan_complete_work);
289 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
290 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300291 break;
292
293 default:
294 wl1271_error("invalid scan state");
295 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300296 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200297
298 if (ret < 0) {
299 cancel_delayed_work(&wl->scan_complete_work);
300 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
301 msecs_to_jiffies(0));
302 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300303}
304
305int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
306 struct cfg80211_scan_request *req)
307{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200308 /*
309 * cfg80211 should guarantee that we don't get more channels
310 * than what we have registered.
311 */
312 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
313
Luciano Coelho08688d62010-07-08 17:50:07 +0300314 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
315 return -EBUSY;
316
317 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
318
319 if (ssid_len && ssid) {
320 wl->scan.ssid_len = ssid_len;
321 memcpy(wl->scan.ssid, ssid, ssid_len);
322 } else {
323 wl->scan.ssid_len = 0;
324 }
325
326 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200327 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300328
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200329 /* we assume failure so that timeout scenarios are handled correctly */
330 wl->scan.failed = true;
331 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
332 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
333
Luciano Coelho08688d62010-07-08 17:50:07 +0300334 wl1271_scan_stm(wl);
335
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300336 return 0;
337}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300338
Eliad Peller2aa01592011-06-27 13:06:44 +0300339int wl1271_scan_stop(struct wl1271 *wl)
340{
341 struct wl1271_cmd_header *cmd = NULL;
342 int ret = 0;
343
344 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
345 return -EINVAL;
346
347 wl1271_debug(DEBUG_CMD, "cmd scan stop");
348
349 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
350 if (!cmd) {
351 ret = -ENOMEM;
352 goto out;
353 }
354
355 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
356 sizeof(*cmd), 0);
357 if (ret < 0) {
358 wl1271_error("cmd stop_scan failed");
359 goto out;
360 }
361out:
362 kfree(cmd);
363 return ret;
364}
365
Luciano Coelho95feadc2011-05-10 14:38:59 +0300366static int
367wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
368 struct cfg80211_sched_scan_request *req,
369 struct conn_scan_ch_params *channels,
370 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300371 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300372{
373 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
374 int i, j;
375 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300376 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300377
378 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300379 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300380 i++) {
381 flags = req->channels[i]->flags;
382
Luciano Coelho66870b12011-05-27 15:34:48 +0300383 if (force_passive)
384 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
385
Luciano Coelho2497a242011-05-27 15:34:46 +0300386 if ((req->channels[i]->band == band) &&
387 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300388 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300389 /* if radar is set, we ignore the passive flag */
390 (radar ||
391 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300392 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
393 req->channels[i]->band,
394 req->channels[i]->center_freq);
395 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
396 req->channels[i]->hw_value,
397 req->channels[i]->flags);
398 wl1271_debug(DEBUG_SCAN, "max_power %d",
399 req->channels[i]->max_power);
400
Luciano Coelho50a66d72011-05-27 15:34:47 +0300401 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300402 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300403 channels[j].passive_duration =
404 cpu_to_le16(c->dwell_time_dfs);
405 }
406 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300407 channels[j].passive_duration =
408 cpu_to_le16(c->dwell_time_passive);
409 } else {
410 channels[j].min_duration =
411 cpu_to_le16(c->min_dwell_time_active);
412 channels[j].max_duration =
413 cpu_to_le16(c->max_dwell_time_active);
414 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300415 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300416 channels[j].channel = req->channels[i]->hw_value;
417
418 j++;
419 }
420 }
421
422 return j - start;
423}
424
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300425static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300426wl1271_scan_sched_scan_channels(struct wl1271 *wl,
427 struct cfg80211_sched_scan_request *req,
428 struct wl1271_cmd_sched_scan_config *cfg)
429{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300430 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300431 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300432 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300433 false, true, 0,
434 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300435 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300436 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300437 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300438 false, false,
439 cfg->passive[0],
440 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300441 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300442 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300443 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300444 false, true, 0,
445 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300446 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300447 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300448 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300449 true, true,
450 cfg->passive[1],
451 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300452 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300453 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300454 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300455 false, false,
456 cfg->passive[1] + cfg->dfs,
457 MAX_CHANNELS_5GHZ);
458 /* 802.11j channels are not supported yet */
459 cfg->passive[2] = 0;
460 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300461
Luciano Coelho95feadc2011-05-10 14:38:59 +0300462 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
463 cfg->active[0], cfg->passive[0]);
464 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
465 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300466 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300467
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300468 return cfg->passive[0] || cfg->active[0] ||
469 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
470 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300471}
472
Luciano Coelhof9520792011-08-23 18:34:44 +0300473/* Returns 0 if no wildcard is used, 1 if wildcard is used or a
474 * negative value on error */
475static 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;
480 struct cfg80211_ssid *ssid = req->ssids;
481 int ret, wildcard = 0;
482
483 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
484
485 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
486 if (!cmd)
487 return -ENOMEM;
488
489 while ((cmd->n_ssids < req->n_ssids) && ssid) {
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300490 if (ssid->ssid_len == 0) {
Luciano Coelhof9520792011-08-23 18:34:44 +0300491 wildcard = 1;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300492 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
493 } else {
494 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_HIDDEN;
495 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300496 cmd->ssids[cmd->n_ssids].len = ssid->ssid_len;
497 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssid->ssid,
498 ssid->ssid_len);
499 ssid++;
500 cmd->n_ssids++;
501 }
502
503 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
504
505 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
506 sizeof(*cmd), 0);
507 if (ret < 0) {
508 wl1271_error("cmd sched scan ssid list failed");
509 goto out;
510 }
511
512 ret = wildcard;
513out:
514 kfree(cmd);
515 return ret;
516}
517
Luciano Coelho95feadc2011-05-10 14:38:59 +0300518int wl1271_scan_sched_scan_config(struct wl1271 *wl,
519 struct cfg80211_sched_scan_request *req,
520 struct ieee80211_sched_scan_ies *ies)
521{
522 struct wl1271_cmd_sched_scan_config *cfg = NULL;
523 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300524 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300525 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300526
527 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
528
529 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
530 if (!cfg)
531 return -ENOMEM;
532
533 cfg->rssi_threshold = c->rssi_threshold;
534 cfg->snr_threshold = c->snr_threshold;
535 cfg->n_probe_reqs = c->num_probe_reqs;
536 /* cycles set to 0 it means infinite (until manually stopped) */
537 cfg->cycles = 0;
538 /* report APs when at least 1 is found */
539 cfg->report_after = 1;
540 /* don't stop scanning automatically when something is found */
541 cfg->terminate = 0;
542 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
543 /* don't filter on BSS type */
544 cfg->bss_type = SCAN_BSS_TYPE_ANY;
545 /* currently NL80211 supports only a single interval */
546 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
547 cfg->intervals[i] = cpu_to_le32(req->interval);
548
Luciano Coelhof9520792011-08-23 18:34:44 +0300549 cfg->ssid_len = 0;
550 if (req->n_ssids == 0) {
551 wl1271_debug(DEBUG_SCAN, "using SCAN_SSID_FILTER_ANY");
Luciano Coelho95feadc2011-05-10 14:38:59 +0300552 cfg->filter_type = SCAN_SSID_FILTER_ANY;
Luciano Coelhof9520792011-08-23 18:34:44 +0300553 } else {
554 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
555 if (ret < 0)
556 goto out;
557 if (ret) {
558 wl1271_debug(DEBUG_SCAN, "using SCAN_SSID_FILTER_DISABLED");
559 cfg->filter_type = SCAN_SSID_FILTER_DISABLED;
560 } else {
561 wl1271_debug(DEBUG_SCAN, "using SCAN_SSID_FILTER_LIST");
562 cfg->filter_type = SCAN_SSID_FILTER_LIST;
563 }
Luciano Coelho95feadc2011-05-10 14:38:59 +0300564 }
565
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300566 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300567 wl1271_error("scan channel list is empty");
568 ret = -EINVAL;
569 goto out;
570 }
571
Luciano Coelho66870b12011-05-27 15:34:48 +0300572 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300573 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
574 req->ssids[0].ssid_len,
575 ies->ie[IEEE80211_BAND_2GHZ],
576 ies->len[IEEE80211_BAND_2GHZ],
577 IEEE80211_BAND_2GHZ);
578 if (ret < 0) {
579 wl1271_error("2.4GHz PROBE request template failed");
580 goto out;
581 }
582 }
583
Luciano Coelho66870b12011-05-27 15:34:48 +0300584 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300585 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
586 req->ssids[0].ssid_len,
587 ies->ie[IEEE80211_BAND_5GHZ],
588 ies->len[IEEE80211_BAND_5GHZ],
589 IEEE80211_BAND_5GHZ);
590 if (ret < 0) {
591 wl1271_error("5GHz PROBE request template failed");
592 goto out;
593 }
594 }
595
596 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
597
598 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
599 sizeof(*cfg), 0);
600 if (ret < 0) {
601 wl1271_error("SCAN configuration failed");
602 goto out;
603 }
604out:
605 kfree(cfg);
606 return ret;
607}
608
609int wl1271_scan_sched_scan_start(struct wl1271 *wl)
610{
611 struct wl1271_cmd_sched_scan_start *start;
612 int ret = 0;
613
614 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
615
Luciano Coelho683c0022011-05-12 17:07:55 +0300616 if (wl->bss_type != BSS_TYPE_STA_BSS)
617 return -EOPNOTSUPP;
618
619 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
620 return -EBUSY;
621
Luciano Coelho95feadc2011-05-10 14:38:59 +0300622 start = kzalloc(sizeof(*start), GFP_KERNEL);
623 if (!start)
624 return -ENOMEM;
625
626 start->tag = WL1271_SCAN_DEFAULT_TAG;
627
628 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
629 sizeof(*start), 0);
630 if (ret < 0) {
631 wl1271_error("failed to send scan start command");
632 goto out_free;
633 }
634
635out_free:
636 kfree(start);
637 return ret;
638}
639
640void wl1271_scan_sched_scan_results(struct wl1271 *wl)
641{
642 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
643
644 ieee80211_sched_scan_results(wl->hw);
645}
646
647void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
648{
649 struct wl1271_cmd_sched_scan_stop *stop;
650 int ret = 0;
651
652 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
653
654 /* FIXME: what to do if alloc'ing to stop fails? */
655 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
656 if (!stop) {
657 wl1271_error("failed to alloc memory to send sched scan stop");
658 return;
659 }
660
661 stop->tag = WL1271_SCAN_DEFAULT_TAG;
662
663 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
664 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300665 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300666 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300667 goto out_free;
668 }
669 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300670
Luciano Coelho33c2c062011-05-10 14:46:02 +0300671out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300672 kfree(stop);
673}