aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/wl12xx/scan.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/wireless/wl12xx/scan.c')
-rw-r--r--drivers/net/wireless/wl12xx/scan.c613
1 files changed, 613 insertions, 0 deletions
diff --git a/drivers/net/wireless/wl12xx/scan.c b/drivers/net/wireless/wl12xx/scan.c
new file mode 100644
index 00000000000..afb7356bd48
--- /dev/null
+++ b/drivers/net/wireless/wl12xx/scan.c
@@ -0,0 +1,613 @@
1/*
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
26#include "wl12xx.h"
27#include "cmd.h"
28#include "scan.h"
29#include "acx.h"
30#include "ps.h"
31
32void wl1271_scan_complete_work(struct work_struct *work)
33{
34 struct delayed_work *dwork;
35 struct wl1271 *wl;
36
37 dwork = container_of(work, struct delayed_work, work);
38 wl = container_of(dwork, struct wl1271, scan_complete_work);
39
40 wl1271_debug(DEBUG_SCAN, "Scanning complete");
41
42 mutex_lock(&wl->mutex);
43
44 if (wl->state == WL1271_STATE_OFF)
45 goto out;
46
47 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
48 goto out;
49
50 wl->scan.state = WL1271_SCAN_STATE_IDLE;
51 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
52 wl->scan.req = NULL;
53 ieee80211_scan_completed(wl->hw, false);
54
55 /* restore hardware connection monitoring template */
56 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
57 if (wl1271_ps_elp_wakeup(wl) == 0) {
58 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
59 wl1271_ps_elp_sleep(wl);
60 }
61 }
62
63 if (wl->scan.failed) {
64 wl1271_info("Scan completed due to error.");
65 wl12xx_queue_recovery_work(wl);
66 }
67
68out:
69 mutex_unlock(&wl->mutex);
70
71}
72
73
74static int wl1271_get_scan_channels(struct wl1271 *wl,
75 struct cfg80211_scan_request *req,
76 struct basic_scan_channel_params *channels,
77 enum ieee80211_band band, bool passive)
78{
79 struct conf_scan_settings *c = &wl->conf.scan;
80 int i, j;
81 u32 flags;
82
83 for (i = 0, j = 0;
84 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
85 i++) {
86 flags = req->channels[i]->flags;
87
88 if (!test_bit(i, wl->scan.scanned_ch) &&
89 !(flags & IEEE80211_CHAN_DISABLED) &&
90 (req->channels[i]->band == band) &&
91 /*
92 * In passive scans, we scan all remaining
93 * channels, even if not marked as such.
94 * In active scans, we only scan channels not
95 * marked as passive.
96 */
97 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
98 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
99 req->channels[i]->band,
100 req->channels[i]->center_freq);
101 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
102 req->channels[i]->hw_value,
103 req->channels[i]->flags);
104 wl1271_debug(DEBUG_SCAN,
105 "max_antenna_gain %d, max_power %d",
106 req->channels[i]->max_antenna_gain,
107 req->channels[i]->max_power);
108 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
109 req->channels[i]->beacon_found);
110
111 if (!passive) {
112 channels[j].min_duration =
113 cpu_to_le32(c->min_dwell_time_active);
114 channels[j].max_duration =
115 cpu_to_le32(c->max_dwell_time_active);
116 } else {
117 channels[j].min_duration =
118 cpu_to_le32(c->min_dwell_time_passive);
119 channels[j].max_duration =
120 cpu_to_le32(c->max_dwell_time_passive);
121 }
122 channels[j].early_termination = 0;
123 channels[j].tx_power_att = req->channels[i]->max_power;
124 channels[j].channel = req->channels[i]->hw_value;
125
126 memset(&channels[j].bssid_lsb, 0xff, 4);
127 memset(&channels[j].bssid_msb, 0xff, 2);
128
129 /* Mark the channels we already used */
130 set_bit(i, wl->scan.scanned_ch);
131
132 j++;
133 }
134 }
135
136 return j;
137}
138
139#define WL1271_NOTHING_TO_SCAN 1
140
141static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
142 bool passive, u32 basic_rate)
143{
144 struct wl1271_cmd_scan *cmd;
145 struct wl1271_cmd_trigger_scan_to *trigger;
146 int ret;
147 u16 scan_options = 0;
148
149 /* skip active scans if we don't have SSIDs */
150 if (!passive && wl->scan.req->n_ssids == 0)
151 return WL1271_NOTHING_TO_SCAN;
152
153 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
154 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
155 if (!cmd || !trigger) {
156 ret = -ENOMEM;
157 goto out;
158 }
159
160 /* We always use high priority scans */
161 scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
162
163 if (passive)
164 scan_options |= WL1271_SCAN_OPT_PASSIVE;
165
166 cmd->params.scan_options = cpu_to_le16(scan_options);
167
168 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
169 cmd->channels,
170 band, passive);
171 if (cmd->params.n_ch == 0) {
172 ret = WL1271_NOTHING_TO_SCAN;
173 goto out;
174 }
175
176 cmd->params.tx_rate = cpu_to_le32(basic_rate);
177 cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
178 cmd->params.rx_filter_options =
179 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
180
181 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
182 cmd->params.tx_rate = cpu_to_le32(basic_rate);
183 cmd->params.tid_trigger = 0;
184 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
185
186 if (band == IEEE80211_BAND_2GHZ)
187 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
188 else
189 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
190
191 if (wl->scan.ssid_len && wl->scan.ssid) {
192 cmd->params.ssid_len = wl->scan.ssid_len;
193 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
194 }
195
196 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
197 wl->scan.req->ie, wl->scan.req->ie_len,
198 band);
199 if (ret < 0) {
200 wl1271_error("PROBE request template failed");
201 goto out;
202 }
203
204 /* disable the timeout */
205 trigger->timeout = 0;
206 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
207 sizeof(*trigger), 0);
208 if (ret < 0) {
209 wl1271_error("trigger scan to failed for hw scan");
210 goto out;
211 }
212
213 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
214
215 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
216 if (ret < 0) {
217 wl1271_error("SCAN failed");
218 goto out;
219 }
220
221out:
222 kfree(cmd);
223 kfree(trigger);
224 return ret;
225}
226
227void wl1271_scan_stm(struct wl1271 *wl)
228{
229 int ret = 0;
230
231 switch (wl->scan.state) {
232 case WL1271_SCAN_STATE_IDLE:
233 break;
234
235 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
236 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
237 wl->conf.tx.basic_rate);
238 if (ret == WL1271_NOTHING_TO_SCAN) {
239 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
240 wl1271_scan_stm(wl);
241 }
242
243 break;
244
245 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
246 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
247 wl->conf.tx.basic_rate);
248 if (ret == WL1271_NOTHING_TO_SCAN) {
249 if (wl->enable_11a)
250 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
251 else
252 wl->scan.state = WL1271_SCAN_STATE_DONE;
253 wl1271_scan_stm(wl);
254 }
255
256 break;
257
258 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
259 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
260 wl->conf.tx.basic_rate_5);
261 if (ret == WL1271_NOTHING_TO_SCAN) {
262 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
263 wl1271_scan_stm(wl);
264 }
265
266 break;
267
268 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
269 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
270 wl->conf.tx.basic_rate_5);
271 if (ret == WL1271_NOTHING_TO_SCAN) {
272 wl->scan.state = WL1271_SCAN_STATE_DONE;
273 wl1271_scan_stm(wl);
274 }
275
276 break;
277
278 case WL1271_SCAN_STATE_DONE:
279 wl->scan.failed = false;
280 cancel_delayed_work(&wl->scan_complete_work);
281 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
282 msecs_to_jiffies(0));
283 break;
284
285 default:
286 wl1271_error("invalid scan state");
287 break;
288 }
289
290 if (ret < 0) {
291 cancel_delayed_work(&wl->scan_complete_work);
292 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
293 msecs_to_jiffies(0));
294 }
295}
296
297int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
298 struct cfg80211_scan_request *req)
299{
300 /*
301 * cfg80211 should guarantee that we don't get more channels
302 * than what we have registered.
303 */
304 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
305
306 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
307 return -EBUSY;
308
309 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
310
311 if (ssid_len && ssid) {
312 wl->scan.ssid_len = ssid_len;
313 memcpy(wl->scan.ssid, ssid, ssid_len);
314 } else {
315 wl->scan.ssid_len = 0;
316 }
317
318 wl->scan.req = req;
319 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
320
321 /* we assume failure so that timeout scenarios are handled correctly */
322 wl->scan.failed = true;
323 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
324 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
325
326 wl1271_scan_stm(wl);
327
328 return 0;
329}
330
331int wl1271_scan_stop(struct wl1271 *wl)
332{
333 struct wl1271_cmd_header *cmd = NULL;
334 int ret = 0;
335
336 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
337 return -EINVAL;
338
339 wl1271_debug(DEBUG_CMD, "cmd scan stop");
340
341 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
342 if (!cmd) {
343 ret = -ENOMEM;
344 goto out;
345 }
346
347 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
348 sizeof(*cmd), 0);
349 if (ret < 0) {
350 wl1271_error("cmd stop_scan failed");
351 goto out;
352 }
353out:
354 kfree(cmd);
355 return ret;
356}
357
358static int
359wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
360 struct cfg80211_sched_scan_request *req,
361 struct conn_scan_ch_params *channels,
362 u32 band, bool radar, bool passive,
363 int start, int max_channels)
364{
365 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
366 int i, j;
367 u32 flags;
368 bool force_passive = !req->n_ssids;
369
370 for (i = 0, j = start;
371 i < req->n_channels && j < max_channels;
372 i++) {
373 flags = req->channels[i]->flags;
374
375 if (force_passive)
376 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
377
378 if ((req->channels[i]->band == band) &&
379 !(flags & IEEE80211_CHAN_DISABLED) &&
380 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
381 /* if radar is set, we ignore the passive flag */
382 (radar ||
383 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
384 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
385 req->channels[i]->band,
386 req->channels[i]->center_freq);
387 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
388 req->channels[i]->hw_value,
389 req->channels[i]->flags);
390 wl1271_debug(DEBUG_SCAN, "max_power %d",
391 req->channels[i]->max_power);
392
393 if (flags & IEEE80211_CHAN_RADAR) {
394 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
395 channels[j].passive_duration =
396 cpu_to_le16(c->dwell_time_dfs);
397 }
398 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
399 channels[j].passive_duration =
400 cpu_to_le16(c->dwell_time_passive);
401 } else {
402 channels[j].min_duration =
403 cpu_to_le16(c->min_dwell_time_active);
404 channels[j].max_duration =
405 cpu_to_le16(c->max_dwell_time_active);
406 }
407 channels[j].tx_power_att = req->channels[i]->max_power;
408 channels[j].channel = req->channels[i]->hw_value;
409
410 j++;
411 }
412 }
413
414 return j - start;
415}
416
417static bool
418wl1271_scan_sched_scan_channels(struct wl1271 *wl,
419 struct cfg80211_sched_scan_request *req,
420 struct wl1271_cmd_sched_scan_config *cfg)
421{
422 cfg->passive[0] =
423 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
424 IEEE80211_BAND_2GHZ,
425 false, true, 0,
426 MAX_CHANNELS_2GHZ);
427 cfg->active[0] =
428 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
429 IEEE80211_BAND_2GHZ,
430 false, false,
431 cfg->passive[0],
432 MAX_CHANNELS_2GHZ);
433 cfg->passive[1] =
434 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
435 IEEE80211_BAND_5GHZ,
436 false, true, 0,
437 MAX_CHANNELS_5GHZ);
438 cfg->dfs =
439 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
440 IEEE80211_BAND_5GHZ,
441 true, true,
442 cfg->passive[1],
443 MAX_CHANNELS_5GHZ);
444 cfg->active[1] =
445 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
446 IEEE80211_BAND_5GHZ,
447 false, false,
448 cfg->passive[1] + cfg->dfs,
449 MAX_CHANNELS_5GHZ);
450 /* 802.11j channels are not supported yet */
451 cfg->passive[2] = 0;
452 cfg->active[2] = 0;
453
454 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
455 cfg->active[0], cfg->passive[0]);
456 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
457 cfg->active[1], cfg->passive[1]);
458 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
459
460 return cfg->passive[0] || cfg->active[0] ||
461 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
462 cfg->passive[2] || cfg->active[2];
463}
464
465int wl1271_scan_sched_scan_config(struct wl1271 *wl,
466 struct cfg80211_sched_scan_request *req,
467 struct ieee80211_sched_scan_ies *ies)
468{
469 struct wl1271_cmd_sched_scan_config *cfg = NULL;
470 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
471 int i, ret;
472 bool force_passive = !req->n_ssids;
473
474 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
475
476 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
477 if (!cfg)
478 return -ENOMEM;
479
480 cfg->rssi_threshold = c->rssi_threshold;
481 cfg->snr_threshold = c->snr_threshold;
482 cfg->n_probe_reqs = c->num_probe_reqs;
483 /* cycles set to 0 it means infinite (until manually stopped) */
484 cfg->cycles = 0;
485 /* report APs when at least 1 is found */
486 cfg->report_after = 1;
487 /* don't stop scanning automatically when something is found */
488 cfg->terminate = 0;
489 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
490 /* don't filter on BSS type */
491 cfg->bss_type = SCAN_BSS_TYPE_ANY;
492 /* currently NL80211 supports only a single interval */
493 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
494 cfg->intervals[i] = cpu_to_le32(req->interval);
495
496 if (!force_passive && req->ssids[0].ssid_len && req->ssids[0].ssid) {
497 cfg->filter_type = SCAN_SSID_FILTER_SPECIFIC;
498 cfg->ssid_len = req->ssids[0].ssid_len;
499 memcpy(cfg->ssid, req->ssids[0].ssid,
500 req->ssids[0].ssid_len);
501 } else {
502 cfg->filter_type = SCAN_SSID_FILTER_ANY;
503 cfg->ssid_len = 0;
504 }
505
506 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
507 wl1271_error("scan channel list is empty");
508 ret = -EINVAL;
509 goto out;
510 }
511
512 if (!force_passive && cfg->active[0]) {
513 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
514 req->ssids[0].ssid_len,
515 ies->ie[IEEE80211_BAND_2GHZ],
516 ies->len[IEEE80211_BAND_2GHZ],
517 IEEE80211_BAND_2GHZ);
518 if (ret < 0) {
519 wl1271_error("2.4GHz PROBE request template failed");
520 goto out;
521 }
522 }
523
524 if (!force_passive && cfg->active[1]) {
525 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
526 req->ssids[0].ssid_len,
527 ies->ie[IEEE80211_BAND_5GHZ],
528 ies->len[IEEE80211_BAND_5GHZ],
529 IEEE80211_BAND_5GHZ);
530 if (ret < 0) {
531 wl1271_error("5GHz PROBE request template failed");
532 goto out;
533 }
534 }
535
536 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
537
538 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
539 sizeof(*cfg), 0);
540 if (ret < 0) {
541 wl1271_error("SCAN configuration failed");
542 goto out;
543 }
544out:
545 kfree(cfg);
546 return ret;
547}
548
549int wl1271_scan_sched_scan_start(struct wl1271 *wl)
550{
551 struct wl1271_cmd_sched_scan_start *start;
552 int ret = 0;
553
554 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
555
556 if (wl->bss_type != BSS_TYPE_STA_BSS)
557 return -EOPNOTSUPP;
558
559 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
560 return -EBUSY;
561
562 start = kzalloc(sizeof(*start), GFP_KERNEL);
563 if (!start)
564 return -ENOMEM;
565
566 start->tag = WL1271_SCAN_DEFAULT_TAG;
567
568 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
569 sizeof(*start), 0);
570 if (ret < 0) {
571 wl1271_error("failed to send scan start command");
572 goto out_free;
573 }
574
575out_free:
576 kfree(start);
577 return ret;
578}
579
580void wl1271_scan_sched_scan_results(struct wl1271 *wl)
581{
582 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
583
584 ieee80211_sched_scan_results(wl->hw);
585}
586
587void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
588{
589 struct wl1271_cmd_sched_scan_stop *stop;
590 int ret = 0;
591
592 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
593
594 /* FIXME: what to do if alloc'ing to stop fails? */
595 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
596 if (!stop) {
597 wl1271_error("failed to alloc memory to send sched scan stop");
598 return;
599 }
600
601 stop->tag = WL1271_SCAN_DEFAULT_TAG;
602
603 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
604 sizeof(*stop), 0);
605 if (ret < 0) {
606 wl1271_error("failed to send sched scan stop command");
607 goto out_free;
608 }
609 wl->sched_scanning = false;
610
611out_free:
612 kfree(stop);
613}