blob: a8576181ab7ff31b8b93181d017528a8c27d5f0a [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 Peller251c1772011-08-14 13:17:17 +030037 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030038 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020039
40 dwork = container_of(work, struct delayed_work, work);
41 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030042
43 wl1271_debug(DEBUG_SCAN, "Scanning complete");
44
45 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020046
Arik Nemtsov24225b32011-03-01 12:27:26 +020047 if (wl->state == WL1271_STATE_OFF)
48 goto out;
49
50 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
51 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020052
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030053 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020054 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020055 wl->scan.req = NULL;
Eliad Peller784f6942011-10-05 11:55:39 +020056 wl->scan_vif = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020057
Eliad Peller251c1772011-08-14 13:17:17 +030058 ret = wl1271_ps_elp_wakeup(wl);
59 if (ret < 0)
60 goto out;
61
Arik Nemtsov24225b32011-03-01 12:27:26 +020062 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030063 /* restore hardware connection monitoring template */
64 wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030065 }
66
67 /* return to ROC if needed */
68 is_sta = (wl->bss_type == BSS_TYPE_STA_BSS);
69 is_ibss = (wl->bss_type == BSS_TYPE_IBSS);
Eliad Pellere0b38262011-08-28 15:11:55 +030070 if (((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
71 (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) &&
72 !test_bit(wl->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030073 /* restore remain on channel */
74 wl12xx_cmd_role_start_dev(wl);
75 wl12xx_roc(wl, wl->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020076 }
Eliad Peller251c1772011-08-14 13:17:17 +030077 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020078
Juuso Oikarinen78abd322010-09-21 06:23:32 +020079 if (wl->scan.failed) {
80 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030081 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020082 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020083
Eliad Peller251c1772011-08-14 13:17:17 +030084 ieee80211_scan_completed(wl->hw, false);
85
Arik Nemtsov24225b32011-03-01 12:27:26 +020086out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020087 mutex_unlock(&wl->mutex);
88
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030089}
90
91
Luciano Coelho08688d62010-07-08 17:50:07 +030092static int wl1271_get_scan_channels(struct wl1271 *wl,
93 struct cfg80211_scan_request *req,
94 struct basic_scan_channel_params *channels,
95 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030096{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +020097 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +030098 int i, j;
99 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300100
Luciano Coelho08688d62010-07-08 17:50:07 +0300101 for (i = 0, j = 0;
102 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
103 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300104 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300105
Luciano Coelho4a31c112011-03-21 23:16:14 +0200106 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300107 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300108 (req->channels[i]->band == band) &&
109 /*
110 * In passive scans, we scan all remaining
111 * channels, even if not marked as such.
112 * In active scans, we only scan channels not
113 * marked as passive.
114 */
115 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300116 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
117 req->channels[i]->band,
118 req->channels[i]->center_freq);
119 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
120 req->channels[i]->hw_value,
121 req->channels[i]->flags);
122 wl1271_debug(DEBUG_SCAN,
123 "max_antenna_gain %d, max_power %d",
124 req->channels[i]->max_antenna_gain,
125 req->channels[i]->max_power);
126 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
127 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300128
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200129 if (!passive) {
130 channels[j].min_duration =
131 cpu_to_le32(c->min_dwell_time_active);
132 channels[j].max_duration =
133 cpu_to_le32(c->max_dwell_time_active);
134 } else {
135 channels[j].min_duration =
136 cpu_to_le32(c->min_dwell_time_passive);
137 channels[j].max_duration =
138 cpu_to_le32(c->max_dwell_time_passive);
139 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300140 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300141 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300142 channels[j].channel = req->channels[i]->hw_value;
143
144 memset(&channels[j].bssid_lsb, 0xff, 4);
145 memset(&channels[j].bssid_msb, 0xff, 2);
146
147 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200148 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300149
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300150 j++;
151 }
152 }
153
Luciano Coelho08688d62010-07-08 17:50:07 +0300154 return j;
155}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300156
Luciano Coelho08688d62010-07-08 17:50:07 +0300157#define WL1271_NOTHING_TO_SCAN 1
158
Eliad Peller784f6942011-10-05 11:55:39 +0200159static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
160 enum ieee80211_band band,
161 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300162{
163 struct wl1271_cmd_scan *cmd;
164 struct wl1271_cmd_trigger_scan_to *trigger;
165 int ret;
166 u16 scan_options = 0;
167
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300168 /* skip active scans if we don't have SSIDs */
169 if (!passive && wl->scan.req->n_ssids == 0)
170 return WL1271_NOTHING_TO_SCAN;
171
Luciano Coelho08688d62010-07-08 17:50:07 +0300172 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
173 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
174 if (!cmd || !trigger) {
175 ret = -ENOMEM;
176 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300177 }
178
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300179 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300180 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300181
Eliad Pellera4e02f32011-08-14 13:17:10 +0300182 if (WARN_ON(wl->role_id == WL12XX_INVALID_ROLE_ID)) {
183 ret = -EINVAL;
184 goto out;
185 }
186 cmd->params.role_id = wl->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300187 cmd->params.scan_options = cpu_to_le16(scan_options);
188
189 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
190 cmd->channels,
191 band, passive);
192 if (cmd->params.n_ch == 0) {
193 ret = WL1271_NOTHING_TO_SCAN;
194 goto out;
195 }
196
197 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200198 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300199 cmd->params.tx_rate = cpu_to_le32(basic_rate);
200 cmd->params.tid_trigger = 0;
201 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
202
203 if (band == IEEE80211_BAND_2GHZ)
204 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
205 else
206 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
207
208 if (wl->scan.ssid_len && wl->scan.ssid) {
209 cmd->params.ssid_len = wl->scan.ssid_len;
210 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
211 }
212
Eliad Peller784f6942011-10-05 11:55:39 +0200213 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300214
Luciano Coelho08688d62010-07-08 17:50:07 +0300215 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
216 wl->scan.req->ie, wl->scan.req->ie_len,
217 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300218 if (ret < 0) {
219 wl1271_error("PROBE request template failed");
220 goto out;
221 }
222
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300223 /* disable the timeout */
224 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300225 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
226 sizeof(*trigger), 0);
227 if (ret < 0) {
228 wl1271_error("trigger scan to failed for hw scan");
229 goto out;
230 }
231
Luciano Coelho08688d62010-07-08 17:50:07 +0300232 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300233
Luciano Coelho08688d62010-07-08 17:50:07 +0300234 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300235 if (ret < 0) {
236 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300237 goto out;
238 }
239
240out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300241 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300242 kfree(trigger);
243 return ret;
244}
245
Eliad Peller784f6942011-10-05 11:55:39 +0200246void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300247{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200248 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300249 enum ieee80211_band band;
250 u32 rate;
Luciano Coelho08688d62010-07-08 17:50:07 +0300251
252 switch (wl->scan.state) {
253 case WL1271_SCAN_STATE_IDLE:
254 break;
255
256 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300257 band = IEEE80211_BAND_2GHZ;
258 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200259 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300260 if (ret == WL1271_NOTHING_TO_SCAN) {
261 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200262 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300263 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300264
265 break;
266
267 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300268 band = IEEE80211_BAND_2GHZ;
269 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200270 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300271 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200272 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300273 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
274 else
275 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200276 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300277 }
278
279 break;
280
281 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300282 band = IEEE80211_BAND_5GHZ;
283 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200284 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300285 if (ret == WL1271_NOTHING_TO_SCAN) {
286 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200287 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300288 }
289
290 break;
291
292 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300293 band = IEEE80211_BAND_5GHZ;
294 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200295 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300296 if (ret == WL1271_NOTHING_TO_SCAN) {
297 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200298 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300299 }
300
301 break;
302
303 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200304 wl->scan.failed = false;
305 cancel_delayed_work(&wl->scan_complete_work);
306 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
307 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300308 break;
309
310 default:
311 wl1271_error("invalid scan state");
312 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300313 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200314
315 if (ret < 0) {
316 cancel_delayed_work(&wl->scan_complete_work);
317 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
318 msecs_to_jiffies(0));
319 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300320}
321
Eliad Peller784f6942011-10-05 11:55:39 +0200322int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
323 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300324 struct cfg80211_scan_request *req)
325{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200326 /*
327 * cfg80211 should guarantee that we don't get more channels
328 * than what we have registered.
329 */
330 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
331
Luciano Coelho08688d62010-07-08 17:50:07 +0300332 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
333 return -EBUSY;
334
335 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
336
337 if (ssid_len && ssid) {
338 wl->scan.ssid_len = ssid_len;
339 memcpy(wl->scan.ssid, ssid, ssid_len);
340 } else {
341 wl->scan.ssid_len = 0;
342 }
343
Eliad Peller784f6942011-10-05 11:55:39 +0200344 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300345 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200346 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300347
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200348 /* we assume failure so that timeout scenarios are handled correctly */
349 wl->scan.failed = true;
350 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
351 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
352
Eliad Peller784f6942011-10-05 11:55:39 +0200353 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300354
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300355 return 0;
356}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300357
Eliad Peller2aa01592011-06-27 13:06:44 +0300358int wl1271_scan_stop(struct wl1271 *wl)
359{
360 struct wl1271_cmd_header *cmd = NULL;
361 int ret = 0;
362
363 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
364 return -EINVAL;
365
366 wl1271_debug(DEBUG_CMD, "cmd scan stop");
367
368 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
369 if (!cmd) {
370 ret = -ENOMEM;
371 goto out;
372 }
373
374 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
375 sizeof(*cmd), 0);
376 if (ret < 0) {
377 wl1271_error("cmd stop_scan failed");
378 goto out;
379 }
380out:
381 kfree(cmd);
382 return ret;
383}
384
Luciano Coelho95feadc2011-05-10 14:38:59 +0300385static int
386wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
387 struct cfg80211_sched_scan_request *req,
388 struct conn_scan_ch_params *channels,
389 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300390 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300391{
392 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
393 int i, j;
394 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300395 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300396
397 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300398 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300399 i++) {
400 flags = req->channels[i]->flags;
401
Luciano Coelho66870b12011-05-27 15:34:48 +0300402 if (force_passive)
403 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
404
Luciano Coelho2497a242011-05-27 15:34:46 +0300405 if ((req->channels[i]->band == band) &&
406 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300407 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300408 /* if radar is set, we ignore the passive flag */
409 (radar ||
410 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300411 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
412 req->channels[i]->band,
413 req->channels[i]->center_freq);
414 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
415 req->channels[i]->hw_value,
416 req->channels[i]->flags);
417 wl1271_debug(DEBUG_SCAN, "max_power %d",
418 req->channels[i]->max_power);
419
Luciano Coelho50a66d72011-05-27 15:34:47 +0300420 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300421 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300422 channels[j].passive_duration =
423 cpu_to_le16(c->dwell_time_dfs);
424 }
425 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300426 channels[j].passive_duration =
427 cpu_to_le16(c->dwell_time_passive);
428 } else {
429 channels[j].min_duration =
430 cpu_to_le16(c->min_dwell_time_active);
431 channels[j].max_duration =
432 cpu_to_le16(c->max_dwell_time_active);
433 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300434 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300435 channels[j].channel = req->channels[i]->hw_value;
436
437 j++;
438 }
439 }
440
441 return j - start;
442}
443
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300444static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300445wl1271_scan_sched_scan_channels(struct wl1271 *wl,
446 struct cfg80211_sched_scan_request *req,
447 struct wl1271_cmd_sched_scan_config *cfg)
448{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300449 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300450 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300451 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300452 false, true, 0,
453 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300454 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300455 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300456 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300457 false, false,
458 cfg->passive[0],
459 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300460 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300461 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300462 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300463 false, true, 0,
464 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300465 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300466 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300467 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300468 true, true,
469 cfg->passive[1],
470 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300471 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300472 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300473 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300474 false, false,
475 cfg->passive[1] + cfg->dfs,
476 MAX_CHANNELS_5GHZ);
477 /* 802.11j channels are not supported yet */
478 cfg->passive[2] = 0;
479 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300480
Luciano Coelho95feadc2011-05-10 14:38:59 +0300481 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
482 cfg->active[0], cfg->passive[0]);
483 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
484 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300485 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300486
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300487 return cfg->passive[0] || cfg->active[0] ||
488 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
489 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300490}
491
Luciano Coelhofb553772011-09-02 14:28:21 +0300492/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300493static int
494wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
495 struct cfg80211_sched_scan_request *req)
496{
497 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300498 struct cfg80211_match_set *sets = req->match_sets;
499 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300500 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300501
502 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
503
Luciano Coelho20a33e52011-09-02 14:28:23 +0300504 /* count the match sets that contain SSIDs */
505 for (i = 0; i < req->n_match_sets; i++)
506 if (sets[i].ssid.ssid_len > 0)
507 n_match_ssids++;
508
Luciano Coelhofb553772011-09-02 14:28:21 +0300509 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300510 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300511 (!req->n_ssids ||
512 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
513 type = SCAN_SSID_FILTER_ANY;
514 goto out;
515 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300516
Luciano Coelhofb553772011-09-02 14:28:21 +0300517 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
518 if (!cmd) {
519 ret = -ENOMEM;
520 goto out;
521 }
522
Luciano Coelho20a33e52011-09-02 14:28:23 +0300523 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300524 /* No filter, with ssids */
525 type = SCAN_SSID_FILTER_DISABLED;
526
527 for (i = 0; i < req->n_ssids; i++) {
528 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
529 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
530 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
531 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
532 ssids[i].ssid_len);
533 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300534 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300535 } else {
536 type = SCAN_SSID_FILTER_LIST;
537
538 /* Add all SSIDs from the filters */
539 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300540 /* ignore sets without SSIDs */
541 if (!sets[i].ssid.ssid_len)
542 continue;
543
Luciano Coelhofb553772011-09-02 14:28:21 +0300544 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
545 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
546 memcpy(cmd->ssids[cmd->n_ssids].ssid,
547 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
548 cmd->n_ssids++;
549 }
550 if ((req->n_ssids > 1) ||
551 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
552 /*
553 * Mark all the SSIDs passed in the SSID list as HIDDEN,
554 * so they're used in probe requests.
555 */
556 for (i = 0; i < req->n_ssids; i++) {
557 for (j = 0; j < cmd->n_ssids; j++)
558 if (!memcmp(req->ssids[i].ssid,
559 cmd->ssids[j].ssid,
560 req->ssids[i].ssid_len)) {
561 cmd->ssids[j].type =
562 SCAN_SSID_TYPE_HIDDEN;
563 break;
564 }
565 /* Fail if SSID isn't present in the filters */
566 if (j == req->n_ssids) {
567 ret = -EINVAL;
568 goto out_free;
569 }
570 }
571 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300572 }
573
574 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
575
576 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
577 sizeof(*cmd), 0);
578 if (ret < 0) {
579 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300580 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300581 }
582
Luciano Coelhofb553772011-09-02 14:28:21 +0300583out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300584 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300585out:
586 if (ret < 0)
587 return ret;
588 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300589}
590
Luciano Coelho95feadc2011-05-10 14:38:59 +0300591int wl1271_scan_sched_scan_config(struct wl1271 *wl,
592 struct cfg80211_sched_scan_request *req,
593 struct ieee80211_sched_scan_ies *ies)
594{
595 struct wl1271_cmd_sched_scan_config *cfg = NULL;
596 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300597 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300598 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300599
600 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
601
602 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
603 if (!cfg)
604 return -ENOMEM;
605
606 cfg->rssi_threshold = c->rssi_threshold;
607 cfg->snr_threshold = c->snr_threshold;
608 cfg->n_probe_reqs = c->num_probe_reqs;
609 /* cycles set to 0 it means infinite (until manually stopped) */
610 cfg->cycles = 0;
611 /* report APs when at least 1 is found */
612 cfg->report_after = 1;
613 /* don't stop scanning automatically when something is found */
614 cfg->terminate = 0;
615 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
616 /* don't filter on BSS type */
617 cfg->bss_type = SCAN_BSS_TYPE_ANY;
618 /* currently NL80211 supports only a single interval */
619 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
620 cfg->intervals[i] = cpu_to_le32(req->interval);
621
Luciano Coelhof9520792011-08-23 18:34:44 +0300622 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300623 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
624 if (ret < 0)
625 goto out;
626
627 cfg->filter_type = ret;
628
629 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300630
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300631 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300632 wl1271_error("scan channel list is empty");
633 ret = -EINVAL;
634 goto out;
635 }
636
Luciano Coelho66870b12011-05-27 15:34:48 +0300637 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300638 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
639 req->ssids[0].ssid_len,
640 ies->ie[IEEE80211_BAND_2GHZ],
641 ies->len[IEEE80211_BAND_2GHZ],
642 IEEE80211_BAND_2GHZ);
643 if (ret < 0) {
644 wl1271_error("2.4GHz PROBE request template failed");
645 goto out;
646 }
647 }
648
Luciano Coelho66870b12011-05-27 15:34:48 +0300649 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300650 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
651 req->ssids[0].ssid_len,
652 ies->ie[IEEE80211_BAND_5GHZ],
653 ies->len[IEEE80211_BAND_5GHZ],
654 IEEE80211_BAND_5GHZ);
655 if (ret < 0) {
656 wl1271_error("5GHz PROBE request template failed");
657 goto out;
658 }
659 }
660
661 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
662
663 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
664 sizeof(*cfg), 0);
665 if (ret < 0) {
666 wl1271_error("SCAN configuration failed");
667 goto out;
668 }
669out:
670 kfree(cfg);
671 return ret;
672}
673
674int wl1271_scan_sched_scan_start(struct wl1271 *wl)
675{
676 struct wl1271_cmd_sched_scan_start *start;
677 int ret = 0;
678
679 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
680
Luciano Coelho683c0022011-05-12 17:07:55 +0300681 if (wl->bss_type != BSS_TYPE_STA_BSS)
682 return -EOPNOTSUPP;
683
684 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
685 return -EBUSY;
686
Luciano Coelho95feadc2011-05-10 14:38:59 +0300687 start = kzalloc(sizeof(*start), GFP_KERNEL);
688 if (!start)
689 return -ENOMEM;
690
691 start->tag = WL1271_SCAN_DEFAULT_TAG;
692
693 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
694 sizeof(*start), 0);
695 if (ret < 0) {
696 wl1271_error("failed to send scan start command");
697 goto out_free;
698 }
699
700out_free:
701 kfree(start);
702 return ret;
703}
704
705void wl1271_scan_sched_scan_results(struct wl1271 *wl)
706{
707 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
708
709 ieee80211_sched_scan_results(wl->hw);
710}
711
712void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
713{
714 struct wl1271_cmd_sched_scan_stop *stop;
715 int ret = 0;
716
717 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
718
719 /* FIXME: what to do if alloc'ing to stop fails? */
720 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
721 if (!stop) {
722 wl1271_error("failed to alloc memory to send sched scan stop");
723 return;
724 }
725
726 stop->tag = WL1271_SCAN_DEFAULT_TAG;
727
728 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
729 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300730 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300731 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300732 goto out_free;
733 }
734 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300735
Luciano Coelho33c2c062011-05-10 14:46:02 +0300736out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300737 kfree(stop);
738}