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
24 #include <linux/ieee80211.h>
34 void wl1271_scan_complete_work(struct work_struct *work)
36 struct delayed_work *dwork;
38 struct ieee80211_vif *vif;
39 struct wl12xx_vif *wlvif;
42 dwork = container_of(work, struct delayed_work, work);
43 wl = container_of(dwork, struct wl1271, scan_complete_work);
45 wl1271_debug(DEBUG_SCAN, "Scanning complete");
47 mutex_lock(&wl->mutex);
49 if (wl->state == WL1271_STATE_OFF)
52 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
56 wlvif = wl12xx_vif_to_data(vif);
58 wl->scan.state = WL1271_SCAN_STATE_IDLE;
59 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
63 ret = wl1271_ps_elp_wakeup(wl);
67 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
68 /* restore hardware connection monitoring template */
69 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
72 wl1271_ps_elp_sleep(wl);
74 if (wl->scan.failed) {
75 wl1271_info("Scan completed due to error.");
76 wl12xx_queue_recovery_work(wl);
79 ieee80211_scan_completed(wl->hw, false);
82 mutex_unlock(&wl->mutex);
87 static int wl1271_get_scan_channels(struct wl1271 *wl,
88 struct cfg80211_scan_request *req,
89 struct basic_scan_channel_params *channels,
90 enum ieee80211_band band, bool passive)
92 struct conf_scan_settings *c = &wl->conf.scan;
97 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
99 flags = req->channels[i]->flags;
101 if (!test_bit(i, wl->scan.scanned_ch) &&
102 !(flags & IEEE80211_CHAN_DISABLED) &&
103 (req->channels[i]->band == band) &&
105 * In passive scans, we scan all remaining
106 * channels, even if not marked as such.
107 * In active scans, we only scan channels not
110 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
111 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
112 req->channels[i]->band,
113 req->channels[i]->center_freq);
114 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
115 req->channels[i]->hw_value,
116 req->channels[i]->flags);
117 wl1271_debug(DEBUG_SCAN,
118 "max_antenna_gain %d, max_power %d",
119 req->channels[i]->max_antenna_gain,
120 req->channels[i]->max_power);
121 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
122 req->channels[i]->beacon_found);
125 channels[j].min_duration =
126 cpu_to_le32(c->min_dwell_time_active);
127 channels[j].max_duration =
128 cpu_to_le32(c->max_dwell_time_active);
130 channels[j].min_duration =
131 cpu_to_le32(c->min_dwell_time_passive);
132 channels[j].max_duration =
133 cpu_to_le32(c->max_dwell_time_passive);
135 channels[j].early_termination = 0;
136 channels[j].tx_power_att = req->channels[i]->max_power;
137 channels[j].channel = req->channels[i]->hw_value;
139 memset(&channels[j].bssid_lsb, 0xff, 4);
140 memset(&channels[j].bssid_msb, 0xff, 2);
142 /* Mark the channels we already used */
143 set_bit(i, wl->scan.scanned_ch);
152 #define WL1271_NOTHING_TO_SCAN 1
154 static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
155 enum ieee80211_band band,
156 bool passive, u32 basic_rate)
158 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
159 struct wl1271_cmd_scan *cmd;
160 struct wl1271_cmd_trigger_scan_to *trigger;
162 u16 scan_options = 0;
164 /* skip active scans if we don't have SSIDs */
165 if (!passive && wl->scan.req->n_ssids == 0)
166 return WL1271_NOTHING_TO_SCAN;
168 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
169 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
170 if (!cmd || !trigger) {
176 scan_options |= WL1271_SCAN_OPT_PASSIVE;
178 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID ||
179 wlvif->dev_role_id == WL12XX_INVALID_ROLE_ID)) {
183 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
184 cmd->params.role_id = wlvif->role_id;
186 cmd->params.role_id = wlvif->dev_role_id;
188 cmd->params.scan_options = cpu_to_le16(scan_options);
190 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
193 if (cmd->params.n_ch == 0) {
194 ret = WL1271_NOTHING_TO_SCAN;
198 cmd->params.tx_rate = cpu_to_le32(basic_rate);
199 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
200 cmd->params.tid_trigger = 0;
201 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
203 if (band == IEEE80211_BAND_2GHZ)
204 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
206 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
208 if (wl->scan.ssid_len && wl->scan.ssid) {
209 cmd->params.ssid_len = wl->scan.ssid_len;
210 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
213 memcpy(cmd->addr, vif->addr, ETH_ALEN);
215 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
216 cmd->params.role_id, band,
217 wl->scan.ssid, wl->scan.ssid_len,
219 wl->scan.req->ie_len);
221 wl1271_error("PROBE request template failed");
225 /* disable the timeout */
226 trigger->timeout = 0;
227 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
228 sizeof(*trigger), 0);
230 wl1271_error("trigger scan to failed for hw scan");
234 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
236 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
238 wl1271_error("SCAN failed");
248 void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
250 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
252 enum ieee80211_band band;
255 switch (wl->scan.state) {
256 case WL1271_SCAN_STATE_IDLE:
259 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
260 band = IEEE80211_BAND_2GHZ;
261 mask = wlvif->bitrate_masks[band];
262 if (wl->scan.req->no_cck) {
263 mask &= ~CONF_TX_CCK_RATES;
265 mask = CONF_TX_RATE_MASK_BASIC_P2P;
267 rate = wl1271_tx_min_rate_get(wl, mask);
268 ret = wl1271_scan_send(wl, vif, band, false, rate);
269 if (ret == WL1271_NOTHING_TO_SCAN) {
270 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
271 wl1271_scan_stm(wl, vif);
276 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
277 band = IEEE80211_BAND_2GHZ;
278 mask = wlvif->bitrate_masks[band];
279 if (wl->scan.req->no_cck) {
280 mask &= ~CONF_TX_CCK_RATES;
282 mask = CONF_TX_RATE_MASK_BASIC_P2P;
284 rate = wl1271_tx_min_rate_get(wl, mask);
285 ret = wl1271_scan_send(wl, vif, band, true, rate);
286 if (ret == WL1271_NOTHING_TO_SCAN) {
288 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
290 wl->scan.state = WL1271_SCAN_STATE_DONE;
291 wl1271_scan_stm(wl, vif);
296 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
297 band = IEEE80211_BAND_5GHZ;
298 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
299 ret = wl1271_scan_send(wl, vif, band, false, rate);
300 if (ret == WL1271_NOTHING_TO_SCAN) {
301 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
302 wl1271_scan_stm(wl, vif);
307 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
308 band = IEEE80211_BAND_5GHZ;
309 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
310 ret = wl1271_scan_send(wl, vif, band, true, rate);
311 if (ret == WL1271_NOTHING_TO_SCAN) {
312 wl->scan.state = WL1271_SCAN_STATE_DONE;
313 wl1271_scan_stm(wl, vif);
318 case WL1271_SCAN_STATE_DONE:
319 wl->scan.failed = false;
320 cancel_delayed_work(&wl->scan_complete_work);
321 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
322 msecs_to_jiffies(0));
326 wl1271_error("invalid scan state");
331 cancel_delayed_work(&wl->scan_complete_work);
332 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
333 msecs_to_jiffies(0));
337 int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
338 const u8 *ssid, size_t ssid_len,
339 struct cfg80211_scan_request *req)
342 * cfg80211 should guarantee that we don't get more channels
343 * than what we have registered.
345 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
347 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
350 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
352 if (ssid_len && ssid) {
353 wl->scan.ssid_len = ssid_len;
354 memcpy(wl->scan.ssid, ssid, ssid_len);
356 wl->scan.ssid_len = 0;
361 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
363 /* we assume failure so that timeout scenarios are handled correctly */
364 wl->scan.failed = true;
365 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
366 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
368 wl1271_scan_stm(wl, vif);
373 int wl1271_scan_stop(struct wl1271 *wl)
375 struct wl1271_cmd_header *cmd = NULL;
378 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
381 wl1271_debug(DEBUG_CMD, "cmd scan stop");
383 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
389 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
392 wl1271_error("cmd stop_scan failed");
401 wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
402 struct cfg80211_sched_scan_request *req,
403 struct conn_scan_ch_params *channels,
404 u32 band, bool radar, bool passive,
405 int start, int max_channels)
407 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
410 bool force_passive = !req->n_ssids;
412 for (i = 0, j = start;
413 i < req->n_channels && j < max_channels;
415 flags = req->channels[i]->flags;
418 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
420 if ((req->channels[i]->band == band) &&
421 !(flags & IEEE80211_CHAN_DISABLED) &&
422 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
423 /* if radar is set, we ignore the passive flag */
425 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
426 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
427 req->channels[i]->band,
428 req->channels[i]->center_freq);
429 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
430 req->channels[i]->hw_value,
431 req->channels[i]->flags);
432 wl1271_debug(DEBUG_SCAN, "max_power %d",
433 req->channels[i]->max_power);
435 if (flags & IEEE80211_CHAN_RADAR) {
436 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
438 channels[j].passive_duration =
439 cpu_to_le16(c->dwell_time_dfs);
441 channels[j].passive_duration =
442 cpu_to_le16(c->dwell_time_passive);
445 channels[j].min_duration =
446 cpu_to_le16(c->min_dwell_time_active);
447 channels[j].max_duration =
448 cpu_to_le16(c->max_dwell_time_active);
450 channels[j].tx_power_att = req->channels[i]->max_power;
451 channels[j].channel = req->channels[i]->hw_value;
461 wl1271_scan_sched_scan_channels(struct wl1271 *wl,
462 struct cfg80211_sched_scan_request *req,
463 struct wl1271_cmd_sched_scan_config *cfg)
466 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
471 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
477 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
482 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
488 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
491 cfg->passive[1] + cfg->dfs,
493 /* 802.11j channels are not supported yet */
497 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
498 cfg->active[0], cfg->passive[0]);
499 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
500 cfg->active[1], cfg->passive[1]);
501 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
503 return cfg->passive[0] || cfg->active[0] ||
504 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
505 cfg->passive[2] || cfg->active[2];
508 /* Returns the scan type to be used or a negative value on error */
510 wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
511 struct cfg80211_sched_scan_request *req)
513 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
514 struct cfg80211_match_set *sets = req->match_sets;
515 struct cfg80211_ssid *ssids = req->ssids;
516 int ret = 0, type, i, j, n_match_ssids = 0;
518 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
520 /* count the match sets that contain SSIDs */
521 for (i = 0; i < req->n_match_sets; i++)
522 if (sets[i].ssid.ssid_len > 0)
525 /* No filter, no ssids or only bcast ssid */
526 if (!n_match_ssids &&
528 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
529 type = SCAN_SSID_FILTER_ANY;
533 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
539 if (!n_match_ssids) {
540 /* No filter, with ssids */
541 type = SCAN_SSID_FILTER_DISABLED;
543 for (i = 0; i < req->n_ssids; i++) {
544 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
545 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
546 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
547 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
552 type = SCAN_SSID_FILTER_LIST;
554 /* Add all SSIDs from the filters */
555 for (i = 0; i < req->n_match_sets; i++) {
556 /* ignore sets without SSIDs */
557 if (!sets[i].ssid.ssid_len)
560 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
561 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
562 memcpy(cmd->ssids[cmd->n_ssids].ssid,
563 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
566 if ((req->n_ssids > 1) ||
567 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
569 * Mark all the SSIDs passed in the SSID list as HIDDEN,
570 * so they're used in probe requests.
572 for (i = 0; i < req->n_ssids; i++) {
573 if (!req->ssids[i].ssid_len)
576 for (j = 0; j < cmd->n_ssids; j++)
577 if (!memcmp(req->ssids[i].ssid,
579 req->ssids[i].ssid_len)) {
581 SCAN_SSID_TYPE_HIDDEN;
584 /* Fail if SSID isn't present in the filters */
585 if (j == cmd->n_ssids) {
593 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
595 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
598 wl1271_error("cmd sched scan ssid list failed");
610 int wl1271_scan_sched_scan_config(struct wl1271 *wl,
611 struct wl12xx_vif *wlvif,
612 struct cfg80211_sched_scan_request *req,
613 struct ieee80211_sched_scan_ies *ies)
615 struct wl1271_cmd_sched_scan_config *cfg = NULL;
616 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
618 bool force_passive = !req->n_ssids;
620 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
622 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
626 cfg->rssi_threshold = c->rssi_threshold;
627 cfg->snr_threshold = c->snr_threshold;
628 cfg->n_probe_reqs = c->num_probe_reqs;
629 /* cycles set to 0 it means infinite (until manually stopped) */
631 /* report APs when at least 1 is found */
632 cfg->report_after = 1;
633 /* don't stop scanning automatically when something is found */
635 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
636 /* don't filter on BSS type */
637 cfg->bss_type = SCAN_BSS_TYPE_ANY;
638 /* currently NL80211 supports only a single interval */
639 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
640 cfg->intervals[i] = cpu_to_le32(req->interval);
643 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
647 cfg->filter_type = ret;
649 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
651 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
652 wl1271_error("scan channel list is empty");
657 if (!force_passive && cfg->active[0]) {
658 u8 band = IEEE80211_BAND_2GHZ;
659 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
660 wlvif->dev_role_id, band,
662 req->ssids[0].ssid_len,
666 wl1271_error("2.4GHz PROBE request template failed");
671 if (!force_passive && cfg->active[1]) {
672 u8 band = IEEE80211_BAND_5GHZ;
673 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
674 wlvif->dev_role_id, band,
676 req->ssids[0].ssid_len,
680 wl1271_error("5GHz PROBE request template failed");
685 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
687 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
690 wl1271_error("SCAN configuration failed");
698 int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
700 struct wl1271_cmd_sched_scan_start *start;
703 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
705 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
708 if (test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
711 start = kzalloc(sizeof(*start), GFP_KERNEL);
715 start->tag = WL1271_SCAN_DEFAULT_TAG;
717 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
720 wl1271_error("failed to send scan start command");
729 void wl1271_scan_sched_scan_results(struct wl1271 *wl)
731 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
733 ieee80211_sched_scan_results(wl->hw);
736 void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
738 struct wl1271_cmd_sched_scan_stop *stop;
741 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
743 /* FIXME: what to do if alloc'ing to stop fails? */
744 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
746 wl1271_error("failed to alloc memory to send sched scan stop");
750 stop->tag = WL1271_SCAN_DEFAULT_TAG;
752 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
755 wl1271_error("failed to send sched scan stop command");