Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / wl12xx / 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 "wl12xx.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 ieee80211_vif *vif;
39         struct wl12xx_vif *wlvif;
40         int ret;
41         bool is_sta, is_ibss;
42
43         dwork = container_of(work, struct delayed_work, work);
44         wl = container_of(dwork, struct wl1271, scan_complete_work);
45
46         wl1271_debug(DEBUG_SCAN, "Scanning complete");
47
48         mutex_lock(&wl->mutex);
49
50         if (wl->state == WL1271_STATE_OFF)
51                 goto out;
52
53         if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
54                 goto out;
55
56         vif = wl->scan_vif;
57         wlvif = wl12xx_vif_to_data(vif);
58
59         wl->scan.state = WL1271_SCAN_STATE_IDLE;
60         memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
61         wl->scan.req = NULL;
62         wl->scan_vif = NULL;
63
64         ret = wl1271_ps_elp_wakeup(wl);
65         if (ret < 0)
66                 goto out;
67
68         if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
69                 /* restore hardware connection monitoring template */
70                 wl1271_cmd_build_ap_probe_req(wl, wlvif, wlvif->probereq);
71         }
72
73         /* return to ROC if needed */
74         is_sta = (wlvif->bss_type == BSS_TYPE_STA_BSS);
75         is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
76         if (((is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) ||
77              (is_ibss && !test_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))) &&
78             !test_bit(wlvif->dev_role_id, wl->roc_map)) {
79                 /* restore remain on channel */
80                 wl12xx_start_dev(wl, wlvif);
81         }
82         wl1271_ps_elp_sleep(wl);
83
84         if (wl->scan.failed) {
85                 wl1271_info("Scan completed due to error.");
86                 wl12xx_queue_recovery_work(wl);
87         }
88
89         ieee80211_scan_completed(wl->hw, false);
90
91 out:
92         mutex_unlock(&wl->mutex);
93
94 }
95
96
97 static int wl1271_get_scan_channels(struct wl1271 *wl,
98                                     struct cfg80211_scan_request *req,
99                                     struct basic_scan_channel_params *channels,
100                                     enum ieee80211_band band, bool passive)
101 {
102         struct conf_scan_settings *c = &wl->conf.scan;
103         int i, j;
104         u32 flags;
105
106         for (i = 0, j = 0;
107              i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
108              i++) {
109                 flags = req->channels[i]->flags;
110
111                 if (!test_bit(i, wl->scan.scanned_ch) &&
112                     !(flags & IEEE80211_CHAN_DISABLED) &&
113                     (req->channels[i]->band == band) &&
114                     /*
115                      * In passive scans, we scan all remaining
116                      * channels, even if not marked as such.
117                      * In active scans, we only scan channels not
118                      * marked as passive.
119                      */
120                     (passive || !(flags & IEEE80211_CHAN_PASSIVE_SCAN))) {
121                         wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
122                                      req->channels[i]->band,
123                                      req->channels[i]->center_freq);
124                         wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
125                                      req->channels[i]->hw_value,
126                                      req->channels[i]->flags);
127                         wl1271_debug(DEBUG_SCAN,
128                                      "max_antenna_gain %d, max_power %d",
129                                      req->channels[i]->max_antenna_gain,
130                                      req->channels[i]->max_power);
131                         wl1271_debug(DEBUG_SCAN, "beacon_found %d",
132                                      req->channels[i]->beacon_found);
133
134                         if (!passive) {
135                                 channels[j].min_duration =
136                                         cpu_to_le32(c->min_dwell_time_active);
137                                 channels[j].max_duration =
138                                         cpu_to_le32(c->max_dwell_time_active);
139                         } else {
140                                 channels[j].min_duration =
141                                         cpu_to_le32(c->min_dwell_time_passive);
142                                 channels[j].max_duration =
143                                         cpu_to_le32(c->max_dwell_time_passive);
144                         }
145                         channels[j].early_termination = 0;
146                         channels[j].tx_power_att = req->channels[i]->max_power;
147                         channels[j].channel = req->channels[i]->hw_value;
148
149                         memset(&channels[j].bssid_lsb, 0xff, 4);
150                         memset(&channels[j].bssid_msb, 0xff, 2);
151
152                         /* Mark the channels we already used */
153                         set_bit(i, wl->scan.scanned_ch);
154
155                         j++;
156                 }
157         }
158
159         return j;
160 }
161
162 #define WL1271_NOTHING_TO_SCAN 1
163
164 static int wl1271_scan_send(struct wl1271 *wl, struct ieee80211_vif *vif,
165                             enum ieee80211_band band,
166                             bool passive, u32 basic_rate)
167 {
168         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
169         struct wl1271_cmd_scan *cmd;
170         struct wl1271_cmd_trigger_scan_to *trigger;
171         int ret;
172         u16 scan_options = 0;
173
174         /* skip active scans if we don't have SSIDs */
175         if (!passive && wl->scan.req->n_ssids == 0)
176                 return WL1271_NOTHING_TO_SCAN;
177
178         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
179         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
180         if (!cmd || !trigger) {
181                 ret = -ENOMEM;
182                 goto out;
183         }
184
185         if (passive)
186                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
187
188         if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID)) {
189                 ret = -EINVAL;
190                 goto out;
191         }
192         cmd->params.role_id = wlvif->role_id;
193         cmd->params.scan_options = cpu_to_le16(scan_options);
194
195         cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
196                                                     cmd->channels,
197                                                     band, passive);
198         if (cmd->params.n_ch == 0) {
199                 ret = WL1271_NOTHING_TO_SCAN;
200                 goto out;
201         }
202
203         cmd->params.tx_rate = cpu_to_le32(basic_rate);
204         cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
205         cmd->params.tid_trigger = 0;
206         cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
207
208         if (band == IEEE80211_BAND_2GHZ)
209                 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
210         else
211                 cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
212
213         if (wl->scan.ssid_len && wl->scan.ssid) {
214                 cmd->params.ssid_len = wl->scan.ssid_len;
215                 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
216         }
217
218         memcpy(cmd->addr, vif->addr, ETH_ALEN);
219
220         ret = wl1271_cmd_build_probe_req(wl, wlvif, wl->scan.ssid,
221                                          wl->scan.ssid_len, wl->scan.req->ie,
222                                          wl->scan.req->ie_len, band);
223         if (ret < 0) {
224                 wl1271_error("PROBE request template failed");
225                 goto out;
226         }
227
228         /* disable the timeout */
229         trigger->timeout = 0;
230         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
231                               sizeof(*trigger), 0);
232         if (ret < 0) {
233                 wl1271_error("trigger scan to failed for hw scan");
234                 goto out;
235         }
236
237         wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
238
239         ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
240         if (ret < 0) {
241                 wl1271_error("SCAN failed");
242                 goto out;
243         }
244
245 out:
246         kfree(cmd);
247         kfree(trigger);
248         return ret;
249 }
250
251 void wl1271_scan_stm(struct wl1271 *wl, struct ieee80211_vif *vif)
252 {
253         struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
254         int ret = 0;
255         enum ieee80211_band band;
256         u32 rate, mask;
257
258         switch (wl->scan.state) {
259         case WL1271_SCAN_STATE_IDLE:
260                 break;
261
262         case WL1271_SCAN_STATE_2GHZ_ACTIVE:
263                 band = IEEE80211_BAND_2GHZ;
264                 mask = wlvif->bitrate_masks[band];
265                 if (wl->scan.req->no_cck) {
266                         mask &= ~CONF_TX_CCK_RATES;
267                         if (!mask)
268                                 mask = CONF_TX_RATE_MASK_BASIC_P2P;
269                 }
270                 rate = wl1271_tx_min_rate_get(wl, mask);
271                 ret = wl1271_scan_send(wl, vif, band, false, rate);
272                 if (ret == WL1271_NOTHING_TO_SCAN) {
273                         wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
274                         wl1271_scan_stm(wl, vif);
275                 }
276
277                 break;
278
279         case WL1271_SCAN_STATE_2GHZ_PASSIVE:
280                 band = IEEE80211_BAND_2GHZ;
281                 mask = wlvif->bitrate_masks[band];
282                 if (wl->scan.req->no_cck) {
283                         mask &= ~CONF_TX_CCK_RATES;
284                         if (!mask)
285                                 mask = CONF_TX_RATE_MASK_BASIC_P2P;
286                 }
287                 rate = wl1271_tx_min_rate_get(wl, mask);
288                 ret = wl1271_scan_send(wl, vif, band, true, rate);
289                 if (ret == WL1271_NOTHING_TO_SCAN) {
290                         if (wl->enable_11a)
291                                 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
292                         else
293                                 wl->scan.state = WL1271_SCAN_STATE_DONE;
294                         wl1271_scan_stm(wl, vif);
295                 }
296
297                 break;
298
299         case WL1271_SCAN_STATE_5GHZ_ACTIVE:
300                 band = IEEE80211_BAND_5GHZ;
301                 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
302                 ret = wl1271_scan_send(wl, vif, band, false, rate);
303                 if (ret == WL1271_NOTHING_TO_SCAN) {
304                         wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
305                         wl1271_scan_stm(wl, vif);
306                 }
307
308                 break;
309
310         case WL1271_SCAN_STATE_5GHZ_PASSIVE:
311                 band = IEEE80211_BAND_5GHZ;
312                 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
313                 ret = wl1271_scan_send(wl, vif, band, true, rate);
314                 if (ret == WL1271_NOTHING_TO_SCAN) {
315                         wl->scan.state = WL1271_SCAN_STATE_DONE;
316                         wl1271_scan_stm(wl, vif);
317                 }
318
319                 break;
320
321         case WL1271_SCAN_STATE_DONE:
322                 wl->scan.failed = false;
323                 cancel_delayed_work(&wl->scan_complete_work);
324                 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
325                                              msecs_to_jiffies(0));
326                 break;
327
328         default:
329                 wl1271_error("invalid scan state");
330                 break;
331         }
332
333         if (ret < 0) {
334                 cancel_delayed_work(&wl->scan_complete_work);
335                 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
336                                              msecs_to_jiffies(0));
337         }
338 }
339
340 int wl1271_scan(struct wl1271 *wl, struct ieee80211_vif *vif,
341                 const u8 *ssid, size_t ssid_len,
342                 struct cfg80211_scan_request *req)
343 {
344         /*
345          * cfg80211 should guarantee that we don't get more channels
346          * than what we have registered.
347          */
348         BUG_ON(req->n_channels > WL1271_MAX_CHANNELS);
349
350         if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
351                 return -EBUSY;
352
353         wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
354
355         if (ssid_len && ssid) {
356                 wl->scan.ssid_len = ssid_len;
357                 memcpy(wl->scan.ssid, ssid, ssid_len);
358         } else {
359                 wl->scan.ssid_len = 0;
360         }
361
362         wl->scan_vif = vif;
363         wl->scan.req = req;
364         memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
365
366         /* we assume failure so that timeout scenarios are handled correctly */
367         wl->scan.failed = true;
368         ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
369                                      msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
370
371         wl1271_scan_stm(wl, vif);
372
373         return 0;
374 }
375
376 int wl1271_scan_stop(struct wl1271 *wl)
377 {
378         struct wl1271_cmd_header *cmd = NULL;
379         int ret = 0;
380
381         if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
382                 return -EINVAL;
383
384         wl1271_debug(DEBUG_CMD, "cmd scan stop");
385
386         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
387         if (!cmd) {
388                 ret = -ENOMEM;
389                 goto out;
390         }
391
392         ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
393                               sizeof(*cmd), 0);
394         if (ret < 0) {
395                 wl1271_error("cmd stop_scan failed");
396                 goto out;
397         }
398 out:
399         kfree(cmd);
400         return ret;
401 }
402
403 static int
404 wl1271_scan_get_sched_scan_channels(struct wl1271 *wl,
405                                     struct cfg80211_sched_scan_request *req,
406                                     struct conn_scan_ch_params *channels,
407                                     u32 band, bool radar, bool passive,
408                                     int start, int max_channels)
409 {
410         struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
411         int i, j;
412         u32 flags;
413         bool force_passive = !req->n_ssids;
414
415         for (i = 0, j = start;
416              i < req->n_channels && j < max_channels;
417              i++) {
418                 flags = req->channels[i]->flags;
419
420                 if (force_passive)
421                         flags |= IEEE80211_CHAN_PASSIVE_SCAN;
422
423                 if ((req->channels[i]->band == band) &&
424                     !(flags & IEEE80211_CHAN_DISABLED) &&
425                     (!!(flags & IEEE80211_CHAN_RADAR) == radar) &&
426                     /* if radar is set, we ignore the passive flag */
427                     (radar ||
428                      !!(flags & IEEE80211_CHAN_PASSIVE_SCAN) == passive)) {
429                         wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
430                                      req->channels[i]->band,
431                                      req->channels[i]->center_freq);
432                         wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
433                                      req->channels[i]->hw_value,
434                                      req->channels[i]->flags);
435                         wl1271_debug(DEBUG_SCAN, "max_power %d",
436                                      req->channels[i]->max_power);
437
438                         if (flags & IEEE80211_CHAN_RADAR) {
439                                 channels[j].flags |= SCAN_CHANNEL_FLAGS_DFS;
440                                 channels[j].passive_duration =
441                                         cpu_to_le16(c->dwell_time_dfs);
442                         }
443                         else if (flags & IEEE80211_CHAN_PASSIVE_SCAN) {
444                                 channels[j].passive_duration =
445                                         cpu_to_le16(c->dwell_time_passive);
446                         } else {
447                                 channels[j].min_duration =
448                                         cpu_to_le16(c->min_dwell_time_active);
449                                 channels[j].max_duration =
450                                         cpu_to_le16(c->max_dwell_time_active);
451                         }
452                         channels[j].tx_power_att = req->channels[i]->max_power;
453                         channels[j].channel = req->channels[i]->hw_value;
454
455                         j++;
456                 }
457         }
458
459         return j - start;
460 }
461
462 static bool
463 wl1271_scan_sched_scan_channels(struct wl1271 *wl,
464                                 struct cfg80211_sched_scan_request *req,
465                                 struct wl1271_cmd_sched_scan_config *cfg)
466 {
467         cfg->passive[0] =
468                 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
469                                                     IEEE80211_BAND_2GHZ,
470                                                     false, true, 0,
471                                                     MAX_CHANNELS_2GHZ);
472         cfg->active[0] =
473                 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_2,
474                                                     IEEE80211_BAND_2GHZ,
475                                                     false, false,
476                                                     cfg->passive[0],
477                                                     MAX_CHANNELS_2GHZ);
478         cfg->passive[1] =
479                 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
480                                                     IEEE80211_BAND_5GHZ,
481                                                     false, true, 0,
482                                                     MAX_CHANNELS_5GHZ);
483         cfg->dfs =
484                 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
485                                                     IEEE80211_BAND_5GHZ,
486                                                     true, true,
487                                                     cfg->passive[1],
488                                                     MAX_CHANNELS_5GHZ);
489         cfg->active[1] =
490                 wl1271_scan_get_sched_scan_channels(wl, req, cfg->channels_5,
491                                                     IEEE80211_BAND_5GHZ,
492                                                     false, false,
493                                                     cfg->passive[1] + cfg->dfs,
494                                                     MAX_CHANNELS_5GHZ);
495         /* 802.11j channels are not supported yet */
496         cfg->passive[2] = 0;
497         cfg->active[2] = 0;
498
499         wl1271_debug(DEBUG_SCAN, "    2.4GHz: active %d passive %d",
500                      cfg->active[0], cfg->passive[0]);
501         wl1271_debug(DEBUG_SCAN, "    5GHz: active %d passive %d",
502                      cfg->active[1], cfg->passive[1]);
503         wl1271_debug(DEBUG_SCAN, "    DFS: %d", cfg->dfs);
504
505         return  cfg->passive[0] || cfg->active[0] ||
506                 cfg->passive[1] || cfg->active[1] || cfg->dfs ||
507                 cfg->passive[2] || cfg->active[2];
508 }
509
510 /* Returns the scan type to be used or a negative value on error */
511 static int
512 wl12xx_scan_sched_scan_ssid_list(struct wl1271 *wl,
513                                  struct cfg80211_sched_scan_request *req)
514 {
515         struct wl1271_cmd_sched_scan_ssid_list *cmd = NULL;
516         struct cfg80211_match_set *sets = req->match_sets;
517         struct cfg80211_ssid *ssids = req->ssids;
518         int ret = 0, type, i, j, n_match_ssids = 0;
519
520         wl1271_debug(DEBUG_CMD, "cmd sched scan ssid list");
521
522         /* count the match sets that contain SSIDs */
523         for (i = 0; i < req->n_match_sets; i++)
524                 if (sets[i].ssid.ssid_len > 0)
525                         n_match_ssids++;
526
527         /* No filter, no ssids or only bcast ssid */
528         if (!n_match_ssids &&
529             (!req->n_ssids ||
530              (req->n_ssids == 1 && req->ssids[0].ssid_len == 0))) {
531                 type = SCAN_SSID_FILTER_ANY;
532                 goto out;
533         }
534
535         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
536         if (!cmd) {
537                 ret = -ENOMEM;
538                 goto out;
539         }
540
541         if (!n_match_ssids) {
542                 /* No filter, with ssids */
543                 type = SCAN_SSID_FILTER_DISABLED;
544
545                 for (i = 0; i < req->n_ssids; i++) {
546                         cmd->ssids[cmd->n_ssids].type = (ssids[i].ssid_len) ?
547                                 SCAN_SSID_TYPE_HIDDEN : SCAN_SSID_TYPE_PUBLIC;
548                         cmd->ssids[cmd->n_ssids].len = ssids[i].ssid_len;
549                         memcpy(cmd->ssids[cmd->n_ssids].ssid, ssids[i].ssid,
550                                ssids[i].ssid_len);
551                         cmd->n_ssids++;
552                 }
553         } else {
554                 type = SCAN_SSID_FILTER_LIST;
555
556                 /* Add all SSIDs from the filters */
557                 for (i = 0; i < req->n_match_sets; i++) {
558                         /* ignore sets without SSIDs */
559                         if (!sets[i].ssid.ssid_len)
560                                 continue;
561
562                         cmd->ssids[cmd->n_ssids].type = SCAN_SSID_TYPE_PUBLIC;
563                         cmd->ssids[cmd->n_ssids].len = sets[i].ssid.ssid_len;
564                         memcpy(cmd->ssids[cmd->n_ssids].ssid,
565                                sets[i].ssid.ssid, sets[i].ssid.ssid_len);
566                         cmd->n_ssids++;
567                 }
568                 if ((req->n_ssids > 1) ||
569                     (req->n_ssids == 1 && req->ssids[0].ssid_len > 0)) {
570                         /*
571                          * Mark all the SSIDs passed in the SSID list as HIDDEN,
572                          * so they're used in probe requests.
573                          */
574                         for (i = 0; i < req->n_ssids; i++) {
575                                 for (j = 0; j < cmd->n_ssids; j++)
576                                         if (!memcmp(req->ssids[i].ssid,
577                                                    cmd->ssids[j].ssid,
578                                                    req->ssids[i].ssid_len)) {
579                                                 cmd->ssids[j].type =
580                                                         SCAN_SSID_TYPE_HIDDEN;
581                                                 break;
582                                         }
583                                 /* Fail if SSID isn't present in the filters */
584                                 if (j == cmd->n_ssids) {
585                                         ret = -EINVAL;
586                                         goto out_free;
587                                 }
588                         }
589                 }
590         }
591
592         wl1271_dump(DEBUG_SCAN, "SSID_LIST: ", cmd, sizeof(*cmd));
593
594         ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_SSID_CFG, cmd,
595                               sizeof(*cmd), 0);
596         if (ret < 0) {
597                 wl1271_error("cmd sched scan ssid list failed");
598                 goto out_free;
599         }
600
601 out_free:
602         kfree(cmd);
603 out:
604         if (ret < 0)
605                 return ret;
606         return type;
607 }
608
609 int wl1271_scan_sched_scan_config(struct wl1271 *wl,
610                                   struct wl12xx_vif *wlvif,
611                                   struct cfg80211_sched_scan_request *req,
612                                   struct ieee80211_sched_scan_ies *ies)
613 {
614         struct wl1271_cmd_sched_scan_config *cfg = NULL;
615         struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
616         int i, ret;
617         bool force_passive = !req->n_ssids;
618
619         wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
620
621         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
622         if (!cfg)
623                 return -ENOMEM;
624
625         cfg->rssi_threshold = c->rssi_threshold;
626         cfg->snr_threshold  = c->snr_threshold;
627         cfg->n_probe_reqs = c->num_probe_reqs;
628         /* cycles set to 0 it means infinite (until manually stopped) */
629         cfg->cycles = 0;
630         /* report APs when at least 1 is found */
631         cfg->report_after = 1;
632         /* don't stop scanning automatically when something is found */
633         cfg->terminate = 0;
634         cfg->tag = WL1271_SCAN_DEFAULT_TAG;
635         /* don't filter on BSS type */
636         cfg->bss_type = SCAN_BSS_TYPE_ANY;
637         /* currently NL80211 supports only a single interval */
638         for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
639                 cfg->intervals[i] = cpu_to_le32(req->interval);
640
641         cfg->ssid_len = 0;
642         ret = wl12xx_scan_sched_scan_ssid_list(wl, req);
643         if (ret < 0)
644                 goto out;
645
646         cfg->filter_type = ret;
647
648         wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
649
650         if (!wl1271_scan_sched_scan_channels(wl, req, cfg)) {
651                 wl1271_error("scan channel list is empty");
652                 ret = -EINVAL;
653                 goto out;
654         }
655
656         if (!force_passive && cfg->active[0]) {
657                 ret = wl1271_cmd_build_probe_req(wl, wlvif, req->ssids[0].ssid,
658                                                  req->ssids[0].ssid_len,
659                                                  ies->ie[IEEE80211_BAND_2GHZ],
660                                                  ies->len[IEEE80211_BAND_2GHZ],
661                                                  IEEE80211_BAND_2GHZ);
662                 if (ret < 0) {
663                         wl1271_error("2.4GHz PROBE request template failed");
664                         goto out;
665                 }
666         }
667
668         if (!force_passive && cfg->active[1]) {
669                 ret = wl1271_cmd_build_probe_req(wl, wlvif, req->ssids[0].ssid,
670                                                  req->ssids[0].ssid_len,
671                                                  ies->ie[IEEE80211_BAND_5GHZ],
672                                                  ies->len[IEEE80211_BAND_5GHZ],
673                                                  IEEE80211_BAND_5GHZ);
674                 if (ret < 0) {
675                         wl1271_error("5GHz PROBE request template failed");
676                         goto out;
677                 }
678         }
679
680         wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
681
682         ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
683                               sizeof(*cfg), 0);
684         if (ret < 0) {
685                 wl1271_error("SCAN configuration failed");
686                 goto out;
687         }
688 out:
689         kfree(cfg);
690         return ret;
691 }
692
693 int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
694 {
695         struct wl1271_cmd_sched_scan_start *start;
696         int ret = 0;
697
698         wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
699
700         if (wlvif->bss_type != BSS_TYPE_STA_BSS)
701                 return -EOPNOTSUPP;
702
703         if (!test_bit(WL1271_FLAG_IDLE, &wl->flags))
704                 return -EBUSY;
705
706         start = kzalloc(sizeof(*start), GFP_KERNEL);
707         if (!start)
708                 return -ENOMEM;
709
710         start->tag = WL1271_SCAN_DEFAULT_TAG;
711
712         ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
713                               sizeof(*start), 0);
714         if (ret < 0) {
715                 wl1271_error("failed to send scan start command");
716                 goto out_free;
717         }
718
719 out_free:
720         kfree(start);
721         return ret;
722 }
723
724 void wl1271_scan_sched_scan_results(struct wl1271 *wl)
725 {
726         wl1271_debug(DEBUG_SCAN, "got periodic scan results");
727
728         ieee80211_sched_scan_results(wl->hw);
729 }
730
731 void wl1271_scan_sched_scan_stop(struct wl1271 *wl)
732 {
733         struct wl1271_cmd_sched_scan_stop *stop;
734         int ret = 0;
735
736         wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
737
738         /* FIXME: what to do if alloc'ing to stop fails? */
739         stop = kzalloc(sizeof(*stop), GFP_KERNEL);
740         if (!stop) {
741                 wl1271_error("failed to alloc memory to send sched scan stop");
742                 return;
743         }
744
745         stop->tag = WL1271_SCAN_DEFAULT_TAG;
746
747         ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
748                               sizeof(*stop), 0);
749         if (ret < 0) {
750                 wl1271_error("failed to send sched scan stop command");
751                 goto out_free;
752         }
753         wl->sched_scanning = false;
754
755 out_free:
756         kfree(stop);
757 }