1 /* 2 * This file is part of wl12xx 3 * 4 * Copyright (C) 2012 Texas Instruments. All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * version 2 as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 18 * 02110-1301 USA 19 * 20 */ 21 22 #include <linux/ieee80211.h> 23 #include "scan.h" 24 #include "../wlcore/debug.h" 25 #include "../wlcore/tx.h" 26 27 static int wl1271_get_scan_channels(struct wl1271 *wl, 28 struct cfg80211_scan_request *req, 29 struct basic_scan_channel_params *channels, 30 enum ieee80211_band band, bool passive) 31 { 32 struct conf_scan_settings *c = &wl->conf.scan; 33 int i, j; 34 u32 flags; 35 36 for (i = 0, j = 0; 37 i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS; 38 i++) { 39 flags = req->channels[i]->flags; 40 41 if (!test_bit(i, wl->scan.scanned_ch) && 42 !(flags & IEEE80211_CHAN_DISABLED) && 43 (req->channels[i]->band == band) && 44 /* 45 * In passive scans, we scan all remaining 46 * channels, even if not marked as such. 47 * In active scans, we only scan channels not 48 * marked as passive. 49 */ 50 (passive || !(flags & IEEE80211_CHAN_NO_IR))) { 51 wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ", 52 req->channels[i]->band, 53 req->channels[i]->center_freq); 54 wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X", 55 req->channels[i]->hw_value, 56 req->channels[i]->flags); 57 wl1271_debug(DEBUG_SCAN, 58 "max_antenna_gain %d, max_power %d", 59 req->channels[i]->max_antenna_gain, 60 req->channels[i]->max_power); 61 wl1271_debug(DEBUG_SCAN, "beacon_found %d", 62 req->channels[i]->beacon_found); 63 64 if (!passive) { 65 channels[j].min_duration = 66 cpu_to_le32(c->min_dwell_time_active); 67 channels[j].max_duration = 68 cpu_to_le32(c->max_dwell_time_active); 69 } else { 70 channels[j].min_duration = 71 cpu_to_le32(c->dwell_time_passive); 72 channels[j].max_duration = 73 cpu_to_le32(c->dwell_time_passive); 74 } 75 channels[j].early_termination = 0; 76 channels[j].tx_power_att = req->channels[i]->max_power; 77 channels[j].channel = req->channels[i]->hw_value; 78 79 memset(&channels[j].bssid_lsb, 0xff, 4); 80 memset(&channels[j].bssid_msb, 0xff, 2); 81 82 /* Mark the channels we already used */ 83 set_bit(i, wl->scan.scanned_ch); 84 85 j++; 86 } 87 } 88 89 return j; 90 } 91 92 #define WL1271_NOTHING_TO_SCAN 1 93 94 static int wl1271_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif, 95 enum ieee80211_band band, 96 bool passive, u32 basic_rate) 97 { 98 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 99 struct wl1271_cmd_scan *cmd; 100 struct wl1271_cmd_trigger_scan_to *trigger; 101 int ret; 102 u16 scan_options = 0; 103 104 /* skip active scans if we don't have SSIDs */ 105 if (!passive && wl->scan.req->n_ssids == 0) 106 return WL1271_NOTHING_TO_SCAN; 107 108 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 109 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL); 110 if (!cmd || !trigger) { 111 ret = -ENOMEM; 112 goto out; 113 } 114 115 if (wl->conf.scan.split_scan_timeout) 116 scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN; 117 118 if (passive) 119 scan_options |= WL1271_SCAN_OPT_PASSIVE; 120 121 /* scan on the dev role if the regular one is not started */ 122 if (wlcore_is_p2p_mgmt(wlvif)) 123 cmd->params.role_id = wlvif->dev_role_id; 124 else 125 cmd->params.role_id = wlvif->role_id; 126 127 if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) { 128 ret = -EINVAL; 129 goto out; 130 } 131 132 cmd->params.scan_options = cpu_to_le16(scan_options); 133 134 cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req, 135 cmd->channels, 136 band, passive); 137 if (cmd->params.n_ch == 0) { 138 ret = WL1271_NOTHING_TO_SCAN; 139 goto out; 140 } 141 142 cmd->params.tx_rate = cpu_to_le32(basic_rate); 143 cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs; 144 cmd->params.tid_trigger = CONF_TX_AC_ANY_TID; 145 cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG; 146 147 if (band == IEEE80211_BAND_2GHZ) 148 cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ; 149 else 150 cmd->params.band = WL1271_SCAN_BAND_5_GHZ; 151 152 if (wl->scan.ssid_len && wl->scan.ssid) { 153 cmd->params.ssid_len = wl->scan.ssid_len; 154 memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len); 155 } 156 157 memcpy(cmd->addr, vif->addr, ETH_ALEN); 158 159 ret = wl12xx_cmd_build_probe_req(wl, wlvif, 160 cmd->params.role_id, band, 161 wl->scan.ssid, wl->scan.ssid_len, 162 wl->scan.req->ie, 163 wl->scan.req->ie_len, NULL, 0, false); 164 if (ret < 0) { 165 wl1271_error("PROBE request template failed"); 166 goto out; 167 } 168 169 trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout); 170 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger, 171 sizeof(*trigger), 0); 172 if (ret < 0) { 173 wl1271_error("trigger scan to failed for hw scan"); 174 goto out; 175 } 176 177 wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd)); 178 179 ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0); 180 if (ret < 0) { 181 wl1271_error("SCAN failed"); 182 goto out; 183 } 184 185 out: 186 kfree(cmd); 187 kfree(trigger); 188 return ret; 189 } 190 191 int wl12xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif) 192 { 193 struct wl1271_cmd_header *cmd = NULL; 194 int ret = 0; 195 196 if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE)) 197 return -EINVAL; 198 199 wl1271_debug(DEBUG_CMD, "cmd scan stop"); 200 201 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 202 if (!cmd) { 203 ret = -ENOMEM; 204 goto out; 205 } 206 207 ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd, 208 sizeof(*cmd), 0); 209 if (ret < 0) { 210 wl1271_error("cmd stop_scan failed"); 211 goto out; 212 } 213 out: 214 kfree(cmd); 215 return ret; 216 } 217 218 void wl1271_scan_stm(struct wl1271 *wl, struct wl12xx_vif *wlvif) 219 { 220 int ret = 0; 221 enum ieee80211_band band; 222 u32 rate, mask; 223 224 switch (wl->scan.state) { 225 case WL1271_SCAN_STATE_IDLE: 226 break; 227 228 case WL1271_SCAN_STATE_2GHZ_ACTIVE: 229 band = IEEE80211_BAND_2GHZ; 230 mask = wlvif->bitrate_masks[band]; 231 if (wl->scan.req->no_cck) { 232 mask &= ~CONF_TX_CCK_RATES; 233 if (!mask) 234 mask = CONF_TX_RATE_MASK_BASIC_P2P; 235 } 236 rate = wl1271_tx_min_rate_get(wl, mask); 237 ret = wl1271_scan_send(wl, wlvif, band, false, rate); 238 if (ret == WL1271_NOTHING_TO_SCAN) { 239 wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE; 240 wl1271_scan_stm(wl, wlvif); 241 } 242 243 break; 244 245 case WL1271_SCAN_STATE_2GHZ_PASSIVE: 246 band = IEEE80211_BAND_2GHZ; 247 mask = wlvif->bitrate_masks[band]; 248 if (wl->scan.req->no_cck) { 249 mask &= ~CONF_TX_CCK_RATES; 250 if (!mask) 251 mask = CONF_TX_RATE_MASK_BASIC_P2P; 252 } 253 rate = wl1271_tx_min_rate_get(wl, mask); 254 ret = wl1271_scan_send(wl, wlvif, band, true, rate); 255 if (ret == WL1271_NOTHING_TO_SCAN) { 256 if (wl->enable_11a) 257 wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE; 258 else 259 wl->scan.state = WL1271_SCAN_STATE_DONE; 260 wl1271_scan_stm(wl, wlvif); 261 } 262 263 break; 264 265 case WL1271_SCAN_STATE_5GHZ_ACTIVE: 266 band = IEEE80211_BAND_5GHZ; 267 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]); 268 ret = wl1271_scan_send(wl, wlvif, band, false, rate); 269 if (ret == WL1271_NOTHING_TO_SCAN) { 270 wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE; 271 wl1271_scan_stm(wl, wlvif); 272 } 273 274 break; 275 276 case WL1271_SCAN_STATE_5GHZ_PASSIVE: 277 band = IEEE80211_BAND_5GHZ; 278 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]); 279 ret = wl1271_scan_send(wl, wlvif, band, true, rate); 280 if (ret == WL1271_NOTHING_TO_SCAN) { 281 wl->scan.state = WL1271_SCAN_STATE_DONE; 282 wl1271_scan_stm(wl, wlvif); 283 } 284 285 break; 286 287 case WL1271_SCAN_STATE_DONE: 288 wl->scan.failed = false; 289 cancel_delayed_work(&wl->scan_complete_work); 290 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work, 291 msecs_to_jiffies(0)); 292 break; 293 294 default: 295 wl1271_error("invalid scan state"); 296 break; 297 } 298 299 if (ret < 0) { 300 cancel_delayed_work(&wl->scan_complete_work); 301 ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work, 302 msecs_to_jiffies(0)); 303 } 304 } 305 306 static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config *cmd, 307 struct wlcore_scan_channels *cmd_channels) 308 { 309 memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive)); 310 memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active)); 311 cmd->dfs = cmd_channels->dfs; 312 cmd->n_pactive_ch = cmd_channels->passive_active; 313 314 memcpy(cmd->channels_2, cmd_channels->channels_2, 315 sizeof(cmd->channels_2)); 316 memcpy(cmd->channels_5, cmd_channels->channels_5, 317 sizeof(cmd->channels_5)); 318 /* channels_4 are not supported, so no need to copy them */ 319 } 320 321 int wl1271_scan_sched_scan_config(struct wl1271 *wl, 322 struct wl12xx_vif *wlvif, 323 struct cfg80211_sched_scan_request *req, 324 struct ieee80211_scan_ies *ies) 325 { 326 struct wl1271_cmd_sched_scan_config *cfg = NULL; 327 struct wlcore_scan_channels *cfg_channels = NULL; 328 struct conf_sched_scan_settings *c = &wl->conf.sched_scan; 329 int i, ret; 330 bool force_passive = !req->n_ssids; 331 332 wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config"); 333 334 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 335 if (!cfg) 336 return -ENOMEM; 337 338 cfg->role_id = wlvif->role_id; 339 cfg->rssi_threshold = c->rssi_threshold; 340 cfg->snr_threshold = c->snr_threshold; 341 cfg->n_probe_reqs = c->num_probe_reqs; 342 /* cycles set to 0 it means infinite (until manually stopped) */ 343 cfg->cycles = 0; 344 /* report APs when at least 1 is found */ 345 cfg->report_after = 1; 346 /* don't stop scanning automatically when something is found */ 347 cfg->terminate = 0; 348 cfg->tag = WL1271_SCAN_DEFAULT_TAG; 349 /* don't filter on BSS type */ 350 cfg->bss_type = SCAN_BSS_TYPE_ANY; 351 /* currently NL80211 supports only a single interval */ 352 for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++) 353 cfg->intervals[i] = cpu_to_le32(req->interval); 354 355 cfg->ssid_len = 0; 356 ret = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req); 357 if (ret < 0) 358 goto out; 359 360 cfg->filter_type = ret; 361 362 wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type); 363 364 cfg_channels = kzalloc(sizeof(*cfg_channels), GFP_KERNEL); 365 if (!cfg_channels) { 366 ret = -ENOMEM; 367 goto out; 368 } 369 370 if (!wlcore_set_scan_chan_params(wl, cfg_channels, req->channels, 371 req->n_channels, req->n_ssids, 372 SCAN_TYPE_PERIODIC)) { 373 wl1271_error("scan channel list is empty"); 374 ret = -EINVAL; 375 goto out; 376 } 377 wl12xx_adjust_channels(cfg, cfg_channels); 378 379 if (!force_passive && cfg->active[0]) { 380 u8 band = IEEE80211_BAND_2GHZ; 381 ret = wl12xx_cmd_build_probe_req(wl, wlvif, 382 wlvif->role_id, band, 383 req->ssids[0].ssid, 384 req->ssids[0].ssid_len, 385 ies->ies[band], 386 ies->len[band], 387 ies->common_ies, 388 ies->common_ie_len, 389 true); 390 if (ret < 0) { 391 wl1271_error("2.4GHz PROBE request template failed"); 392 goto out; 393 } 394 } 395 396 if (!force_passive && cfg->active[1]) { 397 u8 band = IEEE80211_BAND_5GHZ; 398 ret = wl12xx_cmd_build_probe_req(wl, wlvif, 399 wlvif->role_id, band, 400 req->ssids[0].ssid, 401 req->ssids[0].ssid_len, 402 ies->ies[band], 403 ies->len[band], 404 ies->common_ies, 405 ies->common_ie_len, 406 true); 407 if (ret < 0) { 408 wl1271_error("5GHz PROBE request template failed"); 409 goto out; 410 } 411 } 412 413 wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg)); 414 415 ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg, 416 sizeof(*cfg), 0); 417 if (ret < 0) { 418 wl1271_error("SCAN configuration failed"); 419 goto out; 420 } 421 out: 422 kfree(cfg_channels); 423 kfree(cfg); 424 return ret; 425 } 426 427 int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif) 428 { 429 struct wl1271_cmd_sched_scan_start *start; 430 int ret = 0; 431 432 wl1271_debug(DEBUG_CMD, "cmd periodic scan start"); 433 434 if (wlvif->bss_type != BSS_TYPE_STA_BSS) 435 return -EOPNOTSUPP; 436 437 if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) && 438 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) 439 return -EBUSY; 440 441 start = kzalloc(sizeof(*start), GFP_KERNEL); 442 if (!start) 443 return -ENOMEM; 444 445 start->role_id = wlvif->role_id; 446 start->tag = WL1271_SCAN_DEFAULT_TAG; 447 448 ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start, 449 sizeof(*start), 0); 450 if (ret < 0) { 451 wl1271_error("failed to send scan start command"); 452 goto out_free; 453 } 454 455 out_free: 456 kfree(start); 457 return ret; 458 } 459 460 int wl12xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif, 461 struct cfg80211_sched_scan_request *req, 462 struct ieee80211_scan_ies *ies) 463 { 464 int ret; 465 466 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies); 467 if (ret < 0) 468 return ret; 469 470 return wl1271_scan_sched_scan_start(wl, wlvif); 471 } 472 473 void wl12xx_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif) 474 { 475 struct wl1271_cmd_sched_scan_stop *stop; 476 int ret = 0; 477 478 wl1271_debug(DEBUG_CMD, "cmd periodic scan stop"); 479 480 /* FIXME: what to do if alloc'ing to stop fails? */ 481 stop = kzalloc(sizeof(*stop), GFP_KERNEL); 482 if (!stop) { 483 wl1271_error("failed to alloc memory to send sched scan stop"); 484 return; 485 } 486 487 stop->role_id = wlvif->role_id; 488 stop->tag = WL1271_SCAN_DEFAULT_TAG; 489 490 ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop, 491 sizeof(*stop), 0); 492 if (ret < 0) { 493 wl1271_error("failed to send sched scan stop command"); 494 goto out_free; 495 } 496 497 out_free: 498 kfree(stop); 499 } 500 501 int wl12xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif, 502 struct cfg80211_scan_request *req) 503 { 504 wl1271_scan_stm(wl, wlvif); 505 return 0; 506 } 507 508 void wl12xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif) 509 { 510 wl1271_scan_stm(wl, wlvif); 511 } 512