blob: e3566ca90f0da617f5ea982db72a7455ce7218a9 [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
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300175 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300176 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300177
Eliad Peller35d77422012-02-02 13:54:26 +0200178 if (wlvif->bss_type == BSS_TYPE_AP_BSS ||
179 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
Eliad Pellerc059beb2012-01-31 11:57:17 +0200180 cmd->params.role_id = wlvif->role_id;
181 else
182 cmd->params.role_id = wlvif->dev_role_id;
183
Eliad Peller35d77422012-02-02 13:54:26 +0200184 if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
185 ret = -EINVAL;
186 goto out;
187 }
188
Luciano Coelho08688d62010-07-08 17:50:07 +0300189 cmd->params.scan_options = cpu_to_le16(scan_options);
190
191 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
192 cmd->channels,
193 band, passive);
194 if (cmd->params.n_ch == 0) {
195 ret = WL1271_NOTHING_TO_SCAN;
196 goto out;
197 }
198
199 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200200 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300201 cmd->params.tid_trigger = 0;
202 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
203
204 if (band == IEEE80211_BAND_2GHZ)
205 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
206 else
207 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
208
209 if (wl->scan.ssid_len && wl->scan.ssid) {
210 cmd->params.ssid_len = wl->scan.ssid_len;
211 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
212 }
213
Eliad Peller784f6942011-10-05 11:55:39 +0200214 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300215
Eliad Pellercdaac622012-01-31 11:57:16 +0200216 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200217 cmd->params.role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200218 wl->scan.ssid, wl->scan.ssid_len,
219 wl->scan.req->ie,
220 wl->scan.req->ie_len);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300221 if (ret < 0) {
222 wl1271_error("PROBE request template failed");
223 goto out;
224 }
225
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300226 /* disable the timeout */
227 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300228 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
229 sizeof(*trigger), 0);
230 if (ret < 0) {
231 wl1271_error("trigger scan to failed for hw scan");
232 goto out;
233 }
234
Luciano Coelho08688d62010-07-08 17:50:07 +0300235 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300236
Luciano Coelho08688d62010-07-08 17:50:07 +0300237 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300238 if (ret < 0) {
239 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300240 goto out;
241 }
242
243out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300244 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300245 kfree(trigger);
246 return ret;
247}
248
Eliad Peller784f6942011-10-05 11:55:39 +0200249void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300250{
Eliad Peller83587502011-10-10 10:12:53 +0200251 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200252 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300253 enum ieee80211_band band;
Guy Eilambfafba82011-11-01 09:23:51 +0200254 u32 rate, mask;
Luciano Coelho08688d62010-07-08 17:50:07 +0300255
256 switch (wl->scan.state) {
257 case WL1271_SCAN_STATE_IDLE:
258 break;
259
260 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300261 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200262 mask = wlvif->bitrate_masks[band];
263 if (wl->scan.req->no_cck) {
264 mask &= ~CONF_TX_CCK_RATES;
265 if (!mask)
266 mask = CONF_TX_RATE_MASK_BASIC_P2P;
267 }
268 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200269 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300270 if (ret == WL1271_NOTHING_TO_SCAN) {
271 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200272 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300273 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300274
275 break;
276
277 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300278 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200279 mask = wlvif->bitrate_masks[band];
280 if (wl->scan.req->no_cck) {
281 mask &= ~CONF_TX_CCK_RATES;
282 if (!mask)
283 mask = CONF_TX_RATE_MASK_BASIC_P2P;
284 }
285 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200286 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300287 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200288 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300289 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
290 else
291 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200292 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300293 }
294
295 break;
296
297 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300298 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200299 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200300 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300301 if (ret == WL1271_NOTHING_TO_SCAN) {
302 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200303 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300304 }
305
306 break;
307
308 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300309 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200310 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200311 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300312 if (ret == WL1271_NOTHING_TO_SCAN) {
313 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200314 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300315 }
316
317 break;
318
319 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200320 wl->scan.failed = false;
321 cancel_delayed_work(&wl->scan_complete_work);
322 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
323 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300324 break;
325
326 default:
327 wl1271_error("invalid scan state");
328 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300329 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200330
331 if (ret < 0) {
332 cancel_delayed_work(&wl->scan_complete_work);
333 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
334 msecs_to_jiffies(0));
335 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300336}
337
Eliad Peller784f6942011-10-05 11:55:39 +0200338int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
339 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300340 struct cfg80211_scan_request *req)
341{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200342 /*
343 * cfg80211 should guarantee that we don't get more channels
344 * than what we have registered.
345 */
346 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
347
Luciano Coelho08688d62010-07-08 17:50:07 +0300348 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
349 return -EBUSY;
350
351 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
352
353 if (ssid_len && ssid) {
354 wl->scan.ssid_len = ssid_len;
355 memcpy(wl->scan.ssid, ssid, ssid_len);
356 } else {
357 wl->scan.ssid_len = 0;
358 }
359
Eliad Peller784f6942011-10-05 11:55:39 +0200360 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300361 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200362 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300363
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200364 /* we assume failure so that timeout scenarios are handled correctly */
365 wl->scan.failed = true;
366 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
367 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
368
Eliad Peller784f6942011-10-05 11:55:39 +0200369 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300370
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300371 return 0;
372}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300373
Eliad Peller2aa01592011-06-27 13:06:44 +0300374int wl1271_scan_stop(struct wl1271 *wl)
375{
376 struct wl1271_cmd_header *cmd = NULL;
377 int ret = 0;
378
379 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
380 return -EINVAL;
381
382 wl1271_debug(DEBUG_CMD, "cmd scan stop");
383
384 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
385 if (!cmd) {
386 ret = -ENOMEM;
387 goto out;
388 }
389
390 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
391 sizeof(*cmd), 0);
392 if (ret < 0) {
393 wl1271_error("cmd stop_scan failed");
394 goto out;
395 }
396out:
397 kfree(cmd);
398 return ret;
399}
400
Luciano Coelho95feadc2011-05-10 14:38:59 +0300401static int
402wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
403 struct cfg80211_sched_scan_request *req,
404 struct conn_scan_ch_params *channels,
405 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300406 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300407{
408 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
409 int i, j;
410 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300411 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300412
413 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300414 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300415 i++) {
416 flags = req->channels[i]->flags;
417
Luciano Coelho66870b12011-05-27 15:34:48 +0300418 if (force_passive)
419 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
420
Luciano Coelho2497a242011-05-27 15:34:46 +0300421 if ((req->channels[i]->band == band) &&
422 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300423 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300424 /* if radar is set, we ignore the passive flag */
425 (radar ||
426 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300427 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
428 req->channels[i]->band,
429 req->channels[i]->center_freq);
430 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
431 req->channels[i]->hw_value,
432 req->channels[i]->flags);
433 wl1271_debug(DEBUG_SCAN, "max_power %d",
434 req->channels[i]->max_power);
435
Luciano Coelho50a66d72011-05-27 15:34:47 +0300436 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300437 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Eyal Shapirafea2a612011-12-20 12:04:01 +0200438
Luciano Coelho50a66d72011-05-27 15:34:47 +0300439 channels[j].passive_duration =
440 cpu_to_le16(c->dwell_time_dfs);
Eyal Shapirafea2a612011-12-20 12:04:01 +0200441 } else {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300442 channels[j].passive_duration =
443 cpu_to_le16(c->dwell_time_passive);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300444 }
Eyal Shapirafea2a612011-12-20 12:04:01 +0200445
446 channels[j].min_duration =
447 cpu_to_le16(c->min_dwell_time_active);
448 channels[j].max_duration =
449 cpu_to_le16(c->max_dwell_time_active);
450
Luciano Coelho2497a242011-05-27 15:34:46 +0300451 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300452 channels[j].channel = req->channels[i]->hw_value;
453
454 j++;
455 }
456 }
457
458 return j - start;
459}
460
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300461static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300462wl1271_scan_sched_scan_channels(struct wl1271 *wl,
463 struct cfg80211_sched_scan_request *req,
464 struct wl1271_cmd_sched_scan_config *cfg)
465{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300466 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300467 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300468 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300469 false, true, 0,
470 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300471 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300472 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300473 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300474 false, false,
475 cfg->passive[0],
476 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300477 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300478 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300479 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300480 false, true, 0,
481 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300482 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300483 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300484 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300485 true, true,
486 cfg->passive[1],
487 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300488 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300489 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300490 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300491 false, false,
492 cfg->passive[1] + cfg->dfs,
493 MAX_CHANNELS_5GHZ);
494 /* 802.11j channels are not supported yet */
495 cfg->passive[2] = 0;
496 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300497
Luciano Coelho95feadc2011-05-10 14:38:59 +0300498 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
499 cfg->active[0], cfg->passive[0]);
500 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
501 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300502 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300503
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300504 return cfg->passive[0] || cfg->active[0] ||
505 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
506 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300507}
508
Luciano Coelhofb553772011-09-02 14:28:21 +0300509/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300510static int
511wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
512 struct cfg80211_sched_scan_request *req)
513{
514 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300515 struct cfg80211_match_set *sets = req->match_sets;
516 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300517 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300518
519 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
520
Luciano Coelho20a33e52011-09-02 14:28:23 +0300521 /* count the match sets that contain SSIDs */
522 for (i = 0; i < req->n_match_sets; i++)
523 if (sets[i].ssid.ssid_len > 0)
524 n_match_ssids++;
525
Luciano Coelhofb553772011-09-02 14:28:21 +0300526 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300527 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300528 (!req->n_ssids ||
529 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
530 type = SCAN_SSID_FILTER_ANY;
531 goto out;
532 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300533
Luciano Coelhofb553772011-09-02 14:28:21 +0300534 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
535 if (!cmd) {
536 ret = -ENOMEM;
537 goto out;
538 }
539
Luciano Coelho20a33e52011-09-02 14:28:23 +0300540 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300541 /* No filter, with ssids */
542 type = SCAN_SSID_FILTER_DISABLED;
543
544 for (i = 0; i < req->n_ssids; i++) {
545 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
546 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
547 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
548 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
549 ssids[i].ssid_len);
550 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300551 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300552 } else {
553 type = SCAN_SSID_FILTER_LIST;
554
555 /* Add all SSIDs from the filters */
556 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300557 /* ignore sets without SSIDs */
558 if (!sets[i].ssid.ssid_len)
559 continue;
560
Luciano Coelhofb553772011-09-02 14:28:21 +0300561 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
562 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
563 memcpy(cmd->ssids[cmd->n_ssids].ssid,
564 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
565 cmd->n_ssids++;
566 }
567 if ((req->n_ssids > 1) ||
568 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
569 /*
570 * Mark all the SSIDs passed in the SSID list as HIDDEN,
571 * so they're used in probe requests.
572 */
573 for (i = 0; i < req->n_ssids; i++) {
Eyal Shapira1b04b732011-12-07 12:37:04 +0200574 if (!req->ssids[i].ssid_len)
575 continue;
576
Luciano Coelhofb553772011-09-02 14:28:21 +0300577 for (j = 0; j < cmd->n_ssids; j++)
578 if (!memcmp(req->ssids[i].ssid,
579 cmd->ssids[j].ssid,
580 req->ssids[i].ssid_len)) {
581 cmd->ssids[j].type =
582 SCAN_SSID_TYPE_HIDDEN;
583 break;
584 }
585 /* Fail if SSID isn't present in the filters */
Eyal Shapiracc438fc2011-11-08 15:54:46 +0200586 if (j == cmd->n_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300587 ret = -EINVAL;
588 goto out_free;
589 }
590 }
591 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300592 }
593
594 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
595
596 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
597 sizeof(*cmd), 0);
598 if (ret < 0) {
599 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300600 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300601 }
602
Luciano Coelhofb553772011-09-02 14:28:21 +0300603out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300604 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300605out:
606 if (ret < 0)
607 return ret;
608 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300609}
610
Luciano Coelho95feadc2011-05-10 14:38:59 +0300611int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200612 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300613 struct cfg80211_sched_scan_request *req,
614 struct ieee80211_sched_scan_ies *ies)
615{
616 struct wl1271_cmd_sched_scan_config *cfg = NULL;
617 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300618 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300619 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300620
621 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
622
623 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
624 if (!cfg)
625 return -ENOMEM;
626
627 cfg->rssi_threshold = c->rssi_threshold;
628 cfg->snr_threshold = c->snr_threshold;
629 cfg->n_probe_reqs = c->num_probe_reqs;
630 /* cycles set to 0 it means infinite (until manually stopped) */
631 cfg->cycles = 0;
632 /* report APs when at least 1 is found */
633 cfg->report_after = 1;
634 /* don't stop scanning automatically when something is found */
635 cfg->terminate = 0;
636 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
637 /* don't filter on BSS type */
638 cfg->bss_type = SCAN_BSS_TYPE_ANY;
639 /* currently NL80211 supports only a single interval */
640 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
641 cfg->intervals[i] = cpu_to_le32(req->interval);
642
Luciano Coelhof9520792011-08-23 18:34:44 +0300643 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300644 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
645 if (ret < 0)
646 goto out;
647
648 cfg->filter_type = ret;
649
650 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300651
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300652 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300653 wl1271_error("scan channel list is empty");
654 ret = -EINVAL;
655 goto out;
656 }
657
Luciano Coelho66870b12011-05-27 15:34:48 +0300658 if (!force_passive && cfg->active[0]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200659 u8 band = IEEE80211_BAND_2GHZ;
660 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200661 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200662 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300663 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200664 ies->ie[band],
665 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300666 if (ret < 0) {
667 wl1271_error("2.4GHz PROBE request template failed");
668 goto out;
669 }
670 }
671
Luciano Coelho66870b12011-05-27 15:34:48 +0300672 if (!force_passive && cfg->active[1]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200673 u8 band = IEEE80211_BAND_5GHZ;
674 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200675 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200676 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300677 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200678 ies->ie[band],
679 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300680 if (ret < 0) {
681 wl1271_error("5GHz PROBE request template failed");
682 goto out;
683 }
684 }
685
686 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
687
688 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
689 sizeof(*cfg), 0);
690 if (ret < 0) {
691 wl1271_error("SCAN configuration failed");
692 goto out;
693 }
694out:
695 kfree(cfg);
696 return ret;
697}
698
Eliad Peller536129c2011-10-05 11:55:45 +0200699int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300700{
701 struct wl1271_cmd_sched_scan_start *start;
702 int ret = 0;
703
704 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
705
Eliad Peller536129c2011-10-05 11:55:45 +0200706 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300707 return -EOPNOTSUPP;
708
Eliad Pellera0c7b782011-12-18 20:25:41 +0200709 if (test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
Luciano Coelho683c0022011-05-12 17:07:55 +0300710 return -EBUSY;
711
Luciano Coelho95feadc2011-05-10 14:38:59 +0300712 start = kzalloc(sizeof(*start), GFP_KERNEL);
713 if (!start)
714 return -ENOMEM;
715
716 start->tag = WL1271_SCAN_DEFAULT_TAG;
717
718 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
719 sizeof(*start), 0);
720 if (ret < 0) {
721 wl1271_error("failed to send scan start command");
722 goto out_free;
723 }
724
725out_free:
726 kfree(start);
727 return ret;
728}
729
730void wl1271_scan_sched_scan_results(struct wl1271 *wl)
731{
732 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
733
734 ieee80211_sched_scan_results(wl->hw);
735}
736
737void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
738{
739 struct wl1271_cmd_sched_scan_stop *stop;
740 int ret = 0;
741
742 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
743
744 /* FIXME: what to do if alloc'ing to stop fails? */
745 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
746 if (!stop) {
747 wl1271_error("failed to alloc memory to send sched scan stop");
748 return;
749 }
750
751 stop->tag = WL1271_SCAN_DEFAULT_TAG;
752
753 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
754 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300755 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300756 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300757 goto out_free;
758 }
Luciano Coelho95feadc2011-05-10 14:38:59 +0300759
Luciano Coelho33c2c062011-05-10 14:46:02 +0300760out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300761 kfree(stop);
762}