blob: 9372136b27e4a6dfaaa526377d22c0bad6379f35 [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 Peller7edebf52011-10-05 11:55:52 +020037 struct ieee80211_vif *vif;
Eliad Peller536129c2011-10-05 11:55:45 +020038 struct wl12xx_vif *wlvif;
Eliad Peller251c1772011-08-14 13:17:17 +030039 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030040 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020041
42 dwork = container_of(work, struct delayed_work, work);
43 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030044
45 wl1271_debug(DEBUG_SCAN, "Scanning complete");
46
47 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020048
Arik Nemtsov24225b32011-03-01 12:27:26 +020049 if (wl->state == WL1271_STATE_OFF)
50 goto out;
51
52 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
53 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020054
Eliad Peller7edebf52011-10-05 11:55:52 +020055 vif = wl->scan_vif;
56 wlvif = wl12xx_vif_to_data(vif);
Eliad Peller536129c2011-10-05 11:55:45 +020057
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030058 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020059 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020060 wl->scan.req = NULL;
Eliad Peller784f6942011-10-05 11:55:39 +020061 wl->scan_vif = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020062
Eliad Peller251c1772011-08-14 13:17:17 +030063 ret = wl1271_ps_elp_wakeup(wl);
64 if (ret < 0)
65 goto out;
66
Arik Nemtsov24225b32011-03-01 12:27:26 +020067 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030068 /* restore hardware connection monitoring template */
Eliad Pellerbddb29b2011-10-05 11:55:49 +020069 wl1271_cmd_build_ap_probe_req(wl, wlvif->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030070 }
71
72 /* return to ROC if needed */
Eliad Peller536129c2011-10-05 11:55:45 +020073 is_sta = (wlvif->bss_type == BSS_TYPE_STA_BSS);
74 is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
Eliad Pellere0b38262011-08-28 15:11:55 +030075 if (((is_sta && !test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) ||
76 (is_ibss && !test_bit(WL1271_FLAG_IBSS_JOINED, &wl->flags))) &&
Eliad Peller7edebf52011-10-05 11:55:52 +020077 !test_bit(wlvif->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030078 /* restore remain on channel */
Eliad Peller7edebf52011-10-05 11:55:52 +020079 wl12xx_cmd_role_start_dev(wl, wlvif);
80 wl12xx_roc(wl, wlvif->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020081 }
Eliad Peller251c1772011-08-14 13:17:17 +030082 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020083
Juuso Oikarinen78abd322010-09-21 06:23:32 +020084 if (wl->scan.failed) {
85 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030086 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020087 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020088
Eliad Peller251c1772011-08-14 13:17:17 +030089 ieee80211_scan_completed(wl->hw, false);
90
Arik Nemtsov24225b32011-03-01 12:27:26 +020091out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020092 mutex_unlock(&wl->mutex);
93
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030094}
95
96
Luciano Coelho08688d62010-07-08 17:50:07 +030097static int wl1271_get_scan_channels(struct wl1271 *wl,
98 struct cfg80211_scan_request *req,
99 struct basic_scan_channel_params *channels,
100 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300101{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200102 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +0300103 int i, j;
104 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300105
Luciano Coelho08688d62010-07-08 17:50:07 +0300106 for (i = 0, j = 0;
107 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
108 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300109 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300110
Luciano Coelho4a31c112011-03-21 23:16:14 +0200111 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300112 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300113 (req->channels[i]->band == band) &&
114 /*
115 * In passive scans, we scan all remaining
116 * channels, even if not marked as such.
117 * In active scans, we only scan channels not
118 * marked as passive.
119 */
120 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300121 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
122 req->channels[i]->band,
123 req->channels[i]->center_freq);
124 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
125 req->channels[i]->hw_value,
126 req->channels[i]->flags);
127 wl1271_debug(DEBUG_SCAN,
128 "max_antenna_gain %d, max_power %d",
129 req->channels[i]->max_antenna_gain,
130 req->channels[i]->max_power);
131 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
132 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300133
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200134 if (!passive) {
135 channels[j].min_duration =
136 cpu_to_le32(c->min_dwell_time_active);
137 channels[j].max_duration =
138 cpu_to_le32(c->max_dwell_time_active);
139 } else {
140 channels[j].min_duration =
141 cpu_to_le32(c->min_dwell_time_passive);
142 channels[j].max_duration =
143 cpu_to_le32(c->max_dwell_time_passive);
144 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300145 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300146 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300147 channels[j].channel = req->channels[i]->hw_value;
148
149 memset(&channels[j].bssid_lsb, 0xff, 4);
150 memset(&channels[j].bssid_msb, 0xff, 2);
151
152 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200153 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300154
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300155 j++;
156 }
157 }
158
Luciano Coelho08688d62010-07-08 17:50:07 +0300159 return j;
160}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300161
Luciano Coelho08688d62010-07-08 17:50:07 +0300162#define WL1271_NOTHING_TO_SCAN 1
163
Eliad Peller784f6942011-10-05 11:55:39 +0200164static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
165 enum ieee80211_band band,
166 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300167{
Eliad Peller0603d892011-10-05 11:55:51 +0200168 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300169 struct wl1271_cmd_scan *cmd;
170 struct wl1271_cmd_trigger_scan_to *trigger;
171 int ret;
172 u16 scan_options = 0;
173
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300174 /* skip active scans if we don't have SSIDs */
175 if (!passive && wl->scan.req->n_ssids == 0)
176 return WL1271_NOTHING_TO_SCAN;
177
Luciano Coelho08688d62010-07-08 17:50:07 +0300178 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
179 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
180 if (!cmd || !trigger) {
181 ret = -ENOMEM;
182 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300183 }
184
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300185 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300186 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300187
Eliad Peller0603d892011-10-05 11:55:51 +0200188 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID)) {
Eliad Pellera4e02f32011-08-14 13:17:10 +0300189 ret = -EINVAL;
190 goto out;
191 }
Eliad Peller0603d892011-10-05 11:55:51 +0200192 cmd->params.role_id = wlvif->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300193 cmd->params.scan_options = cpu_to_le16(scan_options);
194
195 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
196 cmd->channels,
197 band, passive);
198 if (cmd->params.n_ch == 0) {
199 ret = WL1271_NOTHING_TO_SCAN;
200 goto out;
201 }
202
203 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200204 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300205 cmd->params.tx_rate = cpu_to_le32(basic_rate);
206 cmd->params.tid_trigger = 0;
207 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
208
209 if (band == IEEE80211_BAND_2GHZ)
210 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
211 else
212 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
213
214 if (wl->scan.ssid_len && wl->scan.ssid) {
215 cmd->params.ssid_len = wl->scan.ssid_len;
216 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
217 }
218
Eliad Peller784f6942011-10-05 11:55:39 +0200219 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300220
Luciano Coelho08688d62010-07-08 17:50:07 +0300221 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
222 wl->scan.req->ie, wl->scan.req->ie_len,
223 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300224 if (ret < 0) {
225 wl1271_error("PROBE request template failed");
226 goto out;
227 }
228
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300229 /* disable the timeout */
230 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300231 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
232 sizeof(*trigger), 0);
233 if (ret < 0) {
234 wl1271_error("trigger scan to failed for hw scan");
235 goto out;
236 }
237
Luciano Coelho08688d62010-07-08 17:50:07 +0300238 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300239
Luciano Coelho08688d62010-07-08 17:50:07 +0300240 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300241 if (ret < 0) {
242 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300243 goto out;
244 }
245
246out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300247 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300248 kfree(trigger);
249 return ret;
250}
251
Eliad Peller784f6942011-10-05 11:55:39 +0200252void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300253{
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200254 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300255 enum ieee80211_band band;
256 u32 rate;
Luciano Coelho08688d62010-07-08 17:50:07 +0300257
258 switch (wl->scan.state) {
259 case WL1271_SCAN_STATE_IDLE:
260 break;
261
262 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300263 band = IEEE80211_BAND_2GHZ;
264 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200265 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300266 if (ret == WL1271_NOTHING_TO_SCAN) {
267 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200268 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300269 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300270
271 break;
272
273 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300274 band = IEEE80211_BAND_2GHZ;
275 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200276 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300277 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200278 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300279 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
280 else
281 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200282 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300283 }
284
285 break;
286
287 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300288 band = IEEE80211_BAND_5GHZ;
289 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200290 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300291 if (ret == WL1271_NOTHING_TO_SCAN) {
292 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200293 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300294 }
295
296 break;
297
298 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300299 band = IEEE80211_BAND_5GHZ;
300 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200301 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300302 if (ret == WL1271_NOTHING_TO_SCAN) {
303 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200304 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300305 }
306
307 break;
308
309 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200310 wl->scan.failed = false;
311 cancel_delayed_work(&wl->scan_complete_work);
312 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
313 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300314 break;
315
316 default:
317 wl1271_error("invalid scan state");
318 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300319 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200320
321 if (ret < 0) {
322 cancel_delayed_work(&wl->scan_complete_work);
323 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
324 msecs_to_jiffies(0));
325 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300326}
327
Eliad Peller784f6942011-10-05 11:55:39 +0200328int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
329 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300330 struct cfg80211_scan_request *req)
331{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200332 /*
333 * cfg80211 should guarantee that we don't get more channels
334 * than what we have registered.
335 */
336 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
337
Luciano Coelho08688d62010-07-08 17:50:07 +0300338 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
339 return -EBUSY;
340
341 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
342
343 if (ssid_len && ssid) {
344 wl->scan.ssid_len = ssid_len;
345 memcpy(wl->scan.ssid, ssid, ssid_len);
346 } else {
347 wl->scan.ssid_len = 0;
348 }
349
Eliad Peller784f6942011-10-05 11:55:39 +0200350 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300351 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200352 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300353
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200354 /* we assume failure so that timeout scenarios are handled correctly */
355 wl->scan.failed = true;
356 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
357 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
358
Eliad Peller784f6942011-10-05 11:55:39 +0200359 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300360
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300361 return 0;
362}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300363
Eliad Peller2aa01592011-06-27 13:06:44 +0300364int wl1271_scan_stop(struct wl1271 *wl)
365{
366 struct wl1271_cmd_header *cmd = NULL;
367 int ret = 0;
368
369 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
370 return -EINVAL;
371
372 wl1271_debug(DEBUG_CMD, "cmd scan stop");
373
374 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
375 if (!cmd) {
376 ret = -ENOMEM;
377 goto out;
378 }
379
380 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
381 sizeof(*cmd), 0);
382 if (ret < 0) {
383 wl1271_error("cmd stop_scan failed");
384 goto out;
385 }
386out:
387 kfree(cmd);
388 return ret;
389}
390
Luciano Coelho95feadc2011-05-10 14:38:59 +0300391static int
392wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
393 struct cfg80211_sched_scan_request *req,
394 struct conn_scan_ch_params *channels,
395 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300396 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300397{
398 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
399 int i, j;
400 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300401 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300402
403 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300404 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300405 i++) {
406 flags = req->channels[i]->flags;
407
Luciano Coelho66870b12011-05-27 15:34:48 +0300408 if (force_passive)
409 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
410
Luciano Coelho2497a242011-05-27 15:34:46 +0300411 if ((req->channels[i]->band == band) &&
412 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300413 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300414 /* if radar is set, we ignore the passive flag */
415 (radar ||
416 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300417 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
418 req->channels[i]->band,
419 req->channels[i]->center_freq);
420 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
421 req->channels[i]->hw_value,
422 req->channels[i]->flags);
423 wl1271_debug(DEBUG_SCAN, "max_power %d",
424 req->channels[i]->max_power);
425
Luciano Coelho50a66d72011-05-27 15:34:47 +0300426 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300427 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300428 channels[j].passive_duration =
429 cpu_to_le16(c->dwell_time_dfs);
430 }
431 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300432 channels[j].passive_duration =
433 cpu_to_le16(c->dwell_time_passive);
434 } else {
435 channels[j].min_duration =
436 cpu_to_le16(c->min_dwell_time_active);
437 channels[j].max_duration =
438 cpu_to_le16(c->max_dwell_time_active);
439 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300440 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300441 channels[j].channel = req->channels[i]->hw_value;
442
443 j++;
444 }
445 }
446
447 return j - start;
448}
449
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300450static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300451wl1271_scan_sched_scan_channels(struct wl1271 *wl,
452 struct cfg80211_sched_scan_request *req,
453 struct wl1271_cmd_sched_scan_config *cfg)
454{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300455 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300456 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300457 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300458 false, true, 0,
459 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300460 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300461 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300462 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300463 false, false,
464 cfg->passive[0],
465 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300466 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300467 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300468 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300469 false, true, 0,
470 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300471 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300472 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300473 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300474 true, true,
475 cfg->passive[1],
476 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300477 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300478 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300479 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300480 false, false,
481 cfg->passive[1] + cfg->dfs,
482 MAX_CHANNELS_5GHZ);
483 /* 802.11j channels are not supported yet */
484 cfg->passive[2] = 0;
485 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300486
Luciano Coelho95feadc2011-05-10 14:38:59 +0300487 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
488 cfg->active[0], cfg->passive[0]);
489 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
490 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300491 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300492
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300493 return cfg->passive[0] || cfg->active[0] ||
494 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
495 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300496}
497
Luciano Coelhofb553772011-09-02 14:28:21 +0300498/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300499static int
500wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
501 struct cfg80211_sched_scan_request *req)
502{
503 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300504 struct cfg80211_match_set *sets = req->match_sets;
505 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300506 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300507
508 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
509
Luciano Coelho20a33e52011-09-02 14:28:23 +0300510 /* count the match sets that contain SSIDs */
511 for (i = 0; i < req->n_match_sets; i++)
512 if (sets[i].ssid.ssid_len > 0)
513 n_match_ssids++;
514
Luciano Coelhofb553772011-09-02 14:28:21 +0300515 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300516 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300517 (!req->n_ssids ||
518 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
519 type = SCAN_SSID_FILTER_ANY;
520 goto out;
521 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300522
Luciano Coelhofb553772011-09-02 14:28:21 +0300523 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
524 if (!cmd) {
525 ret = -ENOMEM;
526 goto out;
527 }
528
Luciano Coelho20a33e52011-09-02 14:28:23 +0300529 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300530 /* No filter, with ssids */
531 type = SCAN_SSID_FILTER_DISABLED;
532
533 for (i = 0; i < req->n_ssids; i++) {
534 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
535 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
536 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
537 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
538 ssids[i].ssid_len);
539 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300540 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300541 } else {
542 type = SCAN_SSID_FILTER_LIST;
543
544 /* Add all SSIDs from the filters */
545 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300546 /* ignore sets without SSIDs */
547 if (!sets[i].ssid.ssid_len)
548 continue;
549
Luciano Coelhofb553772011-09-02 14:28:21 +0300550 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
551 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
552 memcpy(cmd->ssids[cmd->n_ssids].ssid,
553 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
554 cmd->n_ssids++;
555 }
556 if ((req->n_ssids > 1) ||
557 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
558 /*
559 * Mark all the SSIDs passed in the SSID list as HIDDEN,
560 * so they're used in probe requests.
561 */
562 for (i = 0; i < req->n_ssids; i++) {
563 for (j = 0; j < cmd->n_ssids; j++)
564 if (!memcmp(req->ssids[i].ssid,
565 cmd->ssids[j].ssid,
566 req->ssids[i].ssid_len)) {
567 cmd->ssids[j].type =
568 SCAN_SSID_TYPE_HIDDEN;
569 break;
570 }
571 /* Fail if SSID isn't present in the filters */
572 if (j == req->n_ssids) {
573 ret = -EINVAL;
574 goto out_free;
575 }
576 }
577 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300578 }
579
580 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
581
582 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
583 sizeof(*cmd), 0);
584 if (ret < 0) {
585 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300586 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300587 }
588
Luciano Coelhofb553772011-09-02 14:28:21 +0300589out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300590 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300591out:
592 if (ret < 0)
593 return ret;
594 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300595}
596
Luciano Coelho95feadc2011-05-10 14:38:59 +0300597int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200598 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300599 struct cfg80211_sched_scan_request *req,
600 struct ieee80211_sched_scan_ies *ies)
601{
602 struct wl1271_cmd_sched_scan_config *cfg = NULL;
603 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300604 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300605 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300606
607 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
608
609 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
610 if (!cfg)
611 return -ENOMEM;
612
613 cfg->rssi_threshold = c->rssi_threshold;
614 cfg->snr_threshold = c->snr_threshold;
615 cfg->n_probe_reqs = c->num_probe_reqs;
616 /* cycles set to 0 it means infinite (until manually stopped) */
617 cfg->cycles = 0;
618 /* report APs when at least 1 is found */
619 cfg->report_after = 1;
620 /* don't stop scanning automatically when something is found */
621 cfg->terminate = 0;
622 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
623 /* don't filter on BSS type */
624 cfg->bss_type = SCAN_BSS_TYPE_ANY;
625 /* currently NL80211 supports only a single interval */
626 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
627 cfg->intervals[i] = cpu_to_le32(req->interval);
628
Luciano Coelhof9520792011-08-23 18:34:44 +0300629 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300630 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
631 if (ret < 0)
632 goto out;
633
634 cfg->filter_type = ret;
635
636 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300637
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300638 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300639 wl1271_error("scan channel list is empty");
640 ret = -EINVAL;
641 goto out;
642 }
643
Luciano Coelho66870b12011-05-27 15:34:48 +0300644 if (!force_passive && cfg->active[0]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300645 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
646 req->ssids[0].ssid_len,
647 ies->ie[IEEE80211_BAND_2GHZ],
648 ies->len[IEEE80211_BAND_2GHZ],
649 IEEE80211_BAND_2GHZ);
650 if (ret < 0) {
651 wl1271_error("2.4GHz PROBE request template failed");
652 goto out;
653 }
654 }
655
Luciano Coelho66870b12011-05-27 15:34:48 +0300656 if (!force_passive && cfg->active[1]) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300657 ret = wl1271_cmd_build_probe_req(wl, req->ssids[0].ssid,
658 req->ssids[0].ssid_len,
659 ies->ie[IEEE80211_BAND_5GHZ],
660 ies->len[IEEE80211_BAND_5GHZ],
661 IEEE80211_BAND_5GHZ);
662 if (ret < 0) {
663 wl1271_error("5GHz PROBE request template failed");
664 goto out;
665 }
666 }
667
668 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
669
670 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
671 sizeof(*cfg), 0);
672 if (ret < 0) {
673 wl1271_error("SCAN configuration failed");
674 goto out;
675 }
676out:
677 kfree(cfg);
678 return ret;
679}
680
Eliad Peller536129c2011-10-05 11:55:45 +0200681int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300682{
683 struct wl1271_cmd_sched_scan_start *start;
684 int ret = 0;
685
686 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
687
Eliad Peller536129c2011-10-05 11:55:45 +0200688 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300689 return -EOPNOTSUPP;
690
691 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
692 return -EBUSY;
693
Luciano Coelho95feadc2011-05-10 14:38:59 +0300694 start = kzalloc(sizeof(*start), GFP_KERNEL);
695 if (!start)
696 return -ENOMEM;
697
698 start->tag = WL1271_SCAN_DEFAULT_TAG;
699
700 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
701 sizeof(*start), 0);
702 if (ret < 0) {
703 wl1271_error("failed to send scan start command");
704 goto out_free;
705 }
706
707out_free:
708 kfree(start);
709 return ret;
710}
711
712void wl1271_scan_sched_scan_results(struct wl1271 *wl)
713{
714 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
715
716 ieee80211_sched_scan_results(wl->hw);
717}
718
719void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
720{
721 struct wl1271_cmd_sched_scan_stop *stop;
722 int ret = 0;
723
724 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
725
726 /* FIXME: what to do if alloc'ing to stop fails? */
727 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
728 if (!stop) {
729 wl1271_error("failed to alloc memory to send sched scan stop");
730 return;
731 }
732
733 stop->tag = WL1271_SCAN_DEFAULT_TAG;
734
735 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
736 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300737 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300738 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300739 goto out_free;
740 }
741 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300742
Luciano Coelho33c2c062011-05-10 14:46:02 +0300743out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300744 kfree(stop);
745}