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
;
37 dwork
= container_of(work
, struct delayed_work
, work
);
38 wl
= container_of(dwork
, struct wl1271
, scan_complete_work
);
40 wl1271_debug(DEBUG_SCAN
, "Scanning complete");
42 mutex_lock(&wl
->mutex
);
44 if (wl
->state
== WL1271_STATE_OFF
)
47 if (wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
)
50 wl
->scan
.state
= WL1271_SCAN_STATE_IDLE
;
51 memset(wl
->scan
.scanned_ch
, 0, sizeof(wl
->scan
.scanned_ch
));
53 ieee80211_scan_completed(wl
->hw
, false);
55 /* restore hardware connection monitoring template */
56 if (test_bit(WL1271_FLAG_STA_ASSOCIATED
, &wl
->flags
)) {
57 if (wl1271_ps_elp_wakeup(wl
) == 0) {
58 wl1271_cmd_build_ap_probe_req(wl
, wl
->probereq
);
59 wl1271_ps_elp_sleep(wl
);
63 if (wl
->scan
.failed
) {
64 wl1271_info("Scan completed due to error.");
65 wl12xx_queue_recovery_work(wl
);
69 mutex_unlock(&wl
->mutex
);
74 static int wl1271_get_scan_channels(struct wl1271
*wl
,
75 struct cfg80211_scan_request
*req
,
76 struct basic_scan_channel_params
*channels
,
77 enum ieee80211_band band
, bool passive
)
79 struct conf_scan_settings
*c
= &wl
->conf
.scan
;
84 i
< req
->n_channels
&& j
< WL1271_SCAN_MAX_CHANNELS
;
87 flags
= req
->channels
[i
]->flags
;
89 if (!test_bit(i
, wl
->scan
.scanned_ch
) &&
90 !(flags
& IEEE80211_CHAN_DISABLED
) &&
91 ((!!(flags
& IEEE80211_CHAN_PASSIVE_SCAN
)) == passive
) &&
92 (req
->channels
[i
]->band
== band
)) {
94 wl1271_debug(DEBUG_SCAN
, "band %d, center_freq %d ",
95 req
->channels
[i
]->band
,
96 req
->channels
[i
]->center_freq
);
97 wl1271_debug(DEBUG_SCAN
, "hw_value %d, flags %X",
98 req
->channels
[i
]->hw_value
,
99 req
->channels
[i
]->flags
);
100 wl1271_debug(DEBUG_SCAN
,
101 "max_antenna_gain %d, max_power %d",
102 req
->channels
[i
]->max_antenna_gain
,
103 req
->channels
[i
]->max_power
);
104 wl1271_debug(DEBUG_SCAN
, "beacon_found %d",
105 req
->channels
[i
]->beacon_found
);
108 channels
[j
].min_duration
=
109 cpu_to_le32(c
->min_dwell_time_active
);
110 channels
[j
].max_duration
=
111 cpu_to_le32(c
->max_dwell_time_active
);
113 channels
[j
].min_duration
=
114 cpu_to_le32(c
->min_dwell_time_passive
);
115 channels
[j
].max_duration
=
116 cpu_to_le32(c
->max_dwell_time_passive
);
118 channels
[j
].early_termination
= 0;
119 channels
[j
].tx_power_att
= req
->channels
[i
]->max_power
;
120 channels
[j
].channel
= req
->channels
[i
]->hw_value
;
122 memset(&channels
[j
].bssid_lsb
, 0xff, 4);
123 memset(&channels
[j
].bssid_msb
, 0xff, 2);
125 /* Mark the channels we already used */
126 set_bit(i
, wl
->scan
.scanned_ch
);
135 #define WL1271_NOTHING_TO_SCAN 1
137 static int wl1271_scan_send(struct wl1271
*wl
, enum ieee80211_band band
,
138 bool passive
, u32 basic_rate
)
140 struct wl1271_cmd_scan
*cmd
;
141 struct wl1271_cmd_trigger_scan_to
*trigger
;
143 u16 scan_options
= 0;
145 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
146 trigger
= kzalloc(sizeof(*trigger
), GFP_KERNEL
);
147 if (!cmd
|| !trigger
) {
152 /* We always use high priority scans */
153 scan_options
= WL1271_SCAN_OPT_PRIORITY_HIGH
;
155 /* No SSIDs means that we have a forced passive scan */
156 if (passive
|| wl
->scan
.req
->n_ssids
== 0)
157 scan_options
|= WL1271_SCAN_OPT_PASSIVE
;
159 cmd
->params
.scan_options
= cpu_to_le16(scan_options
);
161 cmd
->params
.n_ch
= wl1271_get_scan_channels(wl
, wl
->scan
.req
,
164 if (cmd
->params
.n_ch
== 0) {
165 ret
= WL1271_NOTHING_TO_SCAN
;
169 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
170 cmd
->params
.rx_config_options
= cpu_to_le32(CFG_RX_ALL_GOOD
);
171 cmd
->params
.rx_filter_options
=
172 cpu_to_le32(CFG_RX_PRSP_EN
| CFG_RX_MGMT_EN
| CFG_RX_BCN_EN
);
174 cmd
->params
.n_probe_reqs
= wl
->conf
.scan
.num_probe_reqs
;
175 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
176 cmd
->params
.tid_trigger
= 0;
177 cmd
->params
.scan_tag
= WL1271_SCAN_DEFAULT_TAG
;
179 if (band
== IEEE80211_BAND_2GHZ
)
180 cmd
->params
.band
= WL1271_SCAN_BAND_2_4_GHZ
;
182 cmd
->params
.band
= WL1271_SCAN_BAND_5_GHZ
;
184 if (wl
->scan
.ssid_len
&& wl
->scan
.ssid
) {
185 cmd
->params
.ssid_len
= wl
->scan
.ssid_len
;
186 memcpy(cmd
->params
.ssid
, wl
->scan
.ssid
, wl
->scan
.ssid_len
);
189 ret
= wl1271_cmd_build_probe_req(wl
, wl
->scan
.ssid
, wl
->scan
.ssid_len
,
190 wl
->scan
.req
->ie
, wl
->scan
.req
->ie_len
,
193 wl1271_error("PROBE request template failed");
197 /* disable the timeout */
198 trigger
->timeout
= 0;
199 ret
= wl1271_cmd_send(wl
, CMD_TRIGGER_SCAN_TO
, trigger
,
200 sizeof(*trigger
), 0);
202 wl1271_error("trigger scan to failed for hw scan");
206 wl1271_dump(DEBUG_SCAN
, "SCAN: ", cmd
, sizeof(*cmd
));
208 ret
= wl1271_cmd_send(wl
, CMD_SCAN
, cmd
, sizeof(*cmd
), 0);
210 wl1271_error("SCAN failed");
220 void wl1271_scan_stm(struct wl1271
*wl
)
224 switch (wl
->scan
.state
) {
225 case WL1271_SCAN_STATE_IDLE
:
228 case WL1271_SCAN_STATE_2GHZ_ACTIVE
:
229 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_2GHZ
, false,
230 wl
->conf
.tx
.basic_rate
);
231 if (ret
== WL1271_NOTHING_TO_SCAN
) {
232 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_PASSIVE
;
238 case WL1271_SCAN_STATE_2GHZ_PASSIVE
:
239 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_2GHZ
, true,
240 wl
->conf
.tx
.basic_rate
);
241 if (ret
== WL1271_NOTHING_TO_SCAN
) {
243 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_ACTIVE
;
245 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
251 case WL1271_SCAN_STATE_5GHZ_ACTIVE
:
252 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_5GHZ
, false,
253 wl
->conf
.tx
.basic_rate_5
);
254 if (ret
== WL1271_NOTHING_TO_SCAN
) {
255 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_PASSIVE
;
261 case WL1271_SCAN_STATE_5GHZ_PASSIVE
:
262 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_5GHZ
, true,
263 wl
->conf
.tx
.basic_rate_5
);
264 if (ret
== WL1271_NOTHING_TO_SCAN
) {
265 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
271 case WL1271_SCAN_STATE_DONE
:
272 wl
->scan
.failed
= false;
273 cancel_delayed_work(&wl
->scan_complete_work
);
274 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
275 msecs_to_jiffies(0));
279 wl1271_error("invalid scan state");
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 int wl1271_scan(struct wl1271
*wl
, const u8
*ssid
, size_t ssid_len
,
291 struct cfg80211_scan_request
*req
)
294 * cfg80211 should guarantee that we don't get more channels
295 * than what we have registered.
297 BUG_ON(req
->n_channels
> WL1271_MAX_CHANNELS
);
299 if (wl
->scan
.state
!= WL1271_SCAN_STATE_IDLE
)
302 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_ACTIVE
;
304 if (ssid_len
&& ssid
) {
305 wl
->scan
.ssid_len
= ssid_len
;
306 memcpy(wl
->scan
.ssid
, ssid
, ssid_len
);
308 wl
->scan
.ssid_len
= 0;
312 memset(wl
->scan
.scanned_ch
, 0, sizeof(wl
->scan
.scanned_ch
));
314 /* we assume failure so that timeout scenarios are handled correctly */
315 wl
->scan
.failed
= true;
316 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
317 msecs_to_jiffies(WL1271_SCAN_TIMEOUT
));
324 int wl1271_scan_stop(struct wl1271
*wl
)
326 struct wl1271_cmd_header
*cmd
= NULL
;
329 if (WARN_ON(wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
))
332 wl1271_debug(DEBUG_CMD
, "cmd scan stop");
334 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
340 ret
= wl1271_cmd_send(wl
, CMD_STOP_SCAN
, cmd
,
343 wl1271_error("cmd stop_scan failed");
352 wl1271_scan_get_sched_scan_channels(struct wl1271
*wl
,
353 struct cfg80211_sched_scan_request
*req
,
354 struct conn_scan_ch_params
*channels
,
355 u32 band
, bool radar
, bool passive
,
356 int start
, int max_channels
)
358 struct conf_sched_scan_settings
*c
= &wl
->conf
.sched_scan
;
361 bool force_passive
= !req
->n_ssids
;
363 for (i
= 0, j
= start
;
364 i
< req
->n_channels
&& j
< max_channels
;
366 flags
= req
->channels
[i
]->flags
;
369 flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
371 if ((req
->channels
[i
]->band
== band
) &&
372 !(flags
& IEEE80211_CHAN_DISABLED
) &&
373 (!!(flags
& IEEE80211_CHAN_RADAR
) == radar
) &&
374 /* if radar is set, we ignore the passive flag */
376 !!(flags
& IEEE80211_CHAN_PASSIVE_SCAN
) == passive
)) {
377 wl1271_debug(DEBUG_SCAN
, "band %d, center_freq %d ",
378 req
->channels
[i
]->band
,
379 req
->channels
[i
]->center_freq
);
380 wl1271_debug(DEBUG_SCAN
, "hw_value %d, flags %X",
381 req
->channels
[i
]->hw_value
,
382 req
->channels
[i
]->flags
);
383 wl1271_debug(DEBUG_SCAN
, "max_power %d",
384 req
->channels
[i
]->max_power
);
386 if (flags
& IEEE80211_CHAN_RADAR
) {
387 channels
[j
].flags
|= SCAN_CHANNEL_FLAGS_DFS
;
388 channels
[j
].passive_duration
=
389 cpu_to_le16(c
->dwell_time_dfs
);
391 else if (flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
392 channels
[j
].passive_duration
=
393 cpu_to_le16(c
->dwell_time_passive
);
395 channels
[j
].min_duration
=
396 cpu_to_le16(c
->min_dwell_time_active
);
397 channels
[j
].max_duration
=
398 cpu_to_le16(c
->max_dwell_time_active
);
400 channels
[j
].tx_power_att
= req
->channels
[i
]->max_power
;
401 channels
[j
].channel
= req
->channels
[i
]->hw_value
;
411 wl1271_scan_sched_scan_channels(struct wl1271
*wl
,
412 struct cfg80211_sched_scan_request
*req
,
413 struct wl1271_cmd_sched_scan_config
*cfg
)
416 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_2
,
421 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_2
,
427 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_5
,
432 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_5
,
438 wl1271_scan_get_sched_scan_channels(wl
, req
, cfg
->channels_5
,
441 cfg
->passive
[1] + cfg
->dfs
,
443 /* 802.11j channels are not supported yet */
447 wl1271_debug(DEBUG_SCAN
, " 2.4GHz: active %d passive %d",
448 cfg
->active
[0], cfg
->passive
[0]);
449 wl1271_debug(DEBUG_SCAN
, " 5GHz: active %d passive %d",
450 cfg
->active
[1], cfg
->passive
[1]);
451 wl1271_debug(DEBUG_SCAN
, " DFS: %d", cfg
->dfs
);
453 return cfg
->passive
[0] || cfg
->active
[0] ||
454 cfg
->passive
[1] || cfg
->active
[1] || cfg
->dfs
||
455 cfg
->passive
[2] || cfg
->active
[2];
458 int wl1271_scan_sched_scan_config(struct wl1271
*wl
,
459 struct cfg80211_sched_scan_request
*req
,
460 struct ieee80211_sched_scan_ies
*ies
)
462 struct wl1271_cmd_sched_scan_config
*cfg
= NULL
;
463 struct conf_sched_scan_settings
*c
= &wl
->conf
.sched_scan
;
465 bool force_passive
= !req
->n_ssids
;
467 wl1271_debug(DEBUG_CMD
, "cmd sched_scan scan config");
469 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
473 cfg
->rssi_threshold
= c
->rssi_threshold
;
474 cfg
->snr_threshold
= c
->snr_threshold
;
475 cfg
->n_probe_reqs
= c
->num_probe_reqs
;
476 /* cycles set to 0 it means infinite (until manually stopped) */
478 /* report APs when at least 1 is found */
479 cfg
->report_after
= 1;
480 /* don't stop scanning automatically when something is found */
482 cfg
->tag
= WL1271_SCAN_DEFAULT_TAG
;
483 /* don't filter on BSS type */
484 cfg
->bss_type
= SCAN_BSS_TYPE_ANY
;
485 /* currently NL80211 supports only a single interval */
486 for (i
= 0; i
< SCAN_MAX_CYCLE_INTERVALS
; i
++)
487 cfg
->intervals
[i
] = cpu_to_le32(req
->interval
);
489 if (!force_passive
&& req
->ssids
[0].ssid_len
&& req
->ssids
[0].ssid
) {
490 cfg
->filter_type
= SCAN_SSID_FILTER_SPECIFIC
;
491 cfg
->ssid_len
= req
->ssids
[0].ssid_len
;
492 memcpy(cfg
->ssid
, req
->ssids
[0].ssid
,
493 req
->ssids
[0].ssid_len
);
495 cfg
->filter_type
= SCAN_SSID_FILTER_ANY
;
499 if (!wl1271_scan_sched_scan_channels(wl
, req
, cfg
)) {
500 wl1271_error("scan channel list is empty");
505 if (!force_passive
&& cfg
->active
[0]) {
506 ret
= wl1271_cmd_build_probe_req(wl
, req
->ssids
[0].ssid
,
507 req
->ssids
[0].ssid_len
,
508 ies
->ie
[IEEE80211_BAND_2GHZ
],
509 ies
->len
[IEEE80211_BAND_2GHZ
],
510 IEEE80211_BAND_2GHZ
);
512 wl1271_error("2.4GHz PROBE request template failed");
517 if (!force_passive
&& cfg
->active
[1]) {
518 ret
= wl1271_cmd_build_probe_req(wl
, req
->ssids
[0].ssid
,
519 req
->ssids
[0].ssid_len
,
520 ies
->ie
[IEEE80211_BAND_5GHZ
],
521 ies
->len
[IEEE80211_BAND_5GHZ
],
522 IEEE80211_BAND_5GHZ
);
524 wl1271_error("5GHz PROBE request template failed");
529 wl1271_dump(DEBUG_SCAN
, "SCAN_CFG: ", cfg
, sizeof(*cfg
));
531 ret
= wl1271_cmd_send(wl
, CMD_CONNECTION_SCAN_CFG
, cfg
,
534 wl1271_error("SCAN configuration failed");
542 int wl1271_scan_sched_scan_start(struct wl1271
*wl
)
544 struct wl1271_cmd_sched_scan_start
*start
;
547 wl1271_debug(DEBUG_CMD
, "cmd periodic scan start");
549 if (wl
->bss_type
!= BSS_TYPE_STA_BSS
)
552 if (!test_bit(WL1271_FLAG_IDLE
, &wl
->flags
))
555 start
= kzalloc(sizeof(*start
), GFP_KERNEL
);
559 start
->tag
= WL1271_SCAN_DEFAULT_TAG
;
561 ret
= wl1271_cmd_send(wl
, CMD_START_PERIODIC_SCAN
, start
,
564 wl1271_error("failed to send scan start command");
573 void wl1271_scan_sched_scan_results(struct wl1271
*wl
)
575 wl1271_debug(DEBUG_SCAN
, "got periodic scan results");
577 ieee80211_sched_scan_results(wl
->hw
);
580 void wl1271_scan_sched_scan_stop(struct wl1271
*wl
)
582 struct wl1271_cmd_sched_scan_stop
*stop
;
585 wl1271_debug(DEBUG_CMD
, "cmd periodic scan stop");
587 /* FIXME: what to do if alloc'ing to stop fails? */
588 stop
= kzalloc(sizeof(*stop
), GFP_KERNEL
);
590 wl1271_error("failed to alloc memory to send sched scan stop");
594 stop
->tag
= WL1271_SCAN_DEFAULT_TAG
;
596 ret
= wl1271_cmd_send(wl
, CMD_STOP_PERIODIC_SCAN
, stop
,
599 wl1271_error("failed to send sched scan stop command");
602 wl
->sched_scanning
= false;