Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 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 "wl1271.h" |
| 27 | #include "wl1271_cmd.h" |
| 28 | #include "wl1271_scan.h" |
| 29 | #include "wl1271_acx.h" |
| 30 | |
Juuso Oikarinen | c454f1d | 2010-08-24 06:28:03 +0300 | [diff] [blame^] | 31 | void wl1271_scan_complete_work(struct work_struct *work) |
| 32 | { |
| 33 | struct wl1271 *wl = |
| 34 | container_of(work, struct wl1271, scan_complete_work); |
| 35 | |
| 36 | wl1271_debug(DEBUG_SCAN, "Scanning complete"); |
| 37 | |
| 38 | mutex_lock(&wl->mutex); |
| 39 | wl->scan.state = WL1271_SCAN_STATE_IDLE; |
| 40 | kfree(wl->scan.scanned_ch); |
| 41 | wl->scan.scanned_ch = NULL; |
| 42 | mutex_unlock(&wl->mutex); |
| 43 | |
| 44 | ieee80211_scan_completed(wl->hw, false); |
| 45 | } |
| 46 | |
| 47 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 48 | static int wl1271_get_scan_channels(struct wl1271 *wl, |
| 49 | struct cfg80211_scan_request *req, |
| 50 | struct basic_scan_channel_params *channels, |
| 51 | enum ieee80211_band band, bool passive) |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 52 | { |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 53 | int i, j; |
| 54 | u32 flags; |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 55 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 56 | for (i = 0, j = 0; |
| 57 | i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS; |
| 58 | i++) { |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 59 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 60 | flags = req->channels[i]->flags; |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 61 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 62 | if (!wl->scan.scanned_ch[i] && |
| 63 | !(flags & IEEE80211_CHAN_DISABLED) && |
| 64 | ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) && |
| 65 | (req->channels[i]->band == band)) { |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 66 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 67 | wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ", |
| 68 | req->channels[i]->band, |
| 69 | req->channels[i]->center_freq); |
| 70 | wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X", |
| 71 | req->channels[i]->hw_value, |
| 72 | req->channels[i]->flags); |
| 73 | wl1271_debug(DEBUG_SCAN, |
| 74 | "max_antenna_gain %d, max_power %d", |
| 75 | req->channels[i]->max_antenna_gain, |
| 76 | req->channels[i]->max_power); |
| 77 | wl1271_debug(DEBUG_SCAN, "beacon_found %d", |
| 78 | req->channels[i]->beacon_found); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 79 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 80 | channels[j].min_duration = |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 81 | cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION); |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 82 | channels[j].max_duration = |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 83 | cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION); |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 84 | channels[j].early_termination = 0; |
Luciano Coelho | 3cc7b54 | 2010-07-08 17:50:08 +0300 | [diff] [blame] | 85 | channels[j].tx_power_att = req->channels[i]->max_power; |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 86 | channels[j].channel = req->channels[i]->hw_value; |
| 87 | |
| 88 | memset(&channels[j].bssid_lsb, 0xff, 4); |
| 89 | memset(&channels[j].bssid_msb, 0xff, 2); |
| 90 | |
| 91 | /* Mark the channels we already used */ |
| 92 | wl->scan.scanned_ch[i] = true; |
| 93 | |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 94 | j++; |
| 95 | } |
| 96 | } |
| 97 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 98 | return j; |
| 99 | } |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 100 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 101 | #define WL1271_NOTHING_TO_SCAN 1 |
| 102 | |
| 103 | static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band, |
| 104 | bool passive, u32 basic_rate) |
| 105 | { |
| 106 | struct wl1271_cmd_scan *cmd; |
| 107 | struct wl1271_cmd_trigger_scan_to *trigger; |
| 108 | int ret; |
| 109 | u16 scan_options = 0; |
| 110 | |
| 111 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 112 | trigger = kzalloc(sizeof(*trigger), GFP_KERNEL); |
| 113 | if (!cmd || !trigger) { |
| 114 | ret = -ENOMEM; |
| 115 | goto out; |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 116 | } |
| 117 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 118 | /* We always use high priority scans */ |
| 119 | scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH; |
Luciano Coelho | 4f35c02 | 2010-08-04 04:36:32 +0300 | [diff] [blame] | 120 | |
| 121 | /* No SSIDs means that we have a forced passive scan */ |
| 122 | if (passive || wl->scan.req->n_ssids == 0) |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 123 | scan_options |= WL1271_SCAN_OPT_PASSIVE; |
Luciano Coelho | 4f35c02 | 2010-08-04 04:36:32 +0300 | [diff] [blame] | 124 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 125 | cmd->params.scan_options = cpu_to_le16(scan_options); |
| 126 | |
| 127 | cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req, |
| 128 | cmd->channels, |
| 129 | band, passive); |
| 130 | if (cmd->params.n_ch == 0) { |
| 131 | ret = WL1271_NOTHING_TO_SCAN; |
| 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | cmd->params.tx_rate = cpu_to_le32(basic_rate); |
| 136 | cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD); |
| 137 | cmd->params.rx_filter_options = |
| 138 | cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN); |
| 139 | |
| 140 | cmd->params.n_probe_reqs = WL1271_SCAN_PROBE_REQS; |
| 141 | cmd->params.tx_rate = cpu_to_le32(basic_rate); |
| 142 | cmd->params.tid_trigger = 0; |
| 143 | cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG; |
| 144 | |
| 145 | if (band == IEEE80211_BAND_2GHZ) |
| 146 | cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ; |
| 147 | else |
| 148 | cmd->params.band = WL1271_SCAN_BAND_5_GHZ; |
| 149 | |
| 150 | if (wl->scan.ssid_len && wl->scan.ssid) { |
| 151 | cmd->params.ssid_len = wl->scan.ssid_len; |
| 152 | memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len); |
| 153 | } |
| 154 | |
| 155 | ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len, |
| 156 | wl->scan.req->ie, wl->scan.req->ie_len, |
| 157 | band); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 158 | if (ret < 0) { |
| 159 | wl1271_error("PROBE request template failed"); |
| 160 | goto out; |
| 161 | } |
| 162 | |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 163 | /* disable the timeout */ |
| 164 | trigger->timeout = 0; |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 165 | ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger, |
| 166 | sizeof(*trigger), 0); |
| 167 | if (ret < 0) { |
| 168 | wl1271_error("trigger scan to failed for hw scan"); |
| 169 | goto out; |
| 170 | } |
| 171 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 172 | wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd)); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 173 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 174 | ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 175 | if (ret < 0) { |
| 176 | wl1271_error("SCAN failed"); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 177 | goto out; |
| 178 | } |
| 179 | |
| 180 | out: |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 181 | kfree(cmd); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 182 | kfree(trigger); |
| 183 | return ret; |
| 184 | } |
| 185 | |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 186 | void wl1271_scan_stm(struct wl1271 *wl) |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 187 | { |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | switch (wl->scan.state) { |
| 191 | case WL1271_SCAN_STATE_IDLE: |
| 192 | break; |
| 193 | |
| 194 | case WL1271_SCAN_STATE_2GHZ_ACTIVE: |
| 195 | ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false, |
| 196 | wl->conf.tx.basic_rate); |
| 197 | if (ret == WL1271_NOTHING_TO_SCAN) { |
| 198 | wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE; |
| 199 | wl1271_scan_stm(wl); |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 200 | } |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 201 | |
| 202 | break; |
| 203 | |
| 204 | case WL1271_SCAN_STATE_2GHZ_PASSIVE: |
| 205 | ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true, |
| 206 | wl->conf.tx.basic_rate); |
| 207 | if (ret == WL1271_NOTHING_TO_SCAN) { |
Juuso Oikarinen | 02fabb0 | 2010-08-19 04:41:15 +0200 | [diff] [blame] | 208 | if (wl->enable_11a) |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 209 | wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE; |
| 210 | else |
| 211 | wl->scan.state = WL1271_SCAN_STATE_DONE; |
| 212 | wl1271_scan_stm(wl); |
| 213 | } |
| 214 | |
| 215 | break; |
| 216 | |
| 217 | case WL1271_SCAN_STATE_5GHZ_ACTIVE: |
| 218 | ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false, |
| 219 | wl->conf.tx.basic_rate_5); |
| 220 | if (ret == WL1271_NOTHING_TO_SCAN) { |
| 221 | wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE; |
| 222 | wl1271_scan_stm(wl); |
| 223 | } |
| 224 | |
| 225 | break; |
| 226 | |
| 227 | case WL1271_SCAN_STATE_5GHZ_PASSIVE: |
| 228 | ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true, |
| 229 | wl->conf.tx.basic_rate_5); |
| 230 | if (ret == WL1271_NOTHING_TO_SCAN) { |
| 231 | wl->scan.state = WL1271_SCAN_STATE_DONE; |
| 232 | wl1271_scan_stm(wl); |
| 233 | } |
| 234 | |
| 235 | break; |
| 236 | |
| 237 | case WL1271_SCAN_STATE_DONE: |
Juuso Oikarinen | c454f1d | 2010-08-24 06:28:03 +0300 | [diff] [blame^] | 238 | ieee80211_queue_work(wl->hw, &wl->scan_complete_work); |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 239 | break; |
| 240 | |
| 241 | default: |
| 242 | wl1271_error("invalid scan state"); |
| 243 | break; |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 244 | } |
Luciano Coelho | 08688d6 | 2010-07-08 17:50:07 +0300 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len, |
| 248 | struct cfg80211_scan_request *req) |
| 249 | { |
| 250 | if (wl->scan.state != WL1271_SCAN_STATE_IDLE) |
| 251 | return -EBUSY; |
| 252 | |
| 253 | wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE; |
| 254 | |
| 255 | if (ssid_len && ssid) { |
| 256 | wl->scan.ssid_len = ssid_len; |
| 257 | memcpy(wl->scan.ssid, ssid, ssid_len); |
| 258 | } else { |
| 259 | wl->scan.ssid_len = 0; |
| 260 | } |
| 261 | |
| 262 | wl->scan.req = req; |
| 263 | |
| 264 | wl->scan.scanned_ch = kzalloc(req->n_channels * |
| 265 | sizeof(*wl->scan.scanned_ch), |
| 266 | GFP_KERNEL); |
| 267 | wl1271_scan_stm(wl); |
| 268 | |
Luciano Coelho | 34dd2aa | 2010-07-08 17:50:06 +0300 | [diff] [blame] | 269 | return 0; |
| 270 | } |