1 /******************************************************************************
5 * Copyright(c) 2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
24 * Contact Information:
25 * Tomas Winkler <tomas.winkler@intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28 #include <net/mac80211.h>
29 #include <linux/etherdevice.h>
31 #include "iwl-eeprom.h"
36 #include "iwl-helpers.h"
38 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
39 * sending probe req. This should be set long enough to hear probe responses
40 * from more than one AP. */
41 #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
42 #define IWL_ACTIVE_DWELL_TIME_52 (20)
44 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
45 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
47 /* For faster active scanning, scan will move to the next channel if fewer than
48 * PLCP_QUIET_THRESH packets are heard on this channel within
49 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
50 * time if it's a quiet channel (nothing responded to our probe, and there's
52 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
53 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
54 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
56 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
57 * Must be set longer than active dwell time.
58 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
59 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
60 #define IWL_PASSIVE_DWELL_TIME_52 (10)
61 #define IWL_PASSIVE_DWELL_BASE (100)
62 #define IWL_CHANNEL_TUNE_TIME 5
64 #define IWL_SCAN_PROBE_MASK(n) cpu_to_le32((BIT(n) | (BIT(n) - BIT(1))))
67 static int scan_tx_ant[3] = {
68 RATE_MCS_ANT_A_MSK, RATE_MCS_ANT_B_MSK, RATE_MCS_ANT_C_MSK
73 static int iwl_is_empty_essid(const char *essid, int essid_len)
75 /* Single white space is for Linksys APs */
76 if (essid_len == 1 && essid[0] == ' ')
79 /* Otherwise, if the entire essid is 0, we assume it is hidden */
82 if (essid[essid_len] != '\0')
91 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
93 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
94 const char *s = essid;
97 if (iwl_is_empty_essid(essid, essid_len)) {
98 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
102 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
103 while (essid_len--) {
116 * iwl_scan_cancel - Cancel any currently executing HW scan
118 * NOTE: priv->mutex is not required before calling this function
120 int iwl_scan_cancel(struct iwl_priv *priv)
122 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
123 clear_bit(STATUS_SCANNING, &priv->status);
127 if (test_bit(STATUS_SCANNING, &priv->status)) {
128 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
129 IWL_DEBUG_SCAN("Queuing scan abort.\n");
130 set_bit(STATUS_SCAN_ABORTING, &priv->status);
131 queue_work(priv->workqueue, &priv->abort_scan);
134 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
136 return test_bit(STATUS_SCANNING, &priv->status);
141 EXPORT_SYMBOL(iwl_scan_cancel);
143 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
144 * @ms: amount of time to wait (in milliseconds) for scan to abort
146 * NOTE: priv->mutex must be held before calling this function
148 int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
150 unsigned long now = jiffies;
153 ret = iwl_scan_cancel(priv);
155 mutex_unlock(&priv->mutex);
156 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
157 test_bit(STATUS_SCANNING, &priv->status))
159 mutex_lock(&priv->mutex);
161 return test_bit(STATUS_SCANNING, &priv->status);
166 EXPORT_SYMBOL(iwl_scan_cancel_timeout);
168 static int iwl_send_scan_abort(struct iwl_priv *priv)
171 struct iwl_rx_packet *res;
172 struct iwl_host_cmd cmd = {
173 .id = REPLY_SCAN_ABORT_CMD,
174 .meta.flags = CMD_WANT_SKB,
177 /* If there isn't a scan actively going on in the hardware
178 * then we are in between scan bands and not actually
179 * actively scanning, so don't send the abort command */
180 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
181 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
185 ret = iwl_send_cmd_sync(priv, &cmd);
187 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
191 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
192 if (res->u.status != CAN_ABORT_STATUS) {
193 /* The scan abort will return 1 for success or
194 * 2 for "failure". A failure condition can be
195 * due to simply not being in an active scan which
196 * can occur if we send the scan abort before we
197 * the microcode has notified us that a scan is
199 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
200 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
201 clear_bit(STATUS_SCAN_HW, &priv->status);
204 priv->alloc_rxb_skb--;
205 dev_kfree_skb_any(cmd.meta.u.skb);
211 /* Service response to REPLY_SCAN_CMD (0x80) */
212 static void iwl_rx_reply_scan(struct iwl_priv *priv,
213 struct iwl_rx_mem_buffer *rxb)
215 #ifdef CONFIG_IWLWIFI_DEBUG
216 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
217 struct iwl_scanreq_notification *notif =
218 (struct iwl_scanreq_notification *)pkt->u.raw;
220 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
224 /* Service SCAN_START_NOTIFICATION (0x82) */
225 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
226 struct iwl_rx_mem_buffer *rxb)
228 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
229 struct iwl_scanstart_notification *notif =
230 (struct iwl_scanstart_notification *)pkt->u.raw;
231 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
232 IWL_DEBUG_SCAN("Scan start: "
234 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
236 notif->band ? "bg" : "a",
237 le32_to_cpu(notif->tsf_high),
238 le32_to_cpu(notif->tsf_low),
239 notif->status, notif->beacon_timer);
242 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
243 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
244 struct iwl_rx_mem_buffer *rxb)
246 #ifdef CONFIG_IWLWIFI_DEBUG
247 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
248 struct iwl_scanresults_notification *notif =
249 (struct iwl_scanresults_notification *)pkt->u.raw;
251 IWL_DEBUG_SCAN("Scan ch.res: "
253 "(TSF: 0x%08X:%08X) - %d "
254 "elapsed=%lu usec (%dms since last)\n",
256 notif->band ? "bg" : "a",
257 le32_to_cpu(notif->tsf_high),
258 le32_to_cpu(notif->tsf_low),
259 le32_to_cpu(notif->statistics[0]),
260 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
261 jiffies_to_msecs(elapsed_jiffies
262 (priv->last_scan_jiffies, jiffies)));
265 priv->last_scan_jiffies = jiffies;
266 priv->next_scan_jiffies = 0;
269 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
270 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
271 struct iwl_rx_mem_buffer *rxb)
273 #ifdef CONFIG_IWLWIFI_DEBUG
274 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
275 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
277 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
278 scan_notif->scanned_channels,
280 scan_notif->tsf_high, scan_notif->status);
283 /* The HW is no longer scanning */
284 clear_bit(STATUS_SCAN_HW, &priv->status);
286 /* The scan completion notification came in, so kill that timer... */
287 cancel_delayed_work(&priv->scan_check);
289 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
290 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
292 jiffies_to_msecs(elapsed_jiffies
293 (priv->scan_pass_start, jiffies)));
295 /* Remove this scanned band from the list of pending
296 * bands to scan, band G precedes A in order of scanning
297 * as seen in iwl_bg_request_scan */
298 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
299 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
300 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
301 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
303 /* If a request to abort was given, or the scan did not succeed
304 * then we reset the scan state machine and terminate,
305 * re-queuing another scan if one has been requested */
306 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
307 IWL_DEBUG_INFO("Aborted scan completed.\n");
308 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
310 /* If there are more bands on this scan pass reschedule */
311 if (priv->scan_bands)
315 priv->last_scan_jiffies = jiffies;
316 priv->next_scan_jiffies = 0;
317 IWL_DEBUG_INFO("Setting scan to off\n");
319 clear_bit(STATUS_SCANNING, &priv->status);
321 IWL_DEBUG_INFO("Scan took %dms\n",
322 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
324 queue_work(priv->workqueue, &priv->scan_completed);
329 priv->scan_pass_start = jiffies;
330 queue_work(priv->workqueue, &priv->request_scan);
333 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
336 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
337 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
338 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
339 iwl_rx_scan_results_notif;
340 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
341 iwl_rx_scan_complete_notif;
343 EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
345 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
346 enum ieee80211_band band,
349 if (band == IEEE80211_BAND_5GHZ)
350 return IWL_ACTIVE_DWELL_TIME_52 +
351 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
353 return IWL_ACTIVE_DWELL_TIME_24 +
354 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
357 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
358 enum ieee80211_band band)
360 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
361 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
362 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
364 if (iwl_is_associated(priv)) {
365 /* If we're associated, we clamp the maximum passive
366 * dwell time to be 98% of the beacon interval (minus
367 * 2 * channel tune time) */
368 passive = priv->beacon_int;
369 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
370 passive = IWL_PASSIVE_DWELL_BASE;
371 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
377 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
378 enum ieee80211_band band,
379 u8 is_active, u8 n_probes,
380 struct iwl_scan_channel *scan_ch)
382 const struct ieee80211_channel *channels = NULL;
383 const struct ieee80211_supported_band *sband;
384 const struct iwl_channel_info *ch_info;
385 u16 passive_dwell = 0;
386 u16 active_dwell = 0;
390 sband = iwl_get_hw_mode(priv, band);
394 channels = sband->channels;
396 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
397 passive_dwell = iwl_get_passive_dwell_time(priv, band);
399 if (passive_dwell <= active_dwell)
400 passive_dwell = active_dwell + 1;
402 for (i = 0, added = 0; i < sband->n_channels; i++) {
403 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
407 ieee80211_frequency_to_channel(channels[i].center_freq);
408 scan_ch->channel = cpu_to_le16(channel);
410 ch_info = iwl_get_channel_info(priv, band, channel);
411 if (!is_channel_valid(ch_info)) {
412 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
417 if (!is_active || is_channel_passive(ch_info) ||
418 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
419 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
421 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
424 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
426 scan_ch->active_dwell = cpu_to_le16(active_dwell);
427 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
429 /* Set txpower levels to defaults */
430 scan_ch->dsp_atten = 110;
432 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
434 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
436 if (band == IEEE80211_BAND_5GHZ)
437 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
439 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
441 IWL_DEBUG_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n",
442 channel, le32_to_cpu(scan_ch->type),
443 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
444 "ACTIVE" : "PASSIVE",
445 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
446 active_dwell : passive_dwell);
452 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
456 void iwl_init_scan_params(struct iwl_priv *priv)
458 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
459 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = RATE_MCS_ANT_INIT_IND;
460 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
461 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = RATE_MCS_ANT_INIT_IND;
464 int iwl_scan_initiate(struct iwl_priv *priv)
466 if (!iwl_is_ready_rf(priv)) {
467 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
471 if (test_bit(STATUS_SCANNING, &priv->status)) {
472 IWL_DEBUG_SCAN("Scan already in progress.\n");
476 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
477 IWL_DEBUG_SCAN("Scan request while abort pending\n");
481 IWL_DEBUG_INFO("Starting scan...\n");
482 if (priv->cfg->sku & IWL_SKU_G)
483 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
484 if (priv->cfg->sku & IWL_SKU_A)
485 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
486 set_bit(STATUS_SCANNING, &priv->status);
487 priv->scan_start = jiffies;
488 priv->scan_pass_start = priv->scan_start;
490 queue_work(priv->workqueue, &priv->request_scan);
494 EXPORT_SYMBOL(iwl_scan_initiate);
496 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
498 static void iwl_bg_scan_check(struct work_struct *data)
500 struct iwl_priv *priv =
501 container_of(data, struct iwl_priv, scan_check.work);
503 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
506 mutex_lock(&priv->mutex);
507 if (test_bit(STATUS_SCANNING, &priv->status) ||
508 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
509 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
511 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
513 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
514 iwl_send_scan_abort(priv);
516 mutex_unlock(&priv->mutex);
519 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
521 * return : set the bit for each supported rate insert in ie
523 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
524 u16 basic_rate, int *left)
526 u16 ret_rates = 0, bit;
531 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
532 if (bit & supported_rate) {
534 rates[*cnt] = iwl_rates[i].ieee |
535 ((bit & basic_rate) ? 0x80 : 0x00);
539 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
548 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
551 struct ieee80211_ht_cap *ht_cap;
553 if (!sband || !sband->ht_info.ht_supported)
556 if (*left < sizeof(struct ieee80211_ht_cap))
559 *pos++ = sizeof(struct ieee80211_ht_cap);
560 ht_cap = (struct ieee80211_ht_cap *) pos;
562 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
563 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
564 ht_cap->ampdu_params_info =
565 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
566 ((sband->ht_info.ampdu_density << 2) &
567 IEEE80211_HT_CAP_AMPDU_DENSITY);
568 *left -= sizeof(struct ieee80211_ht_cap);
572 * iwl_fill_probe_req - fill in all required fields and IE for probe request
575 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
576 enum ieee80211_band band,
577 struct ieee80211_mgmt *frame,
582 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
583 const struct ieee80211_supported_band *sband =
584 iwl_get_hw_mode(priv, band);
587 /* Make sure there is enough space for the probe request,
588 * two mandatory IEs and the data */
593 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
594 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
595 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
596 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
602 pos = &frame->u.probe_req.variable[0];
604 /* fill in our indirect SSID IE */
608 *pos++ = WLAN_EID_SSID;
613 /* fill in supported rate */
618 *pos++ = WLAN_EID_SUPP_RATES;
621 /* exclude 60M rate */
622 active_rates = priv->rates_mask;
623 active_rates &= ~IWL_RATE_60M_MASK;
625 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
627 cck_rates = IWL_CCK_RATES_MASK & active_rates;
628 ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
629 active_rate_basic, &left);
630 active_rates &= ~ret_rates;
632 ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
633 active_rate_basic, &left);
634 active_rates &= ~ret_rates;
639 if (active_rates == 0)
642 /* fill in supported extended rate */
647 /* ... fill it in... */
648 *pos++ = WLAN_EID_EXT_SUPP_RATES;
650 iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left);
664 *pos++ = WLAN_EID_HT_CAPABILITY;
666 iwl_ht_cap_to_ie(sband, pos, &left);
673 static u32 iwl_scan_tx_ant(struct iwl_priv *priv, enum ieee80211_band band)
677 ind = priv->scan_tx_ant[band];
678 for (i = 0; i < priv->hw_params.tx_chains_num; i++) {
679 ind = (ind+1) >= priv->hw_params.tx_chains_num ? 0 : ind+1;
680 if (priv->hw_params.valid_tx_ant & (1 << ind)) {
681 priv->scan_tx_ant[band] = ind;
685 IWL_DEBUG_SCAN("select TX ANT = %c\n", 'A' + ind);
686 return scan_tx_ant[ind];
690 static void iwl_bg_request_scan(struct work_struct *data)
692 struct iwl_priv *priv =
693 container_of(data, struct iwl_priv, request_scan);
694 struct iwl_host_cmd cmd = {
695 .id = REPLY_SCAN_CMD,
696 .len = sizeof(struct iwl_scan_cmd),
697 .meta.flags = CMD_SIZE_HUGE,
699 struct iwl_scan_cmd *scan;
700 struct ieee80211_conf *conf = NULL;
704 enum ieee80211_band band;
706 u8 rx_chain = priv->hw_params.valid_rx_ant;
708 conf = ieee80211_get_hw_conf(priv->hw);
710 mutex_lock(&priv->mutex);
712 if (!iwl_is_ready(priv)) {
713 IWL_WARNING("request scan called when driver not ready.\n");
717 /* Make sure the scan wasn't cancelled before this queued work
718 * was given the chance to run... */
719 if (!test_bit(STATUS_SCANNING, &priv->status))
722 /* This should never be called or scheduled if there is currently
723 * a scan active in the hardware. */
724 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
725 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
726 "Ignoring second request.\n");
731 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
732 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
736 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
737 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
741 if (iwl_is_rfkill(priv)) {
742 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
746 if (!test_bit(STATUS_READY, &priv->status)) {
747 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
751 if (!priv->scan_bands) {
752 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
757 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
758 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
765 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
767 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
768 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
770 if (iwl_is_associated(priv)) {
773 u32 suspend_time = 100;
774 u32 scan_suspend_time = 100;
777 IWL_DEBUG_INFO("Scanning while associated...\n");
779 spin_lock_irqsave(&priv->lock, flags);
780 interval = priv->beacon_int;
781 spin_unlock_irqrestore(&priv->lock, flags);
783 scan->suspend_time = 0;
784 scan->max_out_time = cpu_to_le32(200 * 1024);
786 interval = suspend_time;
788 extra = (suspend_time / interval) << 22;
789 scan_suspend_time = (extra |
790 ((suspend_time % interval) * 1024));
791 scan->suspend_time = cpu_to_le32(scan_suspend_time);
792 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
793 scan_suspend_time, interval);
796 /* We should add the ability for user to lock to PASSIVE ONLY */
797 if (priv->one_direct_scan) {
798 IWL_DEBUG_SCAN("Start direct scan for '%s'\n",
799 iwl_escape_essid(priv->direct_ssid,
800 priv->direct_ssid_len));
801 scan->direct_scan[0].id = WLAN_EID_SSID;
802 scan->direct_scan[0].len = priv->direct_ssid_len;
803 memcpy(scan->direct_scan[0].ssid,
804 priv->direct_ssid, priv->direct_ssid_len);
806 } else if (!iwl_is_associated(priv) && priv->essid_len) {
807 IWL_DEBUG_SCAN("Start direct scan for '%s' (not associated)\n",
808 iwl_escape_essid(priv->essid, priv->essid_len));
809 scan->direct_scan[0].id = WLAN_EID_SSID;
810 scan->direct_scan[0].len = priv->essid_len;
811 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
814 IWL_DEBUG_SCAN("Start indirect scan.\n");
817 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
818 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
819 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
822 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
823 band = IEEE80211_BAND_2GHZ;
824 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
825 tx_ant = iwl_scan_tx_ant(priv, band);
826 if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK)
827 scan->tx_cmd.rate_n_flags =
828 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
831 scan->tx_cmd.rate_n_flags =
832 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
835 scan->good_CRC_th = 0;
836 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
837 band = IEEE80211_BAND_5GHZ;
838 tx_ant = iwl_scan_tx_ant(priv, band);
839 scan->tx_cmd.rate_n_flags =
840 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
842 scan->good_CRC_th = IWL_GOOD_CRC_TH;
844 /* Force use of chains B and C (0x6) for scan Rx for 4965
845 * Avoid A (0x1) because of its off-channel reception on A-band.
847 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
850 IWL_WARNING("Invalid scan band count\n");
854 /* MIMO is not used here, but value is required */
855 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
856 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
857 (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) |
858 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
860 cmd_len = iwl_fill_probe_req(priv, band,
861 (struct ieee80211_mgmt *)scan->data,
862 IWL_MAX_SCAN_SIZE - sizeof(*scan));
864 scan->tx_cmd.len = cpu_to_le16(cmd_len);
866 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
867 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
869 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
870 RXON_FILTER_BCON_AWARE_MSK);
872 scan->channel_count =
873 iwl_get_channels_for_scan(priv, band, 1, /* active */
875 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
877 if (scan->channel_count == 0) {
878 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
882 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
883 scan->channel_count * sizeof(struct iwl_scan_channel);
885 scan->len = cpu_to_le16(cmd.len);
887 set_bit(STATUS_SCAN_HW, &priv->status);
888 ret = iwl_send_cmd_sync(priv, &cmd);
892 queue_delayed_work(priv->workqueue, &priv->scan_check,
893 IWL_SCAN_CHECK_WATCHDOG);
895 mutex_unlock(&priv->mutex);
899 /* Cannot perform scan. Make sure we clear scanning
900 * bits from status so next scan request can be performed.
901 * If we don't clear scanning status bit here all next scan
904 clear_bit(STATUS_SCAN_HW, &priv->status);
905 clear_bit(STATUS_SCANNING, &priv->status);
906 /* inform mac80211 scan aborted */
907 queue_work(priv->workqueue, &priv->scan_completed);
908 mutex_unlock(&priv->mutex);
911 static void iwl_bg_abort_scan(struct work_struct *work)
913 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
915 if (!iwl_is_ready(priv))
918 mutex_lock(&priv->mutex);
920 set_bit(STATUS_SCAN_ABORTING, &priv->status);
921 iwl_send_scan_abort(priv);
923 mutex_unlock(&priv->mutex);
926 static void iwl_bg_scan_completed(struct work_struct *work)
928 struct iwl_priv *priv =
929 container_of(work, struct iwl_priv, scan_completed);
931 IWL_DEBUG_SCAN("SCAN complete scan\n");
933 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
936 ieee80211_scan_completed(priv->hw);
938 /* Since setting the TXPOWER may have been deferred while
939 * performing the scan, fire one off */
940 mutex_lock(&priv->mutex);
941 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
942 mutex_unlock(&priv->mutex);
946 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
948 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
949 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
950 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
951 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
953 EXPORT_SYMBOL(iwl_setup_scan_deferred_work);