wlcore: update events enum/struct to new fw api
[cascardo/linux.git] / drivers / net / wireless / ti / wlcore / 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 "wlcore.h"
27 #include "debug.h"
28 #include "cmd.h"
29 #include "scan.h"
30 #include "acx.h"
31 #include "ps.h"
32 #include "tx.h"
33
34 void wl1271_scan_complete_work(struct work_struct *work)
35 {
36         struct delayed_work *dwork;
37         struct wl1271 *wl;
38         struct wl12xx_vif *wlvif;
39         int ret;
40
41         dwork = container_of(work, struct delayed_work, work);
42         wl = container_of(dwork, struct wl1271, scan_complete_work);
43
44         wl1271_debug(DEBUG_SCAN, "Scanning complete");
45
46         mutex_lock(&wl->mutex);
47
48         if (unlikely(wl->state != WLCORE_STATE_ON))
49                 goto out;
50
51         if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
52                 goto out;
53
54         wlvif = wl->scan_wlvif;
55
56         /*
57          * Rearm the tx watchdog just before idling scan. This
58          * prevents just-finished scans from triggering the watchdog
59          */
60         wl12xx_rearm_tx_watchdog_locked(wl);
61
62         wl->scan.state = WL1271_SCAN_STATE_IDLE;
63         memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
64         wl->scan.req = NULL;
65         wl->scan_wlvif = NULL;
66
67         ret = wl1271_ps_elp_wakeup(wl);
68         if (ret < 0)
69                 goto out;
70
71         if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
72                 /* restore hardware connection monitoring template */
73                 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
74         }
75
76         wl1271_ps_elp_sleep(wl);
77
78         if (wl->scan.failed) {
79                 wl1271_info("Scan completed due to error.");
80                 wl12xx_queue_recovery_work(wl);
81         }
82
83         ieee80211_scan_completed(wl->hw, false);
84
85 out:
86         mutex_unlock(&wl->mutex);
87
88 }
89
90 static int
91 wlcore_scan_get_channels(struct wl1271 *wl,
92                          struct ieee80211_channel *req_channels[],
93                          u32 n_channels,
94                          u32 n_ssids,
95                          struct conn_scan_ch_params *channels,
96                          u32 band, bool radar, bool passive,
97                          int start, int max_channels,
98                          u8 *n_pactive_ch)
99 {
100         struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
101         int i, j;
102         u32 flags;
103         bool force_passive = !n_ssids;
104         u32 min_dwell_time_active, max_dwell_time_active, delta_per_probe;
105         u32 dwell_time_passive, dwell_time_dfs;
106
107         if (band == IEEE80211_BAND_5GHZ)
108                 delta_per_probe = c->dwell_time_delta_per_probe_5;
109         else
110                 delta_per_probe = c->dwell_time_delta_per_probe;
111
112         min_dwell_time_active = c->base_dwell_time +
113                  n_ssids * c->num_probe_reqs * delta_per_probe;
114
115         max_dwell_time_active = min_dwell_time_active + c->max_dwell_time_delta;
116
117         min_dwell_time_active = DIV_ROUND_UP(min_dwell_time_active, 1000);
118         max_dwell_time_active = DIV_ROUND_UP(max_dwell_time_active, 1000);
119         dwell_time_passive = DIV_ROUND_UP(c->dwell_time_passive, 1000);
120         dwell_time_dfs = DIV_ROUND_UP(c->dwell_time_dfs, 1000);
121
122         for (i = 0, j = start;
123              i < n_channels && j < max_channels;
124              i++) {
125                 flags = req_channels[i]->flags;
126
127                 if (force_passive)
128                         flags |= IEEE80211_CHAN_PASSIVE_SCAN;
129
130                 if ((req_channels[i]->band == band) &&
131                     !(flags & IEEE80211_CHAN_DISABLED) &&
132                     (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
133                     /* if radar is set, we ignore the passive flag */
134                     (radar ||
135                      !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
136                         wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
137                                      req_channels[i]->band,
138                                      req_channels[i]->center_freq);
139                         wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
140                                      req_channels[i]->hw_value,
141                                      req_channels[i]->flags);
142                         wl1271_debug(DEBUG_SCAN, "max_power %d",
143                                      req_channels[i]->max_power);
144                         wl1271_debug(DEBUG_SCAN, "min_dwell_time %d max dwell time %d",
145                                      min_dwell_time_active,
146                                      max_dwell_time_active);
147
148                         if (flags & IEEE80211_CHAN_RADAR) {
149                                 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
150
151                                 channels[j].passive_duration =
152                                         cpu_to_le16(dwell_time_dfs);
153                         } else {
154                                 channels[j].passive_duration =
155                                         cpu_to_le16(dwell_time_passive);
156                         }
157
158                         channels[j].min_duration =
159                                 cpu_to_le16(min_dwell_time_active);
160                         channels[j].max_duration =
161                                 cpu_to_le16(max_dwell_time_active);
162
163                         channels[j].tx_power_att = req_channels[i]->max_power;
164                         channels[j].channel = req_channels[i]->hw_value;
165
166                         if (n_pactive_ch &&
167                             (band == IEEE80211_BAND_2GHZ) &&
168                             (channels[j].channel >= 12) &&
169                             (channels[j].channel <= 14) &&
170                             (flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
171                             !force_passive) {
172                                 /* pactive channels treated as DFS */
173                                 channels[j].flags = SCAN_CHANNEL_FLAGS_DFS;
174
175                                 /*
176                                  * n_pactive_ch is counted down from the end of
177                                  * the passive channel list
178                                  */
179                                 (*n_pactive_ch)++;
180                                 wl1271_debug(DEBUG_SCAN, "n_pactive_ch = %d",
181                                              *n_pactive_ch);
182                         }
183
184                         j++;
185                 }
186         }
187
188         return j - start;
189 }
190
191 bool
192 wlcore_set_scan_chan_params(struct wl1271 *wl,
193                             struct wlcore_scan_channels *cfg,
194                             struct ieee80211_channel *channels[],
195                             u32 n_channels,
196                             u32 n_ssids)
197 {
198         u8 n_pactive_ch = 0;
199
200         cfg->passive[0] =
201                 wlcore_scan_get_channels(wl,
202                                          channels,
203                                          n_channels,
204                                          n_ssids,
205                                          cfg->channels_2,
206                                          IEEE80211_BAND_2GHZ,
207                                          false, true, 0,
208                                          MAX_CHANNELS_2GHZ,
209                                          &n_pactive_ch);
210         cfg->active[0] =
211                 wlcore_scan_get_channels(wl,
212                                          channels,
213                                          n_channels,
214                                          n_ssids,
215                                          cfg->channels_2,
216                                          IEEE80211_BAND_2GHZ,
217                                          false, false,
218                                          cfg->passive[0],
219                                          MAX_CHANNELS_2GHZ,
220                                          &n_pactive_ch);
221         cfg->passive[1] =
222                 wlcore_scan_get_channels(wl,
223                                          channels,
224                                          n_channels,
225                                          n_ssids,
226                                          cfg->channels_5,
227                                          IEEE80211_BAND_5GHZ,
228                                          false, true, 0,
229                                          wl->max_channels_5,
230                                          &n_pactive_ch);
231         cfg->dfs =
232                 wlcore_scan_get_channels(wl,
233                                          channels,
234                                          n_channels,
235                                          n_ssids,
236                                          cfg->channels_5,
237                                          IEEE80211_BAND_5GHZ,
238                                          true, true,
239                                          cfg->passive[1],
240                                          wl->max_channels_5,
241                                          &n_pactive_ch);
242         cfg->active[1] =
243                 wlcore_scan_get_channels(wl,
244                                          channels,
245                                          n_channels,
246                                          n_ssids,
247                                          cfg->channels_5,
248                                          IEEE80211_BAND_5GHZ,
249                                          false, false,
250                                          cfg->passive[1] + cfg->dfs,
251                                          wl->max_channels_5,
252                                          &n_pactive_ch);
253
254         /* 802.11j channels are not supported yet */
255         cfg->passive[2] = 0;
256         cfg->active[2] = 0;
257
258         cfg->passive_active = n_pactive_ch;
259
260         wl1271_debug(DEBUG_SCAN, "    2.4GHz: active %d passive %d",
261                      cfg->active[0], cfg->passive[0]);
262         wl1271_debug(DEBUG_SCAN, "    5GHz: active %d passive %d",
263                      cfg->active[1], cfg->passive[1]);
264         wl1271_debug(DEBUG_SCAN, "    DFS: %d", cfg->dfs);
265
266         return  cfg->passive[0] || cfg->active[0] ||
267                 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
268                 cfg->passive[2] || cfg->active[2];
269 }
270 EXPORT_SYMBOL_GPL(wlcore_set_scan_chan_params);
271
272 int wlcore_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
273                 const u8 *ssid, size_t ssid_len,
274                 struct cfg80211_scan_request *req)
275 {
276         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
277
278         /*
279          * cfg80211 should guarantee that we don't get more channels
280          * than what we have registered.
281          */
282         BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
283
284         if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
285                 return -EBUSY;
286
287         wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
288
289         if (ssid_len && ssid) {
290                 wl->scan.ssid_len = ssid_len;
291                 memcpy(wl->scan.ssid, ssid, ssid_len);
292         } else {
293                 wl->scan.ssid_len = 0;
294         }
295
296         wl->scan_wlvif = wlvif;
297         wl->scan.req = req;
298         memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
299
300         /* we assume failure so that timeout scenarios are handled correctly */
301         wl->scan.failed = true;
302         ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
303                                      msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
304
305         wl->ops->scan_start(wl, wlvif, req);
306
307         return 0;
308 }
309 /* Returns the scan type to be used or a negative value on error */
310 int
311 wlcore_scan_sched_scan_ssid_list(struct wl1271 *wl,
312                                  struct wl12xx_vif *wlvif,
313                                  struct cfg80211_sched_scan_request *req)
314 {
315         struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
316         struct cfg80211_match_set *sets = req->match_sets;
317         struct cfg80211_ssid *ssids = req->ssids;
318         int ret = 0, type, i, j, n_match_ssids = 0;
319
320         wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
321
322         /* count the match sets that contain SSIDs */
323         for (i = 0; i < req->n_match_sets; i++)
324                 if (sets[i].ssid.ssid_len > 0)
325                         n_match_ssids++;
326
327         /* No filter, no ssids or only bcast ssid */
328         if (!n_match_ssids &&
329             (!req->n_ssids ||
330              (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
331                 type = SCAN_SSID_FILTER_ANY;
332                 goto out;
333         }
334
335         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
336         if (!cmd) {
337                 ret = -ENOMEM;
338                 goto out;
339         }
340
341         cmd->role_id = wlvif->role_id;
342         if (!n_match_ssids) {
343                 /* No filter, with ssids */
344                 type = SCAN_SSID_FILTER_DISABLED;
345
346                 for (i = 0; i < req->n_ssids; i++) {
347                         cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
348                                 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
349                         cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
350                         memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
351                                ssids[i].ssid_len);
352                         cmd->n_ssids++;
353                 }
354         } else {
355                 type = SCAN_SSID_FILTER_LIST;
356
357                 /* Add all SSIDs from the filters */
358                 for (i = 0; i < req->n_match_sets; i++) {
359                         /* ignore sets without SSIDs */
360                         if (!sets[i].ssid.ssid_len)
361                                 continue;
362
363                         cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
364                         cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
365                         memcpy(cmd->ssids[cmd->n_ssids].ssid,
366                                sets[i].ssid.ssid, sets[i].ssid.ssid_len);
367                         cmd->n_ssids++;
368                 }
369                 if ((req->n_ssids > 1) ||
370                     (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
371                         /*
372                          * Mark all the SSIDs passed in the SSID list as HIDDEN,
373                          * so they're used in probe requests.
374                          */
375                         for (i = 0; i < req->n_ssids; i++) {
376                                 if (!req->ssids[i].ssid_len)
377                                         continue;
378
379                                 for (j = 0; j < cmd->n_ssids; j++)
380                                         if ((req->ssids[i].ssid_len ==
381                                              cmd->ssids[j].len) &&
382                                             !memcmp(req->ssids[i].ssid,
383                                                    cmd->ssids[j].ssid,
384                                                    req->ssids[i].ssid_len)) {
385                                                 cmd->ssids[j].type =
386                                                         SCAN_SSID_TYPE_HIDDEN;
387                                                 break;
388                                         }
389                                 /* Fail if SSID isn't present in the filters */
390                                 if (j == cmd->n_ssids) {
391                                         ret = -EINVAL;
392                                         goto out_free;
393                                 }
394                         }
395                 }
396         }
397
398         wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
399
400         ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
401                               sizeof(*cmd), 0);
402         if (ret < 0) {
403                 wl1271_error("cmd sched scan ssid list failed");
404                 goto out_free;
405         }
406
407 out_free:
408         kfree(cmd);
409 out:
410         if (ret < 0)
411                 return ret;
412         return type;
413 }
414 EXPORT_SYMBOL_GPL(wlcore_scan_sched_scan_ssid_list);
415
416 void wl1271_scan_sched_scan_results(struct wl1271 *wl)
417 {
418         wl1271_debug(DEBUG_SCAN, "got periodic scan results");
419
420         ieee80211_sched_scan_results(wl->hw);
421 }