blob: e43a6b2c1d91894b5c375e9a8483518079c1986c [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
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
Eliad Pellerba8447f2011-10-10 10:13:00 +020067 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030068 /* restore hardware connection monitoring template */
Eliad Peller83587502011-10-10 10:12:53 +020069 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030070 }
71
Eliad Peller251c1772011-08-14 13:17:17 +030072 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020073
Juuso Oikarinen78abd322010-09-21 06:23:32 +020074 if (wl->scan.failed) {
75 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030076 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020077 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020078
Eliad Peller251c1772011-08-14 13:17:17 +030079 ieee80211_scan_completed(wl->hw, false);
80
Arik Nemtsov24225b32011-03-01 12:27:26 +020081out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020082 mutex_unlock(&wl->mutex);
83
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030084}
85
86
Luciano Coelho08688d62010-07-08 17:50:07 +030087static int wl1271_get_scan_channels(struct wl1271 *wl,
88 struct cfg80211_scan_request *req,
89 struct basic_scan_channel_params *channels,
90 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030091{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +020092 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +030093 int i, j;
94 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030095
Luciano Coelho08688d62010-07-08 17:50:07 +030096 for (i = 0, j = 0;
97 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
98 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +030099 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300100
Luciano Coelho4a31c112011-03-21 23:16:14 +0200101 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300102 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300103 (req->channels[i]->band == band) &&
104 /*
105 * In passive scans, we scan all remaining
106 * channels, even if not marked as such.
107 * In active scans, we only scan channels not
108 * marked as passive.
109 */
110 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300111 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
112 req->channels[i]->band,
113 req->channels[i]->center_freq);
114 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
115 req->channels[i]->hw_value,
116 req->channels[i]->flags);
117 wl1271_debug(DEBUG_SCAN,
118 "max_antenna_gain %d, max_power %d",
119 req->channels[i]->max_antenna_gain,
120 req->channels[i]->max_power);
121 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
122 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300123
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200124 if (!passive) {
125 channels[j].min_duration =
126 cpu_to_le32(c->min_dwell_time_active);
127 channels[j].max_duration =
128 cpu_to_le32(c->max_dwell_time_active);
129 } else {
130 channels[j].min_duration =
131 cpu_to_le32(c->min_dwell_time_passive);
132 channels[j].max_duration =
133 cpu_to_le32(c->max_dwell_time_passive);
134 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300135 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300136 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300137 channels[j].channel = req->channels[i]->hw_value;
138
139 memset(&channels[j].bssid_lsb, 0xff, 4);
140 memset(&channels[j].bssid_msb, 0xff, 2);
141
142 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200143 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300144
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300145 j++;
146 }
147 }
148
Luciano Coelho08688d62010-07-08 17:50:07 +0300149 return j;
150}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300151
Luciano Coelho08688d62010-07-08 17:50:07 +0300152#define WL1271_NOTHING_TO_SCAN 1
153
Eliad Peller784f6942011-10-05 11:55:39 +0200154static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
155 enum ieee80211_band band,
156 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300157{
Eliad Peller0603d892011-10-05 11:55:51 +0200158 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300159 struct wl1271_cmd_scan *cmd;
160 struct wl1271_cmd_trigger_scan_to *trigger;
161 int ret;
162 u16 scan_options = 0;
163
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300164 /* skip active scans if we don't have SSIDs */
165 if (!passive && wl->scan.req->n_ssids == 0)
166 return WL1271_NOTHING_TO_SCAN;
167
Luciano Coelho08688d62010-07-08 17:50:07 +0300168 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
169 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
170 if (!cmd || !trigger) {
171 ret = -ENOMEM;
172 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300173 }
174
Eyal Shapirad647f2d2012-02-02 13:54:28 +0200175 if (wl->conf.scan.split_scan_timeout)
176 scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;
177
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300178 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300179 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300180
Eliad Peller35d77422012-02-02 13:54:26 +0200181 if (wlvif->bss_type == BSS_TYPE_AP_BSS ||
182 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
Eliad Pellerc059beb2012-01-31 11:57:17 +0200183 cmd->params.role_id = wlvif->role_id;
184 else
185 cmd->params.role_id = wlvif->dev_role_id;
186
Eliad Peller35d77422012-02-02 13:54:26 +0200187 if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
188 ret = -EINVAL;
189 goto out;
190 }
191
Luciano Coelho08688d62010-07-08 17:50:07 +0300192 cmd->params.scan_options = cpu_to_le16(scan_options);
193
194 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
195 cmd->channels,
196 band, passive);
197 if (cmd->params.n_ch == 0) {
198 ret = WL1271_NOTHING_TO_SCAN;
199 goto out;
200 }
201
202 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200203 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Eyal Shapirad647f2d2012-02-02 13:54:28 +0200204 cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
Luciano Coelho08688d62010-07-08 17:50:07 +0300205 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
206
207 if (band == IEEE80211_BAND_2GHZ)
208 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
209 else
210 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
211
212 if (wl->scan.ssid_len && wl->scan.ssid) {
213 cmd->params.ssid_len = wl->scan.ssid_len;
214 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
215 }
216
Eliad Peller784f6942011-10-05 11:55:39 +0200217 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300218
Eliad Pellercdaac622012-01-31 11:57:16 +0200219 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200220 cmd->params.role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200221 wl->scan.ssid, wl->scan.ssid_len,
222 wl->scan.req->ie,
223 wl->scan.req->ie_len);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300224 if (ret < 0) {
225 wl1271_error("PROBE request template failed");
226 goto out;
227 }
228
Eyal Shapirad647f2d2012-02-02 13:54:28 +0200229 trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300230 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
231 sizeof(*trigger), 0);
232 if (ret < 0) {
233 wl1271_error("trigger scan to failed for hw scan");
234 goto out;
235 }
236
Luciano Coelho08688d62010-07-08 17:50:07 +0300237 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300238
Luciano Coelho08688d62010-07-08 17:50:07 +0300239 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300240 if (ret < 0) {
241 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300242 goto out;
243 }
244
245out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300246 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300247 kfree(trigger);
248 return ret;
249}
250
Eliad Peller784f6942011-10-05 11:55:39 +0200251void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300252{
Eliad Peller83587502011-10-10 10:12:53 +0200253 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200254 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300255 enum ieee80211_band band;
Guy Eilambfafba82011-11-01 09:23:51 +0200256 u32 rate, mask;
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;
Guy Eilambfafba82011-11-01 09:23:51 +0200264 mask = wlvif->bitrate_masks[band];
265 if (wl->scan.req->no_cck) {
266 mask &= ~CONF_TX_CCK_RATES;
267 if (!mask)
268 mask = CONF_TX_RATE_MASK_BASIC_P2P;
269 }
270 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200271 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300272 if (ret == WL1271_NOTHING_TO_SCAN) {
273 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200274 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300275 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300276
277 break;
278
279 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300280 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200281 mask = wlvif->bitrate_masks[band];
282 if (wl->scan.req->no_cck) {
283 mask &= ~CONF_TX_CCK_RATES;
284 if (!mask)
285 mask = CONF_TX_RATE_MASK_BASIC_P2P;
286 }
287 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200288 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300289 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200290 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300291 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
292 else
293 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200294 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300295 }
296
297 break;
298
299 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300300 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200301 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200302 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300303 if (ret == WL1271_NOTHING_TO_SCAN) {
304 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200305 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300306 }
307
308 break;
309
310 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300311 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200312 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200313 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300314 if (ret == WL1271_NOTHING_TO_SCAN) {
315 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200316 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300317 }
318
319 break;
320
321 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200322 wl->scan.failed = false;
323 cancel_delayed_work(&wl->scan_complete_work);
324 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
325 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300326 break;
327
328 default:
329 wl1271_error("invalid scan state");
330 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300331 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200332
333 if (ret < 0) {
334 cancel_delayed_work(&wl->scan_complete_work);
335 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
336 msecs_to_jiffies(0));
337 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300338}
339
Eliad Peller784f6942011-10-05 11:55:39 +0200340int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
341 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300342 struct cfg80211_scan_request *req)
343{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200344 /*
345 * cfg80211 should guarantee that we don't get more channels
346 * than what we have registered.
347 */
348 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
349
Luciano Coelho08688d62010-07-08 17:50:07 +0300350 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
351 return -EBUSY;
352
353 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
354
355 if (ssid_len && ssid) {
356 wl->scan.ssid_len = ssid_len;
357 memcpy(wl->scan.ssid, ssid, ssid_len);
358 } else {
359 wl->scan.ssid_len = 0;
360 }
361
Eliad Peller784f6942011-10-05 11:55:39 +0200362 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300363 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200364 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300365
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200366 /* we assume failure so that timeout scenarios are handled correctly */
367 wl->scan.failed = true;
368 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
369 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
370
Eliad Peller784f6942011-10-05 11:55:39 +0200371 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300372
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300373 return 0;
374}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300375
Eliad Peller2aa01592011-06-27 13:06:44 +0300376int wl1271_scan_stop(struct wl1271 *wl)
377{
378 struct wl1271_cmd_header *cmd = NULL;
379 int ret = 0;
380
381 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
382 return -EINVAL;
383
384 wl1271_debug(DEBUG_CMD, "cmd scan stop");
385
386 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
387 if (!cmd) {
388 ret = -ENOMEM;
389 goto out;
390 }
391
392 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
393 sizeof(*cmd), 0);
394 if (ret < 0) {
395 wl1271_error("cmd stop_scan failed");
396 goto out;
397 }
398out:
399 kfree(cmd);
400 return ret;
401}
402
Luciano Coelho95feadc2011-05-10 14:38:59 +0300403static int
404wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
405 struct cfg80211_sched_scan_request *req,
406 struct conn_scan_ch_params *channels,
407 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300408 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300409{
410 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
411 int i, j;
412 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300413 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300414
415 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300416 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300417 i++) {
418 flags = req->channels[i]->flags;
419
Luciano Coelho66870b12011-05-27 15:34:48 +0300420 if (force_passive)
421 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
422
Luciano Coelho2497a242011-05-27 15:34:46 +0300423 if ((req->channels[i]->band == band) &&
424 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300425 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300426 /* if radar is set, we ignore the passive flag */
427 (radar ||
428 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300429 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
430 req->channels[i]->band,
431 req->channels[i]->center_freq);
432 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
433 req->channels[i]->hw_value,
434 req->channels[i]->flags);
435 wl1271_debug(DEBUG_SCAN, "max_power %d",
436 req->channels[i]->max_power);
437
Luciano Coelho50a66d72011-05-27 15:34:47 +0300438 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300439 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Eyal Shapirafea2a612011-12-20 12:04:01 +0200440
Luciano Coelho50a66d72011-05-27 15:34:47 +0300441 channels[j].passive_duration =
442 cpu_to_le16(c->dwell_time_dfs);
Eyal Shapirafea2a612011-12-20 12:04:01 +0200443 } else {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300444 channels[j].passive_duration =
445 cpu_to_le16(c->dwell_time_passive);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300446 }
Eyal Shapirafea2a612011-12-20 12:04:01 +0200447
448 channels[j].min_duration =
449 cpu_to_le16(c->min_dwell_time_active);
450 channels[j].max_duration =
451 cpu_to_le16(c->max_dwell_time_active);
452
Luciano Coelho2497a242011-05-27 15:34:46 +0300453 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300454 channels[j].channel = req->channels[i]->hw_value;
455
456 j++;
457 }
458 }
459
460 return j - start;
461}
462
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300463static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300464wl1271_scan_sched_scan_channels(struct wl1271 *wl,
465 struct cfg80211_sched_scan_request *req,
466 struct wl1271_cmd_sched_scan_config *cfg)
467{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300468 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300469 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300470 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300471 false, true, 0,
472 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300473 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300474 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300475 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300476 false, false,
477 cfg->passive[0],
478 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300479 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300480 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300481 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300482 false, true, 0,
483 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300484 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300485 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300486 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300487 true, true,
488 cfg->passive[1],
489 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300490 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300491 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300492 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300493 false, false,
494 cfg->passive[1] + cfg->dfs,
495 MAX_CHANNELS_5GHZ);
496 /* 802.11j channels are not supported yet */
497 cfg->passive[2] = 0;
498 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300499
Luciano Coelho95feadc2011-05-10 14:38:59 +0300500 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
501 cfg->active[0], cfg->passive[0]);
502 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
503 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300504 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300505
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300506 return cfg->passive[0] || cfg->active[0] ||
507 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
508 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300509}
510
Luciano Coelhofb553772011-09-02 14:28:21 +0300511/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300512static int
513wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
514 struct cfg80211_sched_scan_request *req)
515{
516 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300517 struct cfg80211_match_set *sets = req->match_sets;
518 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300519 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300520
521 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
522
Luciano Coelho20a33e52011-09-02 14:28:23 +0300523 /* count the match sets that contain SSIDs */
524 for (i = 0; i < req->n_match_sets; i++)
525 if (sets[i].ssid.ssid_len > 0)
526 n_match_ssids++;
527
Luciano Coelhofb553772011-09-02 14:28:21 +0300528 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300529 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300530 (!req->n_ssids ||
531 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
532 type = SCAN_SSID_FILTER_ANY;
533 goto out;
534 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300535
Luciano Coelhofb553772011-09-02 14:28:21 +0300536 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
537 if (!cmd) {
538 ret = -ENOMEM;
539 goto out;
540 }
541
Luciano Coelho20a33e52011-09-02 14:28:23 +0300542 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300543 /* No filter, with ssids */
544 type = SCAN_SSID_FILTER_DISABLED;
545
546 for (i = 0; i < req->n_ssids; i++) {
547 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
548 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
549 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
550 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
551 ssids[i].ssid_len);
552 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300553 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300554 } else {
555 type = SCAN_SSID_FILTER_LIST;
556
557 /* Add all SSIDs from the filters */
558 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300559 /* ignore sets without SSIDs */
560 if (!sets[i].ssid.ssid_len)
561 continue;
562
Luciano Coelhofb553772011-09-02 14:28:21 +0300563 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
564 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
565 memcpy(cmd->ssids[cmd->n_ssids].ssid,
566 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
567 cmd->n_ssids++;
568 }
569 if ((req->n_ssids > 1) ||
570 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
571 /*
572 * Mark all the SSIDs passed in the SSID list as HIDDEN,
573 * so they're used in probe requests.
574 */
575 for (i = 0; i < req->n_ssids; i++) {
Eyal Shapira1b04b732011-12-07 12:37:04 +0200576 if (!req->ssids[i].ssid_len)
577 continue;
578
Luciano Coelhofb553772011-09-02 14:28:21 +0300579 for (j = 0; j < cmd->n_ssids; j++)
580 if (!memcmp(req->ssids[i].ssid,
581 cmd->ssids[j].ssid,
582 req->ssids[i].ssid_len)) {
583 cmd->ssids[j].type =
584 SCAN_SSID_TYPE_HIDDEN;
585 break;
586 }
587 /* Fail if SSID isn't present in the filters */
Eyal Shapiracc438fc2011-11-08 15:54:46 +0200588 if (j == cmd->n_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300589 ret = -EINVAL;
590 goto out_free;
591 }
592 }
593 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300594 }
595
596 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
597
598 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
599 sizeof(*cmd), 0);
600 if (ret < 0) {
601 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300602 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300603 }
604
Luciano Coelhofb553772011-09-02 14:28:21 +0300605out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300606 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300607out:
608 if (ret < 0)
609 return ret;
610 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300611}
612
Luciano Coelho95feadc2011-05-10 14:38:59 +0300613int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200614 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300615 struct cfg80211_sched_scan_request *req,
616 struct ieee80211_sched_scan_ies *ies)
617{
618 struct wl1271_cmd_sched_scan_config *cfg = NULL;
619 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300620 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300621 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300622
623 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
624
625 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
626 if (!cfg)
627 return -ENOMEM;
628
629 cfg->rssi_threshold = c->rssi_threshold;
630 cfg->snr_threshold = c->snr_threshold;
631 cfg->n_probe_reqs = c->num_probe_reqs;
632 /* cycles set to 0 it means infinite (until manually stopped) */
633 cfg->cycles = 0;
634 /* report APs when at least 1 is found */
635 cfg->report_after = 1;
636 /* don't stop scanning automatically when something is found */
637 cfg->terminate = 0;
638 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
639 /* don't filter on BSS type */
640 cfg->bss_type = SCAN_BSS_TYPE_ANY;
641 /* currently NL80211 supports only a single interval */
642 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
643 cfg->intervals[i] = cpu_to_le32(req->interval);
644
Luciano Coelhof9520792011-08-23 18:34:44 +0300645 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300646 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
647 if (ret < 0)
648 goto out;
649
650 cfg->filter_type = ret;
651
652 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300653
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300654 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300655 wl1271_error("scan channel list is empty");
656 ret = -EINVAL;
657 goto out;
658 }
659
Luciano Coelho66870b12011-05-27 15:34:48 +0300660 if (!force_passive && cfg->active[0]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200661 u8 band = IEEE80211_BAND_2GHZ;
662 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200663 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200664 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300665 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200666 ies->ie[band],
667 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300668 if (ret < 0) {
669 wl1271_error("2.4GHz PROBE request template failed");
670 goto out;
671 }
672 }
673
Luciano Coelho66870b12011-05-27 15:34:48 +0300674 if (!force_passive && cfg->active[1]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200675 u8 band = IEEE80211_BAND_5GHZ;
676 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200677 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200678 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300679 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200680 ies->ie[band],
681 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300682 if (ret < 0) {
683 wl1271_error("5GHz PROBE request template failed");
684 goto out;
685 }
686 }
687
688 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
689
690 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
691 sizeof(*cfg), 0);
692 if (ret < 0) {
693 wl1271_error("SCAN configuration failed");
694 goto out;
695 }
696out:
697 kfree(cfg);
698 return ret;
699}
700
Eliad Peller536129c2011-10-05 11:55:45 +0200701int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300702{
703 struct wl1271_cmd_sched_scan_start *start;
704 int ret = 0;
705
706 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
707
Eliad Peller536129c2011-10-05 11:55:45 +0200708 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300709 return -EOPNOTSUPP;
710
Eliad Pellera0c7b782011-12-18 20:25:41 +0200711 if (test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
Luciano Coelho683c0022011-05-12 17:07:55 +0300712 return -EBUSY;
713
Luciano Coelho95feadc2011-05-10 14:38:59 +0300714 start = kzalloc(sizeof(*start), GFP_KERNEL);
715 if (!start)
716 return -ENOMEM;
717
718 start->tag = WL1271_SCAN_DEFAULT_TAG;
719
720 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
721 sizeof(*start), 0);
722 if (ret < 0) {
723 wl1271_error("failed to send scan start command");
724 goto out_free;
725 }
726
727out_free:
728 kfree(start);
729 return ret;
730}
731
732void wl1271_scan_sched_scan_results(struct wl1271 *wl)
733{
734 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
735
736 ieee80211_sched_scan_results(wl->hw);
737}
738
739void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
740{
741 struct wl1271_cmd_sched_scan_stop *stop;
742 int ret = 0;
743
744 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
745
746 /* FIXME: what to do if alloc'ing to stop fails? */
747 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
748 if (!stop) {
749 wl1271_error("failed to alloc memory to send sched scan stop");
750 return;
751 }
752
753 stop->tag = WL1271_SCAN_DEFAULT_TAG;
754
755 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
756 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300757 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300758 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300759 goto out_free;
760 }
Luciano Coelho95feadc2011-05-10 14:38:59 +0300761
Luciano Coelho33c2c062011-05-10 14:46:02 +0300762out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300763 kfree(stop);
764}