blob: 9d0dfb516e26459b35264394e25a2977850b04ee [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 */
Eliad Pellerbddb29b2011-10-05 11:55:49 +020067 wl1271_cmd_build_ap_probe_req(wl, wlvif->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{
Eliad Peller0603d892011-10-05 11:55:51 +0200166 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300167 struct wl1271_cmd_scan *cmd;
168 struct wl1271_cmd_trigger_scan_to *trigger;
169 int ret;
170 u16 scan_options = 0;
171
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300172 /* skip active scans if we don't have SSIDs */
173 if (!passive && wl->scan.req->n_ssids == 0)
174 return WL1271_NOTHING_TO_SCAN;
175
Luciano Coelho08688d62010-07-08 17:50:07 +0300176 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
177 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
178 if (!cmd || !trigger) {
179 ret = -ENOMEM;
180 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300181 }
182
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300183 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300184 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300185
Eliad Peller0603d892011-10-05 11:55:51 +0200186 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID)) {
Eliad Pellera4e02f32011-08-14 13:17:10 +0300187 ret = -EINVAL;
188 goto out;
189 }
Eliad Peller0603d892011-10-05 11:55:51 +0200190 cmd->params.role_id = wlvif->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300191 cmd->params.scan_options = cpu_to_le16(scan_options);
192
193 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
194 cmd->channels,
195 band, passive);
196 if (cmd->params.n_ch == 0) {
197 ret = WL1271_NOTHING_TO_SCAN;
198 goto out;
199 }
200
201 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200202 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300203 cmd->params.tx_rate = cpu_to_le32(basic_rate);
204 cmd->params.tid_trigger = 0;
205 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
206
207 if (band == IEEE80211_BAND_2GHZ)
208 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
209 else
210 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
211
212 if (wl->scan.ssid_len && wl->scan.ssid) {
213 cmd->params.ssid_len = wl->scan.ssid_len;
214 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
215 }
216
Eliad Peller784f6942011-10-05 11:55:39 +0200217 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300218
Luciano Coelho08688d62010-07-08 17:50:07 +0300219 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
220 wl->scan.req->ie, wl->scan.req->ie_len,
221 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300222 if (ret < 0) {
223 wl1271_error("PROBE request template failed");
224 goto out;
225 }
226
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300227 /* disable the timeout */
228 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300229 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
230 sizeof(*trigger), 0);
231 if (ret < 0) {
232 wl1271_error("trigger scan to failed for hw scan");
233 goto out;
234 }
235
Luciano Coelho08688d62010-07-08 17:50:07 +0300236 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300237
Luciano Coelho08688d62010-07-08 17:50:07 +0300238 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300239 if (ret < 0) {
240 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300241 goto out;
242 }
243
244out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300245 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300246 kfree(trigger);
247 return ret;
248}
249
Eliad Peller784f6942011-10-05 11:55:39 +0200250void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300251{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200252 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300253 enum ieee80211_band band;
254 u32 rate;
Luciano Coelho08688d62010-07-08 17:50:07 +0300255
256 switch (wl->scan.state) {
257 case WL1271_SCAN_STATE_IDLE:
258 break;
259
260 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300261 band = IEEE80211_BAND_2GHZ;
262 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200263 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300264 if (ret == WL1271_NOTHING_TO_SCAN) {
265 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200266 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300267 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300268
269 break;
270
271 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300272 band = IEEE80211_BAND_2GHZ;
273 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200274 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300275 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200276 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300277 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
278 else
279 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200280 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300281 }
282
283 break;
284
285 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300286 band = IEEE80211_BAND_5GHZ;
287 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200288 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300289 if (ret == WL1271_NOTHING_TO_SCAN) {
290 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200291 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300292 }
293
294 break;
295
296 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300297 band = IEEE80211_BAND_5GHZ;
298 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200299 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300300 if (ret == WL1271_NOTHING_TO_SCAN) {
301 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200302 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300303 }
304
305 break;
306
307 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200308 wl->scan.failed = false;
309 cancel_delayed_work(&wl->scan_complete_work);
310 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
311 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300312 break;
313
314 default:
315 wl1271_error("invalid scan state");
316 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300317 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200318
319 if (ret < 0) {
320 cancel_delayed_work(&wl->scan_complete_work);
321 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
322 msecs_to_jiffies(0));
323 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300324}
325
Eliad Peller784f6942011-10-05 11:55:39 +0200326int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
327 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300328 struct cfg80211_scan_request *req)
329{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200330 /*
331 * cfg80211 should guarantee that we don't get more channels
332 * than what we have registered.
333 */
334 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
335
Luciano Coelho08688d62010-07-08 17:50:07 +0300336 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
337 return -EBUSY;
338
339 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
340
341 if (ssid_len && ssid) {
342 wl->scan.ssid_len = ssid_len;
343 memcpy(wl->scan.ssid, ssid, ssid_len);
344 } else {
345 wl->scan.ssid_len = 0;
346 }
347
Eliad Peller784f6942011-10-05 11:55:39 +0200348 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300349 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200350 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300351
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200352 /* we assume failure so that timeout scenarios are handled correctly */
353 wl->scan.failed = true;
354 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
355 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
356
Eliad Peller784f6942011-10-05 11:55:39 +0200357 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300358
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300359 return 0;
360}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300361
Eliad Peller2aa01592011-06-27 13:06:44 +0300362int wl1271_scan_stop(struct wl1271 *wl)
363{
364 struct wl1271_cmd_header *cmd = NULL;
365 int ret = 0;
366
367 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
368 return -EINVAL;
369
370 wl1271_debug(DEBUG_CMD, "cmd scan stop");
371
372 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
373 if (!cmd) {
374 ret = -ENOMEM;
375 goto out;
376 }
377
378 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
379 sizeof(*cmd), 0);
380 if (ret < 0) {
381 wl1271_error("cmd stop_scan failed");
382 goto out;
383 }
384out:
385 kfree(cmd);
386 return ret;
387}
388
Luciano Coelho95feadc2011-05-10 14:38:59 +0300389static int
390wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
391 struct cfg80211_sched_scan_request *req,
392 struct conn_scan_ch_params *channels,
393 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300394 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300395{
396 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
397 int i, j;
398 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300399 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300400
401 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300402 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300403 i++) {
404 flags = req->channels[i]->flags;
405
Luciano Coelho66870b12011-05-27 15:34:48 +0300406 if (force_passive)
407 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
408
Luciano Coelho2497a242011-05-27 15:34:46 +0300409 if ((req->channels[i]->band == band) &&
410 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300411 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300412 /* if radar is set, we ignore the passive flag */
413 (radar ||
414 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300415 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
416 req->channels[i]->band,
417 req->channels[i]->center_freq);
418 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
419 req->channels[i]->hw_value,
420 req->channels[i]->flags);
421 wl1271_debug(DEBUG_SCAN, "max_power %d",
422 req->channels[i]->max_power);
423
Luciano Coelho50a66d72011-05-27 15:34:47 +0300424 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300425 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300426 channels[j].passive_duration =
427 cpu_to_le16(c->dwell_time_dfs);
428 }
429 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300430 channels[j].passive_duration =
431 cpu_to_le16(c->dwell_time_passive);
432 } else {
433 channels[j].min_duration =
434 cpu_to_le16(c->min_dwell_time_active);
435 channels[j].max_duration =
436 cpu_to_le16(c->max_dwell_time_active);
437 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300438 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300439 channels[j].channel = req->channels[i]->hw_value;
440
441 j++;
442 }
443 }
444
445 return j - start;
446}
447
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300448static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300449wl1271_scan_sched_scan_channels(struct wl1271 *wl,
450 struct cfg80211_sched_scan_request *req,
451 struct wl1271_cmd_sched_scan_config *cfg)
452{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300453 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300454 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300455 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300456 false, true, 0,
457 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300458 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300459 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300460 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300461 false, false,
462 cfg->passive[0],
463 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300464 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300465 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300466 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300467 false, true, 0,
468 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300469 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300470 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300471 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300472 true, true,
473 cfg->passive[1],
474 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300475 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300476 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300477 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300478 false, false,
479 cfg->passive[1] + cfg->dfs,
480 MAX_CHANNELS_5GHZ);
481 /* 802.11j channels are not supported yet */
482 cfg->passive[2] = 0;
483 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300484
Luciano Coelho95feadc2011-05-10 14:38:59 +0300485 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
486 cfg->active[0], cfg->passive[0]);
487 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
488 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300489 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300490
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300491 return cfg->passive[0] || cfg->active[0] ||
492 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
493 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300494}
495
Luciano Coelhofb553772011-09-02 14:28:21 +0300496/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300497static int
498wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
499 struct cfg80211_sched_scan_request *req)
500{
501 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300502 struct cfg80211_match_set *sets = req->match_sets;
503 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300504 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300505
506 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
507
Luciano Coelho20a33e52011-09-02 14:28:23 +0300508 /* count the match sets that contain SSIDs */
509 for (i = 0; i < req->n_match_sets; i++)
510 if (sets[i].ssid.ssid_len > 0)
511 n_match_ssids++;
512
Luciano Coelhofb553772011-09-02 14:28:21 +0300513 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300514 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300515 (!req->n_ssids ||
516 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
517 type = SCAN_SSID_FILTER_ANY;
518 goto out;
519 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300520
Luciano Coelhofb553772011-09-02 14:28:21 +0300521 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
522 if (!cmd) {
523 ret = -ENOMEM;
524 goto out;
525 }
526
Luciano Coelho20a33e52011-09-02 14:28:23 +0300527 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300528 /* No filter, with ssids */
529 type = SCAN_SSID_FILTER_DISABLED;
530
531 for (i = 0; i < req->n_ssids; i++) {
532 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
533 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
534 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
535 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
536 ssids[i].ssid_len);
537 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300538 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300539 } else {
540 type = SCAN_SSID_FILTER_LIST;
541
542 /* Add all SSIDs from the filters */
543 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300544 /* ignore sets without SSIDs */
545 if (!sets[i].ssid.ssid_len)
546 continue;
547
Luciano Coelhofb553772011-09-02 14:28:21 +0300548 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
549 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
550 memcpy(cmd->ssids[cmd->n_ssids].ssid,
551 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
552 cmd->n_ssids++;
553 }
554 if ((req->n_ssids > 1) ||
555 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
556 /*
557 * Mark all the SSIDs passed in the SSID list as HIDDEN,
558 * so they're used in probe requests.
559 */
560 for (i = 0; i < req->n_ssids; i++) {
561 for (j = 0; j < cmd->n_ssids; j++)
562 if (!memcmp(req->ssids[i].ssid,
563 cmd->ssids[j].ssid,
564 req->ssids[i].ssid_len)) {
565 cmd->ssids[j].type =
566 SCAN_SSID_TYPE_HIDDEN;
567 break;
568 }
569 /* Fail if SSID isn't present in the filters */
570 if (j == req->n_ssids) {
571 ret = -EINVAL;
572 goto out_free;
573 }
574 }
575 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300576 }
577
578 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
579
580 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
581 sizeof(*cmd), 0);
582 if (ret < 0) {
583 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300584 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300585 }
586
Luciano Coelhofb553772011-09-02 14:28:21 +0300587out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300588 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300589out:
590 if (ret < 0)
591 return ret;
592 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300593}
594
Luciano Coelho95feadc2011-05-10 14:38:59 +0300595int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200596 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300597 struct cfg80211_sched_scan_request *req,
598 struct ieee80211_sched_scan_ies *ies)
599{
600 struct wl1271_cmd_sched_scan_config *cfg = NULL;
601 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300602 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300603 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300604
605 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
606
607 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
608 if (!cfg)
609 return -ENOMEM;
610
611 cfg->rssi_threshold = c->rssi_threshold;
612 cfg->snr_threshold = c->snr_threshold;
613 cfg->n_probe_reqs = c->num_probe_reqs;
614 /* cycles set to 0 it means infinite (until manually stopped) */
615 cfg->cycles = 0;
616 /* report APs when at least 1 is found */
617 cfg->report_after = 1;
618 /* don't stop scanning automatically when something is found */
619 cfg->terminate = 0;
620 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
621 /* don't filter on BSS type */
622 cfg->bss_type = SCAN_BSS_TYPE_ANY;
623 /* currently NL80211 supports only a single interval */
624 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
625 cfg->intervals[i] = cpu_to_le32(req->interval);
626
Luciano Coelhof9520792011-08-23 18:34:44 +0300627 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300628 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
629 if (ret < 0)
630 goto out;
631
632 cfg->filter_type = ret;
633
634 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300635
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300636 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300637 wl1271_error("scan channel list is empty");
638 ret = -EINVAL;
639 goto out;
640 }
641
Luciano Coelho66870b12011-05-27 15:34:48 +0300642 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300643 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
644 req->ssids[0].ssid_len,
645 ies->ie[IEEE80211_BAND_2GHZ],
646 ies->len[IEEE80211_BAND_2GHZ],
647 IEEE80211_BAND_2GHZ);
648 if (ret < 0) {
649 wl1271_error("2.4GHz PROBE request template failed");
650 goto out;
651 }
652 }
653
Luciano Coelho66870b12011-05-27 15:34:48 +0300654 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300655 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
656 req->ssids[0].ssid_len,
657 ies->ie[IEEE80211_BAND_5GHZ],
658 ies->len[IEEE80211_BAND_5GHZ],
659 IEEE80211_BAND_5GHZ);
660 if (ret < 0) {
661 wl1271_error("5GHz PROBE request template failed");
662 goto out;
663 }
664 }
665
666 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
667
668 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
669 sizeof(*cfg), 0);
670 if (ret < 0) {
671 wl1271_error("SCAN configuration failed");
672 goto out;
673 }
674out:
675 kfree(cfg);
676 return ret;
677}
678
Eliad Peller536129c2011-10-05 11:55:45 +0200679int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300680{
681 struct wl1271_cmd_sched_scan_start *start;
682 int ret = 0;
683
684 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
685
Eliad Peller536129c2011-10-05 11:55:45 +0200686 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300687 return -EOPNOTSUPP;
688
689 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
690 return -EBUSY;
691
Luciano Coelho95feadc2011-05-10 14:38:59 +0300692 start = kzalloc(sizeof(*start), GFP_KERNEL);
693 if (!start)
694 return -ENOMEM;
695
696 start->tag = WL1271_SCAN_DEFAULT_TAG;
697
698 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
699 sizeof(*start), 0);
700 if (ret < 0) {
701 wl1271_error("failed to send scan start command");
702 goto out_free;
703 }
704
705out_free:
706 kfree(start);
707 return ret;
708}
709
710void wl1271_scan_sched_scan_results(struct wl1271 *wl)
711{
712 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
713
714 ieee80211_sched_scan_results(wl->hw);
715}
716
717void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
718{
719 struct wl1271_cmd_sched_scan_stop *stop;
720 int ret = 0;
721
722 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
723
724 /* FIXME: what to do if alloc'ing to stop fails? */
725 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
726 if (!stop) {
727 wl1271_error("failed to alloc memory to send sched scan stop");
728 return;
729 }
730
731 stop->tag = WL1271_SCAN_DEFAULT_TAG;
732
733 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
734 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300735 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300736 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300737 goto out_free;
738 }
739 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300740
Luciano Coelho33c2c062011-05-10 14:46:02 +0300741out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300742 kfree(stop);
743}