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