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>
31 void wl1271_scan_complete_work(struct work_struct
*work
)
33 struct delayed_work
*dwork
;
36 dwork
= container_of(work
, struct delayed_work
, work
);
37 wl
= container_of(dwork
, struct wl1271
, scan_complete_work
);
39 wl1271_debug(DEBUG_SCAN
, "Scanning complete");
41 mutex_lock(&wl
->mutex
);
43 if (wl
->scan
.state
== WL1271_SCAN_STATE_IDLE
) {
44 mutex_unlock(&wl
->mutex
);
48 wl
->scan
.state
= WL1271_SCAN_STATE_IDLE
;
49 kfree(wl
->scan
.scanned_ch
);
50 wl
->scan
.scanned_ch
= NULL
;
52 ieee80211_scan_completed(wl
->hw
, false);
54 /* restore hardware connection monitoring template */
55 if (test_bit(WL1271_FLAG_STA_ASSOCIATED
, &wl
->flags
))
56 wl1271_cmd_build_ap_probe_req(wl
, wl
->probereq
);
58 if (wl
->scan
.failed
) {
59 wl1271_info("Scan completed due to error.");
60 ieee80211_queue_work(wl
->hw
, &wl
->recovery_work
);
62 mutex_unlock(&wl
->mutex
);
67 static int wl1271_get_scan_channels(struct wl1271
*wl
,
68 struct cfg80211_scan_request
*req
,
69 struct basic_scan_channel_params
*channels
,
70 enum ieee80211_band band
, bool passive
)
72 struct conf_scan_settings
*c
= &wl
->conf
.scan
;
77 i
< req
->n_channels
&& j
< WL1271_SCAN_MAX_CHANNELS
;
80 flags
= req
->channels
[i
]->flags
;
82 if (!wl
->scan
.scanned_ch
[i
] &&
83 !(flags
& IEEE80211_CHAN_DISABLED
) &&
84 ((!!(flags
& IEEE80211_CHAN_PASSIVE_SCAN
)) == passive
) &&
85 (req
->channels
[i
]->band
== band
)) {
87 wl1271_debug(DEBUG_SCAN
, "band %d, center_freq %d ",
88 req
->channels
[i
]->band
,
89 req
->channels
[i
]->center_freq
);
90 wl1271_debug(DEBUG_SCAN
, "hw_value %d, flags %X",
91 req
->channels
[i
]->hw_value
,
92 req
->channels
[i
]->flags
);
93 wl1271_debug(DEBUG_SCAN
,
94 "max_antenna_gain %d, max_power %d",
95 req
->channels
[i
]->max_antenna_gain
,
96 req
->channels
[i
]->max_power
);
97 wl1271_debug(DEBUG_SCAN
, "beacon_found %d",
98 req
->channels
[i
]->beacon_found
);
101 channels
[j
].min_duration
=
102 cpu_to_le32(c
->min_dwell_time_active
);
103 channels
[j
].max_duration
=
104 cpu_to_le32(c
->max_dwell_time_active
);
106 channels
[j
].min_duration
=
107 cpu_to_le32(c
->min_dwell_time_passive
);
108 channels
[j
].max_duration
=
109 cpu_to_le32(c
->max_dwell_time_passive
);
111 channels
[j
].early_termination
= 0;
112 channels
[j
].tx_power_att
= req
->channels
[i
]->max_power
;
113 channels
[j
].channel
= req
->channels
[i
]->hw_value
;
115 memset(&channels
[j
].bssid_lsb
, 0xff, 4);
116 memset(&channels
[j
].bssid_msb
, 0xff, 2);
118 /* Mark the channels we already used */
119 wl
->scan
.scanned_ch
[i
] = true;
128 #define WL1271_NOTHING_TO_SCAN 1
130 static int wl1271_scan_send(struct wl1271
*wl
, enum ieee80211_band band
,
131 bool passive
, u32 basic_rate
)
133 struct wl1271_cmd_scan
*cmd
;
134 struct wl1271_cmd_trigger_scan_to
*trigger
;
136 u16 scan_options
= 0;
138 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
139 trigger
= kzalloc(sizeof(*trigger
), GFP_KERNEL
);
140 if (!cmd
|| !trigger
) {
145 /* We always use high priority scans */
146 scan_options
= WL1271_SCAN_OPT_PRIORITY_HIGH
;
148 /* No SSIDs means that we have a forced passive scan */
149 if (passive
|| wl
->scan
.req
->n_ssids
== 0)
150 scan_options
|= WL1271_SCAN_OPT_PASSIVE
;
152 cmd
->params
.scan_options
= cpu_to_le16(scan_options
);
154 cmd
->params
.n_ch
= wl1271_get_scan_channels(wl
, wl
->scan
.req
,
157 if (cmd
->params
.n_ch
== 0) {
158 ret
= WL1271_NOTHING_TO_SCAN
;
162 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
163 cmd
->params
.rx_config_options
= cpu_to_le32(CFG_RX_ALL_GOOD
);
164 cmd
->params
.rx_filter_options
=
165 cpu_to_le32(CFG_RX_PRSP_EN
| CFG_RX_MGMT_EN
| CFG_RX_BCN_EN
);
167 cmd
->params
.n_probe_reqs
= wl
->conf
.scan
.num_probe_reqs
;
168 cmd
->params
.tx_rate
= cpu_to_le32(basic_rate
);
169 cmd
->params
.tid_trigger
= 0;
170 cmd
->params
.scan_tag
= WL1271_SCAN_DEFAULT_TAG
;
172 if (band
== IEEE80211_BAND_2GHZ
)
173 cmd
->params
.band
= WL1271_SCAN_BAND_2_4_GHZ
;
175 cmd
->params
.band
= WL1271_SCAN_BAND_5_GHZ
;
177 if (wl
->scan
.ssid_len
&& wl
->scan
.ssid
) {
178 cmd
->params
.ssid_len
= wl
->scan
.ssid_len
;
179 memcpy(cmd
->params
.ssid
, wl
->scan
.ssid
, wl
->scan
.ssid_len
);
182 ret
= wl1271_cmd_build_probe_req(wl
, wl
->scan
.ssid
, wl
->scan
.ssid_len
,
183 wl
->scan
.req
->ie
, wl
->scan
.req
->ie_len
,
186 wl1271_error("PROBE request template failed");
190 /* disable the timeout */
191 trigger
->timeout
= 0;
192 ret
= wl1271_cmd_send(wl
, CMD_TRIGGER_SCAN_TO
, trigger
,
193 sizeof(*trigger
), 0);
195 wl1271_error("trigger scan to failed for hw scan");
199 wl1271_dump(DEBUG_SCAN
, "SCAN: ", cmd
, sizeof(*cmd
));
201 ret
= wl1271_cmd_send(wl
, CMD_SCAN
, cmd
, sizeof(*cmd
), 0);
203 wl1271_error("SCAN failed");
213 void wl1271_scan_stm(struct wl1271
*wl
)
217 switch (wl
->scan
.state
) {
218 case WL1271_SCAN_STATE_IDLE
:
221 case WL1271_SCAN_STATE_2GHZ_ACTIVE
:
222 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_2GHZ
, false,
223 wl
->conf
.tx
.basic_rate
);
224 if (ret
== WL1271_NOTHING_TO_SCAN
) {
225 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_PASSIVE
;
231 case WL1271_SCAN_STATE_2GHZ_PASSIVE
:
232 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_2GHZ
, true,
233 wl
->conf
.tx
.basic_rate
);
234 if (ret
== WL1271_NOTHING_TO_SCAN
) {
236 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_ACTIVE
;
238 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
244 case WL1271_SCAN_STATE_5GHZ_ACTIVE
:
245 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_5GHZ
, false,
246 wl
->conf
.tx
.basic_rate_5
);
247 if (ret
== WL1271_NOTHING_TO_SCAN
) {
248 wl
->scan
.state
= WL1271_SCAN_STATE_5GHZ_PASSIVE
;
254 case WL1271_SCAN_STATE_5GHZ_PASSIVE
:
255 ret
= wl1271_scan_send(wl
, IEEE80211_BAND_5GHZ
, true,
256 wl
->conf
.tx
.basic_rate_5
);
257 if (ret
== WL1271_NOTHING_TO_SCAN
) {
258 wl
->scan
.state
= WL1271_SCAN_STATE_DONE
;
264 case WL1271_SCAN_STATE_DONE
:
265 wl
->scan
.failed
= false;
266 cancel_delayed_work(&wl
->scan_complete_work
);
267 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
268 msecs_to_jiffies(0));
272 wl1271_error("invalid scan state");
277 cancel_delayed_work(&wl
->scan_complete_work
);
278 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
279 msecs_to_jiffies(0));
283 int wl1271_scan(struct wl1271
*wl
, const u8
*ssid
, size_t ssid_len
,
284 struct cfg80211_scan_request
*req
)
286 if (wl
->scan
.state
!= WL1271_SCAN_STATE_IDLE
)
289 wl
->scan
.state
= WL1271_SCAN_STATE_2GHZ_ACTIVE
;
291 if (ssid_len
&& ssid
) {
292 wl
->scan
.ssid_len
= ssid_len
;
293 memcpy(wl
->scan
.ssid
, ssid
, ssid_len
);
295 wl
->scan
.ssid_len
= 0;
300 wl
->scan
.scanned_ch
= kcalloc(req
->n_channels
,
301 sizeof(*wl
->scan
.scanned_ch
),
303 /* we assume failure so that timeout scenarios are handled correctly */
304 wl
->scan
.failed
= true;
305 ieee80211_queue_delayed_work(wl
->hw
, &wl
->scan_complete_work
,
306 msecs_to_jiffies(WL1271_SCAN_TIMEOUT
));