]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ti/wlcore/scan.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless-next
[karo-tx-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         wlcore_cmd_regdomain_config_locked(wl);
84
85         ieee80211_scan_completed(wl->hw, false);
86
87 out:
88         mutex_unlock(&wl->mutex);
89
90 }
91
92 static void wlcore_started_vifs_iter(void *data, u8 *mac,
93                                      struct ieee80211_vif *vif)
94 {
95         int *count = (int *)data;
96
97         if (!vif->bss_conf.idle)
98                 (*count)++;
99 }
100
101 static int wlcore_count_started_vifs(struct wl1271 *wl)
102 {
103         int count = 0;
104
105         ieee80211_iterate_active_interfaces_atomic(wl->hw,
106                                         IEEE80211_IFACE_ITER_RESUME_ALL,
107                                         wlcore_started_vifs_iter, &count);
108         return count;
109 }
110
111 static int
112 wlcore_scan_get_channels(struct wl1271 *wl,
113                          struct ieee80211_channel *req_channels[],
114                          u32 n_channels,
115                          u32 n_ssids,
116                          struct conn_scan_ch_params *channels,
117                          u32 band, bool radar, bool passive,
118                          int start, int max_channels,
119                          u8 *n_pactive_ch,
120                          int scan_type)
121 {
122         int i, j;
123         u32 flags;
124         bool force_passive = !n_ssids;
125         u32 min_dwell_time_active, max_dwell_time_active;
126         u32 dwell_time_passive, dwell_time_dfs;
127
128         /* configure dwell times according to scan type */
129         if (scan_type == SCAN_TYPE_SEARCH) {
130                 struct conf_scan_settings *c = &wl->conf.scan;
131                 bool active_vif_exists = !!wlcore_count_started_vifs(wl);
132
133                 min_dwell_time_active = active_vif_exists ?
134                         c->min_dwell_time_active :
135                         c->min_dwell_time_active_long;
136                 max_dwell_time_active = active_vif_exists ?
137                         c->max_dwell_time_active :
138                         c->max_dwell_time_active_long;
139                 dwell_time_passive = c->dwell_time_passive;
140                 dwell_time_dfs = c->dwell_time_dfs;
141         } else {
142                 struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
143                 u32 delta_per_probe;
144
145                 if (band == IEEE80211_BAND_5GHZ)
146                         delta_per_probe = c->dwell_time_delta_per_probe_5;
147                 else
148                         delta_per_probe = c->dwell_time_delta_per_probe;
149
150                 min_dwell_time_active = c->base_dwell_time +
151                          n_ssids * c->num_probe_reqs * delta_per_probe;
152
153                 max_dwell_time_active = min_dwell_time_active +
154                                         c->max_dwell_time_delta;
155                 dwell_time_passive = c->dwell_time_passive;
156                 dwell_time_dfs = c->dwell_time_dfs;
157         }
158         min_dwell_time_active = DIV_ROUND_UP(min_dwell_time_active, 1000);
159         max_dwell_time_active = DIV_ROUND_UP(max_dwell_time_active, 1000);
160         dwell_time_passive = DIV_ROUND_UP(dwell_time_passive, 1000);
161         dwell_time_dfs = DIV_ROUND_UP(dwell_time_dfs, 1000);
162
163         for (i = 0, j = start;
164              i < n_channels && j < max_channels;
165              i++) {
166                 flags = req_channels[i]->flags;
167
168                 if (force_passive)
169                         flags |= IEEE80211_CHAN_PASSIVE_SCAN;
170
171                 if ((req_channels[i]->band == band) &&
172                     !(flags & IEEE80211_CHAN_DISABLED) &&
173                     (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
174                     /* if radar is set, we ignore the passive flag */
175                     (radar ||
176                      !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
177
178
179                         if (flags & IEEE80211_CHAN_RADAR) {
180                                 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
181
182                                 channels[j].passive_duration =
183                                         cpu_to_le16(dwell_time_dfs);
184                         } else {
185                                 channels[j].passive_duration =
186                                         cpu_to_le16(dwell_time_passive);
187                         }
188
189                         channels[j].min_duration =
190                                 cpu_to_le16(min_dwell_time_active);
191                         channels[j].max_duration =
192                                 cpu_to_le16(max_dwell_time_active);
193
194                         channels[j].tx_power_att = req_channels[i]->max_power;
195                         channels[j].channel = req_channels[i]->hw_value;
196
197                         if (n_pactive_ch &&
198                             (band == IEEE80211_BAND_2GHZ) &&
199                             (channels[j].channel >= 12) &&
200                             (channels[j].channel <= 14) &&
201                             (flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
202                             !force_passive) {
203                                 /* pactive channels treated as DFS */
204                                 channels[j].flags = SCAN_CHANNEL_FLAGS_DFS;
205
206                                 /*
207                                  * n_pactive_ch is counted down from the end of
208                                  * the passive channel list
209                                  */
210                                 (*n_pactive_ch)++;
211                                 wl1271_debug(DEBUG_SCAN, "n_pactive_ch = %d",
212                                              *n_pactive_ch);
213                         }
214
215                         wl1271_debug(DEBUG_SCAN, "freq %d, ch. %d, flags 0x%x, power %d, min/max_dwell %d/%d%s%s",
216                                      req_channels[i]->center_freq,
217                                      req_channels[i]->hw_value,
218                                      req_channels[i]->flags,
219                                      req_channels[i]->max_power,
220                                      min_dwell_time_active,
221                                      max_dwell_time_active,
222                                      flags & IEEE80211_CHAN_RADAR ?
223                                         ", DFS" : "",
224                                      flags & IEEE80211_CHAN_PASSIVE_SCAN ?
225                                         ", PASSIVE" : "");
226                         j++;
227                 }
228         }
229
230         return j - start;
231 }
232
233 bool
234 wlcore_set_scan_chan_params(struct wl1271 *wl,
235                             struct wlcore_scan_channels *cfg,
236                             struct ieee80211_channel *channels[],
237                             u32 n_channels,
238                             u32 n_ssids,
239                             int scan_type)
240 {
241         u8 n_pactive_ch = 0;
242
243         cfg->passive[0] =
244                 wlcore_scan_get_channels(wl,
245                                          channels,
246                                          n_channels,
247                                          n_ssids,
248                                          cfg->channels_2,
249                                          IEEE80211_BAND_2GHZ,
250                                          false, true, 0,
251                                          MAX_CHANNELS_2GHZ,
252                                          &n_pactive_ch,
253                                          scan_type);
254         cfg->active[0] =
255                 wlcore_scan_get_channels(wl,
256                                          channels,
257                                          n_channels,
258                                          n_ssids,
259                                          cfg->channels_2,
260                                          IEEE80211_BAND_2GHZ,
261                                          false, false,
262                                          cfg->passive[0],
263                                          MAX_CHANNELS_2GHZ,
264                                          &n_pactive_ch,
265                                          scan_type);
266         cfg->passive[1] =
267                 wlcore_scan_get_channels(wl,
268                                          channels,
269                                          n_channels,
270                                          n_ssids,
271                                          cfg->channels_5,
272                                          IEEE80211_BAND_5GHZ,
273                                          false, true, 0,
274                                          wl->max_channels_5,
275                                          &n_pactive_ch,
276                                          scan_type);
277         cfg->dfs =
278                 wlcore_scan_get_channels(wl,
279                                          channels,
280                                          n_channels,
281                                          n_ssids,
282                                          cfg->channels_5,
283                                          IEEE80211_BAND_5GHZ,
284                                          true, true,
285                                          cfg->passive[1],
286                                          wl->max_channels_5,
287                                          &n_pactive_ch,
288                                          scan_type);
289         cfg->active[1] =
290                 wlcore_scan_get_channels(wl,
291                                          channels,
292                                          n_channels,
293                                          n_ssids,
294                                          cfg->channels_5,
295                                          IEEE80211_BAND_5GHZ,
296                                          false, false,
297                                          cfg->passive[1] + cfg->dfs,
298                                          wl->max_channels_5,
299                                          &n_pactive_ch,
300                                          scan_type);
301
302         /* 802.11j channels are not supported yet */
303         cfg->passive[2] = 0;
304         cfg->active[2] = 0;
305
306         cfg->passive_active = n_pactive_ch;
307
308         wl1271_debug(DEBUG_SCAN, "    2.4GHz: active %d passive %d",
309                      cfg->active[0], cfg->passive[0]);
310         wl1271_debug(DEBUG_SCAN, "    5GHz: active %d passive %d",
311                      cfg->active[1], cfg->passive[1]);
312         wl1271_debug(DEBUG_SCAN, "    DFS: %d", cfg->dfs);
313
314         return  cfg->passive[0] || cfg->active[0] ||
315                 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
316                 cfg->passive[2] || cfg->active[2];
317 }
318 EXPORT_SYMBOL_GPL(wlcore_set_scan_chan_params);
319
320 int wlcore_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
321                 const u8 *ssid, size_t ssid_len,
322                 struct cfg80211_scan_request *req)
323 {
324         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
325
326         /*
327          * cfg80211 should guarantee that we don't get more channels
328          * than what we have registered.
329          */
330         BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
331
332         if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
333                 return -EBUSY;
334
335         wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
336
337         if (ssid_len && ssid) {
338                 wl->scan.ssid_len = ssid_len;
339                 memcpy(wl->scan.ssid, ssid, ssid_len);
340         } else {
341                 wl->scan.ssid_len = 0;
342         }
343
344         wl->scan_wlvif = wlvif;
345         wl->scan.req = req;
346         memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
347
348         /* we assume failure so that timeout scenarios are handled correctly */
349         wl->scan.failed = true;
350         ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
351                                      msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
352
353         wl->ops->scan_start(wl, wlvif, req);
354
355         return 0;
356 }
357 /* Returns the scan type to be used or a negative value on error */
358 int
359 wlcore_scan_sched_scan_ssid_list(struct wl1271 *wl,
360                                  struct wl12xx_vif *wlvif,
361                                  struct cfg80211_sched_scan_request *req)
362 {
363         struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
364         struct cfg80211_match_set *sets = req->match_sets;
365         struct cfg80211_ssid *ssids = req->ssids;
366         int ret = 0, type, i, j, n_match_ssids = 0;
367
368         wl1271_debug((DEBUG_CMD | DEBUG_SCAN), "cmd sched scan ssid list");
369
370         /* count the match sets that contain SSIDs */
371         for (i = 0; i < req->n_match_sets; i++)
372                 if (sets[i].ssid.ssid_len > 0)
373                         n_match_ssids++;
374
375         /* No filter, no ssids or only bcast ssid */
376         if (!n_match_ssids &&
377             (!req->n_ssids ||
378              (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
379                 type = SCAN_SSID_FILTER_ANY;
380                 goto out;
381         }
382
383         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
384         if (!cmd) {
385                 ret = -ENOMEM;
386                 goto out;
387         }
388
389         cmd->role_id = wlvif->role_id;
390         if (!n_match_ssids) {
391                 /* No filter, with ssids */
392                 type = SCAN_SSID_FILTER_DISABLED;
393
394                 for (i = 0; i < req->n_ssids; i++) {
395                         cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
396                                 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
397                         cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
398                         memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
399                                ssids[i].ssid_len);
400                         cmd->n_ssids++;
401                 }
402         } else {
403                 type = SCAN_SSID_FILTER_LIST;
404
405                 /* Add all SSIDs from the filters */
406                 for (i = 0; i < req->n_match_sets; i++) {
407                         /* ignore sets without SSIDs */
408                         if (!sets[i].ssid.ssid_len)
409                                 continue;
410
411                         cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
412                         cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
413                         memcpy(cmd->ssids[cmd->n_ssids].ssid,
414                                sets[i].ssid.ssid, sets[i].ssid.ssid_len);
415                         cmd->n_ssids++;
416                 }
417                 if ((req->n_ssids > 1) ||
418                     (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
419                         /*
420                          * Mark all the SSIDs passed in the SSID list as HIDDEN,
421                          * so they're used in probe requests.
422                          */
423                         for (i = 0; i < req->n_ssids; i++) {
424                                 if (!req->ssids[i].ssid_len)
425                                         continue;
426
427                                 for (j = 0; j < cmd->n_ssids; j++)
428                                         if ((req->ssids[i].ssid_len ==
429                                              cmd->ssids[j].len) &&
430                                             !memcmp(req->ssids[i].ssid,
431                                                    cmd->ssids[j].ssid,
432                                                    req->ssids[i].ssid_len)) {
433                                                 cmd->ssids[j].type =
434                                                         SCAN_SSID_TYPE_HIDDEN;
435                                                 break;
436                                         }
437                                 /* Fail if SSID isn't present in the filters */
438                                 if (j == cmd->n_ssids) {
439                                         ret = -EINVAL;
440                                         goto out_free;
441                                 }
442                         }
443                 }
444         }
445
446         ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
447                               sizeof(*cmd), 0);
448         if (ret < 0) {
449                 wl1271_error("cmd sched scan ssid list failed");
450                 goto out_free;
451         }
452
453 out_free:
454         kfree(cmd);
455 out:
456         if (ret < 0)
457                 return ret;
458         return type;
459 }
460 EXPORT_SYMBOL_GPL(wlcore_scan_sched_scan_ssid_list);
461
462 void wlcore_scan_sched_scan_results(struct wl1271 *wl)
463 {
464         wl1271_debug(DEBUG_SCAN, "got periodic scan results");
465
466         ieee80211_sched_scan_results(wl->hw);
467 }
468 EXPORT_SYMBOL_GPL(wlcore_scan_sched_scan_results);