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