blob: 197d2c2b1e7413570f7b14b2154950637b69b82f [file] [log] [blame]
Luciano Coelho34dd2aa2010-07-08 17:50:06 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/ieee80211.h>
25
Shahar Levi00d20102010-11-08 11:20:10 +000026#include "wl12xx.h"
27#include "cmd.h"
28#include "scan.h"
29#include "acx.h"
Arik Nemtsov24225b32011-03-01 12:27:26 +020030#include "ps.h"
Eliad Pelleraf7fbb22011-09-19 13:51:42 +030031#include "tx.h"
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030032
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030033void wl1271_scan_complete_work(struct work_struct *work)
34{
Juuso Oikarinen78abd322010-09-21 06:23:32 +020035 struct delayed_work *dwork;
36 struct wl1271 *wl;
Eliad Peller536129c2011-10-05 11:55:45 +020037 struct wl12xx_vif *wlvif;
Eliad Peller251c1772011-08-14 13:17:17 +030038 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030039 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020040
41 dwork = container_of(work, struct delayed_work, work);
42 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030043
44 wl1271_debug(DEBUG_SCAN, "Scanning complete");
45
46 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020047
Arik Nemtsov24225b32011-03-01 12:27:26 +020048 if (wl->state == WL1271_STATE_OFF)
49 goto out;
50
51 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
52 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020053
Eliad Peller536129c2011-10-05 11:55:45 +020054 wlvif = wl12xx_vif_to_data(wl->scan_vif);
55
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030056 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020057 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020058 wl->scan.req = NULL;
Eliad Peller784f6942011-10-05 11:55:39 +020059 wl->scan_vif = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020060
Eliad Peller251c1772011-08-14 13:17:17 +030061 ret = wl1271_ps_elp_wakeup(wl);
62 if (ret < 0)
63 goto out;
64
Arik Nemtsov24225b32011-03-01 12:27:26 +020065 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030066 /* restore hardware connection monitoring template */
67 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030068 }
69
70 /* return to ROC if needed */
Eliad Peller536129c2011-10-05 11:55:45 +020071 is_sta = (wlvif->bss_type == BSS_TYPE_STA_BSS);
72 is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
Eliad Pellere0b38262011-08-28 15:11:55 +030073 if (((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
74 (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) &&
75 !test_bit(wl->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030076 /* restore remain on channel */
77 wl12xx_cmd_role_start_dev(wl);
78 wl12xx_roc(wl, wl->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020079 }
Eliad Peller251c1772011-08-14 13:17:17 +030080 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020081
Juuso Oikarinen78abd322010-09-21 06:23:32 +020082 if (wl->scan.failed) {
83 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030084 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020085 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020086
Eliad Peller251c1772011-08-14 13:17:17 +030087 ieee80211_scan_completed(wl->hw, false);
88
Arik Nemtsov24225b32011-03-01 12:27:26 +020089out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020090 mutex_unlock(&wl->mutex);
91
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030092}
93
94
Luciano Coelho08688d62010-07-08 17:50:07 +030095static int wl1271_get_scan_channels(struct wl1271 *wl,
96 struct cfg80211_scan_request *req,
97 struct basic_scan_channel_params *channels,
98 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030099{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200100 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +0300101 int i, j;
102 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300103
Luciano Coelho08688d62010-07-08 17:50:07 +0300104 for (i = 0, j = 0;
105 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
106 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300107 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300108
Luciano Coelho4a31c112011-03-21 23:16:14 +0200109 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300110 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300111 (req->channels[i]->band == band) &&
112 /*
113 * In passive scans, we scan all remaining
114 * channels, even if not marked as such.
115 * In active scans, we only scan channels not
116 * marked as passive.
117 */
118 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300119 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
120 req->channels[i]->band,
121 req->channels[i]->center_freq);
122 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
123 req->channels[i]->hw_value,
124 req->channels[i]->flags);
125 wl1271_debug(DEBUG_SCAN,
126 "max_antenna_gain %d, max_power %d",
127 req->channels[i]->max_antenna_gain,
128 req->channels[i]->max_power);
129 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
130 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300131
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200132 if (!passive) {
133 channels[j].min_duration =
134 cpu_to_le32(c->min_dwell_time_active);
135 channels[j].max_duration =
136 cpu_to_le32(c->max_dwell_time_active);
137 } else {
138 channels[j].min_duration =
139 cpu_to_le32(c->min_dwell_time_passive);
140 channels[j].max_duration =
141 cpu_to_le32(c->max_dwell_time_passive);
142 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300143 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300144 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300145 channels[j].channel = req->channels[i]->hw_value;
146
147 memset(&channels[j].bssid_lsb, 0xff, 4);
148 memset(&channels[j].bssid_msb, 0xff, 2);
149
150 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200151 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300152
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300153 j++;
154 }
155 }
156
Luciano Coelho08688d62010-07-08 17:50:07 +0300157 return j;
158}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300159
Luciano Coelho08688d62010-07-08 17:50:07 +0300160#define WL1271_NOTHING_TO_SCAN 1
161
Eliad Peller784f6942011-10-05 11:55:39 +0200162static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
163 enum ieee80211_band band,
164 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300165{
166 struct wl1271_cmd_scan *cmd;
167 struct wl1271_cmd_trigger_scan_to *trigger;
168 int ret;
169 u16 scan_options = 0;
170
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300171 /* skip active scans if we don't have SSIDs */
172 if (!passive && wl->scan.req->n_ssids == 0)
173 return WL1271_NOTHING_TO_SCAN;
174
Luciano Coelho08688d62010-07-08 17:50:07 +0300175 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
176 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
177 if (!cmd || !trigger) {
178 ret = -ENOMEM;
179 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300180 }
181
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300182 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300183 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300184
Eliad Pellera4e02f32011-08-14 13:17:10 +0300185 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
186 ret = -EINVAL;
187 goto out;
188 }
189 cmd->params.role_id = wl->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300190 cmd->params.scan_options = cpu_to_le16(scan_options);
191
192 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
193 cmd->channels,
194 band, passive);
195 if (cmd->params.n_ch == 0) {
196 ret = WL1271_NOTHING_TO_SCAN;
197 goto out;
198 }
199
200 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200201 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300202 cmd->params.tx_rate = cpu_to_le32(basic_rate);
203 cmd->params.tid_trigger = 0;
204 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
205
206 if (band == IEEE80211_BAND_2GHZ)
207 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
208 else
209 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
210
211 if (wl->scan.ssid_len && wl->scan.ssid) {
212 cmd->params.ssid_len = wl->scan.ssid_len;
213 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
214 }
215
Eliad Peller784f6942011-10-05 11:55:39 +0200216 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300217
Luciano Coelho08688d62010-07-08 17:50:07 +0300218 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
219 wl->scan.req->ie, wl->scan.req->ie_len,
220 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300221 if (ret < 0) {
222 wl1271_error("PROBE request template failed");
223 goto out;
224 }
225
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300226 /* disable the timeout */
227 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300228 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
229 sizeof(*trigger), 0);
230 if (ret < 0) {
231 wl1271_error("trigger scan to failed for hw scan");
232 goto out;
233 }
234
Luciano Coelho08688d62010-07-08 17:50:07 +0300235 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300236
Luciano Coelho08688d62010-07-08 17:50:07 +0300237 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300238 if (ret < 0) {
239 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300240 goto out;
241 }
242
243out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300244 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300245 kfree(trigger);
246 return ret;
247}
248
Eliad Peller784f6942011-10-05 11:55:39 +0200249void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300250{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200251 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300252 enum ieee80211_band band;
253 u32 rate;
Luciano Coelho08688d62010-07-08 17:50:07 +0300254
255 switch (wl->scan.state) {
256 case WL1271_SCAN_STATE_IDLE:
257 break;
258
259 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300260 band = IEEE80211_BAND_2GHZ;
261 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200262 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300263 if (ret == WL1271_NOTHING_TO_SCAN) {
264 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200265 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300266 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300267
268 break;
269
270 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300271 band = IEEE80211_BAND_2GHZ;
272 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200273 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300274 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200275 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300276 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
277 else
278 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200279 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300280 }
281
282 break;
283
284 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300285 band = IEEE80211_BAND_5GHZ;
286 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200287 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300288 if (ret == WL1271_NOTHING_TO_SCAN) {
289 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200290 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300291 }
292
293 break;
294
295 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300296 band = IEEE80211_BAND_5GHZ;
297 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200298 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300299 if (ret == WL1271_NOTHING_TO_SCAN) {
300 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200301 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300302 }
303
304 break;
305
306 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200307 wl->scan.failed = false;
308 cancel_delayed_work(&wl->scan_complete_work);
309 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
310 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300311 break;
312
313 default:
314 wl1271_error("invalid scan state");
315 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300316 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200317
318 if (ret < 0) {
319 cancel_delayed_work(&wl->scan_complete_work);
320 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
321 msecs_to_jiffies(0));
322 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300323}
324
Eliad Peller784f6942011-10-05 11:55:39 +0200325int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
326 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300327 struct cfg80211_scan_request *req)
328{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200329 /*
330 * cfg80211 should guarantee that we don't get more channels
331 * than what we have registered.
332 */
333 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
334
Luciano Coelho08688d62010-07-08 17:50:07 +0300335 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
336 return -EBUSY;
337
338 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
339
340 if (ssid_len && ssid) {
341 wl->scan.ssid_len = ssid_len;
342 memcpy(wl->scan.ssid, ssid, ssid_len);
343 } else {
344 wl->scan.ssid_len = 0;
345 }
346
Eliad Peller784f6942011-10-05 11:55:39 +0200347 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300348 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200349 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300350
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200351 /* we assume failure so that timeout scenarios are handled correctly */
352 wl->scan.failed = true;
353 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
354 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
355
Eliad Peller784f6942011-10-05 11:55:39 +0200356 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300357
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300358 return 0;
359}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300360
Eliad Peller2aa01592011-06-27 13:06:44 +0300361int wl1271_scan_stop(struct wl1271 *wl)
362{
363 struct wl1271_cmd_header *cmd = NULL;
364 int ret = 0;
365
366 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
367 return -EINVAL;
368
369 wl1271_debug(DEBUG_CMD, "cmd scan stop");
370
371 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
372 if (!cmd) {
373 ret = -ENOMEM;
374 goto out;
375 }
376
377 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
378 sizeof(*cmd), 0);
379 if (ret < 0) {
380 wl1271_error("cmd stop_scan failed");
381 goto out;
382 }
383out:
384 kfree(cmd);
385 return ret;
386}
387
Luciano Coelho95feadc2011-05-10 14:38:59 +0300388static int
389wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
390 struct cfg80211_sched_scan_request *req,
391 struct conn_scan_ch_params *channels,
392 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300393 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300394{
395 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
396 int i, j;
397 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300398 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300399
400 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300401 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300402 i++) {
403 flags = req->channels[i]->flags;
404
Luciano Coelho66870b12011-05-27 15:34:48 +0300405 if (force_passive)
406 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
407
Luciano Coelho2497a242011-05-27 15:34:46 +0300408 if ((req->channels[i]->band == band) &&
409 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300410 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300411 /* if radar is set, we ignore the passive flag */
412 (radar ||
413 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300414 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
415 req->channels[i]->band,
416 req->channels[i]->center_freq);
417 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
418 req->channels[i]->hw_value,
419 req->channels[i]->flags);
420 wl1271_debug(DEBUG_SCAN, "max_power %d",
421 req->channels[i]->max_power);
422
Luciano Coelho50a66d72011-05-27 15:34:47 +0300423 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300424 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300425 channels[j].passive_duration =
426 cpu_to_le16(c->dwell_time_dfs);
427 }
428 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300429 channels[j].passive_duration =
430 cpu_to_le16(c->dwell_time_passive);
431 } else {
432 channels[j].min_duration =
433 cpu_to_le16(c->min_dwell_time_active);
434 channels[j].max_duration =
435 cpu_to_le16(c->max_dwell_time_active);
436 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300437 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300438 channels[j].channel = req->channels[i]->hw_value;
439
440 j++;
441 }
442 }
443
444 return j - start;
445}
446
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300447static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300448wl1271_scan_sched_scan_channels(struct wl1271 *wl,
449 struct cfg80211_sched_scan_request *req,
450 struct wl1271_cmd_sched_scan_config *cfg)
451{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300452 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300453 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300454 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300455 false, true, 0,
456 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300457 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300458 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300459 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300460 false, false,
461 cfg->passive[0],
462 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300463 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300464 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300465 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300466 false, true, 0,
467 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300468 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300469 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300470 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300471 true, true,
472 cfg->passive[1],
473 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300474 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300475 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300476 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300477 false, false,
478 cfg->passive[1] + cfg->dfs,
479 MAX_CHANNELS_5GHZ);
480 /* 802.11j channels are not supported yet */
481 cfg->passive[2] = 0;
482 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300483
Luciano Coelho95feadc2011-05-10 14:38:59 +0300484 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
485 cfg->active[0], cfg->passive[0]);
486 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
487 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300488 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300489
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300490 return cfg->passive[0] || cfg->active[0] ||
491 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
492 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300493}
494
Luciano Coelhofb553772011-09-02 14:28:21 +0300495/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300496static int
497wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
498 struct cfg80211_sched_scan_request *req)
499{
500 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300501 struct cfg80211_match_set *sets = req->match_sets;
502 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300503 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300504
505 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
506
Luciano Coelho20a33e52011-09-02 14:28:23 +0300507 /* count the match sets that contain SSIDs */
508 for (i = 0; i < req->n_match_sets; i++)
509 if (sets[i].ssid.ssid_len > 0)
510 n_match_ssids++;
511
Luciano Coelhofb553772011-09-02 14:28:21 +0300512 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300513 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300514 (!req->n_ssids ||
515 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
516 type = SCAN_SSID_FILTER_ANY;
517 goto out;
518 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300519
Luciano Coelhofb553772011-09-02 14:28:21 +0300520 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
521 if (!cmd) {
522 ret = -ENOMEM;
523 goto out;
524 }
525
Luciano Coelho20a33e52011-09-02 14:28:23 +0300526 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300527 /* No filter, with ssids */
528 type = SCAN_SSID_FILTER_DISABLED;
529
530 for (i = 0; i < req->n_ssids; i++) {
531 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
532 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
533 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
534 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
535 ssids[i].ssid_len);
536 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300537 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300538 } else {
539 type = SCAN_SSID_FILTER_LIST;
540
541 /* Add all SSIDs from the filters */
542 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300543 /* ignore sets without SSIDs */
544 if (!sets[i].ssid.ssid_len)
545 continue;
546
Luciano Coelhofb553772011-09-02 14:28:21 +0300547 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
548 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
549 memcpy(cmd->ssids[cmd->n_ssids].ssid,
550 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
551 cmd->n_ssids++;
552 }
553 if ((req->n_ssids > 1) ||
554 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
555 /*
556 * Mark all the SSIDs passed in the SSID list as HIDDEN,
557 * so they're used in probe requests.
558 */
559 for (i = 0; i < req->n_ssids; i++) {
560 for (j = 0; j < cmd->n_ssids; j++)
561 if (!memcmp(req->ssids[i].ssid,
562 cmd->ssids[j].ssid,
563 req->ssids[i].ssid_len)) {
564 cmd->ssids[j].type =
565 SCAN_SSID_TYPE_HIDDEN;
566 break;
567 }
568 /* Fail if SSID isn't present in the filters */
569 if (j == req->n_ssids) {
570 ret = -EINVAL;
571 goto out_free;
572 }
573 }
574 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300575 }
576
577 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
578
579 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
580 sizeof(*cmd), 0);
581 if (ret < 0) {
582 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300583 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300584 }
585
Luciano Coelhofb553772011-09-02 14:28:21 +0300586out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300587 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300588out:
589 if (ret < 0)
590 return ret;
591 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300592}
593
Luciano Coelho95feadc2011-05-10 14:38:59 +0300594int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200595 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300596 struct cfg80211_sched_scan_request *req,
597 struct ieee80211_sched_scan_ies *ies)
598{
599 struct wl1271_cmd_sched_scan_config *cfg = NULL;
600 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300601 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300602 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300603
604 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
605
606 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
607 if (!cfg)
608 return -ENOMEM;
609
610 cfg->rssi_threshold = c->rssi_threshold;
611 cfg->snr_threshold = c->snr_threshold;
612 cfg->n_probe_reqs = c->num_probe_reqs;
613 /* cycles set to 0 it means infinite (until manually stopped) */
614 cfg->cycles = 0;
615 /* report APs when at least 1 is found */
616 cfg->report_after = 1;
617 /* don't stop scanning automatically when something is found */
618 cfg->terminate = 0;
619 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
620 /* don't filter on BSS type */
621 cfg->bss_type = SCAN_BSS_TYPE_ANY;
622 /* currently NL80211 supports only a single interval */
623 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
624 cfg->intervals[i] = cpu_to_le32(req->interval);
625
Luciano Coelhof9520792011-08-23 18:34:44 +0300626 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300627 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
628 if (ret < 0)
629 goto out;
630
631 cfg->filter_type = ret;
632
633 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300634
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300635 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300636 wl1271_error("scan channel list is empty");
637 ret = -EINVAL;
638 goto out;
639 }
640
Luciano Coelho66870b12011-05-27 15:34:48 +0300641 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300642 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
643 req->ssids[0].ssid_len,
644 ies->ie[IEEE80211_BAND_2GHZ],
645 ies->len[IEEE80211_BAND_2GHZ],
646 IEEE80211_BAND_2GHZ);
647 if (ret < 0) {
648 wl1271_error("2.4GHz PROBE request template failed");
649 goto out;
650 }
651 }
652
Luciano Coelho66870b12011-05-27 15:34:48 +0300653 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300654 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
655 req->ssids[0].ssid_len,
656 ies->ie[IEEE80211_BAND_5GHZ],
657 ies->len[IEEE80211_BAND_5GHZ],
658 IEEE80211_BAND_5GHZ);
659 if (ret < 0) {
660 wl1271_error("5GHz PROBE request template failed");
661 goto out;
662 }
663 }
664
665 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
666
667 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
668 sizeof(*cfg), 0);
669 if (ret < 0) {
670 wl1271_error("SCAN configuration failed");
671 goto out;
672 }
673out:
674 kfree(cfg);
675 return ret;
676}
677
Eliad Peller536129c2011-10-05 11:55:45 +0200678int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300679{
680 struct wl1271_cmd_sched_scan_start *start;
681 int ret = 0;
682
683 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
684
Eliad Peller536129c2011-10-05 11:55:45 +0200685 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300686 return -EOPNOTSUPP;
687
688 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
689 return -EBUSY;
690
Luciano Coelho95feadc2011-05-10 14:38:59 +0300691 start = kzalloc(sizeof(*start), GFP_KERNEL);
692 if (!start)
693 return -ENOMEM;
694
695 start->tag = WL1271_SCAN_DEFAULT_TAG;
696
697 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
698 sizeof(*start), 0);
699 if (ret < 0) {
700 wl1271_error("failed to send scan start command");
701 goto out_free;
702 }
703
704out_free:
705 kfree(start);
706 return ret;
707}
708
709void wl1271_scan_sched_scan_results(struct wl1271 *wl)
710{
711 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
712
713 ieee80211_sched_scan_results(wl->hw);
714}
715
716void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
717{
718 struct wl1271_cmd_sched_scan_stop *stop;
719 int ret = 0;
720
721 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
722
723 /* FIXME: what to do if alloc'ing to stop fails? */
724 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
725 if (!stop) {
726 wl1271_error("failed to alloc memory to send sched scan stop");
727 return;
728 }
729
730 stop->tag = WL1271_SCAN_DEFAULT_TAG;
731
732 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
733 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300734 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300735 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300736 goto out_free;
737 }
738 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300739
Luciano Coelho33c2c062011-05-10 14:46:02 +0300740out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300741 kfree(stop);
742}