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;
38 dwork = container_of(work, struct delayed_work, work);
39 wl = container_of(dwork, struct wl1271, scan_complete_work);
41 wl1271_debug(DEBUG_SCAN, "Scanning complete");
43 mutex_lock(&wl->mutex);
45 if (wl->state == WL1271_STATE_OFF)
48 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
51 wl->scan.state = WL1271_SCAN_STATE_IDLE;
52 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
55 ret = wl1271_ps_elp_wakeup(wl);
59 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
60 /* restore hardware connection monitoring template */
61 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
63 /* restore remain on channel */
64 wl12xx_cmd_role_start_dev(wl);
65 wl12xx_roc(wl, wl->dev_role_id);
67 wl1271_ps_elp_sleep(wl);
69 if (wl->scan.failed) {
70 wl1271_info("Scan completed due to error.");
71 wl12xx_queue_recovery_work(wl);
74 ieee80211_scan_completed(wl->hw, false);
77 mutex_unlock(&wl->mutex);
82 static int wl1271_get_scan_channels(struct wl1271 *wl,
83 struct cfg80211_scan_request *req,
84 struct basic_scan_channel_params *channels,
85 enum ieee80211_band band, bool passive)
87 struct conf_scan_settings *c = &wl->conf.scan;
92 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
95 flags = req->channels[i]->flags;
97 if (!test_bit(i, wl->scan.scanned_ch) &&
98 !(flags & IEEE80211_CHAN_DISABLED) &&
99 ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
100 (req->channels[i]->band == band)) {
102 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
103 req->channels[i]->band,
104 req->channels[i]->center_freq);
105 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
106 req->channels[i]->hw_value,
107 req->channels[i]->flags);
108 wl1271_debug(DEBUG_SCAN,
109 "max_antenna_gain %d, max_power %d",
110 req->channels[i]->max_antenna_gain,
111 req->channels[i]->max_power);
112 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
113 req->channels[i]->beacon_found);
116 channels[j].min_duration =
117 cpu_to_le32(c->min_dwell_time_active);
118 channels[j].max_duration =
119 cpu_to_le32(c->max_dwell_time_active);
121 channels[j].min_duration =
122 cpu_to_le32(c->min_dwell_time_passive);
123 channels[j].max_duration =
124 cpu_to_le32(c->max_dwell_time_passive);
126 channels[j].early_termination = 0;
127 channels[j].tx_power_att = req->channels[i]->max_power;
128 channels[j].channel = req->channels[i]->hw_value;
130 memset(&channels[j].bssid_lsb, 0xff, 4);
131 memset(&channels[j].bssid_msb, 0xff, 2);
133 /* Mark the channels we already used */
134 set_bit(i, wl->scan.scanned_ch);
143 #define WL1271_NOTHING_TO_SCAN 1
145 static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
146 bool passive, u32 basic_rate)
148 struct wl1271_cmd_scan *cmd;
149 struct wl1271_cmd_trigger_scan_to *trigger;
151 u16 scan_options = 0;
153 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
154 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
155 if (!cmd || !trigger) {
160 /* We always use high priority scans */
161 scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
163 /* No SSIDs means that we have a forced passive scan */
164 if (passive || wl->scan.req->n_ssids == 0)
165 scan_options |= WL1271_SCAN_OPT_PASSIVE;
167 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
171 cmd->params.role_id = wl->role_id;
172 cmd->params.scan_options = cpu_to_le16(scan_options);
174 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
177 if (cmd->params.n_ch == 0) {
178 ret = WL1271_NOTHING_TO_SCAN;
182 cmd->params.tx_rate = cpu_to_le32(basic_rate);
183 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
184 cmd->params.tx_rate = cpu_to_le32(basic_rate);
185 cmd->params.tid_trigger = 0;
186 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
188 if (band == IEEE80211_BAND_2GHZ)
189 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
191 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
193 if (wl->scan.ssid_len && wl->scan.ssid) {
194 cmd->params.ssid_len = wl->scan.ssid_len;
195 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
198 memcpy(cmd->addr, wl->mac_addr, ETH_ALEN);
200 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
201 wl->scan.req->ie, wl->scan.req->ie_len,
204 wl1271_error("PROBE request template failed");
208 /* disable the timeout */
209 trigger->timeout = 0;
210 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
211 sizeof(*trigger), 0);
213 wl1271_error("trigger scan to failed for hw scan");
217 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
219 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
221 wl1271_error("SCAN failed");
231 void wl1271_scan_stm(struct wl1271 *wl)
235 switch (wl->scan.state) {
236 case WL1271_SCAN_STATE_IDLE:
239 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
240 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
241 wl->conf.tx.basic_rate);
242 if (ret == WL1271_NOTHING_TO_SCAN) {
243 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
249 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
250 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
251 wl->conf.tx.basic_rate);
252 if (ret == WL1271_NOTHING_TO_SCAN) {
254 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
256 wl->scan.state = WL1271_SCAN_STATE_DONE;
262 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
263 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
264 wl->conf.tx.basic_rate_5);
265 if (ret == WL1271_NOTHING_TO_SCAN) {
266 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
272 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
273 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
274 wl->conf.tx.basic_rate_5);
275 if (ret == WL1271_NOTHING_TO_SCAN) {
276 wl->scan.state = WL1271_SCAN_STATE_DONE;
282 case WL1271_SCAN_STATE_DONE:
283 wl->scan.failed = false;
284 cancel_delayed_work(&wl->scan_complete_work);
285 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
286 msecs_to_jiffies(0));
290 wl1271_error("invalid scan state");
295 cancel_delayed_work(&wl->scan_complete_work);
296 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
297 msecs_to_jiffies(0));
301 int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
302 struct cfg80211_scan_request *req)
305 * cfg80211 should guarantee that we don't get more channels
306 * than what we have registered.
308 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
310 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
313 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
315 if (ssid_len && ssid) {
316 wl->scan.ssid_len = ssid_len;
317 memcpy(wl->scan.ssid, ssid, ssid_len);
319 wl->scan.ssid_len = 0;
323 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
325 /* we assume failure so that timeout scenarios are handled correctly */
326 wl->scan.failed = true;
327 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
328 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
335 int wl1271_scan_stop(struct wl1271 *wl)
337 struct wl1271_cmd_header *cmd = NULL;
340 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
343 wl1271_debug(DEBUG_CMD, "cmd scan stop");
345 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
351 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
354 wl1271_error("cmd stop_scan failed");
363 wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
364 struct cfg80211_sched_scan_request *req,
365 struct conn_scan_ch_params *channels,
366 u32 band, bool radar, bool passive,
367 int start, int max_channels)
369 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
372 bool force_passive = !req->n_ssids;
374 for (i = 0, j = start;
375 i < req->n_channels && j < max_channels;
377 flags = req->channels[i]->flags;
380 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
382 if ((req->channels[i]->band == band) &&
383 !(flags & IEEE80211_CHAN_DISABLED) &&
384 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
385 /* if radar is set, we ignore the passive flag */
387 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
388 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
389 req->channels[i]->band,
390 req->channels[i]->center_freq);
391 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
392 req->channels[i]->hw_value,
393 req->channels[i]->flags);
394 wl1271_debug(DEBUG_SCAN, "max_power %d",
395 req->channels[i]->max_power);
397 if (flags & IEEE80211_CHAN_RADAR) {
398 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
399 channels[j].passive_duration =
400 cpu_to_le16(c->dwell_time_dfs);
402 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
403 channels[j].passive_duration =
404 cpu_to_le16(c->dwell_time_passive);
406 channels[j].min_duration =
407 cpu_to_le16(c->min_dwell_time_active);
408 channels[j].max_duration =
409 cpu_to_le16(c->max_dwell_time_active);
411 channels[j].tx_power_att = req->channels[i]->max_power;
412 channels[j].channel = req->channels[i]->hw_value;
422 wl1271_scan_sched_scan_channels(struct wl1271 *wl,
423 struct cfg80211_sched_scan_request *req,
424 struct wl1271_cmd_sched_scan_config *cfg)
427 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
432 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
438 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
443 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
449 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
452 cfg->passive[1] + cfg->dfs,
454 /* 802.11j channels are not supported yet */
458 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
459 cfg->active[0], cfg->passive[0]);
460 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
461 cfg->active[1], cfg->passive[1]);
462 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
464 return cfg->passive[0] || cfg->active[0] ||
465 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
466 cfg->passive[2] || cfg->active[2];
469 int wl1271_scan_sched_scan_config(struct wl1271 *wl,
470 struct cfg80211_sched_scan_request *req,
471 struct ieee80211_sched_scan_ies *ies)
473 struct wl1271_cmd_sched_scan_config *cfg = NULL;
474 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
476 bool force_passive = !req->n_ssids;
478 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
480 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
484 cfg->rssi_threshold = c->rssi_threshold;
485 cfg->snr_threshold = c->snr_threshold;
486 cfg->n_probe_reqs = c->num_probe_reqs;
487 /* cycles set to 0 it means infinite (until manually stopped) */
489 /* report APs when at least 1 is found */
490 cfg->report_after = 1;
491 /* don't stop scanning automatically when something is found */
493 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
494 /* don't filter on BSS type */
495 cfg->bss_type = SCAN_BSS_TYPE_ANY;
496 /* currently NL80211 supports only a single interval */
497 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
498 cfg->intervals[i] = cpu_to_le32(req->interval);
500 if (!force_passive && req->ssids[0].ssid_len && req->ssids[0].ssid) {
501 cfg->filter_type = SCAN_SSID_FILTER_SPECIFIC;
502 cfg->ssid_len = req->ssids[0].ssid_len;
503 memcpy(cfg->ssid, req->ssids[0].ssid,
504 req->ssids[0].ssid_len);
506 cfg->filter_type = SCAN_SSID_FILTER_ANY;
510 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
511 wl1271_error("scan channel list is empty");
516 if (!force_passive && cfg->active[0]) {
517 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
518 req->ssids[0].ssid_len,
519 ies->ie[IEEE80211_BAND_2GHZ],
520 ies->len[IEEE80211_BAND_2GHZ],
521 IEEE80211_BAND_2GHZ);
523 wl1271_error("2.4GHz PROBE request template failed");
528 if (!force_passive && cfg->active[1]) {
529 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
530 req->ssids[0].ssid_len,
531 ies->ie[IEEE80211_BAND_5GHZ],
532 ies->len[IEEE80211_BAND_5GHZ],
533 IEEE80211_BAND_5GHZ);
535 wl1271_error("5GHz PROBE request template failed");
540 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
542 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
545 wl1271_error("SCAN configuration failed");
553 int wl1271_scan_sched_scan_start(struct wl1271 *wl)
555 struct wl1271_cmd_sched_scan_start *start;
558 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
560 if (wl->bss_type != BSS_TYPE_STA_BSS)
563 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
566 start = kzalloc(sizeof(*start), GFP_KERNEL);
570 start->tag = WL1271_SCAN_DEFAULT_TAG;
572 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
575 wl1271_error("failed to send scan start command");
584 void wl1271_scan_sched_scan_results(struct wl1271 *wl)
586 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
588 ieee80211_sched_scan_results(wl->hw);
591 void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
593 struct wl1271_cmd_sched_scan_stop *stop;
596 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
598 /* FIXME: what to do if alloc'ing to stop fails? */
599 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
601 wl1271_error("failed to alloc memory to send sched scan stop");
605 stop->tag = WL1271_SCAN_DEFAULT_TAG;
607 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
610 wl1271_error("failed to send sched scan stop command");
613 wl->sched_scanning = false;