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>
33 void wl1271_scan_complete_work(struct work_struct
*work
)
35 struct delayed_work
*dwork
;
40 dwork
= container_of(work
, struct delayed_work
, work
);
41 wl
= container_of(dwork
, struct wl1271
, scan_complete_work
);
43 wl1271_debug(DEBUG_SCAN
, "Scanning complete");
45 mutex_lock(&wl
->mutex
);
47 if (wl
->state
== WL1271_STATE_OFF
)
50 if (wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
)
53 wl
->scan
.state
= WL1271_SCAN_STATE_IDLE
;
54 memset(wl
->scan
.scanned_ch
, 0, sizeof(wl
->scan
.scanned_ch
));
57 ret
= wl1271_ps_elp_wakeup(wl
);
61 if (test_bit(WL1271_FLAG_STA_ASSOCIATED
, &wl
->flags
)) {
62 /* restore hardware connection monitoring template */
63 wl1271_cmd_build_ap_probe_req(wl
, wl
->probereq
);
66 /* return to ROC if needed */
67 is_sta
= (wl
->bss_type
== BSS_TYPE_STA_BSS
);
68 is_ibss
= (wl
->bss_type
== BSS_TYPE_IBSS
);
69 if (((is_sta
&& !test_bit(WL1271_FLAG_STA_ASSOCIATED
, &wl
->flags
)) ||
70 (is_ibss
&& !test_bit(WL1271_FLAG_IBSS_JOINED
, &wl
->flags
))) &&
71 !test_bit(wl
->dev_role_id
, wl
->roc_map
)) {
72 /* restore remain on channel */
73 wl12xx_cmd_role_start_dev(wl
);
74 wl12xx_roc(wl
, wl
->dev_role_id
);
76 wl1271_ps_elp_sleep(wl
);
78 if (wl
->scan
.failed
) {
79 wl1271_info("Scan completed due to error.");
80 wl12xx_queue_recovery_work(wl
);
83 ieee80211_scan_completed(wl
->hw
, false);
86 mutex_unlock(&wl
->mutex
);
91 static int wl1271_get_scan_channels(struct wl1271
*wl
,
92 struct cfg80211_scan_request
*req
,
93 struct basic_scan_channel_params
*channels
,
94 enum ieee80211_band band
, bool passive
)
96 struct conf_scan_settings
*c
= &wl
->conf
.scan
;
101 i
< req
->n_channels
&& j
< WL1271_SCAN_MAX_CHANNELS
;
103 flags
= req
->channels
[i
]->flags
;
105 if (!test_bit(i
, wl
->scan
.scanned_ch
) &&
106 !(flags
& IEEE80211_CHAN_DISABLED
) &&
107 (req
->channels
[i
]->band
== band
) &&
109 * In passive scans, we scan all remaining
110 * channels, even if not marked as such.
111 * In active scans, we only scan channels not
114 (passive
|| !(flags
& IEEE80211_CHAN_PASSIVE_SCAN
))) {
115 wl1271_debug(DEBUG_SCAN
, "band %d, center_freq %d ",
116 req
->channels
[i
]->band
,
117 req
->channels
[i
]->center_freq
);
118 wl1271_debug(DEBUG_SCAN
, "hw_value %d, flags %X",
119 req
->channels
[i
]->hw_value
,
120 req
->channels
[i
]->flags
);
121 wl1271_debug(DEBUG_SCAN
,
122 "max_antenna_gain %d, max_power %d",
123 req
->channels
[i
]->max_antenna_gain
,
124 req
->channels
[i
]->max_power
);
125 wl1271_debug(DEBUG_SCAN
, "beacon_found %d",
126 req
->channels
[i
]->beacon_found
);
129 channels
[j
].min_duration
=
130 cpu_to_le32(c
->min_dwell_time_active
);
131 channels
[j
].max_duration
=
132 cpu_to_le32(c
->max_dwell_time_active
);
134 channels
[j
].min_duration
=
135 cpu_to_le32(c
->min_dwell_time_passive
);
136 channels
[j
].max_duration
=
137 cpu_to_le32(c
->max_dwell_time_passive
);
139 channels
[j
].early_termination
= 0;
140 channels
[j
].tx_power_att
= req
->channels
[i
]->max_power
;
141 channels
[j
].channel
= req
->channels
[i
]->hw_value
;
143 memset(&channels
[j
].bssid_lsb
, 0xff, 4);
144 memset(&channels
[j
].bssid_msb
, 0xff, 2);
146 /* Mark the channels we already used */
147 set_bit(i
, wl
->scan
.scanned_ch
);
156 #define WL1271_NOTHING_TO_SCAN 1
158 static int wl1271_scan_send(struct wl1271
*wl
, enum ieee80211_band band
,
159 bool passive
, u32 basic_rate
)
161 struct wl1271_cmd_scan
*cmd
;
162 struct wl1271_cmd_trigger_scan_to
*trigger
;
164 u16 scan_options
= 0;
166 /* skip active scans if we don't have SSIDs */
167 if (!passive
&& wl
->scan
.req
->n_ssids
== 0)
168 return WL1271_NOTHING_TO_SCAN
;
170 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
171 trigger
= kzalloc(sizeof(*trigger
), GFP_KERNEL
);
172 if (!cmd
|| !trigger
) {
178 scan_options
|= WL1271_SCAN_OPT_PASSIVE
;
180 if (WARN_ON(wl
->role_id
== WL12XX_INVALID_ROLE_ID
)) {
184 cmd
->params
.role_id
= wl
->role_id
;
185 cmd
->params
.scan_options
= cpu_to_le16(scan_options
);
187 cmd
->params
.n_ch
= wl1271_get_scan_channels(wl
, wl
->scan
.req
,
190 if (cmd
->params
.n_ch
== 0) {
191 ret
= WL1271_NOTHING_TO_SCAN
;
195 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
196 cmd
->params
.n_probe_reqs
= wl
->conf
.scan
.num_probe_reqs
;
197 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
198 cmd
->params
.tid_trigger
= 0;
199 cmd
->params
.scan_tag
= WL1271_SCAN_DEFAULT_TAG
;
201 if (band
== IEEE80211_BAND_2GHZ
)
202 cmd
->params
.band
= WL1271_SCAN_BAND_2_4_GHZ
;
204 cmd
->params
.band
= WL1271_SCAN_BAND_5_GHZ
;
206 if (wl
->scan
.ssid_len
&& wl
->scan
.ssid
) {
207 cmd
->params
.ssid_len
= wl
->scan
.ssid_len
;
208 memcpy(cmd
->params
.ssid
, wl
->scan
.ssid
, wl
->scan
.ssid_len
);
211 memcpy(cmd
->addr
, wl
->mac_addr
, ETH_ALEN
);
213 ret
= wl1271_cmd_build_probe_req(wl
, wl
->scan
.ssid
, wl
->scan
.ssid_len
,
214 wl
->scan
.req
->ie
, wl
->scan
.req
->ie_len
,
217 wl1271_error("PROBE request template failed");
221 /* disable the timeout */
222 trigger
->timeout
= 0;
223 ret
= wl1271_cmd_send(wl
, CMD_TRIGGER_SCAN_TO
, trigger
,
224 sizeof(*trigger
), 0);
226 wl1271_error("trigger scan to failed for hw scan");
230 wl1271_dump(DEBUG_SCAN
, "SCAN: ", cmd
, sizeof(*cmd
));
232 ret
= wl1271_cmd_send(wl
, CMD_SCAN
, cmd
, sizeof(*cmd
), 0);
234 wl1271_error("SCAN failed");
244 void wl1271_scan_stm(struct wl1271
*wl
)
247 enum ieee80211_band band
;
250 switch (wl
->scan
.state
) {
251 case WL1271_SCAN_STATE_IDLE
:
254 case WL1271_SCAN_STATE_2GHZ_ACTIVE
:
255 band
= IEEE80211_BAND_2GHZ
;
256 rate
= wl1271_tx_min_rate_get(wl
, wl
->bitrate_masks
[band
]);
257 ret
= wl1271_scan_send(wl
, band
, false, rate
);
258 if (ret
== WL1271_NOTHING_TO_SCAN
) {
259 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_PASSIVE
;
265 case WL1271_SCAN_STATE_2GHZ_PASSIVE
:
266 band
= IEEE80211_BAND_2GHZ
;
267 rate
= wl1271_tx_min_rate_get(wl
, wl
->bitrate_masks
[band
]);
268 ret
= wl1271_scan_send(wl
, band
, true, rate
);
269 if (ret
== WL1271_NOTHING_TO_SCAN
) {
271 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_ACTIVE
;
273 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
279 case WL1271_SCAN_STATE_5GHZ_ACTIVE
:
280 band
= IEEE80211_BAND_5GHZ
;
281 rate
= wl1271_tx_min_rate_get(wl
, wl
->bitrate_masks
[band
]);
282 ret
= wl1271_scan_send(wl
, band
, false, rate
);
283 if (ret
== WL1271_NOTHING_TO_SCAN
) {
284 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_PASSIVE
;
290 case WL1271_SCAN_STATE_5GHZ_PASSIVE
:
291 band
= IEEE80211_BAND_5GHZ
;
292 rate
= wl1271_tx_min_rate_get(wl
, wl
->bitrate_masks
[band
]);
293 ret
= wl1271_scan_send(wl
, band
, true, rate
);
294 if (ret
== WL1271_NOTHING_TO_SCAN
) {
295 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
301 case WL1271_SCAN_STATE_DONE
:
302 wl
->scan
.failed
= false;
303 cancel_delayed_work(&wl
->scan_complete_work
);
304 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
305 msecs_to_jiffies(0));
309 wl1271_error("invalid scan state");
314 cancel_delayed_work(&wl
->scan_complete_work
);
315 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
316 msecs_to_jiffies(0));
320 int wl1271_scan(struct wl1271
*wl
, const u8
*ssid
, size_t ssid_len
,
321 struct cfg80211_scan_request
*req
)
324 * cfg80211 should guarantee that we don't get more channels
325 * than what we have registered.
327 BUG_ON(req
->n_channels
> WL1271_MAX_CHANNELS
);
329 if (wl
->scan
.state
!= WL1271_SCAN_STATE_IDLE
)
332 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_ACTIVE
;
334 if (ssid_len
&& ssid
) {
335 wl
->scan
.ssid_len
= ssid_len
;
336 memcpy(wl
->scan
.ssid
, ssid
, ssid_len
);
338 wl
->scan
.ssid_len
= 0;
342 memset(wl
->scan
.scanned_ch
, 0, sizeof(wl
->scan
.scanned_ch
));
344 /* we assume failure so that timeout scenarios are handled correctly */
345 wl
->scan
.failed
= true;
346 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
347 msecs_to_jiffies(WL1271_SCAN_TIMEOUT
));
354 int wl1271_scan_stop(struct wl1271
*wl
)
356 struct wl1271_cmd_header
*cmd
= NULL
;
359 if (WARN_ON(wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
))
362 wl1271_debug(DEBUG_CMD
, "cmd scan stop");
364 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
370 ret
= wl1271_cmd_send(wl
, CMD_STOP_SCAN
, cmd
,
373 wl1271_error("cmd stop_scan failed");
382 wl1271_scan_get_sched_scan_channels(struct wl1271
*wl
,
383 struct cfg80211_sched_scan_request
*req
,
384 struct conn_scan_ch_params
*channels
,
385 u32 band
, bool radar
, bool passive
,
386 int start
, int max_channels
)
388 struct conf_sched_scan_settings
*c
= &wl
->conf
.sched_scan
;
391 bool force_passive
= !req
->n_ssids
;
393 for (i
= 0, j
= start
;
394 i
< req
->n_channels
&& j
< max_channels
;
396 flags
= req
->channels
[i
]->flags
;
399 flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
401 if ((req
->channels
[i
]->band
== band
) &&
402 !(flags
& IEEE80211_CHAN_DISABLED
) &&
403 (!!(flags
& IEEE80211_CHAN_RADAR
) == radar
) &&
404 /* if radar is set, we ignore the passive flag */
406 !!(flags
& IEEE80211_CHAN_PASSIVE_SCAN
) == passive
)) {
407 wl1271_debug(DEBUG_SCAN
, "band %d, center_freq %d ",
408 req
->channels
[i
]->band
,
409 req
->channels
[i
]->center_freq
);
410 wl1271_debug(DEBUG_SCAN
, "hw_value %d, flags %X",
411 req
->channels
[i
]->hw_value
,
412 req
->channels
[i
]->flags
);
413 wl1271_debug(DEBUG_SCAN
, "max_power %d",
414 req
->channels
[i
]->max_power
);
416 if (flags
& IEEE80211_CHAN_RADAR
) {
417 channels
[j
].flags
|= SCAN_CHANNEL_FLAGS_DFS
;
418 channels
[j
].passive_duration
=
419 cpu_to_le16(c
->dwell_time_dfs
);
421 else if (flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
422 channels
[j
].passive_duration
=
423 cpu_to_le16(c
->dwell_time_passive
);
425 channels
[j
].min_duration
=
426 cpu_to_le16(c
->min_dwell_time_active
);
427 channels
[j
].max_duration
=
428 cpu_to_le16(c
->max_dwell_time_active
);
430 channels
[j
].tx_power_att
= req
->channels
[i
]->max_power
;
431 channels
[j
].channel
= req
->channels
[i
]->hw_value
;
441 wl1271_scan_sched_scan_channels(struct wl1271
*wl
,
442 struct cfg80211_sched_scan_request
*req
,
443 struct wl1271_cmd_sched_scan_config
*cfg
)
446 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_2
,
451 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_2
,
457 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_5
,
462 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_5
,
468 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_5
,
471 cfg
->passive
[1] + cfg
->dfs
,
473 /* 802.11j channels are not supported yet */
477 wl1271_debug(DEBUG_SCAN
, " 2.4GHz: active %d passive %d",
478 cfg
->active
[0], cfg
->passive
[0]);
479 wl1271_debug(DEBUG_SCAN
, " 5GHz: active %d passive %d",
480 cfg
->active
[1], cfg
->passive
[1]);
481 wl1271_debug(DEBUG_SCAN
, " DFS: %d", cfg
->dfs
);
483 return cfg
->passive
[0] || cfg
->active
[0] ||
484 cfg
->passive
[1] || cfg
->active
[1] || cfg
->dfs
||
485 cfg
->passive
[2] || cfg
->active
[2];
488 /* Returns the scan type to be used or a negative value on error */
490 wl12xx_scan_sched_scan_ssid_list(struct wl1271
*wl
,
491 struct cfg80211_sched_scan_request
*req
)
493 struct wl1271_cmd_sched_scan_ssid_list
*cmd
= NULL
;
494 struct cfg80211_match_set
*sets
= req
->match_sets
;
495 struct cfg80211_ssid
*ssids
= req
->ssids
;
496 int ret
= 0, type
, i
, j
, n_match_ssids
= 0;
498 wl1271_debug(DEBUG_CMD
, "cmd sched scan ssid list");
500 /* count the match sets that contain SSIDs */
501 for (i
= 0; i
< req
->n_match_sets
; i
++)
502 if (sets
[i
].ssid
.ssid_len
> 0)
505 /* No filter, no ssids or only bcast ssid */
506 if (!n_match_ssids
&&
508 (req
->n_ssids
== 1 && req
->ssids
[0].ssid_len
== 0))) {
509 type
= SCAN_SSID_FILTER_ANY
;
513 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
519 if (!n_match_ssids
) {
520 /* No filter, with ssids */
521 type
= SCAN_SSID_FILTER_DISABLED
;
523 for (i
= 0; i
< req
->n_ssids
; i
++) {
524 cmd
->ssids
[cmd
->n_ssids
].type
= (ssids
[i
].ssid_len
) ?
525 SCAN_SSID_TYPE_HIDDEN
: SCAN_SSID_TYPE_PUBLIC
;
526 cmd
->ssids
[cmd
->n_ssids
].len
= ssids
[i
].ssid_len
;
527 memcpy(cmd
->ssids
[cmd
->n_ssids
].ssid
, ssids
[i
].ssid
,
532 type
= SCAN_SSID_FILTER_LIST
;
534 /* Add all SSIDs from the filters */
535 for (i
= 0; i
< req
->n_match_sets
; i
++) {
536 /* ignore sets without SSIDs */
537 if (!sets
[i
].ssid
.ssid_len
)
540 cmd
->ssids
[cmd
->n_ssids
].type
= SCAN_SSID_TYPE_PUBLIC
;
541 cmd
->ssids
[cmd
->n_ssids
].len
= sets
[i
].ssid
.ssid_len
;
542 memcpy(cmd
->ssids
[cmd
->n_ssids
].ssid
,
543 sets
[i
].ssid
.ssid
, sets
[i
].ssid
.ssid_len
);
546 if ((req
->n_ssids
> 1) ||
547 (req
->n_ssids
== 1 && req
->ssids
[0].ssid_len
> 0)) {
549 * Mark all the SSIDs passed in the SSID list as HIDDEN,
550 * so they're used in probe requests.
552 for (i
= 0; i
< req
->n_ssids
; i
++) {
553 for (j
= 0; j
< cmd
->n_ssids
; j
++)
554 if (!memcmp(req
->ssids
[i
].ssid
,
556 req
->ssids
[i
].ssid_len
)) {
558 SCAN_SSID_TYPE_HIDDEN
;
561 /* Fail if SSID isn't present in the filters */
562 if (j
== req
->n_ssids
) {
570 wl1271_dump(DEBUG_SCAN
, "SSID_LIST: ", cmd
, sizeof(*cmd
));
572 ret
= wl1271_cmd_send(wl
, CMD_CONNECTION_SCAN_SSID_CFG
, cmd
,
575 wl1271_error("cmd sched scan ssid list failed");
587 int wl1271_scan_sched_scan_config(struct wl1271
*wl
,
588 struct cfg80211_sched_scan_request
*req
,
589 struct ieee80211_sched_scan_ies
*ies
)
591 struct wl1271_cmd_sched_scan_config
*cfg
= NULL
;
592 struct conf_sched_scan_settings
*c
= &wl
->conf
.sched_scan
;
594 bool force_passive
= !req
->n_ssids
;
596 wl1271_debug(DEBUG_CMD
, "cmd sched_scan scan config");
598 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
602 cfg
->rssi_threshold
= c
->rssi_threshold
;
603 cfg
->snr_threshold
= c
->snr_threshold
;
604 cfg
->n_probe_reqs
= c
->num_probe_reqs
;
605 /* cycles set to 0 it means infinite (until manually stopped) */
607 /* report APs when at least 1 is found */
608 cfg
->report_after
= 1;
609 /* don't stop scanning automatically when something is found */
611 cfg
->tag
= WL1271_SCAN_DEFAULT_TAG
;
612 /* don't filter on BSS type */
613 cfg
->bss_type
= SCAN_BSS_TYPE_ANY
;
614 /* currently NL80211 supports only a single interval */
615 for (i
= 0; i
< SCAN_MAX_CYCLE_INTERVALS
; i
++)
616 cfg
->intervals
[i
] = cpu_to_le32(req
->interval
);
619 ret
= wl12xx_scan_sched_scan_ssid_list(wl
, req
);
623 cfg
->filter_type
= ret
;
625 wl1271_debug(DEBUG_SCAN
, "filter_type = %d", cfg
->filter_type
);
627 if (!wl1271_scan_sched_scan_channels(wl
, req
, cfg
)) {
628 wl1271_error("scan channel list is empty");
633 if (!force_passive
&& cfg
->active
[0]) {
634 ret
= wl1271_cmd_build_probe_req(wl
, req
->ssids
[0].ssid
,
635 req
->ssids
[0].ssid_len
,
636 ies
->ie
[IEEE80211_BAND_2GHZ
],
637 ies
->len
[IEEE80211_BAND_2GHZ
],
638 IEEE80211_BAND_2GHZ
);
640 wl1271_error("2.4GHz PROBE request template failed");
645 if (!force_passive
&& cfg
->active
[1]) {
646 ret
= wl1271_cmd_build_probe_req(wl
, req
->ssids
[0].ssid
,
647 req
->ssids
[0].ssid_len
,
648 ies
->ie
[IEEE80211_BAND_5GHZ
],
649 ies
->len
[IEEE80211_BAND_5GHZ
],
650 IEEE80211_BAND_5GHZ
);
652 wl1271_error("5GHz PROBE request template failed");
657 wl1271_dump(DEBUG_SCAN
, "SCAN_CFG: ", cfg
, sizeof(*cfg
));
659 ret
= wl1271_cmd_send(wl
, CMD_CONNECTION_SCAN_CFG
, cfg
,
662 wl1271_error("SCAN configuration failed");
670 int wl1271_scan_sched_scan_start(struct wl1271
*wl
)
672 struct wl1271_cmd_sched_scan_start
*start
;
675 wl1271_debug(DEBUG_CMD
, "cmd periodic scan start");
677 if (wl
->bss_type
!= BSS_TYPE_STA_BSS
)
680 if (!test_bit(WL1271_FLAG_IDLE
, &wl
->flags
))
683 start
= kzalloc(sizeof(*start
), GFP_KERNEL
);
687 start
->tag
= WL1271_SCAN_DEFAULT_TAG
;
689 ret
= wl1271_cmd_send(wl
, CMD_START_PERIODIC_SCAN
, start
,
692 wl1271_error("failed to send scan start command");
701 void wl1271_scan_sched_scan_results(struct wl1271
*wl
)
703 wl1271_debug(DEBUG_SCAN
, "got periodic scan results");
705 ieee80211_sched_scan_results(wl
->hw
);
708 void wl1271_scan_sched_scan_stop(struct wl1271
*wl
)
710 struct wl1271_cmd_sched_scan_stop
*stop
;
713 wl1271_debug(DEBUG_CMD
, "cmd periodic scan stop");
715 /* FIXME: what to do if alloc'ing to stop fails? */
716 stop
= kzalloc(sizeof(*stop
), GFP_KERNEL
);
718 wl1271_error("failed to alloc memory to send sched scan stop");
722 stop
->tag
= WL1271_SCAN_DEFAULT_TAG
;
724 ret
= wl1271_cmd_send(wl
, CMD_STOP_PERIODIC_SCAN
, stop
,
727 wl1271_error("failed to send sched scan stop command");
730 wl
->sched_scanning
= false;