added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / wireless / iwlwifi / iwl-scan.c
blob3c803f6922efe0a03c498fe2efc53c78c4db5c50
1 /******************************************************************************
3 * GPL LICENSE SUMMARY
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,
19 * USA
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28 #include <linux/types.h>
29 #include <linux/etherdevice.h>
30 #include <net/lib80211.h>
31 #include <net/mac80211.h>
33 #include "iwl-eeprom.h"
34 #include "iwl-dev.h"
35 #include "iwl-core.h"
36 #include "iwl-sta.h"
37 #include "iwl-io.h"
38 #include "iwl-helpers.h"
40 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
41 * sending probe req. This should be set long enough to hear probe responses
42 * from more than one AP. */
43 #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
44 #define IWL_ACTIVE_DWELL_TIME_52 (20)
46 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
47 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
49 /* For faster active scanning, scan will move to the next channel if fewer than
50 * PLCP_QUIET_THRESH packets are heard on this channel within
51 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
52 * time if it's a quiet channel (nothing responded to our probe, and there's
53 * no other traffic).
54 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
55 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
56 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
58 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
59 * Must be set longer than active dwell time.
60 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
61 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
62 #define IWL_PASSIVE_DWELL_TIME_52 (10)
63 #define IWL_PASSIVE_DWELL_BASE (100)
64 #define IWL_CHANNEL_TUNE_TIME 5
66 #define IWL_SCAN_PROBE_MASK(n) cpu_to_le32((BIT(n) | (BIT(n) - BIT(1))))
69 /**
70 * iwl_scan_cancel - Cancel any currently executing HW scan
72 * NOTE: priv->mutex is not required before calling this function
74 int iwl_scan_cancel(struct iwl_priv *priv)
76 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
77 clear_bit(STATUS_SCANNING, &priv->status);
78 return 0;
81 if (test_bit(STATUS_SCANNING, &priv->status)) {
82 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
83 IWL_DEBUG_SCAN("Queuing scan abort.\n");
84 set_bit(STATUS_SCAN_ABORTING, &priv->status);
85 queue_work(priv->workqueue, &priv->abort_scan);
87 } else
88 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
90 return test_bit(STATUS_SCANNING, &priv->status);
93 return 0;
95 EXPORT_SYMBOL(iwl_scan_cancel);
96 /**
97 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
98 * @ms: amount of time to wait (in milliseconds) for scan to abort
100 * NOTE: priv->mutex must be held before calling this function
102 int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
104 unsigned long now = jiffies;
105 int ret;
107 ret = iwl_scan_cancel(priv);
108 if (ret && ms) {
109 mutex_unlock(&priv->mutex);
110 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
111 test_bit(STATUS_SCANNING, &priv->status))
112 msleep(1);
113 mutex_lock(&priv->mutex);
115 return test_bit(STATUS_SCANNING, &priv->status);
118 return ret;
120 EXPORT_SYMBOL(iwl_scan_cancel_timeout);
122 static int iwl_send_scan_abort(struct iwl_priv *priv)
124 int ret = 0;
125 struct iwl_rx_packet *res;
126 struct iwl_host_cmd cmd = {
127 .id = REPLY_SCAN_ABORT_CMD,
128 .meta.flags = CMD_WANT_SKB,
131 /* If there isn't a scan actively going on in the hardware
132 * then we are in between scan bands and not actually
133 * actively scanning, so don't send the abort command */
134 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
135 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
136 return 0;
139 ret = iwl_send_cmd_sync(priv, &cmd);
140 if (ret) {
141 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
142 return ret;
145 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
146 if (res->u.status != CAN_ABORT_STATUS) {
147 /* The scan abort will return 1 for success or
148 * 2 for "failure". A failure condition can be
149 * due to simply not being in an active scan which
150 * can occur if we send the scan abort before we
151 * the microcode has notified us that a scan is
152 * completed. */
153 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
154 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
155 clear_bit(STATUS_SCAN_HW, &priv->status);
158 priv->alloc_rxb_skb--;
159 dev_kfree_skb_any(cmd.meta.u.skb);
161 return ret;
165 /* Service response to REPLY_SCAN_CMD (0x80) */
166 static void iwl_rx_reply_scan(struct iwl_priv *priv,
167 struct iwl_rx_mem_buffer *rxb)
169 #ifdef CONFIG_IWLWIFI_DEBUG
170 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
171 struct iwl_scanreq_notification *notif =
172 (struct iwl_scanreq_notification *)pkt->u.raw;
174 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
175 #endif
178 /* Service SCAN_START_NOTIFICATION (0x82) */
179 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
180 struct iwl_rx_mem_buffer *rxb)
182 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
183 struct iwl_scanstart_notification *notif =
184 (struct iwl_scanstart_notification *)pkt->u.raw;
185 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
186 IWL_DEBUG_SCAN("Scan start: "
187 "%d [802.11%s] "
188 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
189 notif->channel,
190 notif->band ? "bg" : "a",
191 le32_to_cpu(notif->tsf_high),
192 le32_to_cpu(notif->tsf_low),
193 notif->status, notif->beacon_timer);
196 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
197 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
198 struct iwl_rx_mem_buffer *rxb)
200 #ifdef CONFIG_IWLWIFI_DEBUG
201 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
202 struct iwl_scanresults_notification *notif =
203 (struct iwl_scanresults_notification *)pkt->u.raw;
205 IWL_DEBUG_SCAN("Scan ch.res: "
206 "%d [802.11%s] "
207 "(TSF: 0x%08X:%08X) - %d "
208 "elapsed=%lu usec (%dms since last)\n",
209 notif->channel,
210 notif->band ? "bg" : "a",
211 le32_to_cpu(notif->tsf_high),
212 le32_to_cpu(notif->tsf_low),
213 le32_to_cpu(notif->statistics[0]),
214 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
215 jiffies_to_msecs(elapsed_jiffies
216 (priv->last_scan_jiffies, jiffies)));
217 #endif
219 priv->last_scan_jiffies = jiffies;
220 priv->next_scan_jiffies = 0;
223 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
224 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
225 struct iwl_rx_mem_buffer *rxb)
227 #ifdef CONFIG_IWLWIFI_DEBUG
228 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
229 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
231 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
232 scan_notif->scanned_channels,
233 scan_notif->tsf_low,
234 scan_notif->tsf_high, scan_notif->status);
235 #endif
237 /* The HW is no longer scanning */
238 clear_bit(STATUS_SCAN_HW, &priv->status);
240 /* The scan completion notification came in, so kill that timer... */
241 cancel_delayed_work(&priv->scan_check);
243 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
244 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
245 "2.4" : "5.2",
246 jiffies_to_msecs(elapsed_jiffies
247 (priv->scan_pass_start, jiffies)));
249 /* Remove this scanned band from the list of pending
250 * bands to scan, band G precedes A in order of scanning
251 * as seen in iwl_bg_request_scan */
252 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
253 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
254 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
255 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
257 /* If a request to abort was given, or the scan did not succeed
258 * then we reset the scan state machine and terminate,
259 * re-queuing another scan if one has been requested */
260 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
261 IWL_DEBUG_INFO("Aborted scan completed.\n");
262 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
263 } else {
264 /* If there are more bands on this scan pass reschedule */
265 if (priv->scan_bands)
266 goto reschedule;
269 priv->last_scan_jiffies = jiffies;
270 priv->next_scan_jiffies = 0;
271 IWL_DEBUG_INFO("Setting scan to off\n");
273 clear_bit(STATUS_SCANNING, &priv->status);
275 IWL_DEBUG_INFO("Scan took %dms\n",
276 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
278 queue_work(priv->workqueue, &priv->scan_completed);
280 return;
282 reschedule:
283 priv->scan_pass_start = jiffies;
284 queue_work(priv->workqueue, &priv->request_scan);
287 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
289 /* scan handlers */
290 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
291 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
292 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
293 iwl_rx_scan_results_notif;
294 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
295 iwl_rx_scan_complete_notif;
297 EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
299 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
300 enum ieee80211_band band,
301 u8 n_probes)
303 if (band == IEEE80211_BAND_5GHZ)
304 return IWL_ACTIVE_DWELL_TIME_52 +
305 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
306 else
307 return IWL_ACTIVE_DWELL_TIME_24 +
308 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
311 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
312 enum ieee80211_band band)
314 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
315 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
316 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
318 if (iwl_is_associated(priv)) {
319 /* If we're associated, we clamp the maximum passive
320 * dwell time to be 98% of the beacon interval (minus
321 * 2 * channel tune time) */
322 passive = priv->beacon_int;
323 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
324 passive = IWL_PASSIVE_DWELL_BASE;
325 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
328 return passive;
331 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
332 enum ieee80211_band band,
333 u8 is_active, u8 n_probes,
334 struct iwl_scan_channel *scan_ch)
336 const struct ieee80211_channel *channels = NULL;
337 const struct ieee80211_supported_band *sband;
338 const struct iwl_channel_info *ch_info;
339 u16 passive_dwell = 0;
340 u16 active_dwell = 0;
341 int added, i;
342 u16 channel;
344 sband = iwl_get_hw_mode(priv, band);
345 if (!sband)
346 return 0;
348 channels = sband->channels;
350 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
351 passive_dwell = iwl_get_passive_dwell_time(priv, band);
353 if (passive_dwell <= active_dwell)
354 passive_dwell = active_dwell + 1;
356 for (i = 0, added = 0; i < sband->n_channels; i++) {
357 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
358 continue;
360 channel =
361 ieee80211_frequency_to_channel(channels[i].center_freq);
362 scan_ch->channel = cpu_to_le16(channel);
364 ch_info = iwl_get_channel_info(priv, band, channel);
365 if (!is_channel_valid(ch_info)) {
366 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
367 channel);
368 continue;
371 if (!is_active || is_channel_passive(ch_info) ||
372 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
373 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
374 else
375 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
377 if (n_probes)
378 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
380 scan_ch->active_dwell = cpu_to_le16(active_dwell);
381 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
383 /* Set txpower levels to defaults */
384 scan_ch->dsp_atten = 110;
386 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
387 * power level:
388 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
390 if (band == IEEE80211_BAND_5GHZ)
391 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
392 else
393 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
395 IWL_DEBUG_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n",
396 channel, le32_to_cpu(scan_ch->type),
397 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
398 "ACTIVE" : "PASSIVE",
399 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
400 active_dwell : passive_dwell);
402 scan_ch++;
403 added++;
406 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
407 return added;
410 void iwl_init_scan_params(struct iwl_priv *priv)
412 u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
413 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
414 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
415 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
416 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
419 int iwl_scan_initiate(struct iwl_priv *priv)
421 if (!iwl_is_ready_rf(priv)) {
422 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
423 return -EIO;
426 if (test_bit(STATUS_SCANNING, &priv->status)) {
427 IWL_DEBUG_SCAN("Scan already in progress.\n");
428 return -EAGAIN;
431 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
432 IWL_DEBUG_SCAN("Scan request while abort pending\n");
433 return -EAGAIN;
436 IWL_DEBUG_INFO("Starting scan...\n");
437 if (priv->cfg->sku & IWL_SKU_G)
438 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
439 if (priv->cfg->sku & IWL_SKU_A)
440 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
441 set_bit(STATUS_SCANNING, &priv->status);
442 priv->scan_start = jiffies;
443 priv->scan_pass_start = priv->scan_start;
445 queue_work(priv->workqueue, &priv->request_scan);
447 return 0;
449 EXPORT_SYMBOL(iwl_scan_initiate);
451 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
453 static void iwl_bg_scan_check(struct work_struct *data)
455 struct iwl_priv *priv =
456 container_of(data, struct iwl_priv, scan_check.work);
458 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
459 return;
461 mutex_lock(&priv->mutex);
462 if (test_bit(STATUS_SCANNING, &priv->status) ||
463 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
464 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
465 "adapter (%dms)\n",
466 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
468 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
469 iwl_send_scan_abort(priv);
471 mutex_unlock(&priv->mutex);
474 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
476 * return : set the bit for each supported rate insert in ie
478 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
479 u16 basic_rate, int *left)
481 u16 ret_rates = 0, bit;
482 int i;
483 u8 *cnt = ie;
484 u8 *rates = ie + 1;
486 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
487 if (bit & supported_rate) {
488 ret_rates |= bit;
489 rates[*cnt] = iwl_rates[i].ieee |
490 ((bit & basic_rate) ? 0x80 : 0x00);
491 (*cnt)++;
492 (*left)--;
493 if ((*left <= 0) ||
494 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
495 break;
499 return ret_rates;
503 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
504 u8 *pos, int *left)
506 struct ieee80211_ht_cap *ht_cap;
508 if (!sband || !sband->ht_cap.ht_supported)
509 return;
511 if (*left < sizeof(struct ieee80211_ht_cap))
512 return;
514 *pos++ = sizeof(struct ieee80211_ht_cap);
515 ht_cap = (struct ieee80211_ht_cap *) pos;
517 ht_cap->cap_info = cpu_to_le16(sband->ht_cap.cap);
518 memcpy(&ht_cap->mcs, &sband->ht_cap.mcs, 16);
519 ht_cap->ampdu_params_info =
520 (sband->ht_cap.ampdu_factor & IEEE80211_HT_AMPDU_PARM_FACTOR) |
521 ((sband->ht_cap.ampdu_density << 2) &
522 IEEE80211_HT_AMPDU_PARM_DENSITY);
523 *left -= sizeof(struct ieee80211_ht_cap);
527 * iwl_fill_probe_req - fill in all required fields and IE for probe request
530 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
531 enum ieee80211_band band,
532 struct ieee80211_mgmt *frame,
533 int left)
535 int len = 0;
536 u8 *pos = NULL;
537 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
538 const struct ieee80211_supported_band *sband =
539 iwl_get_hw_mode(priv, band);
542 /* Make sure there is enough space for the probe request,
543 * two mandatory IEs and the data */
544 left -= 24;
545 if (left < 0)
546 return 0;
548 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
549 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
550 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
551 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
552 frame->seq_ctrl = 0;
554 len += 24;
556 /* ...next IE... */
557 pos = &frame->u.probe_req.variable[0];
559 /* fill in our indirect SSID IE */
560 left -= 2;
561 if (left < 0)
562 return 0;
563 *pos++ = WLAN_EID_SSID;
564 *pos++ = 0;
566 len += 2;
568 /* fill in supported rate */
569 left -= 2;
570 if (left < 0)
571 return 0;
573 *pos++ = WLAN_EID_SUPP_RATES;
574 *pos = 0;
576 /* exclude 60M rate */
577 active_rates = priv->rates_mask;
578 active_rates &= ~IWL_RATE_60M_MASK;
580 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
582 cck_rates = IWL_CCK_RATES_MASK & active_rates;
583 ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
584 active_rate_basic, &left);
585 active_rates &= ~ret_rates;
587 ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
588 active_rate_basic, &left);
589 active_rates &= ~ret_rates;
591 len += 2 + *pos;
592 pos += (*pos) + 1;
594 if (active_rates == 0)
595 goto fill_end;
597 /* fill in supported extended rate */
598 /* ...next IE... */
599 left -= 2;
600 if (left < 0)
601 return 0;
602 /* ... fill it in... */
603 *pos++ = WLAN_EID_EXT_SUPP_RATES;
604 *pos = 0;
605 iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left);
606 if (*pos > 0) {
607 len += 2 + *pos;
608 pos += (*pos) + 1;
609 } else {
610 pos--;
613 fill_end:
615 left -= 2;
616 if (left < 0)
617 return 0;
619 *pos++ = WLAN_EID_HT_CAPABILITY;
620 *pos = 0;
621 iwl_ht_cap_to_ie(sband, pos, &left);
622 if (*pos > 0)
623 len += 2 + *pos;
625 return (u16)len;
628 static void iwl_bg_request_scan(struct work_struct *data)
630 struct iwl_priv *priv =
631 container_of(data, struct iwl_priv, request_scan);
632 struct iwl_host_cmd cmd = {
633 .id = REPLY_SCAN_CMD,
634 .len = sizeof(struct iwl_scan_cmd),
635 .meta.flags = CMD_SIZE_HUGE,
637 struct iwl_scan_cmd *scan;
638 struct ieee80211_conf *conf = NULL;
639 int ret = 0;
640 u32 rate_flags = 0;
641 u16 cmd_len;
642 enum ieee80211_band band;
643 u8 n_probes = 2;
644 u8 rx_chain = priv->hw_params.valid_rx_ant;
645 u8 rate;
646 DECLARE_SSID_BUF(ssid);
648 conf = ieee80211_get_hw_conf(priv->hw);
650 mutex_lock(&priv->mutex);
652 if (!iwl_is_ready(priv)) {
653 IWL_WARNING("request scan called when driver not ready.\n");
654 goto done;
657 /* Make sure the scan wasn't canceled before this queued work
658 * was given the chance to run... */
659 if (!test_bit(STATUS_SCANNING, &priv->status))
660 goto done;
662 /* This should never be called or scheduled if there is currently
663 * a scan active in the hardware. */
664 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
665 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
666 "Ignoring second request.\n");
667 ret = -EIO;
668 goto done;
671 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
672 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
673 goto done;
676 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
677 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
678 goto done;
681 if (iwl_is_rfkill(priv)) {
682 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
683 goto done;
686 if (!test_bit(STATUS_READY, &priv->status)) {
687 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
688 goto done;
691 if (!priv->scan_bands) {
692 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
693 goto done;
696 if (!priv->scan) {
697 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
698 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
699 if (!priv->scan) {
700 ret = -ENOMEM;
701 goto done;
704 scan = priv->scan;
705 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
707 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
708 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
710 if (iwl_is_associated(priv)) {
711 u16 interval = 0;
712 u32 extra;
713 u32 suspend_time = 100;
714 u32 scan_suspend_time = 100;
715 unsigned long flags;
717 IWL_DEBUG_INFO("Scanning while associated...\n");
719 spin_lock_irqsave(&priv->lock, flags);
720 interval = priv->beacon_int;
721 spin_unlock_irqrestore(&priv->lock, flags);
723 scan->suspend_time = 0;
724 scan->max_out_time = cpu_to_le32(200 * 1024);
725 if (!interval)
726 interval = suspend_time;
728 extra = (suspend_time / interval) << 22;
729 scan_suspend_time = (extra |
730 ((suspend_time % interval) * 1024));
731 scan->suspend_time = cpu_to_le32(scan_suspend_time);
732 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
733 scan_suspend_time, interval);
736 /* We should add the ability for user to lock to PASSIVE ONLY */
737 if (priv->one_direct_scan) {
738 IWL_DEBUG_SCAN("Start direct scan for '%s'\n",
739 print_ssid(ssid, priv->direct_ssid,
740 priv->direct_ssid_len));
741 scan->direct_scan[0].id = WLAN_EID_SSID;
742 scan->direct_scan[0].len = priv->direct_ssid_len;
743 memcpy(scan->direct_scan[0].ssid,
744 priv->direct_ssid, priv->direct_ssid_len);
745 n_probes++;
746 } else {
747 IWL_DEBUG_SCAN("Start indirect scan.\n");
750 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
751 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
752 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
755 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
756 band = IEEE80211_BAND_2GHZ;
757 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
758 if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) {
759 rate = IWL_RATE_6M_PLCP;
760 } else {
761 rate = IWL_RATE_1M_PLCP;
762 rate_flags = RATE_MCS_CCK_MSK;
764 scan->good_CRC_th = 0;
765 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
766 band = IEEE80211_BAND_5GHZ;
767 rate = IWL_RATE_6M_PLCP;
768 scan->good_CRC_th = IWL_GOOD_CRC_TH;
770 /* Force use of chains B and C (0x6) for scan Rx for 4965
771 * Avoid A (0x1) because of its off-channel reception on A-band.
773 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
774 rx_chain = 0x6;
775 } else {
776 IWL_WARNING("Invalid scan band count\n");
777 goto done;
780 priv->scan_tx_ant[band] =
781 iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
782 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
783 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
785 /* MIMO is not used here, but value is required */
786 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
787 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
788 (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) |
789 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
791 cmd_len = iwl_fill_probe_req(priv, band,
792 (struct ieee80211_mgmt *)scan->data,
793 IWL_MAX_SCAN_SIZE - sizeof(*scan));
795 scan->tx_cmd.len = cpu_to_le16(cmd_len);
797 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
798 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
800 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
801 RXON_FILTER_BCON_AWARE_MSK);
803 scan->channel_count =
804 iwl_get_channels_for_scan(priv, band, 1, /* active */
805 n_probes,
806 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
808 if (scan->channel_count == 0) {
809 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
810 goto done;
813 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
814 scan->channel_count * sizeof(struct iwl_scan_channel);
815 cmd.data = scan;
816 scan->len = cpu_to_le16(cmd.len);
818 set_bit(STATUS_SCAN_HW, &priv->status);
819 ret = iwl_send_cmd_sync(priv, &cmd);
820 if (ret)
821 goto done;
823 queue_delayed_work(priv->workqueue, &priv->scan_check,
824 IWL_SCAN_CHECK_WATCHDOG);
826 mutex_unlock(&priv->mutex);
827 return;
829 done:
830 /* Cannot perform scan. Make sure we clear scanning
831 * bits from status so next scan request can be performed.
832 * If we don't clear scanning status bit here all next scan
833 * will fail
835 clear_bit(STATUS_SCAN_HW, &priv->status);
836 clear_bit(STATUS_SCANNING, &priv->status);
837 /* inform mac80211 scan aborted */
838 queue_work(priv->workqueue, &priv->scan_completed);
839 mutex_unlock(&priv->mutex);
842 static void iwl_bg_abort_scan(struct work_struct *work)
844 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
846 if (!iwl_is_ready(priv))
847 return;
849 mutex_lock(&priv->mutex);
851 set_bit(STATUS_SCAN_ABORTING, &priv->status);
852 iwl_send_scan_abort(priv);
854 mutex_unlock(&priv->mutex);
857 static void iwl_bg_scan_completed(struct work_struct *work)
859 struct iwl_priv *priv =
860 container_of(work, struct iwl_priv, scan_completed);
862 IWL_DEBUG_SCAN("SCAN complete scan\n");
864 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
865 return;
867 ieee80211_scan_completed(priv->hw);
869 /* Since setting the TXPOWER may have been deferred while
870 * performing the scan, fire one off */
871 mutex_lock(&priv->mutex);
872 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
873 mutex_unlock(&priv->mutex);
877 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
879 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
880 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
881 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
882 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
884 EXPORT_SYMBOL(iwl_setup_scan_deferred_work);