0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/sched.h>
0010 #include "cw1200.h"
0011 #include "scan.h"
0012 #include "sta.h"
0013 #include "pm.h"
0014
0015 static void cw1200_scan_restart_delayed(struct cw1200_common *priv);
0016
0017 static int cw1200_scan_start(struct cw1200_common *priv, struct wsm_scan *scan)
0018 {
0019 int ret, i;
0020 int tmo = 2000;
0021
0022 switch (priv->join_status) {
0023 case CW1200_JOIN_STATUS_PRE_STA:
0024 case CW1200_JOIN_STATUS_JOINING:
0025 return -EBUSY;
0026 default:
0027 break;
0028 }
0029
0030 wiphy_dbg(priv->hw->wiphy, "[SCAN] hw req, type %d, %d channels, flags: 0x%x.\n",
0031 scan->type, scan->num_channels, scan->flags);
0032
0033 for (i = 0; i < scan->num_channels; ++i)
0034 tmo += scan->ch[i].max_chan_time + 10;
0035
0036 cancel_delayed_work_sync(&priv->clear_recent_scan_work);
0037 atomic_set(&priv->scan.in_progress, 1);
0038 atomic_set(&priv->recent_scan, 1);
0039 cw1200_pm_stay_awake(&priv->pm_state, msecs_to_jiffies(tmo));
0040 queue_delayed_work(priv->workqueue, &priv->scan.timeout,
0041 msecs_to_jiffies(tmo));
0042 ret = wsm_scan(priv, scan);
0043 if (ret) {
0044 atomic_set(&priv->scan.in_progress, 0);
0045 cancel_delayed_work_sync(&priv->scan.timeout);
0046 cw1200_scan_restart_delayed(priv);
0047 }
0048 return ret;
0049 }
0050
0051 int cw1200_hw_scan(struct ieee80211_hw *hw,
0052 struct ieee80211_vif *vif,
0053 struct ieee80211_scan_request *hw_req)
0054 {
0055 struct cw1200_common *priv = hw->priv;
0056 struct cfg80211_scan_request *req = &hw_req->req;
0057 struct wsm_template_frame frame = {
0058 .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
0059 };
0060 int i, ret;
0061
0062 if (!priv->vif)
0063 return -EINVAL;
0064
0065
0066 if (priv->join_status == CW1200_JOIN_STATUS_AP)
0067 return -EOPNOTSUPP;
0068
0069 if (req->n_ssids == 1 && !req->ssids[0].ssid_len)
0070 req->n_ssids = 0;
0071
0072 wiphy_dbg(hw->wiphy, "[SCAN] Scan request for %d SSIDs.\n",
0073 req->n_ssids);
0074
0075 if (req->n_ssids > WSM_SCAN_MAX_NUM_OF_SSIDS)
0076 return -EINVAL;
0077
0078 frame.skb = ieee80211_probereq_get(hw, priv->vif->addr, NULL, 0,
0079 req->ie_len);
0080 if (!frame.skb)
0081 return -ENOMEM;
0082
0083 if (req->ie_len)
0084 skb_put_data(frame.skb, req->ie, req->ie_len);
0085
0086
0087 down(&priv->scan.lock);
0088 mutex_lock(&priv->conf_mutex);
0089
0090 ret = wsm_set_template_frame(priv, &frame);
0091 if (!ret) {
0092
0093 ret = wsm_set_probe_responder(priv, true);
0094 }
0095 if (ret) {
0096 mutex_unlock(&priv->conf_mutex);
0097 up(&priv->scan.lock);
0098 dev_kfree_skb(frame.skb);
0099 return ret;
0100 }
0101
0102 wsm_lock_tx(priv);
0103
0104 BUG_ON(priv->scan.req);
0105 priv->scan.req = req;
0106 priv->scan.n_ssids = 0;
0107 priv->scan.status = 0;
0108 priv->scan.begin = &req->channels[0];
0109 priv->scan.curr = priv->scan.begin;
0110 priv->scan.end = &req->channels[req->n_channels];
0111 priv->scan.output_power = priv->output_power;
0112
0113 for (i = 0; i < req->n_ssids; ++i) {
0114 struct wsm_ssid *dst = &priv->scan.ssids[priv->scan.n_ssids];
0115 memcpy(&dst->ssid[0], req->ssids[i].ssid, sizeof(dst->ssid));
0116 dst->length = req->ssids[i].ssid_len;
0117 ++priv->scan.n_ssids;
0118 }
0119
0120 mutex_unlock(&priv->conf_mutex);
0121 dev_kfree_skb(frame.skb);
0122 queue_work(priv->workqueue, &priv->scan.work);
0123 return 0;
0124 }
0125
0126 void cw1200_scan_work(struct work_struct *work)
0127 {
0128 struct cw1200_common *priv = container_of(work, struct cw1200_common,
0129 scan.work);
0130 struct ieee80211_channel **it;
0131 struct wsm_scan scan = {
0132 .type = WSM_SCAN_TYPE_FOREGROUND,
0133 .flags = WSM_SCAN_FLAG_SPLIT_METHOD,
0134 };
0135 bool first_run = (priv->scan.begin == priv->scan.curr &&
0136 priv->scan.begin != priv->scan.end);
0137 int i;
0138
0139 if (first_run) {
0140
0141
0142
0143
0144 if (cancel_delayed_work_sync(&priv->join_timeout) > 0)
0145 cw1200_join_timeout(&priv->join_timeout.work);
0146 }
0147
0148 mutex_lock(&priv->conf_mutex);
0149
0150 if (first_run) {
0151 if (priv->join_status == CW1200_JOIN_STATUS_STA &&
0152 !(priv->powersave_mode.mode & WSM_PSM_PS)) {
0153 struct wsm_set_pm pm = priv->powersave_mode;
0154 pm.mode = WSM_PSM_PS;
0155 cw1200_set_pm(priv, &pm);
0156 } else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
0157
0158
0159
0160 cw1200_disable_listening(priv);
0161 }
0162 }
0163
0164 if (!priv->scan.req || (priv->scan.curr == priv->scan.end)) {
0165 struct cfg80211_scan_info info = {
0166 .aborted = priv->scan.status ? 1 : 0,
0167 };
0168
0169 if (priv->scan.output_power != priv->output_power)
0170 wsm_set_output_power(priv, priv->output_power * 10);
0171 if (priv->join_status == CW1200_JOIN_STATUS_STA &&
0172 !(priv->powersave_mode.mode & WSM_PSM_PS))
0173 cw1200_set_pm(priv, &priv->powersave_mode);
0174
0175 if (priv->scan.status < 0)
0176 wiphy_warn(priv->hw->wiphy,
0177 "[SCAN] Scan failed (%d).\n",
0178 priv->scan.status);
0179 else if (priv->scan.req)
0180 wiphy_dbg(priv->hw->wiphy,
0181 "[SCAN] Scan completed.\n");
0182 else
0183 wiphy_dbg(priv->hw->wiphy,
0184 "[SCAN] Scan canceled.\n");
0185
0186 priv->scan.req = NULL;
0187 cw1200_scan_restart_delayed(priv);
0188 wsm_unlock_tx(priv);
0189 mutex_unlock(&priv->conf_mutex);
0190 ieee80211_scan_completed(priv->hw, &info);
0191 up(&priv->scan.lock);
0192 return;
0193 } else {
0194 struct ieee80211_channel *first = *priv->scan.curr;
0195 for (it = priv->scan.curr + 1, i = 1;
0196 it != priv->scan.end && i < WSM_SCAN_MAX_NUM_OF_CHANNELS;
0197 ++it, ++i) {
0198 if ((*it)->band != first->band)
0199 break;
0200 if (((*it)->flags ^ first->flags) &
0201 IEEE80211_CHAN_NO_IR)
0202 break;
0203 if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
0204 (*it)->max_power != first->max_power)
0205 break;
0206 }
0207 scan.band = first->band;
0208
0209 if (priv->scan.req->no_cck)
0210 scan.max_tx_rate = WSM_TRANSMIT_RATE_6;
0211 else
0212 scan.max_tx_rate = WSM_TRANSMIT_RATE_1;
0213 scan.num_probes =
0214 (first->flags & IEEE80211_CHAN_NO_IR) ? 0 : 2;
0215 scan.num_ssids = priv->scan.n_ssids;
0216 scan.ssids = &priv->scan.ssids[0];
0217 scan.num_channels = it - priv->scan.curr;
0218
0219 scan.probe_delay = 100;
0220
0221
0222
0223
0224 if (priv->join_status == CW1200_JOIN_STATUS_STA) {
0225 scan.type = WSM_SCAN_TYPE_BACKGROUND;
0226 scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
0227 }
0228 scan.ch = kcalloc(it - priv->scan.curr,
0229 sizeof(struct wsm_scan_ch),
0230 GFP_KERNEL);
0231 if (!scan.ch) {
0232 priv->scan.status = -ENOMEM;
0233 goto fail;
0234 }
0235 for (i = 0; i < scan.num_channels; ++i) {
0236 scan.ch[i].number = priv->scan.curr[i]->hw_value;
0237 if (priv->scan.curr[i]->flags & IEEE80211_CHAN_NO_IR) {
0238 scan.ch[i].min_chan_time = 50;
0239 scan.ch[i].max_chan_time = 100;
0240 } else {
0241 scan.ch[i].min_chan_time = 10;
0242 scan.ch[i].max_chan_time = 25;
0243 }
0244 }
0245 if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
0246 priv->scan.output_power != first->max_power) {
0247 priv->scan.output_power = first->max_power;
0248 wsm_set_output_power(priv,
0249 priv->scan.output_power * 10);
0250 }
0251 priv->scan.status = cw1200_scan_start(priv, &scan);
0252 kfree(scan.ch);
0253 if (priv->scan.status)
0254 goto fail;
0255 priv->scan.curr = it;
0256 }
0257 mutex_unlock(&priv->conf_mutex);
0258 return;
0259
0260 fail:
0261 priv->scan.curr = priv->scan.end;
0262 mutex_unlock(&priv->conf_mutex);
0263 queue_work(priv->workqueue, &priv->scan.work);
0264 return;
0265 }
0266
0267 static void cw1200_scan_restart_delayed(struct cw1200_common *priv)
0268 {
0269
0270 if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
0271 cw1200_enable_listening(priv);
0272 cw1200_update_filtering(priv);
0273 }
0274
0275 if (priv->delayed_unjoin) {
0276 priv->delayed_unjoin = false;
0277 if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
0278 wsm_unlock_tx(priv);
0279 } else if (priv->delayed_link_loss) {
0280 wiphy_dbg(priv->hw->wiphy, "[CQM] Requeue BSS loss.\n");
0281 priv->delayed_link_loss = 0;
0282 cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
0283 }
0284 }
0285
0286 static void cw1200_scan_complete(struct cw1200_common *priv)
0287 {
0288 queue_delayed_work(priv->workqueue, &priv->clear_recent_scan_work, HZ);
0289 if (priv->scan.direct_probe) {
0290 wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe complete.\n");
0291 cw1200_scan_restart_delayed(priv);
0292 priv->scan.direct_probe = 0;
0293 up(&priv->scan.lock);
0294 wsm_unlock_tx(priv);
0295 } else {
0296 cw1200_scan_work(&priv->scan.work);
0297 }
0298 }
0299
0300 void cw1200_scan_failed_cb(struct cw1200_common *priv)
0301 {
0302 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
0303
0304 return;
0305
0306 if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
0307 priv->scan.status = -EIO;
0308 queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
0309 }
0310 }
0311
0312
0313 void cw1200_scan_complete_cb(struct cw1200_common *priv,
0314 struct wsm_scan_complete *arg)
0315 {
0316 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
0317
0318 return;
0319
0320 if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
0321 priv->scan.status = 1;
0322 queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
0323 }
0324 }
0325
0326 void cw1200_clear_recent_scan_work(struct work_struct *work)
0327 {
0328 struct cw1200_common *priv =
0329 container_of(work, struct cw1200_common,
0330 clear_recent_scan_work.work);
0331 atomic_xchg(&priv->recent_scan, 0);
0332 }
0333
0334 void cw1200_scan_timeout(struct work_struct *work)
0335 {
0336 struct cw1200_common *priv =
0337 container_of(work, struct cw1200_common, scan.timeout.work);
0338 if (atomic_xchg(&priv->scan.in_progress, 0)) {
0339 if (priv->scan.status > 0) {
0340 priv->scan.status = 0;
0341 } else if (!priv->scan.status) {
0342 wiphy_warn(priv->hw->wiphy,
0343 "Timeout waiting for scan complete notification.\n");
0344 priv->scan.status = -ETIMEDOUT;
0345 priv->scan.curr = priv->scan.end;
0346 wsm_stop_scan(priv);
0347 }
0348 cw1200_scan_complete(priv);
0349 }
0350 }
0351
0352 void cw1200_probe_work(struct work_struct *work)
0353 {
0354 struct cw1200_common *priv =
0355 container_of(work, struct cw1200_common, scan.probe_work.work);
0356 u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
0357 struct cw1200_queue *queue = &priv->tx_queue[queue_id];
0358 const struct cw1200_txpriv *txpriv;
0359 struct wsm_tx *wsm;
0360 struct wsm_template_frame frame = {
0361 .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
0362 };
0363 struct wsm_ssid ssids[1] = {{
0364 .length = 0,
0365 } };
0366 struct wsm_scan_ch ch[1] = {{
0367 .min_chan_time = 0,
0368 .max_chan_time = 10,
0369 } };
0370 struct wsm_scan scan = {
0371 .type = WSM_SCAN_TYPE_FOREGROUND,
0372 .num_probes = 1,
0373 .probe_delay = 0,
0374 .num_channels = 1,
0375 .ssids = ssids,
0376 .ch = ch,
0377 };
0378 u8 *ies;
0379 size_t ies_len;
0380 int ret;
0381
0382 wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe work.\n");
0383
0384 mutex_lock(&priv->conf_mutex);
0385 if (down_trylock(&priv->scan.lock)) {
0386
0387 schedule();
0388 queue_delayed_work(priv->workqueue, &priv->scan.probe_work,
0389 msecs_to_jiffies(100));
0390 mutex_unlock(&priv->conf_mutex);
0391 return;
0392 }
0393
0394
0395 if (cw1200_queue_get_skb(queue, priv->pending_frame_id,
0396 &frame.skb, &txpriv)) {
0397 up(&priv->scan.lock);
0398 mutex_unlock(&priv->conf_mutex);
0399 wsm_unlock_tx(priv);
0400 return;
0401 }
0402 wsm = (struct wsm_tx *)frame.skb->data;
0403 scan.max_tx_rate = wsm->max_tx_rate;
0404 scan.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
0405 WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
0406 if (priv->join_status == CW1200_JOIN_STATUS_STA ||
0407 priv->join_status == CW1200_JOIN_STATUS_IBSS) {
0408 scan.type = WSM_SCAN_TYPE_BACKGROUND;
0409 scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
0410 }
0411 ch[0].number = priv->channel->hw_value;
0412
0413 skb_pull(frame.skb, txpriv->offset);
0414
0415 ies = &frame.skb->data[sizeof(struct ieee80211_hdr_3addr)];
0416 ies_len = frame.skb->len - sizeof(struct ieee80211_hdr_3addr);
0417
0418 if (ies_len) {
0419 u8 *ssidie =
0420 (u8 *)cfg80211_find_ie(WLAN_EID_SSID, ies, ies_len);
0421 if (ssidie && ssidie[1] && ssidie[1] <= sizeof(ssids[0].ssid)) {
0422 u8 *nextie = &ssidie[2 + ssidie[1]];
0423
0424
0425
0426
0427
0428 ssids[0].length = ssidie[1];
0429 memcpy(ssids[0].ssid, &ssidie[2], ssids[0].length);
0430 scan.num_ssids = 1;
0431
0432
0433 ssidie[1] = 0;
0434 memmove(&ssidie[2], nextie, &ies[ies_len] - nextie);
0435 skb_trim(frame.skb, frame.skb->len - ssids[0].length);
0436 }
0437 }
0438
0439
0440 if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
0441 cw1200_disable_listening(priv);
0442 ret = wsm_set_template_frame(priv, &frame);
0443 priv->scan.direct_probe = 1;
0444 if (!ret) {
0445 wsm_flush_tx(priv);
0446 ret = cw1200_scan_start(priv, &scan);
0447 }
0448 mutex_unlock(&priv->conf_mutex);
0449
0450 skb_push(frame.skb, txpriv->offset);
0451 if (!ret)
0452 IEEE80211_SKB_CB(frame.skb)->flags |= IEEE80211_TX_STAT_ACK;
0453 BUG_ON(cw1200_queue_remove(queue, priv->pending_frame_id));
0454
0455 if (ret) {
0456 priv->scan.direct_probe = 0;
0457 up(&priv->scan.lock);
0458 wsm_unlock_tx(priv);
0459 }
0460
0461 return;
0462 }