blob: 4a4d0d03fa1f248fd4cd4ff9b1cf21e5d4eb5454 [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 Coelhof9520792011-08-23 18:34:44 +0300474/* Returns 0 if no wildcard is used, 1 if wildcard is used or a
475 * negative value on error */
476static int
477wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
478 struct cfg80211_sched_scan_request *req)
479{
480 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
481 struct cfg80211_ssid *ssid = req->ssids;
482 int ret, wildcard = 0;
483
484 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
485
486 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
487 if (!cmd)
488 return -ENOMEM;
489
490 while ((cmd->n_ssids < req->n_ssids) && ssid) {
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300491 if (ssid->ssid_len == 0) {
Luciano Coelhof9520792011-08-23 18:34:44 +0300492 wildcard = 1;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300493 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
494 } else {
495 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_HIDDEN;
496 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300497 cmd->ssids[cmd->n_ssids].len = ssid->ssid_len;
498 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssid->ssid,
499 ssid->ssid_len);
500 ssid++;
501 cmd->n_ssids++;
502 }
503
504 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
505
506 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
507 sizeof(*cmd), 0);
508 if (ret < 0) {
509 wl1271_error("cmd sched scan ssid list failed");
510 goto out;
511 }
512
513 ret = wildcard;
514out:
515 kfree(cmd);
516 return ret;
517}
518
Luciano Coelho95feadc2011-05-10 14:38:59 +0300519int wl1271_scan_sched_scan_config(struct wl1271 *wl,
520 struct cfg80211_sched_scan_request *req,
521 struct ieee80211_sched_scan_ies *ies)
522{
523 struct wl1271_cmd_sched_scan_config *cfg = NULL;
524 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300525 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300526 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300527
528 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
529
530 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
531 if (!cfg)
532 return -ENOMEM;
533
534 cfg->rssi_threshold = c->rssi_threshold;
535 cfg->snr_threshold = c->snr_threshold;
536 cfg->n_probe_reqs = c->num_probe_reqs;
537 /* cycles set to 0 it means infinite (until manually stopped) */
538 cfg->cycles = 0;
539 /* report APs when at least 1 is found */
540 cfg->report_after = 1;
541 /* don't stop scanning automatically when something is found */
542 cfg->terminate = 0;
543 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
544 /* don't filter on BSS type */
545 cfg->bss_type = SCAN_BSS_TYPE_ANY;
546 /* currently NL80211 supports only a single interval */
547 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
548 cfg->intervals[i] = cpu_to_le32(req->interval);
549
Luciano Coelhof9520792011-08-23 18:34:44 +0300550 cfg->ssid_len = 0;
551 if (req->n_ssids == 0) {
552 wl1271_debug(DEBUG_SCAN, "using SCAN_SSID_FILTER_ANY");
Luciano Coelho95feadc2011-05-10 14:38:59 +0300553 cfg->filter_type = SCAN_SSID_FILTER_ANY;
Luciano Coelhof9520792011-08-23 18:34:44 +0300554 } else {
555 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
556 if (ret < 0)
557 goto out;
558 if (ret) {
559 wl1271_debug(DEBUG_SCAN, "using SCAN_SSID_FILTER_DISABLED");
560 cfg->filter_type = SCAN_SSID_FILTER_DISABLED;
561 } else {
562 wl1271_debug(DEBUG_SCAN, "using SCAN_SSID_FILTER_LIST");
563 cfg->filter_type = SCAN_SSID_FILTER_LIST;
564 }
Luciano Coelho95feadc2011-05-10 14:38:59 +0300565 }
566
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300567 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300568 wl1271_error("scan channel list is empty");
569 ret = -EINVAL;
570 goto out;
571 }
572
Luciano Coelho66870b12011-05-27 15:34:48 +0300573 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300574 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
575 req->ssids[0].ssid_len,
576 ies->ie[IEEE80211_BAND_2GHZ],
577 ies->len[IEEE80211_BAND_2GHZ],
578 IEEE80211_BAND_2GHZ);
579 if (ret < 0) {
580 wl1271_error("2.4GHz PROBE request template failed");
581 goto out;
582 }
583 }
584
Luciano Coelho66870b12011-05-27 15:34:48 +0300585 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300586 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
587 req->ssids[0].ssid_len,
588 ies->ie[IEEE80211_BAND_5GHZ],
589 ies->len[IEEE80211_BAND_5GHZ],
590 IEEE80211_BAND_5GHZ);
591 if (ret < 0) {
592 wl1271_error("5GHz PROBE request template failed");
593 goto out;
594 }
595 }
596
597 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
598
599 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
600 sizeof(*cfg), 0);
601 if (ret < 0) {
602 wl1271_error("SCAN configuration failed");
603 goto out;
604 }
605out:
606 kfree(cfg);
607 return ret;
608}
609
610int wl1271_scan_sched_scan_start(struct wl1271 *wl)
611{
612 struct wl1271_cmd_sched_scan_start *start;
613 int ret = 0;
614
615 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
616
Luciano Coelho683c0022011-05-12 17:07:55 +0300617 if (wl->bss_type != BSS_TYPE_STA_BSS)
618 return -EOPNOTSUPP;
619
620 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
621 return -EBUSY;
622
Luciano Coelho95feadc2011-05-10 14:38:59 +0300623 start = kzalloc(sizeof(*start), GFP_KERNEL);
624 if (!start)
625 return -ENOMEM;
626
627 start->tag = WL1271_SCAN_DEFAULT_TAG;
628
629 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
630 sizeof(*start), 0);
631 if (ret < 0) {
632 wl1271_error("failed to send scan start command");
633 goto out_free;
634 }
635
636out_free:
637 kfree(start);
638 return ret;
639}
640
641void wl1271_scan_sched_scan_results(struct wl1271 *wl)
642{
643 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
644
645 ieee80211_sched_scan_results(wl->hw);
646}
647
648void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
649{
650 struct wl1271_cmd_sched_scan_stop *stop;
651 int ret = 0;
652
653 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
654
655 /* FIXME: what to do if alloc'ing to stop fails? */
656 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
657 if (!stop) {
658 wl1271_error("failed to alloc memory to send sched scan stop");
659 return;
660 }
661
662 stop->tag = WL1271_SCAN_DEFAULT_TAG;
663
664 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
665 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300666 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300667 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300668 goto out_free;
669 }
670 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300671
Luciano Coelho33c2c062011-05-10 14:46:02 +0300672out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300673 kfree(stop);
674}