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>
32 void wl1271_scan_complete_work(struct work_struct *work)
34 struct delayed_work *dwork;
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)
49 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
52 wl->scan.state = WL1271_SCAN_STATE_IDLE;
53 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
56 ret = wl1271_ps_elp_wakeup(wl);
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);
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;
99 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
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);
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);
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);
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;
158 u16 scan_options = 0;
160 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
161 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
162 if (!cmd || !trigger) {
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)) {
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,
184 if (cmd->params.n_ch == 0) {
185 ret = WL1271_NOTHING_TO_SCAN;
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;
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,
211 wl1271_error("PROBE request template failed");
215 /* disable the timeout */
216 trigger->timeout = 0;
217 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
218 sizeof(*trigger), 0);
220 wl1271_error("trigger scan to failed for hw scan");
224 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
226 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
228 wl1271_error("SCAN failed");
238 void wl1271_scan_stm(struct wl1271 *wl)
242 switch (wl->scan.state) {
243 case WL1271_SCAN_STATE_IDLE:
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;
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) {
261 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
263 wl->scan.state = WL1271_SCAN_STATE_DONE;
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;
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;
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));
297 wl1271_error("invalid scan state");
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)
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);
326 wl->scan.ssid_len = 0;
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));
342 int wl1271_scan_stop(struct wl1271 *wl)
344 struct wl1271_cmd_header *cmd = NULL;
347 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
350 wl1271_debug(DEBUG_CMD, "cmd scan stop");
352 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
358 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
361 wl1271_error("cmd stop_scan failed");
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;
379 bool force_passive = !req->n_ssids;
381 for (i = 0, j = start;
382 i < req->n_channels && j < max_channels;
384 flags = req->channels[i]->flags;
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 */
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);
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;
429 wl1271_scan_sched_scan_channels(struct wl1271 *wl,
430 struct cfg80211_sched_scan_request *req,
431 struct wl1271_cmd_sched_scan_config *cfg)
434 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
439 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
445 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
450 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
456 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
459 cfg->passive[1] + cfg->dfs,
461 /* 802.11j channels are not supported yet */
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;
483 bool force_passive = !req->n_ssids;
485 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
487 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
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) */
496 /* report APs when at least 1 is found */
497 cfg->report_after = 1;
498 /* don't stop scanning automatically when something is found */
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);
513 cfg->filter_type = SCAN_SSID_FILTER_ANY;
517 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
518 wl1271_error("scan channel list is empty");
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);
530 wl1271_error("2.4GHz PROBE request template failed");
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);
542 wl1271_error("5GHz PROBE request template failed");
547 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
549 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
552 wl1271_error("SCAN configuration failed");
560 int wl1271_scan_sched_scan_start(struct wl1271 *wl)
562 struct wl1271_cmd_sched_scan_start *start;
565 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
567 if (wl->bss_type != BSS_TYPE_STA_BSS)
570 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
573 start = kzalloc(sizeof(*start), GFP_KERNEL);
577 start->tag = WL1271_SCAN_DEFAULT_TAG;
579 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
582 wl1271_error("failed to send scan start command");
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;
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);
608 wl1271_error("failed to alloc memory to send sched scan stop");
612 stop->tag = WL1271_SCAN_DEFAULT_TAG;
614 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
617 wl1271_error("failed to send sched scan stop command");
620 wl->sched_scanning = false;