blob: 6adc7333b7e673d1decd3c55be6f69f23c952222 [file] [log] [blame]
Luciano Coelho34dd2aa2010-07-08 17:50:06 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/ieee80211.h>
25
Shahar Levi00d20102010-11-08 11:20:10 +000026#include "wl12xx.h"
Luciano Coelho0f4e3122011-10-07 11:02:42 +030027#include "debug.h"
Shahar Levi00d20102010-11-08 11:20:10 +000028#include "cmd.h"
29#include "scan.h"
30#include "acx.h"
Arik Nemtsov24225b32011-03-01 12:27:26 +020031#include "ps.h"
Eliad Pelleraf7fbb22011-09-19 13:51:42 +030032#include "tx.h"
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030033
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030034void wl1271_scan_complete_work(struct work_struct *work)
35{
Juuso Oikarinen78abd322010-09-21 06:23:32 +020036 struct delayed_work *dwork;
37 struct wl1271 *wl;
Eliad Peller7edebf52011-10-05 11:55:52 +020038 struct ieee80211_vif *vif;
Eliad Peller536129c2011-10-05 11:55:45 +020039 struct wl12xx_vif *wlvif;
Eliad Peller251c1772011-08-14 13:17:17 +030040 int ret;
Eliad Peller227e81e2011-08-14 13:17:26 +030041 bool is_sta, is_ibss;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020042
43 dwork = container_of(work, struct delayed_work, work);
44 wl = container_of(dwork, struct wl1271, scan_complete_work);
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030045
46 wl1271_debug(DEBUG_SCAN, "Scanning complete");
47
48 mutex_lock(&wl->mutex);
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020049
Arik Nemtsov24225b32011-03-01 12:27:26 +020050 if (wl->state == WL1271_STATE_OFF)
51 goto out;
52
53 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
54 goto out;
Juuso Oikarinen52a2a372010-09-21 06:23:30 +020055
Eliad Peller7edebf52011-10-05 11:55:52 +020056 vif = wl->scan_vif;
57 wlvif = wl12xx_vif_to_data(vif);
Eliad Peller536129c2011-10-05 11:55:45 +020058
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030059 wl->scan.state = WL1271_SCAN_STATE_IDLE;
Luciano Coelho4a31c112011-03-21 23:16:14 +020060 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Juuso Oikarinenb739a422010-10-26 13:24:38 +020061 wl->scan.req = NULL;
Eliad Peller784f6942011-10-05 11:55:39 +020062 wl->scan_vif = NULL;
Juuso Oikarinen78abd322010-09-21 06:23:32 +020063
Eliad Peller251c1772011-08-14 13:17:17 +030064 ret = wl1271_ps_elp_wakeup(wl);
65 if (ret < 0)
66 goto out;
67
Eliad Pellerba8447f2011-10-10 10:13:00 +020068 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
Eliad Peller251c1772011-08-14 13:17:17 +030069 /* restore hardware connection monitoring template */
Eliad Peller83587502011-10-10 10:12:53 +020070 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
Eliad Peller227e81e2011-08-14 13:17:26 +030071 }
72
73 /* return to ROC if needed */
Eliad Peller536129c2011-10-05 11:55:45 +020074 is_sta = (wlvif->bss_type == BSS_TYPE_STA_BSS);
75 is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
Eliad Pellerba8447f2011-10-10 10:13:00 +020076 if (((is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) ||
Eliad Pellereee514e2011-10-10 10:13:01 +020077 (is_ibss && !test_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))) &&
Eliad Peller7edebf52011-10-05 11:55:52 +020078 !test_bit(wlvif->dev_role_id, wl->roc_map)) {
Eliad Peller251c1772011-08-14 13:17:17 +030079 /* restore remain on channel */
Eliad Pellerc059beb2012-01-31 11:57:17 +020080 if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID)
81 wl12xx_start_dev(wl, wlvif);
82 else
83 wl12xx_roc(wl, wlvif, wlvif->dev_role_id);
Arik Nemtsov24225b32011-03-01 12:27:26 +020084 }
Eliad Peller251c1772011-08-14 13:17:17 +030085 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +020086
Juuso Oikarinen78abd322010-09-21 06:23:32 +020087 if (wl->scan.failed) {
88 wl1271_info("Scan completed due to error.");
Ido Yarivbaacb9a2011-06-06 14:57:05 +030089 wl12xx_queue_recovery_work(wl);
Juuso Oikarinen78abd322010-09-21 06:23:32 +020090 }
Arik Nemtsov24225b32011-03-01 12:27:26 +020091
Eliad Peller251c1772011-08-14 13:17:17 +030092 ieee80211_scan_completed(wl->hw, false);
93
Arik Nemtsov24225b32011-03-01 12:27:26 +020094out:
Juuso Oikarinenb739a422010-10-26 13:24:38 +020095 mutex_unlock(&wl->mutex);
96
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030097}
98
99
Luciano Coelho08688d62010-07-08 17:50:07 +0300100static int wl1271_get_scan_channels(struct wl1271 *wl,
101 struct cfg80211_scan_request *req,
102 struct basic_scan_channel_params *channels,
103 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300104{
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200105 struct conf_scan_settings *c = &wl->conf.scan;
Luciano Coelho08688d62010-07-08 17:50:07 +0300106 int i, j;
107 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300108
Luciano Coelho08688d62010-07-08 17:50:07 +0300109 for (i = 0, j = 0;
110 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
111 i++) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300112 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300113
Luciano Coelho4a31c112011-03-21 23:16:14 +0200114 if (!test_bit(i, wl->scan.scanned_ch) &&
Luciano Coelho08688d62010-07-08 17:50:07 +0300115 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300116 (req->channels[i]->band == band) &&
117 /*
118 * In passive scans, we scan all remaining
119 * channels, even if not marked as such.
120 * In active scans, we only scan channels not
121 * marked as passive.
122 */
123 (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
Luciano Coelho08688d62010-07-08 17:50:07 +0300124 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
125 req->channels[i]->band,
126 req->channels[i]->center_freq);
127 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
128 req->channels[i]->hw_value,
129 req->channels[i]->flags);
130 wl1271_debug(DEBUG_SCAN,
131 "max_antenna_gain %d, max_power %d",
132 req->channels[i]->max_antenna_gain,
133 req->channels[i]->max_power);
134 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
135 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300136
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200137 if (!passive) {
138 channels[j].min_duration =
139 cpu_to_le32(c->min_dwell_time_active);
140 channels[j].max_duration =
141 cpu_to_le32(c->max_dwell_time_active);
142 } else {
143 channels[j].min_duration =
144 cpu_to_le32(c->min_dwell_time_passive);
145 channels[j].max_duration =
146 cpu_to_le32(c->max_dwell_time_passive);
147 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300148 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +0300149 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +0300150 channels[j].channel = req->channels[i]->hw_value;
151
152 memset(&channels[j].bssid_lsb, 0xff, 4);
153 memset(&channels[j].bssid_msb, 0xff, 2);
154
155 /* Mark the channels we already used */
Luciano Coelho4a31c112011-03-21 23:16:14 +0200156 set_bit(i, wl->scan.scanned_ch);
Luciano Coelho08688d62010-07-08 17:50:07 +0300157
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300158 j++;
159 }
160 }
161
Luciano Coelho08688d62010-07-08 17:50:07 +0300162 return j;
163}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300164
Luciano Coelho08688d62010-07-08 17:50:07 +0300165#define WL1271_NOTHING_TO_SCAN 1
166
Eliad Peller784f6942011-10-05 11:55:39 +0200167static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
168 enum ieee80211_band band,
169 bool passive, u32 basic_rate)
Luciano Coelho08688d62010-07-08 17:50:07 +0300170{
Eliad Peller0603d892011-10-05 11:55:51 +0200171 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300172 struct wl1271_cmd_scan *cmd;
173 struct wl1271_cmd_trigger_scan_to *trigger;
174 int ret;
175 u16 scan_options = 0;
176
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300177 /* skip active scans if we don't have SSIDs */
178 if (!passive && wl->scan.req->n_ssids == 0)
179 return WL1271_NOTHING_TO_SCAN;
180
Luciano Coelho08688d62010-07-08 17:50:07 +0300181 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
182 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
183 if (!cmd || !trigger) {
184 ret = -ENOMEM;
185 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300186 }
187
Luciano Coelho6cd9d212011-09-22 10:06:10 +0300188 if (passive)
Luciano Coelho08688d62010-07-08 17:50:07 +0300189 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300190
Eliad Pellerc059beb2012-01-31 11:57:17 +0200191 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID ||
192 wlvif->dev_role_id == WL12XX_INVALID_ROLE_ID)) {
Eliad Pellera4e02f32011-08-14 13:17:10 +0300193 ret = -EINVAL;
194 goto out;
195 }
Eliad Pellerc059beb2012-01-31 11:57:17 +0200196 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
197 cmd->params.role_id = wlvif->role_id;
198 else
199 cmd->params.role_id = wlvif->dev_role_id;
200
Luciano Coelho08688d62010-07-08 17:50:07 +0300201 cmd->params.scan_options = cpu_to_le16(scan_options);
202
203 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
204 cmd->channels,
205 band, passive);
206 if (cmd->params.n_ch == 0) {
207 ret = WL1271_NOTHING_TO_SCAN;
208 goto out;
209 }
210
211 cmd->params.tx_rate = cpu_to_le32(basic_rate);
Juuso Oikarinenbea39d62010-09-21 08:14:31 +0200212 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
Luciano Coelho08688d62010-07-08 17:50:07 +0300213 cmd->params.tid_trigger = 0;
214 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
215
216 if (band == IEEE80211_BAND_2GHZ)
217 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
218 else
219 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
220
221 if (wl->scan.ssid_len && wl->scan.ssid) {
222 cmd->params.ssid_len = wl->scan.ssid_len;
223 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
224 }
225
Eliad Peller784f6942011-10-05 11:55:39 +0200226 memcpy(cmd->addr, vif->addr, ETH_ALEN);
Eliad Pellera4e02f32011-08-14 13:17:10 +0300227
Eliad Pellercdaac622012-01-31 11:57:16 +0200228 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200229 cmd->params.role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200230 wl->scan.ssid, wl->scan.ssid_len,
231 wl->scan.req->ie,
232 wl->scan.req->ie_len);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300233 if (ret < 0) {
234 wl1271_error("PROBE request template failed");
235 goto out;
236 }
237
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300238 /* disable the timeout */
239 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300240 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
241 sizeof(*trigger), 0);
242 if (ret < 0) {
243 wl1271_error("trigger scan to failed for hw scan");
244 goto out;
245 }
246
Luciano Coelho08688d62010-07-08 17:50:07 +0300247 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300248
Luciano Coelho08688d62010-07-08 17:50:07 +0300249 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300250 if (ret < 0) {
251 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300252 goto out;
253 }
254
255out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300256 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300257 kfree(trigger);
258 return ret;
259}
260
Eliad Peller784f6942011-10-05 11:55:39 +0200261void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300262{
Eliad Peller83587502011-10-10 10:12:53 +0200263 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200264 int ret = 0;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300265 enum ieee80211_band band;
Guy Eilambfafba82011-11-01 09:23:51 +0200266 u32 rate, mask;
Luciano Coelho08688d62010-07-08 17:50:07 +0300267
268 switch (wl->scan.state) {
269 case WL1271_SCAN_STATE_IDLE:
270 break;
271
272 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300273 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200274 mask = wlvif->bitrate_masks[band];
275 if (wl->scan.req->no_cck) {
276 mask &= ~CONF_TX_CCK_RATES;
277 if (!mask)
278 mask = CONF_TX_RATE_MASK_BASIC_P2P;
279 }
280 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200281 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300282 if (ret == WL1271_NOTHING_TO_SCAN) {
283 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200284 wl1271_scan_stm(wl, vif);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300285 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300286
287 break;
288
289 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300290 band = IEEE80211_BAND_2GHZ;
Guy Eilambfafba82011-11-01 09:23:51 +0200291 mask = wlvif->bitrate_masks[band];
292 if (wl->scan.req->no_cck) {
293 mask &= ~CONF_TX_CCK_RATES;
294 if (!mask)
295 mask = CONF_TX_RATE_MASK_BASIC_P2P;
296 }
297 rate = wl1271_tx_min_rate_get(wl, mask);
Eliad Peller784f6942011-10-05 11:55:39 +0200298 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300299 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200300 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300301 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
302 else
303 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200304 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300305 }
306
307 break;
308
309 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300310 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200311 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200312 ret = wl1271_scan_send(wl, vif, band, false, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300313 if (ret == WL1271_NOTHING_TO_SCAN) {
314 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
Eliad Peller784f6942011-10-05 11:55:39 +0200315 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300316 }
317
318 break;
319
320 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
Eliad Pelleraf7fbb22011-09-19 13:51:42 +0300321 band = IEEE80211_BAND_5GHZ;
Eliad Peller83587502011-10-10 10:12:53 +0200322 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
Eliad Peller784f6942011-10-05 11:55:39 +0200323 ret = wl1271_scan_send(wl, vif, band, true, rate);
Luciano Coelho08688d62010-07-08 17:50:07 +0300324 if (ret == WL1271_NOTHING_TO_SCAN) {
325 wl->scan.state = WL1271_SCAN_STATE_DONE;
Eliad Peller784f6942011-10-05 11:55:39 +0200326 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300327 }
328
329 break;
330
331 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200332 wl->scan.failed = false;
333 cancel_delayed_work(&wl->scan_complete_work);
334 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
335 msecs_to_jiffies(0));
Luciano Coelho08688d62010-07-08 17:50:07 +0300336 break;
337
338 default:
339 wl1271_error("invalid scan state");
340 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300341 }
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200342
343 if (ret < 0) {
344 cancel_delayed_work(&wl->scan_complete_work);
345 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
346 msecs_to_jiffies(0));
347 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300348}
349
Eliad Peller784f6942011-10-05 11:55:39 +0200350int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
351 const u8 *ssid, size_t ssid_len,
Luciano Coelho08688d62010-07-08 17:50:07 +0300352 struct cfg80211_scan_request *req)
353{
Luciano Coelho4a31c112011-03-21 23:16:14 +0200354 /*
355 * cfg80211 should guarantee that we don't get more channels
356 * than what we have registered.
357 */
358 BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
359
Luciano Coelho08688d62010-07-08 17:50:07 +0300360 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
361 return -EBUSY;
362
363 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
364
365 if (ssid_len && ssid) {
366 wl->scan.ssid_len = ssid_len;
367 memcpy(wl->scan.ssid, ssid, ssid_len);
368 } else {
369 wl->scan.ssid_len = 0;
370 }
371
Eliad Peller784f6942011-10-05 11:55:39 +0200372 wl->scan_vif = vif;
Luciano Coelho08688d62010-07-08 17:50:07 +0300373 wl->scan.req = req;
Luciano Coelho4a31c112011-03-21 23:16:14 +0200374 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
Luciano Coelho08688d62010-07-08 17:50:07 +0300375
Juuso Oikarinen78abd322010-09-21 06:23:32 +0200376 /* we assume failure so that timeout scenarios are handled correctly */
377 wl->scan.failed = true;
378 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
379 msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
380
Eliad Peller784f6942011-10-05 11:55:39 +0200381 wl1271_scan_stm(wl, vif);
Luciano Coelho08688d62010-07-08 17:50:07 +0300382
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300383 return 0;
384}
Luciano Coelho95feadc2011-05-10 14:38:59 +0300385
Eliad Peller2aa01592011-06-27 13:06:44 +0300386int wl1271_scan_stop(struct wl1271 *wl)
387{
388 struct wl1271_cmd_header *cmd = NULL;
389 int ret = 0;
390
391 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
392 return -EINVAL;
393
394 wl1271_debug(DEBUG_CMD, "cmd scan stop");
395
396 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
397 if (!cmd) {
398 ret = -ENOMEM;
399 goto out;
400 }
401
402 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
403 sizeof(*cmd), 0);
404 if (ret < 0) {
405 wl1271_error("cmd stop_scan failed");
406 goto out;
407 }
408out:
409 kfree(cmd);
410 return ret;
411}
412
Luciano Coelho95feadc2011-05-10 14:38:59 +0300413static int
414wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
415 struct cfg80211_sched_scan_request *req,
416 struct conn_scan_ch_params *channels,
417 u32 band, bool radar, bool passive,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300418 int start, int max_channels)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300419{
420 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
421 int i, j;
422 u32 flags;
Luciano Coelho66870b12011-05-27 15:34:48 +0300423 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300424
425 for (i = 0, j = start;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300426 i < req->n_channels && j < max_channels;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300427 i++) {
428 flags = req->channels[i]->flags;
429
Luciano Coelho66870b12011-05-27 15:34:48 +0300430 if (force_passive)
431 flags |= IEEE80211_CHAN_PASSIVE_SCAN;
432
Luciano Coelho2497a242011-05-27 15:34:46 +0300433 if ((req->channels[i]->band == band) &&
434 !(flags & IEEE80211_CHAN_DISABLED) &&
Luciano Coelhodd086822011-05-27 15:34:45 +0300435 (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
Luciano Coelho2497a242011-05-27 15:34:46 +0300436 /* if radar is set, we ignore the passive flag */
437 (radar ||
438 !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300439 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
440 req->channels[i]->band,
441 req->channels[i]->center_freq);
442 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
443 req->channels[i]->hw_value,
444 req->channels[i]->flags);
445 wl1271_debug(DEBUG_SCAN, "max_power %d",
446 req->channels[i]->max_power);
447
Luciano Coelho50a66d72011-05-27 15:34:47 +0300448 if (flags & IEEE80211_CHAN_RADAR) {
Luciano Coelho2497a242011-05-27 15:34:46 +0300449 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
Eyal Shapirafea2a612011-12-20 12:04:01 +0200450
Luciano Coelho50a66d72011-05-27 15:34:47 +0300451 channels[j].passive_duration =
452 cpu_to_le16(c->dwell_time_dfs);
Eyal Shapirafea2a612011-12-20 12:04:01 +0200453 } else {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300454 channels[j].passive_duration =
455 cpu_to_le16(c->dwell_time_passive);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300456 }
Eyal Shapirafea2a612011-12-20 12:04:01 +0200457
458 channels[j].min_duration =
459 cpu_to_le16(c->min_dwell_time_active);
460 channels[j].max_duration =
461 cpu_to_le16(c->max_dwell_time_active);
462
Luciano Coelho2497a242011-05-27 15:34:46 +0300463 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300464 channels[j].channel = req->channels[i]->hw_value;
465
466 j++;
467 }
468 }
469
470 return j - start;
471}
472
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300473static bool
Luciano Coelho95feadc2011-05-10 14:38:59 +0300474wl1271_scan_sched_scan_channels(struct wl1271 *wl,
475 struct cfg80211_sched_scan_request *req,
476 struct wl1271_cmd_sched_scan_config *cfg)
477{
Luciano Coelho95feadc2011-05-10 14:38:59 +0300478 cfg->passive[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300479 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300480 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300481 false, true, 0,
482 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300483 cfg->active[0] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300484 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300485 IEEE80211_BAND_2GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300486 false, false,
487 cfg->passive[0],
488 MAX_CHANNELS_2GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300489 cfg->passive[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300490 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300491 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300492 false, true, 0,
493 MAX_CHANNELS_5GHZ);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300494 cfg->dfs =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300495 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300496 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300497 true, true,
498 cfg->passive[1],
499 MAX_CHANNELS_5GHZ);
Luciano Coelho2497a242011-05-27 15:34:46 +0300500 cfg->active[1] =
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300501 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
Luciano Coelho2497a242011-05-27 15:34:46 +0300502 IEEE80211_BAND_5GHZ,
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300503 false, false,
504 cfg->passive[1] + cfg->dfs,
505 MAX_CHANNELS_5GHZ);
506 /* 802.11j channels are not supported yet */
507 cfg->passive[2] = 0;
508 cfg->active[2] = 0;
Luciano Coelho2497a242011-05-27 15:34:46 +0300509
Luciano Coelho95feadc2011-05-10 14:38:59 +0300510 wl1271_debug(DEBUG_SCAN, " 2.4GHz: active %d passive %d",
511 cfg->active[0], cfg->passive[0]);
512 wl1271_debug(DEBUG_SCAN, " 5GHz: active %d passive %d",
513 cfg->active[1], cfg->passive[1]);
Luciano Coelho2497a242011-05-27 15:34:46 +0300514 wl1271_debug(DEBUG_SCAN, " DFS: %d", cfg->dfs);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300515
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300516 return cfg->passive[0] || cfg->active[0] ||
517 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
518 cfg->passive[2] || cfg->active[2];
Luciano Coelho95feadc2011-05-10 14:38:59 +0300519}
520
Luciano Coelhofb553772011-09-02 14:28:21 +0300521/* Returns the scan type to be used or a negative value on error */
Luciano Coelhof9520792011-08-23 18:34:44 +0300522static int
523wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
524 struct cfg80211_sched_scan_request *req)
525{
526 struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
Luciano Coelhofb553772011-09-02 14:28:21 +0300527 struct cfg80211_match_set *sets = req->match_sets;
528 struct cfg80211_ssid *ssids = req->ssids;
Luciano Coelho20a33e52011-09-02 14:28:23 +0300529 int ret = 0, type, i, j, n_match_ssids = 0;
Luciano Coelhof9520792011-08-23 18:34:44 +0300530
531 wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
532
Luciano Coelho20a33e52011-09-02 14:28:23 +0300533 /* count the match sets that contain SSIDs */
534 for (i = 0; i < req->n_match_sets; i++)
535 if (sets[i].ssid.ssid_len > 0)
536 n_match_ssids++;
537
Luciano Coelhofb553772011-09-02 14:28:21 +0300538 /* No filter, no ssids or only bcast ssid */
Luciano Coelho20a33e52011-09-02 14:28:23 +0300539 if (!n_match_ssids &&
Luciano Coelhofb553772011-09-02 14:28:21 +0300540 (!req->n_ssids ||
541 (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
542 type = SCAN_SSID_FILTER_ANY;
543 goto out;
544 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300545
Luciano Coelhofb553772011-09-02 14:28:21 +0300546 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
547 if (!cmd) {
548 ret = -ENOMEM;
549 goto out;
550 }
551
Luciano Coelho20a33e52011-09-02 14:28:23 +0300552 if (!n_match_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300553 /* No filter, with ssids */
554 type = SCAN_SSID_FILTER_DISABLED;
555
556 for (i = 0; i < req->n_ssids; i++) {
557 cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
558 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
559 cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
560 memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
561 ssids[i].ssid_len);
562 cmd->n_ssids++;
Luciano Coelhobd4932b2011-08-23 18:34:45 +0300563 }
Luciano Coelhofb553772011-09-02 14:28:21 +0300564 } else {
565 type = SCAN_SSID_FILTER_LIST;
566
567 /* Add all SSIDs from the filters */
568 for (i = 0; i < req->n_match_sets; i++) {
Luciano Coelho20a33e52011-09-02 14:28:23 +0300569 /* ignore sets without SSIDs */
570 if (!sets[i].ssid.ssid_len)
571 continue;
572
Luciano Coelhofb553772011-09-02 14:28:21 +0300573 cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
574 cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
575 memcpy(cmd->ssids[cmd->n_ssids].ssid,
576 sets[i].ssid.ssid, sets[i].ssid.ssid_len);
577 cmd->n_ssids++;
578 }
579 if ((req->n_ssids > 1) ||
580 (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
581 /*
582 * Mark all the SSIDs passed in the SSID list as HIDDEN,
583 * so they're used in probe requests.
584 */
585 for (i = 0; i < req->n_ssids; i++) {
Eyal Shapira1b04b732011-12-07 12:37:04 +0200586 if (!req->ssids[i].ssid_len)
587 continue;
588
Luciano Coelhofb553772011-09-02 14:28:21 +0300589 for (j = 0; j < cmd->n_ssids; j++)
590 if (!memcmp(req->ssids[i].ssid,
591 cmd->ssids[j].ssid,
592 req->ssids[i].ssid_len)) {
593 cmd->ssids[j].type =
594 SCAN_SSID_TYPE_HIDDEN;
595 break;
596 }
597 /* Fail if SSID isn't present in the filters */
Eyal Shapiracc438fc2011-11-08 15:54:46 +0200598 if (j == cmd->n_ssids) {
Luciano Coelhofb553772011-09-02 14:28:21 +0300599 ret = -EINVAL;
600 goto out_free;
601 }
602 }
603 }
Luciano Coelhof9520792011-08-23 18:34:44 +0300604 }
605
606 wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
607
608 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
609 sizeof(*cmd), 0);
610 if (ret < 0) {
611 wl1271_error("cmd sched scan ssid list failed");
Luciano Coelhofb553772011-09-02 14:28:21 +0300612 goto out_free;
Luciano Coelhof9520792011-08-23 18:34:44 +0300613 }
614
Luciano Coelhofb553772011-09-02 14:28:21 +0300615out_free:
Luciano Coelhof9520792011-08-23 18:34:44 +0300616 kfree(cmd);
Luciano Coelhofb553772011-09-02 14:28:21 +0300617out:
618 if (ret < 0)
619 return ret;
620 return type;
Luciano Coelhof9520792011-08-23 18:34:44 +0300621}
622
Luciano Coelho95feadc2011-05-10 14:38:59 +0300623int wl1271_scan_sched_scan_config(struct wl1271 *wl,
Eliad Peller536129c2011-10-05 11:55:45 +0200624 struct wl12xx_vif *wlvif,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300625 struct cfg80211_sched_scan_request *req,
626 struct ieee80211_sched_scan_ies *ies)
627{
628 struct wl1271_cmd_sched_scan_config *cfg = NULL;
629 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300630 int i, ret;
Luciano Coelho66870b12011-05-27 15:34:48 +0300631 bool force_passive = !req->n_ssids;
Luciano Coelho95feadc2011-05-10 14:38:59 +0300632
633 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
634
635 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
636 if (!cfg)
637 return -ENOMEM;
638
639 cfg->rssi_threshold = c->rssi_threshold;
640 cfg->snr_threshold = c->snr_threshold;
641 cfg->n_probe_reqs = c->num_probe_reqs;
642 /* cycles set to 0 it means infinite (until manually stopped) */
643 cfg->cycles = 0;
644 /* report APs when at least 1 is found */
645 cfg->report_after = 1;
646 /* don't stop scanning automatically when something is found */
647 cfg->terminate = 0;
648 cfg->tag = WL1271_SCAN_DEFAULT_TAG;
649 /* don't filter on BSS type */
650 cfg->bss_type = SCAN_BSS_TYPE_ANY;
651 /* currently NL80211 supports only a single interval */
652 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
653 cfg->intervals[i] = cpu_to_le32(req->interval);
654
Luciano Coelhof9520792011-08-23 18:34:44 +0300655 cfg->ssid_len = 0;
Luciano Coelhofb553772011-09-02 14:28:21 +0300656 ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
657 if (ret < 0)
658 goto out;
659
660 cfg->filter_type = ret;
661
662 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300663
Luciano Coelhod2c2bb9f2011-05-31 16:38:56 +0300664 if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300665 wl1271_error("scan channel list is empty");
666 ret = -EINVAL;
667 goto out;
668 }
669
Luciano Coelho66870b12011-05-27 15:34:48 +0300670 if (!force_passive && cfg->active[0]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200671 u8 band = IEEE80211_BAND_2GHZ;
672 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200673 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200674 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300675 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200676 ies->ie[band],
677 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300678 if (ret < 0) {
679 wl1271_error("2.4GHz PROBE request template failed");
680 goto out;
681 }
682 }
683
Luciano Coelho66870b12011-05-27 15:34:48 +0300684 if (!force_passive && cfg->active[1]) {
Eliad Pellercdaac622012-01-31 11:57:16 +0200685 u8 band = IEEE80211_BAND_5GHZ;
686 ret = wl12xx_cmd_build_probe_req(wl, wlvif,
Eliad Pellerc059beb2012-01-31 11:57:17 +0200687 wlvif->dev_role_id, band,
Eliad Pellercdaac622012-01-31 11:57:16 +0200688 req->ssids[0].ssid,
Luciano Coelho95feadc2011-05-10 14:38:59 +0300689 req->ssids[0].ssid_len,
Eliad Pellercdaac622012-01-31 11:57:16 +0200690 ies->ie[band],
691 ies->len[band]);
Luciano Coelho95feadc2011-05-10 14:38:59 +0300692 if (ret < 0) {
693 wl1271_error("5GHz PROBE request template failed");
694 goto out;
695 }
696 }
697
698 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
699
700 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
701 sizeof(*cfg), 0);
702 if (ret < 0) {
703 wl1271_error("SCAN configuration failed");
704 goto out;
705 }
706out:
707 kfree(cfg);
708 return ret;
709}
710
Eliad Peller536129c2011-10-05 11:55:45 +0200711int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelho95feadc2011-05-10 14:38:59 +0300712{
713 struct wl1271_cmd_sched_scan_start *start;
714 int ret = 0;
715
716 wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
717
Eliad Peller536129c2011-10-05 11:55:45 +0200718 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
Luciano Coelho683c0022011-05-12 17:07:55 +0300719 return -EOPNOTSUPP;
720
Eliad Pellera0c7b782011-12-18 20:25:41 +0200721 if (test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
Luciano Coelho683c0022011-05-12 17:07:55 +0300722 return -EBUSY;
723
Luciano Coelho95feadc2011-05-10 14:38:59 +0300724 start = kzalloc(sizeof(*start), GFP_KERNEL);
725 if (!start)
726 return -ENOMEM;
727
728 start->tag = WL1271_SCAN_DEFAULT_TAG;
729
730 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
731 sizeof(*start), 0);
732 if (ret < 0) {
733 wl1271_error("failed to send scan start command");
734 goto out_free;
735 }
736
737out_free:
738 kfree(start);
739 return ret;
740}
741
742void wl1271_scan_sched_scan_results(struct wl1271 *wl)
743{
744 wl1271_debug(DEBUG_SCAN, "got periodic scan results");
745
746 ieee80211_sched_scan_results(wl->hw);
747}
748
749void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
750{
751 struct wl1271_cmd_sched_scan_stop *stop;
752 int ret = 0;
753
754 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
755
756 /* FIXME: what to do if alloc'ing to stop fails? */
757 stop = kzalloc(sizeof(*stop), GFP_KERNEL);
758 if (!stop) {
759 wl1271_error("failed to alloc memory to send sched scan stop");
760 return;
761 }
762
763 stop->tag = WL1271_SCAN_DEFAULT_TAG;
764
765 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
766 sizeof(*stop), 0);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300767 if (ret < 0) {
Luciano Coelho95feadc2011-05-10 14:38:59 +0300768 wl1271_error("failed to send sched scan stop command");
Luciano Coelho33c2c062011-05-10 14:46:02 +0300769 goto out_free;
770 }
Luciano Coelho95feadc2011-05-10 14:38:59 +0300771
Luciano Coelho33c2c062011-05-10 14:46:02 +0300772out_free:
Luciano Coelho95feadc2011-05-10 14:38:59 +0300773 kfree(stop);
774}