blob: fb2c4319749da2f0f6fd2a7ab2757f176c1b6302 [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"
Luciano Coelho0f4e3122011-10-07 11:02:42 +030027#include "debug.h"
Shahar Levi00d20102010-11-08 11:20:10 +000028#include "cmd.h"
29#include "scan.h"
30#include "acx.h"
Arik Nemtsov24225b32011-03-01 12:27:26 +020031#include "ps.h"
Eliad Pelleraf7fbb22011-09-19 13:51:42 +030032#include "tx.h"
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030033
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030034void wl1271_scan_complete_work(struct work_struct *work)
35{
Juuso Oikarinen78abd322010-09-21 06:23:32 +020036 struct delayed_work *dwork;
37 struct wl1271 *wl;
Eliad Peller7edebf52011-10-05 11:55:52 +020038 struct ieee80211_vif *vif;
Eliad Peller536129c2011-10-05 11:55:45 +020039 struct wl12xx_vif *wlvif;
Eliad Peller251c1772011-08-14 13:17:17 +030040 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030041 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020042
43 dwork = container_of(work, struct delayed_work, work);
44 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030045
46 wl1271_debug(DEBUG_SCAN, "Scanning complete");
47
48 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020049
Arik Nemtsov24225b32011-03-01 12:27:26 +020050 if (wl->state == WL1271_STATE_OFF)
51 goto out;
52
53 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
54 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020055
Eliad Peller7edebf52011-10-05 11:55:52 +020056 vif = wl->scan_vif;
57 wlvif = wl12xx_vif_to_data(vif);
Eliad Peller536129c2011-10-05 11:55:45 +020058
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030059 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020060 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020061 wl->scan.req = NULL;
Eliad Peller784f6942011-10-05 11:55:39 +020062 wl->scan_vif = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020063
Eliad Peller251c1772011-08-14 13:17:17 +030064 ret = wl1271_ps_elp_wakeup(wl);
65 if (ret < 0)
66 goto out;
67
Eliad Pellerba8447f2011-10-10 10:13:00 +020068 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030069 /* restore hardware connection monitoring template */
Eliad Peller83587502011-10-10 10:12:53 +020070 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030071 }
72
73 /* return to ROC if needed */
Eliad Peller536129c2011-10-05 11:55:45 +020074 is_sta = (wlvif->bss_type == BSS_TYPE_STA_BSS);
75 is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
Eliad Pellerba8447f2011-10-10 10:13:00 +020076 if (((is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) ||
Eliad Pellereee514e2011-10-10 10:13:01 +020077 (is_ibss && !test_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))) &&
Eliad Peller7edebf52011-10-05 11:55:52 +020078 !test_bit(wlvif->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030079 /* restore remain on channel */
Eliad Peller7edebf52011-10-05 11:55:52 +020080 wl12xx_cmd_role_start_dev(wl, wlvif);
Eliad Peller1b92f152011-10-10 10:13:09 +020081 wl12xx_roc(wl, wlvif, wlvif->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020082 }
Eliad Peller251c1772011-08-14 13:17:17 +030083 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020084
Juuso Oikarinen78abd322010-09-21 06:23:32 +020085 if (wl->scan.failed) {
86 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030087 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020088 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020089
Eliad Peller251c1772011-08-14 13:17:17 +030090 ieee80211_scan_completed(wl->hw, false);
91
Arik Nemtsov24225b32011-03-01 12:27:26 +020092out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020093 mutex_unlock(&wl->mutex);
94
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030095}
96
97
Luciano Coelho08688d62010-07-08 17:50:07 +030098static int wl1271_get_scan_channels(struct wl1271 *wl,
99 struct cfg80211_scan_request *req,
100 struct basic_scan_channel_params *channels,
101 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300102{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200103 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +0300104 int i, j;
105 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300106
Luciano Coelho08688d62010-07-08 17:50:07 +0300107 for (i = 0, j = 0;
108 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
109 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300110 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300111
Luciano Coelho4a31c112011-03-21 23:16:14 +0200112 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300113 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300114 (req->channels[i]->band == band) &&
115 /*
116 * In passive scans, we scan all remaining
117 * channels, even if not marked as such.
118 * In active scans, we only scan channels not
119 * marked as passive.
120 */
121 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300122 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
123 req->channels[i]->band,
124 req->channels[i]->center_freq);
125 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
126 req->channels[i]->hw_value,
127 req->channels[i]->flags);
128 wl1271_debug(DEBUG_SCAN,
129 "max_antenna_gain %d, max_power %d",
130 req->channels[i]->max_antenna_gain,
131 req->channels[i]->max_power);
132 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
133 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300134
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200135 if (!passive) {
136 channels[j].min_duration =
137 cpu_to_le32(c->min_dwell_time_active);
138 channels[j].max_duration =
139 cpu_to_le32(c->max_dwell_time_active);
140 } else {
141 channels[j].min_duration =
142 cpu_to_le32(c->min_dwell_time_passive);
143 channels[j].max_duration =
144 cpu_to_le32(c->max_dwell_time_passive);
145 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300146 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300147 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300148 channels[j].channel = req->channels[i]->hw_value;
149
150 memset(&channels[j].bssid_lsb, 0xff, 4);
151 memset(&channels[j].bssid_msb, 0xff, 2);
152
153 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200154 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300155
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300156 j++;
157 }
158 }
159
Luciano Coelho08688d62010-07-08 17:50:07 +0300160 return j;
161}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300162
Luciano Coelho08688d62010-07-08 17:50:07 +0300163#define WL1271_NOTHING_TO_SCAN 1
164
Eliad Peller784f6942011-10-05 11:55:39 +0200165static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
166 enum ieee80211_band band,
167 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300168{
Eliad Peller0603d892011-10-05 11:55:51 +0200169 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300170 struct wl1271_cmd_scan *cmd;
171 struct wl1271_cmd_trigger_scan_to *trigger;
172 int ret;
173 u16 scan_options = 0;
174
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300175 /* skip active scans if we don't have SSIDs */
176 if (!passive && wl->scan.req->n_ssids == 0)
177 return WL1271_NOTHING_TO_SCAN;
178
Luciano Coelho08688d62010-07-08 17:50:07 +0300179 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
180 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
181 if (!cmd || !trigger) {
182 ret = -ENOMEM;
183 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300184 }
185
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300186 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300187 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300188
Eliad Peller0603d892011-10-05 11:55:51 +0200189 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID)) {
Eliad Pellera4e02f32011-08-14 13:17:10 +0300190 ret = -EINVAL;
191 goto out;
192 }
Eliad Peller0603d892011-10-05 11:55:51 +0200193 cmd->params.role_id = wlvif->role_id;
Luciano Coelho08688d62010-07-08 17:50:07 +0300194 cmd->params.scan_options = cpu_to_le16(scan_options);
195
196 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
197 cmd->channels,
198 band, passive);
199 if (cmd->params.n_ch == 0) {
200 ret = WL1271_NOTHING_TO_SCAN;
201 goto out;
202 }
203
204 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200205 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300206 cmd->params.tx_rate = cpu_to_le32(basic_rate);
207 cmd->params.tid_trigger = 0;
208 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
209
210 if (band == IEEE80211_BAND_2GHZ)
211 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
212 else
213 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
214
215 if (wl->scan.ssid_len && wl->scan.ssid) {
216 cmd->params.ssid_len = wl->scan.ssid_len;
217 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
218 }
219
Eliad Peller784f6942011-10-05 11:55:39 +0200220 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300221
Eliad Peller83587502011-10-10 10:12:53 +0200222 ret = wl1271_cmd_build_probe_req(wl, wlvif, wl->scan.ssid,
223 wl->scan.ssid_len, wl->scan.req->ie,
224 wl->scan.req->ie_len, band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300225 if (ret < 0) {
226 wl1271_error("PROBE request template failed");
227 goto out;
228 }
229
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300230 /* disable the timeout */
231 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300232 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
233 sizeof(*trigger), 0);
234 if (ret < 0) {
235 wl1271_error("trigger scan to failed for hw scan");
236 goto out;
237 }
238
Luciano Coelho08688d62010-07-08 17:50:07 +0300239 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300240
Luciano Coelho08688d62010-07-08 17:50:07 +0300241 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300242 if (ret < 0) {
243 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300244 goto out;
245 }
246
247out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300248 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300249 kfree(trigger);
250 return ret;
251}
252
Eliad Peller784f6942011-10-05 11:55:39 +0200253void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300254{
Eliad Peller83587502011-10-10 10:12:53 +0200255 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200256 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300257 enum ieee80211_band band;
258 u32 rate;
Luciano Coelho08688d62010-07-08 17:50:07 +0300259
260 switch (wl->scan.state) {
261 case WL1271_SCAN_STATE_IDLE:
262 break;
263
264 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300265 band = IEEE80211_BAND_2GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200266 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200267 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300268 if (ret == WL1271_NOTHING_TO_SCAN) {
269 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200270 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300271 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300272
273 break;
274
275 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300276 band = IEEE80211_BAND_2GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200277 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200278 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300279 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200280 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300281 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
282 else
283 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200284 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300285 }
286
287 break;
288
289 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300290 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200291 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200292 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300293 if (ret == WL1271_NOTHING_TO_SCAN) {
294 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200295 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300296 }
297
298 break;
299
300 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300301 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200302 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200303 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300304 if (ret == WL1271_NOTHING_TO_SCAN) {
305 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200306 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300307 }
308
309 break;
310
311 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200312 wl->scan.failed = false;
313 cancel_delayed_work(&wl->scan_complete_work);
314 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
315 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300316 break;
317
318 default:
319 wl1271_error("invalid scan state");
320 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300321 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200322
323 if (ret < 0) {
324 cancel_delayed_work(&wl->scan_complete_work);
325 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
326 msecs_to_jiffies(0));
327 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300328}
329
Eliad Peller784f6942011-10-05 11:55:39 +0200330int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
331 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300332 struct cfg80211_scan_request *req)
333{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200334 /*
335 * cfg80211 should guarantee that we don't get more channels
336 * than what we have registered.
337 */
338 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
339
Luciano Coelho08688d62010-07-08 17:50:07 +0300340 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
341 return -EBUSY;
342
343 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
344
345 if (ssid_len && ssid) {
346 wl->scan.ssid_len = ssid_len;
347 memcpy(wl->scan.ssid, ssid, ssid_len);
348 } else {
349 wl->scan.ssid_len = 0;
350 }
351
Eliad Peller784f6942011-10-05 11:55:39 +0200352 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300353 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200354 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300355
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200356 /* we assume failure so that timeout scenarios are handled correctly */
357 wl->scan.failed = true;
358 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
359 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
360
Eliad Peller784f6942011-10-05 11:55:39 +0200361 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300362
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300363 return 0;
364}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300365
Eliad Peller2aa01592011-06-27 13:06:44 +0300366int wl1271_scan_stop(struct wl1271 *wl)
367{
368 struct wl1271_cmd_header *cmd = NULL;
369 int ret = 0;
370
371 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
372 return -EINVAL;
373
374 wl1271_debug(DEBUG_CMD, "cmd scan stop");
375
376 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
377 if (!cmd) {
378 ret = -ENOMEM;
379 goto out;
380 }
381
382 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
383 sizeof(*cmd), 0);
384 if (ret < 0) {
385 wl1271_error("cmd stop_scan failed");
386 goto out;
387 }
388out:
389 kfree(cmd);
390 return ret;
391}
392
Luciano Coelho95feadc2011-05-10 14:38:59 +0300393static int
394wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
395 struct cfg80211_sched_scan_request *req,
396 struct conn_scan_ch_params *channels,
397 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300398 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300399{
400 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
401 int i, j;
402 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300403 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300404
405 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300406 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300407 i++) {
408 flags = req->channels[i]->flags;
409
Luciano Coelho66870b12011-05-27 15:34:48 +0300410 if (force_passive)
411 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
412
Luciano Coelho2497a242011-05-27 15:34:46 +0300413 if ((req->channels[i]->band == band) &&
414 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300415 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300416 /* if radar is set, we ignore the passive flag */
417 (radar ||
418 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300419 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
420 req->channels[i]->band,
421 req->channels[i]->center_freq);
422 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
423 req->channels[i]->hw_value,
424 req->channels[i]->flags);
425 wl1271_debug(DEBUG_SCAN, "max_power %d",
426 req->channels[i]->max_power);
427
Luciano Coelho50a66d72011-05-27 15:34:47 +0300428 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300429 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Luciano Coelho50a66d72011-05-27 15:34:47 +0300430 channels[j].passive_duration =
431 cpu_to_le16(c->dwell_time_dfs);
432 }
433 else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300434 channels[j].passive_duration =
435 cpu_to_le16(c->dwell_time_passive);
436 } else {
437 channels[j].min_duration =
438 cpu_to_le16(c->min_dwell_time_active);
439 channels[j].max_duration =
440 cpu_to_le16(c->max_dwell_time_active);
441 }
Luciano Coelho2497a242011-05-27 15:34:46 +0300442 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300443 channels[j].channel = req->channels[i]->hw_value;
444
445 j++;
446 }
447 }
448
449 return j - start;
450}
451
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300452static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300453wl1271_scan_sched_scan_channels(struct wl1271 *wl,
454 struct cfg80211_sched_scan_request *req,
455 struct wl1271_cmd_sched_scan_config *cfg)
456{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300457 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300458 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300459 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300460 false, true, 0,
461 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300462 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300463 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300464 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300465 false, false,
466 cfg->passive[0],
467 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300468 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300469 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300470 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300471 false, true, 0,
472 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300473 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300474 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300475 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300476 true, true,
477 cfg->passive[1],
478 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300479 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300480 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300481 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300482 false, false,
483 cfg->passive[1] + cfg->dfs,
484 MAX_CHANNELS_5GHZ);
485 /* 802.11j channels are not supported yet */
486 cfg->passive[2] = 0;
487 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300488
Luciano Coelho95feadc2011-05-10 14:38:59 +0300489 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
490 cfg->active[0], cfg->passive[0]);
491 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
492 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300493 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300494
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300495 return cfg->passive[0] || cfg->active[0] ||
496 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
497 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300498}
499
Luciano Coelhofb553772011-09-02 14:28:21 +0300500/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300501static int
502wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
503 struct cfg80211_sched_scan_request *req)
504{
505 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300506 struct cfg80211_match_set *sets = req->match_sets;
507 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300508 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300509
510 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
511
Luciano Coelho20a33e52011-09-02 14:28:23 +0300512 /* count the match sets that contain SSIDs */
513 for (i = 0; i < req->n_match_sets; i++)
514 if (sets[i].ssid.ssid_len > 0)
515 n_match_ssids++;
516
Luciano Coelhofb553772011-09-02 14:28:21 +0300517 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300518 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300519 (!req->n_ssids ||
520 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
521 type = SCAN_SSID_FILTER_ANY;
522 goto out;
523 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300524
Luciano Coelhofb553772011-09-02 14:28:21 +0300525 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
526 if (!cmd) {
527 ret = -ENOMEM;
528 goto out;
529 }
530
Luciano Coelho20a33e52011-09-02 14:28:23 +0300531 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300532 /* No filter, with ssids */
533 type = SCAN_SSID_FILTER_DISABLED;
534
535 for (i = 0; i < req->n_ssids; i++) {
536 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
537 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
538 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
539 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
540 ssids[i].ssid_len);
541 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300542 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300543 } else {
544 type = SCAN_SSID_FILTER_LIST;
545
546 /* Add all SSIDs from the filters */
547 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300548 /* ignore sets without SSIDs */
549 if (!sets[i].ssid.ssid_len)
550 continue;
551
Luciano Coelhofb553772011-09-02 14:28:21 +0300552 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
553 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
554 memcpy(cmd->ssids[cmd->n_ssids].ssid,
555 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
556 cmd->n_ssids++;
557 }
558 if ((req->n_ssids > 1) ||
559 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
560 /*
561 * Mark all the SSIDs passed in the SSID list as HIDDEN,
562 * so they're used in probe requests.
563 */
564 for (i = 0; i < req->n_ssids; i++) {
565 for (j = 0; j < cmd->n_ssids; j++)
566 if (!memcmp(req->ssids[i].ssid,
567 cmd->ssids[j].ssid,
568 req->ssids[i].ssid_len)) {
569 cmd->ssids[j].type =
570 SCAN_SSID_TYPE_HIDDEN;
571 break;
572 }
573 /* Fail if SSID isn't present in the filters */
574 if (j == req->n_ssids) {
575 ret = -EINVAL;
576 goto out_free;
577 }
578 }
579 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300580 }
581
582 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
583
584 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
585 sizeof(*cmd), 0);
586 if (ret < 0) {
587 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300588 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300589 }
590
Luciano Coelhofb553772011-09-02 14:28:21 +0300591out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300592 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300593out:
594 if (ret < 0)
595 return ret;
596 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300597}
598
Luciano Coelho95feadc2011-05-10 14:38:59 +0300599int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200600 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300601 struct cfg80211_sched_scan_request *req,
602 struct ieee80211_sched_scan_ies *ies)
603{
604 struct wl1271_cmd_sched_scan_config *cfg = NULL;
605 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300606 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300607 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300608
609 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
610
611 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
612 if (!cfg)
613 return -ENOMEM;
614
615 cfg->rssi_threshold = c->rssi_threshold;
616 cfg->snr_threshold = c->snr_threshold;
617 cfg->n_probe_reqs = c->num_probe_reqs;
618 /* cycles set to 0 it means infinite (until manually stopped) */
619 cfg->cycles = 0;
620 /* report APs when at least 1 is found */
621 cfg->report_after = 1;
622 /* don't stop scanning automatically when something is found */
623 cfg->terminate = 0;
624 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
625 /* don't filter on BSS type */
626 cfg->bss_type = SCAN_BSS_TYPE_ANY;
627 /* currently NL80211 supports only a single interval */
628 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
629 cfg->intervals[i] = cpu_to_le32(req->interval);
630
Luciano Coelhof9520792011-08-23 18:34:44 +0300631 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300632 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
633 if (ret < 0)
634 goto out;
635
636 cfg->filter_type = ret;
637
638 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300639
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300640 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300641 wl1271_error("scan channel list is empty");
642 ret = -EINVAL;
643 goto out;
644 }
645
Luciano Coelho66870b12011-05-27 15:34:48 +0300646 if (!force_passive && cfg->active[0]) {
Eliad Peller83587502011-10-10 10:12:53 +0200647 ret = wl1271_cmd_build_probe_req(wl, wlvif, req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300648 req->ssids[0].ssid_len,
649 ies->ie[IEEE80211_BAND_2GHZ],
650 ies->len[IEEE80211_BAND_2GHZ],
651 IEEE80211_BAND_2GHZ);
652 if (ret < 0) {
653 wl1271_error("2.4GHz PROBE request template failed");
654 goto out;
655 }
656 }
657
Luciano Coelho66870b12011-05-27 15:34:48 +0300658 if (!force_passive && cfg->active[1]) {
Eliad Peller83587502011-10-10 10:12:53 +0200659 ret = wl1271_cmd_build_probe_req(wl, wlvif, req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300660 req->ssids[0].ssid_len,
661 ies->ie[IEEE80211_BAND_5GHZ],
662 ies->len[IEEE80211_BAND_5GHZ],
663 IEEE80211_BAND_5GHZ);
664 if (ret < 0) {
665 wl1271_error("5GHz PROBE request template failed");
666 goto out;
667 }
668 }
669
670 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
671
672 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
673 sizeof(*cfg), 0);
674 if (ret < 0) {
675 wl1271_error("SCAN configuration failed");
676 goto out;
677 }
678out:
679 kfree(cfg);
680 return ret;
681}
682
Eliad Peller536129c2011-10-05 11:55:45 +0200683int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300684{
685 struct wl1271_cmd_sched_scan_start *start;
686 int ret = 0;
687
688 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
689
Eliad Peller536129c2011-10-05 11:55:45 +0200690 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300691 return -EOPNOTSUPP;
692
693 if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
694 return -EBUSY;
695
Luciano Coelho95feadc2011-05-10 14:38:59 +0300696 start = kzalloc(sizeof(*start), GFP_KERNEL);
697 if (!start)
698 return -ENOMEM;
699
700 start->tag = WL1271_SCAN_DEFAULT_TAG;
701
702 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
703 sizeof(*start), 0);
704 if (ret < 0) {
705 wl1271_error("failed to send scan start command");
706 goto out_free;
707 }
708
709out_free:
710 kfree(start);
711 return ret;
712}
713
714void wl1271_scan_sched_scan_results(struct wl1271 *wl)
715{
716 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
717
718 ieee80211_sched_scan_results(wl->hw);
719}
720
721void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
722{
723 struct wl1271_cmd_sched_scan_stop *stop;
724 int ret = 0;
725
726 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
727
728 /* FIXME: what to do if alloc'ing to stop fails? */
729 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
730 if (!stop) {
731 wl1271_error("failed to alloc memory to send sched scan stop");
732 return;
733 }
734
735 stop->tag = WL1271_SCAN_DEFAULT_TAG;
736
737 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
738 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300739 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300740 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300741 goto out_free;
742 }
743 wl->sched_scanning = false;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300744
Luciano Coelho33c2c062011-05-10 14:46:02 +0300745out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300746 kfree(stop);
747}