Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / net / wireless / wl12xx / scan.c
blob7229eaa8901807576ddb5e2ecdd820b61763a4f0
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2009-2010 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
24 #include <linux/ieee80211.h>
26 #include "wl12xx.h"
27 #include "cmd.h"
28 #include "scan.h"
29 #include "acx.h"
30 #include "ps.h"
32 void wl1271_scan_complete_work(struct work_struct *work)
34 struct delayed_work *dwork;
35 struct wl1271 *wl;
36 int ret;
37 bool is_sta, is_ibss;
39 dwork = container_of(work, struct delayed_work, work);
40 wl = container_of(dwork, struct wl1271, scan_complete_work);
42 wl1271_debug(DEBUG_SCAN, "Scanning complete");
44 mutex_lock(&wl->mutex);
46 if (wl->state == WL1271_STATE_OFF)
47 goto out;
49 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
50 goto out;
52 wl->scan.state = WL1271_SCAN_STATE_IDLE;
53 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
54 wl->scan.req = NULL;
56 ret = wl1271_ps_elp_wakeup(wl);
57 if (ret < 0)
58 goto out;
60 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
61 /* restore hardware connection monitoring template */
62 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
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))) {
70 /* restore remain on channel */
71 wl12xx_cmd_role_start_dev(wl);
72 wl12xx_roc(wl, wl->dev_role_id);
74 wl1271_ps_elp_sleep(wl);
76 if (wl->scan.failed) {
77 wl1271_info("Scan completed due to error.");
78 wl12xx_queue_recovery_work(wl);
81 ieee80211_scan_completed(wl->hw, false);
83 out:
84 mutex_unlock(&wl->mutex);
89 static 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)
94 struct conf_scan_settings *c = &wl->conf.scan;
95 int i, j;
96 u32 flags;
98 for (i = 0, j = 0;
99 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
100 i++) {
102 flags = req->channels[i]->flags;
104 if (!test_bit(i, wl->scan.scanned_ch) &&
105 !(flags & IEEE80211_CHAN_DISABLED) &&
106 ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
107 (req->channels[i]->band == band)) {
109 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);
122 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);
133 channels[j].early_termination = 0;
134 channels[j].tx_power_att = req->channels[i]->max_power;
135 channels[j].channel = req->channels[i]->hw_value;
137 memset(&channels[j].bssid_lsb, 0xff, 4);
138 memset(&channels[j].bssid_msb, 0xff, 2);
140 /* Mark the channels we already used */
141 set_bit(i, wl->scan.scanned_ch);
143 j++;
147 return j;
150 #define WL1271_NOTHING_TO_SCAN 1
152 static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
153 bool passive, u32 basic_rate)
155 struct wl1271_cmd_scan *cmd;
156 struct wl1271_cmd_trigger_scan_to *trigger;
157 int ret;
158 u16 scan_options = 0;
160 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
161 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
162 if (!cmd || !trigger) {
163 ret = -ENOMEM;
164 goto out;
167 /* We always use high priority scans */
168 scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
170 /* No SSIDs means that we have a forced passive scan */
171 if (passive || wl->scan.req->n_ssids == 0)
172 scan_options |= WL1271_SCAN_OPT_PASSIVE;
174 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
175 ret = -EINVAL;
176 goto out;
178 cmd->params.role_id = wl->role_id;
179 cmd->params.scan_options = cpu_to_le16(scan_options);
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;
189 cmd->params.tx_rate = cpu_to_le32(basic_rate);
190 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
191 cmd->params.tx_rate = cpu_to_le32(basic_rate);
192 cmd->params.tid_trigger = 0;
193 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
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;
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);
205 memcpy(cmd->addr, wl->mac_addr, ETH_ALEN);
207 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);
210 if (ret < 0) {
211 wl1271_error("PROBE request template failed");
212 goto out;
215 /* disable the timeout */
216 trigger->timeout = 0;
217 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;
224 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
226 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
227 if (ret < 0) {
228 wl1271_error("SCAN failed");
229 goto out;
232 out:
233 kfree(cmd);
234 kfree(trigger);
235 return ret;
238 void wl1271_scan_stm(struct wl1271 *wl)
240 int ret = 0;
242 switch (wl->scan.state) {
243 case WL1271_SCAN_STATE_IDLE:
244 break;
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);
254 break;
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) {
260 if (wl->enable_11a)
261 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
262 else
263 wl->scan.state = WL1271_SCAN_STATE_DONE;
264 wl1271_scan_stm(wl);
267 break;
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);
277 break;
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);
287 break;
289 case WL1271_SCAN_STATE_DONE:
290 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));
294 break;
296 default:
297 wl1271_error("invalid scan state");
298 break;
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));
308 int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
309 struct cfg80211_scan_request *req)
312 * cfg80211 should guarantee that we don't get more channels
313 * than what we have registered.
315 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
317 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
318 return -EBUSY;
320 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
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;
329 wl->scan.req = req;
330 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
332 /* 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));
337 wl1271_scan_stm(wl);
339 return 0;
342 int wl1271_scan_stop(struct wl1271 *wl)
344 struct wl1271_cmd_header *cmd = NULL;
345 int ret = 0;
347 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
348 return -EINVAL;
350 wl1271_debug(DEBUG_CMD, "cmd scan stop");
352 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
353 if (!cmd) {
354 ret = -ENOMEM;
355 goto out;
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;
364 out:
365 kfree(cmd);
366 return ret;
369 static int
370 wl1271_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,
374 int start, int max_channels)
376 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
377 int i, j;
378 u32 flags;
379 bool force_passive = !req->n_ssids;
381 for (i = 0, j = start;
382 i < req->n_channels && j < max_channels;
383 i++) {
384 flags = req->channels[i]->flags;
386 if (force_passive)
387 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
389 if ((req->channels[i]->band == band) &&
390 !(flags & IEEE80211_CHAN_DISABLED) &&
391 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
392 /* if radar is set, we ignore the passive flag */
393 (radar ||
394 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
395 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);
404 if (flags & IEEE80211_CHAN_RADAR) {
405 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
406 channels[j].passive_duration =
407 cpu_to_le16(c->dwell_time_dfs);
409 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
410 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);
418 channels[j].tx_power_att = req->channels[i]->max_power;
419 channels[j].channel = req->channels[i]->hw_value;
421 j++;
425 return j - start;
428 static bool
429 wl1271_scan_sched_scan_channels(struct wl1271 *wl,
430 struct cfg80211_sched_scan_request *req,
431 struct wl1271_cmd_sched_scan_config *cfg)
433 cfg->passive[0] =
434 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
435 IEEE80211_BAND_2GHZ,
436 false, true, 0,
437 MAX_CHANNELS_2GHZ);
438 cfg->active[0] =
439 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
440 IEEE80211_BAND_2GHZ,
441 false, false,
442 cfg->passive[0],
443 MAX_CHANNELS_2GHZ);
444 cfg->passive[1] =
445 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
446 IEEE80211_BAND_5GHZ,
447 false, true, 0,
448 MAX_CHANNELS_5GHZ);
449 cfg->dfs =
450 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
451 IEEE80211_BAND_5GHZ,
452 true, true,
453 cfg->passive[1],
454 MAX_CHANNELS_5GHZ);
455 cfg->active[1] =
456 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
457 IEEE80211_BAND_5GHZ,
458 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;
465 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]);
469 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
471 return cfg->passive[0] || cfg->active[0] ||
472 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
473 cfg->passive[2] || cfg->active[2];
476 int wl1271_scan_sched_scan_config(struct wl1271 *wl,
477 struct cfg80211_sched_scan_request *req,
478 struct ieee80211_sched_scan_ies *ies)
480 struct wl1271_cmd_sched_scan_config *cfg = NULL;
481 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
482 int i, ret;
483 bool force_passive = !req->n_ssids;
485 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
487 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
488 if (!cfg)
489 return -ENOMEM;
491 cfg->rssi_threshold = c->rssi_threshold;
492 cfg->snr_threshold = c->snr_threshold;
493 cfg->n_probe_reqs = c->num_probe_reqs;
494 /* cycles set to 0 it means infinite (until manually stopped) */
495 cfg->cycles = 0;
496 /* report APs when at least 1 is found */
497 cfg->report_after = 1;
498 /* don't stop scanning automatically when something is found */
499 cfg->terminate = 0;
500 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
501 /* don't filter on BSS type */
502 cfg->bss_type = SCAN_BSS_TYPE_ANY;
503 /* currently NL80211 supports only a single interval */
504 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
505 cfg->intervals[i] = cpu_to_le32(req->interval);
507 if (!force_passive && req->ssids[0].ssid_len && req->ssids[0].ssid) {
508 cfg->filter_type = SCAN_SSID_FILTER_SPECIFIC;
509 cfg->ssid_len = req->ssids[0].ssid_len;
510 memcpy(cfg->ssid, req->ssids[0].ssid,
511 req->ssids[0].ssid_len);
512 } else {
513 cfg->filter_type = SCAN_SSID_FILTER_ANY;
514 cfg->ssid_len = 0;
517 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
518 wl1271_error("scan channel list is empty");
519 ret = -EINVAL;
520 goto out;
523 if (!force_passive && cfg->active[0]) {
524 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
525 req->ssids[0].ssid_len,
526 ies->ie[IEEE80211_BAND_2GHZ],
527 ies->len[IEEE80211_BAND_2GHZ],
528 IEEE80211_BAND_2GHZ);
529 if (ret < 0) {
530 wl1271_error("2.4GHz PROBE request template failed");
531 goto out;
535 if (!force_passive && cfg->active[1]) {
536 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
537 req->ssids[0].ssid_len,
538 ies->ie[IEEE80211_BAND_5GHZ],
539 ies->len[IEEE80211_BAND_5GHZ],
540 IEEE80211_BAND_5GHZ);
541 if (ret < 0) {
542 wl1271_error("5GHz PROBE request template failed");
543 goto out;
547 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
549 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
550 sizeof(*cfg), 0);
551 if (ret < 0) {
552 wl1271_error("SCAN configuration failed");
553 goto out;
555 out:
556 kfree(cfg);
557 return ret;
560 int wl1271_scan_sched_scan_start(struct wl1271 *wl)
562 struct wl1271_cmd_sched_scan_start *start;
563 int ret = 0;
565 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
567 if (wl->bss_type != BSS_TYPE_STA_BSS)
568 return -EOPNOTSUPP;
570 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
571 return -EBUSY;
573 start = kzalloc(sizeof(*start), GFP_KERNEL);
574 if (!start)
575 return -ENOMEM;
577 start->tag = WL1271_SCAN_DEFAULT_TAG;
579 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
580 sizeof(*start), 0);
581 if (ret < 0) {
582 wl1271_error("failed to send scan start command");
583 goto out_free;
586 out_free:
587 kfree(start);
588 return ret;
591 void wl1271_scan_sched_scan_results(struct wl1271 *wl)
593 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
595 ieee80211_sched_scan_results(wl->hw);
598 void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
600 struct wl1271_cmd_sched_scan_stop *stop;
601 int ret = 0;
603 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
605 /* FIXME: what to do if alloc'ing to stop fails? */
606 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
607 if (!stop) {
608 wl1271_error("failed to alloc memory to send sched scan stop");
609 return;
612 stop->tag = WL1271_SCAN_DEFAULT_TAG;
614 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
615 sizeof(*stop), 0);
616 if (ret < 0) {
617 wl1271_error("failed to send sched scan stop command");
618 goto out_free;
620 wl->sched_scanning = false;
622 out_free:
623 kfree(stop);