909bb47995b6907ee8afad05c3acf37500043d4a
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_scan.c
1 /*
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
31 void wl1271_scan_complete_work(struct work_struct *work)
32 {
33         struct delayed_work *dwork;
34         struct wl1271 *wl;
35
36         dwork = container_of(work, struct delayed_work, work);
37         wl = container_of(dwork, struct wl1271, scan_complete_work);
38
39         wl1271_debug(DEBUG_SCAN, "Scanning complete");
40
41         mutex_lock(&wl->mutex);
42
43         if (wl->scan.state == WL1271_SCAN_STATE_IDLE) {
44                 mutex_unlock(&wl->mutex);
45                 return;
46         }
47
48         wl->scan.state = WL1271_SCAN_STATE_IDLE;
49         kfree(wl->scan.scanned_ch);
50         wl->scan.scanned_ch = NULL;
51         mutex_unlock(&wl->mutex);
52
53         ieee80211_scan_completed(wl->hw, false);
54
55         if (wl->scan.failed) {
56                 wl1271_info("Scan completed due to error.");
57                 ieee80211_queue_work(wl->hw, &wl->recovery_work);
58         }
59 }
60
61
62 static int wl1271_get_scan_channels(struct wl1271 *wl,
63                                     struct cfg80211_scan_request *req,
64                                     struct basic_scan_channel_params *channels,
65                                     enum ieee80211_band band, bool passive)
66 {
67         struct conf_scan_settings *c = &wl->conf.scan;
68         int i, j;
69         u32 flags;
70
71         for (i = 0, j = 0;
72              i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
73              i++) {
74
75                 flags = req->channels[i]->flags;
76
77                 if (!wl->scan.scanned_ch[i] &&
78                     !(flags & IEEE80211_CHAN_DISABLED) &&
79                     ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
80                     (req->channels[i]->band == band)) {
81
82                         wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
83                                      req->channels[i]->band,
84                                      req->channels[i]->center_freq);
85                         wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
86                                      req->channels[i]->hw_value,
87                                      req->channels[i]->flags);
88                         wl1271_debug(DEBUG_SCAN,
89                                      "max_antenna_gain %d, max_power %d",
90                                      req->channels[i]->max_antenna_gain,
91                                      req->channels[i]->max_power);
92                         wl1271_debug(DEBUG_SCAN, "beacon_found %d",
93                                      req->channels[i]->beacon_found);
94
95                         if (!passive) {
96                                 channels[j].min_duration =
97                                         cpu_to_le32(c->min_dwell_time_active);
98                                 channels[j].max_duration =
99                                         cpu_to_le32(c->max_dwell_time_active);
100                         } else {
101                                 channels[j].min_duration =
102                                         cpu_to_le32(c->min_dwell_time_passive);
103                                 channels[j].max_duration =
104                                         cpu_to_le32(c->max_dwell_time_passive);
105                         }
106                         channels[j].early_termination = 0;
107                         channels[j].tx_power_att = req->channels[i]->max_power;
108                         channels[j].channel = req->channels[i]->hw_value;
109
110                         memset(&channels[j].bssid_lsb, 0xff, 4);
111                         memset(&channels[j].bssid_msb, 0xff, 2);
112
113                         /* Mark the channels we already used */
114                         wl->scan.scanned_ch[i] = true;
115
116                         j++;
117                 }
118         }
119
120         return j;
121 }
122
123 #define WL1271_NOTHING_TO_SCAN 1
124
125 static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
126                              bool passive, u32 basic_rate)
127 {
128         struct wl1271_cmd_scan *cmd;
129         struct wl1271_cmd_trigger_scan_to *trigger;
130         int ret;
131         u16 scan_options = 0;
132
133         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
134         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
135         if (!cmd || !trigger) {
136                 ret = -ENOMEM;
137                 goto out;
138         }
139
140         /* We always use high priority scans */
141         scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
142
143         /* No SSIDs means that we have a forced passive scan */
144         if (passive || wl->scan.req->n_ssids == 0)
145                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
146
147         cmd->params.scan_options = cpu_to_le16(scan_options);
148
149         cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
150                                                     cmd->channels,
151                                                     band, passive);
152         if (cmd->params.n_ch == 0) {
153                 ret = WL1271_NOTHING_TO_SCAN;
154                 goto out;
155         }
156
157         cmd->params.tx_rate = cpu_to_le32(basic_rate);
158         cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
159         cmd->params.rx_filter_options =
160                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
161
162         cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
163         cmd->params.tx_rate = cpu_to_le32(basic_rate);
164         cmd->params.tid_trigger = 0;
165         cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
166
167         if (band == IEEE80211_BAND_2GHZ)
168                 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
169         else
170                 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
171
172         if (wl->scan.ssid_len && wl->scan.ssid) {
173                 cmd->params.ssid_len = wl->scan.ssid_len;
174                 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
175         }
176
177         ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
178                                          wl->scan.req->ie, wl->scan.req->ie_len,
179                                          band);
180         if (ret < 0) {
181                 wl1271_error("PROBE request template failed");
182                 goto out;
183         }
184
185         /* disable the timeout */
186         trigger->timeout = 0;
187         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
188                               sizeof(*trigger), 0);
189         if (ret < 0) {
190                 wl1271_error("trigger scan to failed for hw scan");
191                 goto out;
192         }
193
194         wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
195
196         ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
197         if (ret < 0) {
198                 wl1271_error("SCAN failed");
199                 goto out;
200         }
201
202 out:
203         kfree(cmd);
204         kfree(trigger);
205         return ret;
206 }
207
208 void wl1271_scan_stm(struct wl1271 *wl)
209 {
210         int ret = 0;
211
212         switch (wl->scan.state) {
213         case WL1271_SCAN_STATE_IDLE:
214                 break;
215
216         case WL1271_SCAN_STATE_2GHZ_ACTIVE:
217                 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
218                                        wl->conf.tx.basic_rate);
219                 if (ret == WL1271_NOTHING_TO_SCAN) {
220                         wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
221                         wl1271_scan_stm(wl);
222                 }
223
224                 break;
225
226         case WL1271_SCAN_STATE_2GHZ_PASSIVE:
227                 ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
228                                        wl->conf.tx.basic_rate);
229                 if (ret == WL1271_NOTHING_TO_SCAN) {
230                         if (wl->enable_11a)
231                                 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
232                         else
233                                 wl->scan.state = WL1271_SCAN_STATE_DONE;
234                         wl1271_scan_stm(wl);
235                 }
236
237                 break;
238
239         case WL1271_SCAN_STATE_5GHZ_ACTIVE:
240                 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
241                                        wl->conf.tx.basic_rate_5);
242                 if (ret == WL1271_NOTHING_TO_SCAN) {
243                         wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
244                         wl1271_scan_stm(wl);
245                 }
246
247                 break;
248
249         case WL1271_SCAN_STATE_5GHZ_PASSIVE:
250                 ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
251                                        wl->conf.tx.basic_rate_5);
252                 if (ret == WL1271_NOTHING_TO_SCAN) {
253                         wl->scan.state = WL1271_SCAN_STATE_DONE;
254                         wl1271_scan_stm(wl);
255                 }
256
257                 break;
258
259         case WL1271_SCAN_STATE_DONE:
260                 wl->scan.failed = false;
261                 cancel_delayed_work(&wl->scan_complete_work);
262                 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
263                                              msecs_to_jiffies(0));
264                 break;
265
266         default:
267                 wl1271_error("invalid scan state");
268                 break;
269         }
270
271         if (ret < 0) {
272                 cancel_delayed_work(&wl->scan_complete_work);
273                 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
274                                              msecs_to_jiffies(0));
275         }
276 }
277
278 int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
279                 struct cfg80211_scan_request *req)
280 {
281         if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
282                 return -EBUSY;
283
284         wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
285
286         if (ssid_len && ssid) {
287                 wl->scan.ssid_len = ssid_len;
288                 memcpy(wl->scan.ssid, ssid, ssid_len);
289         } else {
290                 wl->scan.ssid_len = 0;
291         }
292
293         wl->scan.req = req;
294
295         wl->scan.scanned_ch = kcalloc(req->n_channels,
296                                       sizeof(*wl->scan.scanned_ch),
297                                       GFP_KERNEL);
298         /* we assume failure so that timeout scenarios are handled correctly */
299         wl->scan.failed = true;
300         ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
301                                      msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
302
303         wl1271_scan_stm(wl);
304
305         return 0;
306 }