blob: af9d53c8e19c88b0a0e4bc5d4ff1db35f0c18ef1 [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 Coelho08688d62010-07-08 17:50:07 +0300167 /* We always use high priority scans */
168 scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300169
170 /* No SSIDs means that we have a forced passive scan */
171 if (passive || wl->scan.req->n_ssids == 0)
Luciano Coelho08688d62010-07-08 17:50:07 +0300172 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300173
Eliad Pellera4e02f32011-08-14 13:17:10 +0300174 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
175 ret = -EINVAL;
176 goto out;
177 }
178 cmd->params.role_id = wl->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300179 cmd->params.scan_options = cpu_to_le16(scan_options);
180
181 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
182 cmd->channels,
183 band, passive);
184 if (cmd->params.n_ch == 0) {
185 ret = WL1271_NOTHING_TO_SCAN;
186 goto out;
187 }
188
189 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200190 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300191 cmd->params.tx_rate = cpu_to_le32(basic_rate);
192 cmd->params.tid_trigger = 0;
193 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
194
195 if (band == IEEE80211_BAND_2GHZ)
196 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
197 else
198 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
199
200 if (wl->scan.ssid_len && wl->scan.ssid) {
201 cmd->params.ssid_len = wl->scan.ssid_len;
202 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
203 }
204
Eliad Pellera4e02f32011-08-14 13:17:10 +0300205 memcpy(cmd->addr, wl->mac_addr, ETH_ALEN);
206
Luciano Coelho08688d62010-07-08 17:50:07 +0300207 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
208 wl->scan.req->ie, wl->scan.req->ie_len,
209 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300210 if (ret < 0) {
211 wl1271_error("PROBE request template failed");
212 goto out;
213 }
214
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300215 /* disable the timeout */
216 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300217 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
218 sizeof(*trigger), 0);
219 if (ret < 0) {
220 wl1271_error("trigger scan to failed for hw scan");
221 goto out;
222 }
223
Luciano Coelho08688d62010-07-08 17:50:07 +0300224 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300225
Luciano Coelho08688d62010-07-08 17:50:07 +0300226 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300227 if (ret < 0) {
228 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300229 goto out;
230 }
231
232out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300233 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300234 kfree(trigger);
235 return ret;
236}
237
Luciano Coelho08688d62010-07-08 17:50:07 +0300238void wl1271_scan_stm(struct wl1271 *wl)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300239{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200240 int ret = 0;
Luciano Coelho08688d62010-07-08 17:50:07 +0300241
242 switch (wl->scan.state) {
243 case WL1271_SCAN_STATE_IDLE:
244 break;
245
246 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
247 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
248 wl->conf.tx.basic_rate);
249 if (ret == WL1271_NOTHING_TO_SCAN) {
250 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
251 wl1271_scan_stm(wl);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300252 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300253
254 break;
255
256 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
257 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
258 wl->conf.tx.basic_rate);
259 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200260 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300261 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
262 else
263 wl->scan.state = WL1271_SCAN_STATE_DONE;
264 wl1271_scan_stm(wl);
265 }
266
267 break;
268
269 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
270 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
271 wl->conf.tx.basic_rate_5);
272 if (ret == WL1271_NOTHING_TO_SCAN) {
273 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
274 wl1271_scan_stm(wl);
275 }
276
277 break;
278
279 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
280 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
281 wl->conf.tx.basic_rate_5);
282 if (ret == WL1271_NOTHING_TO_SCAN) {
283 wl->scan.state = WL1271_SCAN_STATE_DONE;
284 wl1271_scan_stm(wl);
285 }
286
287 break;
288
289 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200290 wl->scan.failed = false;
291 cancel_delayed_work(&wl->scan_complete_work);
292 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
293 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300294 break;
295
296 default:
297 wl1271_error("invalid scan state");
298 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300299 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200300
301 if (ret < 0) {
302 cancel_delayed_work(&wl->scan_complete_work);
303 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
304 msecs_to_jiffies(0));
305 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300306}
307
308int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
309 struct cfg80211_scan_request *req)
310{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200311 /*
312 * cfg80211 should guarantee that we don't get more channels
313 * than what we have registered.
314 */
315 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
316
Luciano Coelho08688d62010-07-08 17:50:07 +0300317 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
318 return -EBUSY;
319
320 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
321
322 if (ssid_len && ssid) {
323 wl->scan.ssid_len = ssid_len;
324 memcpy(wl->scan.ssid, ssid, ssid_len);
325 } else {
326 wl->scan.ssid_len = 0;
327 }
328
329 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200330 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300331
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200332 /* we assume failure so that timeout scenarios are handled correctly */
333 wl->scan.failed = true;
334 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
335 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
336
Luciano Coelho08688d62010-07-08 17:50:07 +0300337 wl1271_scan_stm(wl);
338
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300339 return 0;
340}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300341
Eliad Peller2aa01592011-06-27 13:06:44 +0300342int wl1271_scan_stop(struct wl1271 *wl)
343{
344 struct wl1271_cmd_header *cmd = NULL;
345 int ret = 0;
346
347 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
348 return -EINVAL;
349
350 wl1271_debug(DEBUG_CMD, "cmd scan stop");
351
352 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
353 if (!cmd) {
354 ret = -ENOMEM;
355 goto out;
356 }
357
358 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
359 sizeof(*cmd), 0);
360 if (ret < 0) {
361 wl1271_error("cmd stop_scan failed");
362 goto out;
363 }
364out:
365 kfree(cmd);
366 return ret;
367}
368
Luciano Coelho95feadc2011-05-10 14:38:59 +0300369static int
370wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
371 struct cfg80211_sched_scan_request *req,
372 struct conn_scan_ch_params *channels,
373 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300374 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300375{
376 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
377 int i, j;
378 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300379 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300380
381 for (i = 0, j = start;
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300382 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300383 i++) {
384 flags = req->channels[i]->flags;
385
Luciano Coelho66870b12011-05-27 15:34:48 +0300386 if (force_passive)
387 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
388
Luciano Coelho2497a242011-05-27 15:34:46 +0300389 if ((req->channels[i]->band == band) &&
390 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300391 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300392 /* if radar is set, we ignore the passive flag */
393 (radar ||
394 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300395 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
396 req->channels[i]->band,
397 req->channels[i]->center_freq);
398 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
399 req->channels[i]->hw_value,
400 req->channels[i]->flags);
401 wl1271_debug(DEBUG_SCAN, "max_power %d",
402 req->channels[i]->max_power);
403
Luciano Coelho50a66d72011-05-27 15:34:47 +0300404 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300405 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300406 channels[j].passive_duration =
407 cpu_to_le16(c->dwell_time_dfs);
408 }
409 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300410 channels[j].passive_duration =
411 cpu_to_le16(c->dwell_time_passive);
412 } else {
413 channels[j].min_duration =
414 cpu_to_le16(c->min_dwell_time_active);
415 channels[j].max_duration =
416 cpu_to_le16(c->max_dwell_time_active);
417 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300418 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300419 channels[j].channel = req->channels[i]->hw_value;
420
421 j++;
422 }
423 }
424
425 return j - start;
426}
427
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300428static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300429wl1271_scan_sched_scan_channels(struct wl1271 *wl,
430 struct cfg80211_sched_scan_request *req,
431 struct wl1271_cmd_sched_scan_config *cfg)
432{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300433 cfg->passive[0] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300434 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300435 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300436 false, true, 0,
437 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300438 cfg->active[0] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300439 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300440 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300441 false, false,
442 cfg->passive[0],
443 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300444 cfg->passive[1] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300445 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300446 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300447 false, true, 0,
448 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300449 cfg->dfs =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300450 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300451 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300452 true, true,
453 cfg->passive[1],
454 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300455 cfg->active[1] =
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300456 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300457 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300458 false, false,
459 cfg->passive[1] + cfg->dfs,
460 MAX_CHANNELS_5GHZ);
461 /* 802.11j channels are not supported yet */
462 cfg->passive[2] = 0;
463 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300464
Luciano Coelho95feadc2011-05-10 14:38:59 +0300465 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
466 cfg->active[0], cfg->passive[0]);
467 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
468 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300469 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300470
Luciano Coelhod2c2bb92011-05-31 16:38:56 +0300471 return cfg->passive[0] || cfg->active[0] ||
472 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
473 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300474}
475
Luciano Coelhof9520792011-08-23 18:34:44 +0300476/* Returns 0 if no wildcard is used, 1 if wildcard is used or a
477 * negative value on error */
478static int
479wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
480 struct cfg80211_sched_scan_request *req)
481{
482 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
483 struct cfg80211_ssid *ssid = req->ssids;
484 int ret, wildcard = 0;
485
486 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
487
488 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
489 if (!cmd)
490 return -ENOMEM;
491
492 while ((cmd->n_ssids < req->n_ssids) && ssid) {
493 if (ssid->ssid_len == 0)
494 wildcard = 1;
495 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_HIDDEN;
496 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 Coelhod2c2bb92011-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 Coelhod2c2bb92011-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}