blob: fcba055ef196a050023796152d66cafb550c68e2 [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;
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
Arik Nemtsov55df5af2012-03-03 22:18:00 +020058 /*
59 * Rearm the tx watchdog just before idling scan. This
60 * prevents just-finished scans from triggering the watchdog
61 */
62 wl12xx_rearm_tx_watchdog_locked(wl);
63
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030064 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020065 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020066 wl->scan.req = NULL;
Eliad Peller784f6942011-10-05 11:55:39 +020067 wl->scan_vif = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020068
Eliad Peller251c1772011-08-14 13:17:17 +030069 ret = wl1271_ps_elp_wakeup(wl);
70 if (ret < 0)
71 goto out;
72
Eliad Pellerba8447f2011-10-10 10:13:00 +020073 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030074 /* restore hardware connection monitoring template */
Eliad Peller83587502011-10-10 10:12:53 +020075 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030076 }
77
Eliad Peller251c1772011-08-14 13:17:17 +030078 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020079
Juuso Oikarinen78abd322010-09-21 06:23:32 +020080 if (wl->scan.failed) {
81 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030082 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020083 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020084
Eliad Peller251c1772011-08-14 13:17:17 +030085 ieee80211_scan_completed(wl->hw, false);
86
Arik Nemtsov24225b32011-03-01 12:27:26 +020087out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020088 mutex_unlock(&wl->mutex);
89
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030090}
91
92
Luciano Coelho08688d62010-07-08 17:50:07 +030093static int wl1271_get_scan_channels(struct wl1271 *wl,
94 struct cfg80211_scan_request *req,
95 struct basic_scan_channel_params *channels,
96 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030097{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +020098 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +030099 int i, j;
100 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300101
Luciano Coelho08688d62010-07-08 17:50:07 +0300102 for (i = 0, j = 0;
103 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
104 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300105 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300106
Luciano Coelho4a31c112011-03-21 23:16:14 +0200107 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300108 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300109 (req->channels[i]->band == band) &&
110 /*
111 * In passive scans, we scan all remaining
112 * channels, even if not marked as such.
113 * In active scans, we only scan channels not
114 * marked as passive.
115 */
116 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300117 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
118 req->channels[i]->band,
119 req->channels[i]->center_freq);
120 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
121 req->channels[i]->hw_value,
122 req->channels[i]->flags);
123 wl1271_debug(DEBUG_SCAN,
124 "max_antenna_gain %d, max_power %d",
125 req->channels[i]->max_antenna_gain,
126 req->channels[i]->max_power);
127 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
128 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300129
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200130 if (!passive) {
131 channels[j].min_duration =
132 cpu_to_le32(c->min_dwell_time_active);
133 channels[j].max_duration =
134 cpu_to_le32(c->max_dwell_time_active);
135 } else {
136 channels[j].min_duration =
137 cpu_to_le32(c->min_dwell_time_passive);
138 channels[j].max_duration =
139 cpu_to_le32(c->max_dwell_time_passive);
140 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300141 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300142 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300143 channels[j].channel = req->channels[i]->hw_value;
144
145 memset(&channels[j].bssid_lsb, 0xff, 4);
146 memset(&channels[j].bssid_msb, 0xff, 2);
147
148 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200149 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300150
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300151 j++;
152 }
153 }
154
Luciano Coelho08688d62010-07-08 17:50:07 +0300155 return j;
156}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300157
Luciano Coelho08688d62010-07-08 17:50:07 +0300158#define WL1271_NOTHING_TO_SCAN 1
159
Eliad Peller784f6942011-10-05 11:55:39 +0200160static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
161 enum ieee80211_band band,
162 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300163{
Eliad Peller0603d892011-10-05 11:55:51 +0200164 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300165 struct wl1271_cmd_scan *cmd;
166 struct wl1271_cmd_trigger_scan_to *trigger;
167 int ret;
168 u16 scan_options = 0;
169
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300170 /* skip active scans if we don't have SSIDs */
171 if (!passive && wl->scan.req->n_ssids == 0)
172 return WL1271_NOTHING_TO_SCAN;
173
Luciano Coelho08688d62010-07-08 17:50:07 +0300174 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
175 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
176 if (!cmd || !trigger) {
177 ret = -ENOMEM;
178 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300179 }
180
Eyal Shapirad647f2d2012-02-02 13:54:28 +0200181 if (wl->conf.scan.split_scan_timeout)
182 scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;
183
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300184 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300185 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300186
Eliad Peller35d77422012-02-02 13:54:26 +0200187 if (wlvif->bss_type == BSS_TYPE_AP_BSS ||
188 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
Eliad Pellerc059beb2012-01-31 11:57:17 +0200189 cmd->params.role_id = wlvif->role_id;
190 else
191 cmd->params.role_id = wlvif->dev_role_id;
192
Eliad Peller35d77422012-02-02 13:54:26 +0200193 if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
194 ret = -EINVAL;
195 goto out;
196 }
197
Luciano Coelho08688d62010-07-08 17:50:07 +0300198 cmd->params.scan_options = cpu_to_le16(scan_options);
199
200 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
201 cmd->channels,
202 band, passive);
203 if (cmd->params.n_ch == 0) {
204 ret = WL1271_NOTHING_TO_SCAN;
205 goto out;
206 }
207
208 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200209 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Eyal Shapirad647f2d2012-02-02 13:54:28 +0200210 cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
Luciano Coelho08688d62010-07-08 17:50:07 +0300211 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
212
213 if (band == IEEE80211_BAND_2GHZ)
214 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
215 else
216 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
217
218 if (wl->scan.ssid_len && wl->scan.ssid) {
219 cmd->params.ssid_len = wl->scan.ssid_len;
220 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
221 }
222
Eliad Peller784f6942011-10-05 11:55:39 +0200223 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300224
Eliad Pellercdaac622012-01-31 11:57:16 +0200225 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200226 cmd->params.role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200227 wl->scan.ssid, wl->scan.ssid_len,
228 wl->scan.req->ie,
229 wl->scan.req->ie_len);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300230 if (ret < 0) {
231 wl1271_error("PROBE request template failed");
232 goto out;
233 }
234
Eyal Shapirad647f2d2012-02-02 13:54:28 +0200235 trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300236 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
237 sizeof(*trigger), 0);
238 if (ret < 0) {
239 wl1271_error("trigger scan to failed for hw scan");
240 goto out;
241 }
242
Luciano Coelho08688d62010-07-08 17:50:07 +0300243 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300244
Luciano Coelho08688d62010-07-08 17:50:07 +0300245 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300246 if (ret < 0) {
247 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300248 goto out;
249 }
250
251out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300252 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300253 kfree(trigger);
254 return ret;
255}
256
Eliad Peller784f6942011-10-05 11:55:39 +0200257void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300258{
Eliad Peller83587502011-10-10 10:12:53 +0200259 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200260 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300261 enum ieee80211_band band;
Guy Eilambfafba82011-11-01 09:23:51 +0200262 u32 rate, mask;
Luciano Coelho08688d62010-07-08 17:50:07 +0300263
264 switch (wl->scan.state) {
265 case WL1271_SCAN_STATE_IDLE:
266 break;
267
268 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300269 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200270 mask = wlvif->bitrate_masks[band];
271 if (wl->scan.req->no_cck) {
272 mask &= ~CONF_TX_CCK_RATES;
273 if (!mask)
274 mask = CONF_TX_RATE_MASK_BASIC_P2P;
275 }
276 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200277 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300278 if (ret == WL1271_NOTHING_TO_SCAN) {
279 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200280 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300281 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300282
283 break;
284
285 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300286 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200287 mask = wlvif->bitrate_masks[band];
288 if (wl->scan.req->no_cck) {
289 mask &= ~CONF_TX_CCK_RATES;
290 if (!mask)
291 mask = CONF_TX_RATE_MASK_BASIC_P2P;
292 }
293 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200294 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300295 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200296 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300297 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
298 else
299 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200300 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300301 }
302
303 break;
304
305 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300306 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200307 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200308 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300309 if (ret == WL1271_NOTHING_TO_SCAN) {
310 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200311 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300312 }
313
314 break;
315
316 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300317 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200318 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200319 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300320 if (ret == WL1271_NOTHING_TO_SCAN) {
321 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200322 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300323 }
324
325 break;
326
327 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200328 wl->scan.failed = false;
329 cancel_delayed_work(&wl->scan_complete_work);
330 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
331 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300332 break;
333
334 default:
335 wl1271_error("invalid scan state");
336 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300337 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200338
339 if (ret < 0) {
340 cancel_delayed_work(&wl->scan_complete_work);
341 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
342 msecs_to_jiffies(0));
343 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300344}
345
Eliad Peller784f6942011-10-05 11:55:39 +0200346int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
347 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300348 struct cfg80211_scan_request *req)
349{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200350 /*
351 * cfg80211 should guarantee that we don't get more channels
352 * than what we have registered.
353 */
354 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
355
Luciano Coelho08688d62010-07-08 17:50:07 +0300356 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
357 return -EBUSY;
358
359 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
360
361 if (ssid_len && ssid) {
362 wl->scan.ssid_len = ssid_len;
363 memcpy(wl->scan.ssid, ssid, ssid_len);
364 } else {
365 wl->scan.ssid_len = 0;
366 }
367
Eliad Peller784f6942011-10-05 11:55:39 +0200368 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300369 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200370 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300371
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200372 /* we assume failure so that timeout scenarios are handled correctly */
373 wl->scan.failed = true;
374 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
375 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
376
Eliad Peller784f6942011-10-05 11:55:39 +0200377 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300378
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300379 return 0;
380}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300381
Eliad Peller2aa01592011-06-27 13:06:44 +0300382int wl1271_scan_stop(struct wl1271 *wl)
383{
384 struct wl1271_cmd_header *cmd = NULL;
385 int ret = 0;
386
387 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
388 return -EINVAL;
389
390 wl1271_debug(DEBUG_CMD, "cmd scan stop");
391
392 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
393 if (!cmd) {
394 ret = -ENOMEM;
395 goto out;
396 }
397
398 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
399 sizeof(*cmd), 0);
400 if (ret < 0) {
401 wl1271_error("cmd stop_scan failed");
402 goto out;
403 }
404out:
405 kfree(cmd);
406 return ret;
407}
408
Luciano Coelho95feadc2011-05-10 14:38:59 +0300409static int
410wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
411 struct cfg80211_sched_scan_request *req,
412 struct conn_scan_ch_params *channels,
413 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300414 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300415{
416 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
417 int i, j;
418 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300419 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300420
421 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300422 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300423 i++) {
424 flags = req->channels[i]->flags;
425
Luciano Coelho66870b12011-05-27 15:34:48 +0300426 if (force_passive)
427 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
428
Luciano Coelho2497a242011-05-27 15:34:46 +0300429 if ((req->channels[i]->band == band) &&
430 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300431 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300432 /* if radar is set, we ignore the passive flag */
433 (radar ||
434 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300435 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
436 req->channels[i]->band,
437 req->channels[i]->center_freq);
438 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
439 req->channels[i]->hw_value,
440 req->channels[i]->flags);
441 wl1271_debug(DEBUG_SCAN, "max_power %d",
442 req->channels[i]->max_power);
443
Luciano Coelho50a66d72011-05-27 15:34:47 +0300444 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300445 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Eyal Shapirafea2a612011-12-20 12:04:01 +0200446
Luciano Coelho50a66d72011-05-27 15:34:47 +0300447 channels[j].passive_duration =
448 cpu_to_le16(c->dwell_time_dfs);
Eyal Shapirafea2a612011-12-20 12:04:01 +0200449 } else {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300450 channels[j].passive_duration =
451 cpu_to_le16(c->dwell_time_passive);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300452 }
Eyal Shapirafea2a612011-12-20 12:04:01 +0200453
454 channels[j].min_duration =
455 cpu_to_le16(c->min_dwell_time_active);
456 channels[j].max_duration =
457 cpu_to_le16(c->max_dwell_time_active);
458
Luciano Coelho2497a242011-05-27 15:34:46 +0300459 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300460 channels[j].channel = req->channels[i]->hw_value;
461
462 j++;
463 }
464 }
465
466 return j - start;
467}
468
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300469static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300470wl1271_scan_sched_scan_channels(struct wl1271 *wl,
471 struct cfg80211_sched_scan_request *req,
472 struct wl1271_cmd_sched_scan_config *cfg)
473{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300474 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300475 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300476 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300477 false, true, 0,
478 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300479 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300480 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300481 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300482 false, false,
483 cfg->passive[0],
484 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300485 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300486 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300487 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300488 false, true, 0,
489 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300490 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300491 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300492 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300493 true, true,
494 cfg->passive[1],
495 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300496 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300497 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300498 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300499 false, false,
500 cfg->passive[1] + cfg->dfs,
501 MAX_CHANNELS_5GHZ);
502 /* 802.11j channels are not supported yet */
503 cfg->passive[2] = 0;
504 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300505
Luciano Coelho95feadc2011-05-10 14:38:59 +0300506 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
507 cfg->active[0], cfg->passive[0]);
508 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
509 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300510 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300511
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300512 return cfg->passive[0] || cfg->active[0] ||
513 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
514 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300515}
516
Luciano Coelhofb553772011-09-02 14:28:21 +0300517/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300518static int
519wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
520 struct cfg80211_sched_scan_request *req)
521{
522 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300523 struct cfg80211_match_set *sets = req->match_sets;
524 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300525 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300526
527 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
528
Luciano Coelho20a33e52011-09-02 14:28:23 +0300529 /* count the match sets that contain SSIDs */
530 for (i = 0; i < req->n_match_sets; i++)
531 if (sets[i].ssid.ssid_len > 0)
532 n_match_ssids++;
533
Luciano Coelhofb553772011-09-02 14:28:21 +0300534 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300535 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300536 (!req->n_ssids ||
537 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
538 type = SCAN_SSID_FILTER_ANY;
539 goto out;
540 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300541
Luciano Coelhofb553772011-09-02 14:28:21 +0300542 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
543 if (!cmd) {
544 ret = -ENOMEM;
545 goto out;
546 }
547
Luciano Coelho20a33e52011-09-02 14:28:23 +0300548 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300549 /* No filter, with ssids */
550 type = SCAN_SSID_FILTER_DISABLED;
551
552 for (i = 0; i < req->n_ssids; i++) {
553 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
554 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
555 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
556 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
557 ssids[i].ssid_len);
558 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300559 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300560 } else {
561 type = SCAN_SSID_FILTER_LIST;
562
563 /* Add all SSIDs from the filters */
564 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300565 /* ignore sets without SSIDs */
566 if (!sets[i].ssid.ssid_len)
567 continue;
568
Luciano Coelhofb553772011-09-02 14:28:21 +0300569 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
570 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
571 memcpy(cmd->ssids[cmd->n_ssids].ssid,
572 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
573 cmd->n_ssids++;
574 }
575 if ((req->n_ssids > 1) ||
576 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
577 /*
578 * Mark all the SSIDs passed in the SSID list as HIDDEN,
579 * so they're used in probe requests.
580 */
581 for (i = 0; i < req->n_ssids; i++) {
Eyal Shapira1b04b732011-12-07 12:37:04 +0200582 if (!req->ssids[i].ssid_len)
583 continue;
584
Luciano Coelhofb553772011-09-02 14:28:21 +0300585 for (j = 0; j < cmd->n_ssids; j++)
586 if (!memcmp(req->ssids[i].ssid,
587 cmd->ssids[j].ssid,
588 req->ssids[i].ssid_len)) {
589 cmd->ssids[j].type =
590 SCAN_SSID_TYPE_HIDDEN;
591 break;
592 }
593 /* Fail if SSID isn't present in the filters */
Eyal Shapiracc438fc2011-11-08 15:54:46 +0200594 if (j == cmd->n_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300595 ret = -EINVAL;
596 goto out_free;
597 }
598 }
599 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300600 }
601
602 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
603
604 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
605 sizeof(*cmd), 0);
606 if (ret < 0) {
607 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300608 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300609 }
610
Luciano Coelhofb553772011-09-02 14:28:21 +0300611out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300612 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300613out:
614 if (ret < 0)
615 return ret;
616 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300617}
618
Luciano Coelho95feadc2011-05-10 14:38:59 +0300619int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200620 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300621 struct cfg80211_sched_scan_request *req,
622 struct ieee80211_sched_scan_ies *ies)
623{
624 struct wl1271_cmd_sched_scan_config *cfg = NULL;
625 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300626 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300627 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300628
629 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
630
631 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
632 if (!cfg)
633 return -ENOMEM;
634
635 cfg->rssi_threshold = c->rssi_threshold;
636 cfg->snr_threshold = c->snr_threshold;
637 cfg->n_probe_reqs = c->num_probe_reqs;
638 /* cycles set to 0 it means infinite (until manually stopped) */
639 cfg->cycles = 0;
640 /* report APs when at least 1 is found */
641 cfg->report_after = 1;
642 /* don't stop scanning automatically when something is found */
643 cfg->terminate = 0;
644 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
645 /* don't filter on BSS type */
646 cfg->bss_type = SCAN_BSS_TYPE_ANY;
647 /* currently NL80211 supports only a single interval */
648 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
649 cfg->intervals[i] = cpu_to_le32(req->interval);
650
Luciano Coelhof9520792011-08-23 18:34:44 +0300651 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300652 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
653 if (ret < 0)
654 goto out;
655
656 cfg->filter_type = ret;
657
658 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300659
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300660 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300661 wl1271_error("scan channel list is empty");
662 ret = -EINVAL;
663 goto out;
664 }
665
Luciano Coelho66870b12011-05-27 15:34:48 +0300666 if (!force_passive && cfg->active[0]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200667 u8 band = IEEE80211_BAND_2GHZ;
668 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200669 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200670 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300671 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200672 ies->ie[band],
673 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300674 if (ret < 0) {
675 wl1271_error("2.4GHz PROBE request template failed");
676 goto out;
677 }
678 }
679
Luciano Coelho66870b12011-05-27 15:34:48 +0300680 if (!force_passive && cfg->active[1]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200681 u8 band = IEEE80211_BAND_5GHZ;
682 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200683 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200684 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300685 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200686 ies->ie[band],
687 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300688 if (ret < 0) {
689 wl1271_error("5GHz PROBE request template failed");
690 goto out;
691 }
692 }
693
694 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
695
696 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
697 sizeof(*cfg), 0);
698 if (ret < 0) {
699 wl1271_error("SCAN configuration failed");
700 goto out;
701 }
702out:
703 kfree(cfg);
704 return ret;
705}
706
Eliad Peller536129c2011-10-05 11:55:45 +0200707int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300708{
709 struct wl1271_cmd_sched_scan_start *start;
710 int ret = 0;
711
712 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
713
Eliad Peller536129c2011-10-05 11:55:45 +0200714 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300715 return -EOPNOTSUPP;
716
Eliad Pellera0c7b782011-12-18 20:25:41 +0200717 if (test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
Luciano Coelho683c0022011-05-12 17:07:55 +0300718 return -EBUSY;
719
Luciano Coelho95feadc2011-05-10 14:38:59 +0300720 start = kzalloc(sizeof(*start), GFP_KERNEL);
721 if (!start)
722 return -ENOMEM;
723
724 start->tag = WL1271_SCAN_DEFAULT_TAG;
725
726 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
727 sizeof(*start), 0);
728 if (ret < 0) {
729 wl1271_error("failed to send scan start command");
730 goto out_free;
731 }
732
733out_free:
734 kfree(start);
735 return ret;
736}
737
738void wl1271_scan_sched_scan_results(struct wl1271 *wl)
739{
740 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
741
742 ieee80211_sched_scan_results(wl->hw);
743}
744
745void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
746{
747 struct wl1271_cmd_sched_scan_stop *stop;
748 int ret = 0;
749
750 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
751
752 /* FIXME: what to do if alloc'ing to stop fails? */
753 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
754 if (!stop) {
755 wl1271_error("failed to alloc memory to send sched scan stop");
756 return;
757 }
758
759 stop->tag = WL1271_SCAN_DEFAULT_TAG;
760
761 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
762 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300763 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300764 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300765 goto out_free;
766 }
Luciano Coelho95feadc2011-05-10 14:38:59 +0300767
Luciano Coelho33c2c062011-05-10 14:46:02 +0300768out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300769 kfree(stop);
770}