blob: 9f1da82ed8d61e11141c05c6dad430b4a9e759fa [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
26#include "wl1271.h"
27#include "wl1271_cmd.h"
28#include "wl1271_scan.h"
29#include "wl1271_acx.h"
30
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +030031void wl1271_scan_complete_work(struct work_struct *work)
32{
33 struct wl1271 *wl =
34 container_of(work, struct wl1271, scan_complete_work);
35
36 wl1271_debug(DEBUG_SCAN, "Scanning complete");
37
38 mutex_lock(&wl->mutex);
39 wl->scan.state = WL1271_SCAN_STATE_IDLE;
40 kfree(wl->scan.scanned_ch);
41 wl->scan.scanned_ch = NULL;
42 mutex_unlock(&wl->mutex);
43
44 ieee80211_scan_completed(wl->hw, false);
45}
46
47
Luciano Coelho08688d62010-07-08 17:50:07 +030048static int wl1271_get_scan_channels(struct wl1271 *wl,
49 struct cfg80211_scan_request *req,
50 struct basic_scan_channel_params *channels,
51 enum ieee80211_band band, bool passive)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030052{
Luciano Coelho08688d62010-07-08 17:50:07 +030053 int i, j;
54 u32 flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030055
Luciano Coelho08688d62010-07-08 17:50:07 +030056 for (i = 0, j = 0;
57 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
58 i++) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030059
Luciano Coelho08688d62010-07-08 17:50:07 +030060 flags = req->channels[i]->flags;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030061
Luciano Coelho08688d62010-07-08 17:50:07 +030062 if (!wl->scan.scanned_ch[i] &&
63 !(flags & IEEE80211_CHAN_DISABLED) &&
64 ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
65 (req->channels[i]->band == band)) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030066
Luciano Coelho08688d62010-07-08 17:50:07 +030067 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
68 req->channels[i]->band,
69 req->channels[i]->center_freq);
70 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
71 req->channels[i]->hw_value,
72 req->channels[i]->flags);
73 wl1271_debug(DEBUG_SCAN,
74 "max_antenna_gain %d, max_power %d",
75 req->channels[i]->max_antenna_gain,
76 req->channels[i]->max_power);
77 wl1271_debug(DEBUG_SCAN, "beacon_found %d",
78 req->channels[i]->beacon_found);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030079
Luciano Coelho08688d62010-07-08 17:50:07 +030080 channels[j].min_duration =
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030081 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
Luciano Coelho08688d62010-07-08 17:50:07 +030082 channels[j].max_duration =
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030083 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
Luciano Coelho08688d62010-07-08 17:50:07 +030084 channels[j].early_termination = 0;
Luciano Coelho3cc7b542010-07-08 17:50:08 +030085 channels[j].tx_power_att = req->channels[i]->max_power;
Luciano Coelho08688d62010-07-08 17:50:07 +030086 channels[j].channel = req->channels[i]->hw_value;
87
88 memset(&channels[j].bssid_lsb, 0xff, 4);
89 memset(&channels[j].bssid_msb, 0xff, 2);
90
91 /* Mark the channels we already used */
92 wl->scan.scanned_ch[i] = true;
93
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030094 j++;
95 }
96 }
97
Luciano Coelho08688d62010-07-08 17:50:07 +030098 return j;
99}
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300100
Luciano Coelho08688d62010-07-08 17:50:07 +0300101#define WL1271_NOTHING_TO_SCAN 1
102
103static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
104 bool passive, u32 basic_rate)
105{
106 struct wl1271_cmd_scan *cmd;
107 struct wl1271_cmd_trigger_scan_to *trigger;
108 int ret;
109 u16 scan_options = 0;
110
111 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
112 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
113 if (!cmd || !trigger) {
114 ret = -ENOMEM;
115 goto out;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300116 }
117
Luciano Coelho08688d62010-07-08 17:50:07 +0300118 /* We always use high priority scans */
119 scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300120
121 /* No SSIDs means that we have a forced passive scan */
122 if (passive || wl->scan.req->n_ssids == 0)
Luciano Coelho08688d62010-07-08 17:50:07 +0300123 scan_options |= WL1271_SCAN_OPT_PASSIVE;
Luciano Coelho4f35c022010-08-04 04:36:32 +0300124
Luciano Coelho08688d62010-07-08 17:50:07 +0300125 cmd->params.scan_options = cpu_to_le16(scan_options);
126
127 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
128 cmd->channels,
129 band, passive);
130 if (cmd->params.n_ch == 0) {
131 ret = WL1271_NOTHING_TO_SCAN;
132 goto out;
133 }
134
135 cmd->params.tx_rate = cpu_to_le32(basic_rate);
136 cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
137 cmd->params.rx_filter_options =
138 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
139
140 cmd->params.n_probe_reqs = WL1271_SCAN_PROBE_REQS;
141 cmd->params.tx_rate = cpu_to_le32(basic_rate);
142 cmd->params.tid_trigger = 0;
143 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
144
145 if (band == IEEE80211_BAND_2GHZ)
146 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
147 else
148 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
149
150 if (wl->scan.ssid_len && wl->scan.ssid) {
151 cmd->params.ssid_len = wl->scan.ssid_len;
152 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
153 }
154
155 ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
156 wl->scan.req->ie, wl->scan.req->ie_len,
157 band);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300158 if (ret < 0) {
159 wl1271_error("PROBE request template failed");
160 goto out;
161 }
162
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300163 /* disable the timeout */
164 trigger->timeout = 0;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300165 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
166 sizeof(*trigger), 0);
167 if (ret < 0) {
168 wl1271_error("trigger scan to failed for hw scan");
169 goto out;
170 }
171
Luciano Coelho08688d62010-07-08 17:50:07 +0300172 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300173
Luciano Coelho08688d62010-07-08 17:50:07 +0300174 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300175 if (ret < 0) {
176 wl1271_error("SCAN failed");
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300177 goto out;
178 }
179
180out:
Luciano Coelho08688d62010-07-08 17:50:07 +0300181 kfree(cmd);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300182 kfree(trigger);
183 return ret;
184}
185
Luciano Coelho08688d62010-07-08 17:50:07 +0300186void wl1271_scan_stm(struct wl1271 *wl)
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300187{
Luciano Coelho08688d62010-07-08 17:50:07 +0300188 int ret;
189
190 switch (wl->scan.state) {
191 case WL1271_SCAN_STATE_IDLE:
192 break;
193
194 case WL1271_SCAN_STATE_2GHZ_ACTIVE:
195 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
196 wl->conf.tx.basic_rate);
197 if (ret == WL1271_NOTHING_TO_SCAN) {
198 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
199 wl1271_scan_stm(wl);
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300200 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300201
202 break;
203
204 case WL1271_SCAN_STATE_2GHZ_PASSIVE:
205 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
206 wl->conf.tx.basic_rate);
207 if (ret == WL1271_NOTHING_TO_SCAN) {
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200208 if (wl->enable_11a)
Luciano Coelho08688d62010-07-08 17:50:07 +0300209 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
210 else
211 wl->scan.state = WL1271_SCAN_STATE_DONE;
212 wl1271_scan_stm(wl);
213 }
214
215 break;
216
217 case WL1271_SCAN_STATE_5GHZ_ACTIVE:
218 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
219 wl->conf.tx.basic_rate_5);
220 if (ret == WL1271_NOTHING_TO_SCAN) {
221 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
222 wl1271_scan_stm(wl);
223 }
224
225 break;
226
227 case WL1271_SCAN_STATE_5GHZ_PASSIVE:
228 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
229 wl->conf.tx.basic_rate_5);
230 if (ret == WL1271_NOTHING_TO_SCAN) {
231 wl->scan.state = WL1271_SCAN_STATE_DONE;
232 wl1271_scan_stm(wl);
233 }
234
235 break;
236
237 case WL1271_SCAN_STATE_DONE:
Juuso Oikarinenc454f1d2010-08-24 06:28:03 +0300238 ieee80211_queue_work(wl->hw, &wl->scan_complete_work);
Luciano Coelho08688d62010-07-08 17:50:07 +0300239 break;
240
241 default:
242 wl1271_error("invalid scan state");
243 break;
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300244 }
Luciano Coelho08688d62010-07-08 17:50:07 +0300245}
246
247int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
248 struct cfg80211_scan_request *req)
249{
250 if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
251 return -EBUSY;
252
253 wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
254
255 if (ssid_len && ssid) {
256 wl->scan.ssid_len = ssid_len;
257 memcpy(wl->scan.ssid, ssid, ssid_len);
258 } else {
259 wl->scan.ssid_len = 0;
260 }
261
262 wl->scan.req = req;
263
264 wl->scan.scanned_ch = kzalloc(req->n_channels *
265 sizeof(*wl->scan.scanned_ch),
266 GFP_KERNEL);
267 wl1271_scan_stm(wl);
268
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300269 return 0;
270}