iwl-scan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  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.
  10. *
  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.
  15. *
  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
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  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/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/etherdevice.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn.h"
  39. #include "iwl-trans.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)
  45. #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  46. #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  47. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  48. * Must be set longer than active dwell time.
  49. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  50. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  51. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  52. #define IWL_PASSIVE_DWELL_BASE (100)
  53. #define IWL_CHANNEL_TUNE_TIME 5
  54. static int iwl_send_scan_abort(struct iwl_priv *priv)
  55. {
  56. int ret;
  57. struct iwl_rx_packet *pkt;
  58. struct iwl_host_cmd cmd = {
  59. .id = REPLY_SCAN_ABORT_CMD,
  60. .flags = CMD_SYNC | CMD_WANT_SKB,
  61. };
  62. /* Exit instantly with error when device is not ready
  63. * to receive scan abort command or it does not perform
  64. * hardware scan currently */
  65. if (!test_bit(STATUS_READY, &priv->shrd->status) ||
  66. !test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) ||
  67. !test_bit(STATUS_SCAN_HW, &priv->shrd->status) ||
  68. test_bit(STATUS_FW_ERROR, &priv->shrd->status) ||
  69. test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  70. return -EIO;
  71. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  72. if (ret)
  73. return ret;
  74. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  75. if (pkt->u.status != CAN_ABORT_STATUS) {
  76. /* The scan abort will return 1 for success or
  77. * 2 for "failure". A failure condition can be
  78. * due to simply not being in an active scan which
  79. * can occur if we send the scan abort before we
  80. * the microcode has notified us that a scan is
  81. * completed. */
  82. IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n", pkt->u.status);
  83. ret = -EIO;
  84. }
  85. iwl_free_pages(priv->shrd, cmd.reply_page);
  86. return ret;
  87. }
  88. static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
  89. {
  90. /* check if scan was requested from mac80211 */
  91. if (priv->scan_request) {
  92. IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
  93. ieee80211_scan_completed(priv->hw, aborted);
  94. }
  95. if (priv->scan_type == IWL_SCAN_ROC) {
  96. ieee80211_remain_on_channel_expired(priv->hw);
  97. priv->hw_roc_channel = NULL;
  98. schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
  99. }
  100. priv->scan_type = IWL_SCAN_NORMAL;
  101. priv->scan_vif = NULL;
  102. priv->scan_request = NULL;
  103. }
  104. void iwl_force_scan_end(struct iwl_priv *priv)
  105. {
  106. lockdep_assert_held(&priv->shrd->mutex);
  107. if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  108. IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
  109. return;
  110. }
  111. IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
  112. clear_bit(STATUS_SCANNING, &priv->shrd->status);
  113. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  114. clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
  115. iwl_complete_scan(priv, true);
  116. }
  117. static void iwl_do_scan_abort(struct iwl_priv *priv)
  118. {
  119. int ret;
  120. lockdep_assert_held(&priv->shrd->mutex);
  121. if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  122. IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
  123. return;
  124. }
  125. if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
  126. IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
  127. return;
  128. }
  129. ret = iwl_send_scan_abort(priv);
  130. if (ret) {
  131. IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
  132. iwl_force_scan_end(priv);
  133. } else
  134. IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
  135. }
  136. /**
  137. * iwl_scan_cancel - Cancel any currently executing HW scan
  138. */
  139. int iwl_scan_cancel(struct iwl_priv *priv)
  140. {
  141. IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
  142. queue_work(priv->shrd->workqueue, &priv->abort_scan);
  143. return 0;
  144. }
  145. /**
  146. * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  147. * @ms: amount of time to wait (in milliseconds) for scan to abort
  148. *
  149. */
  150. int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  151. {
  152. unsigned long timeout = jiffies + msecs_to_jiffies(ms);
  153. lockdep_assert_held(&priv->shrd->mutex);
  154. IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
  155. iwl_do_scan_abort(priv);
  156. while (time_before_eq(jiffies, timeout)) {
  157. if (!test_bit(STATUS_SCAN_HW, &priv->shrd->status))
  158. break;
  159. msleep(20);
  160. }
  161. return test_bit(STATUS_SCAN_HW, &priv->shrd->status);
  162. }
  163. /* Service response to REPLY_SCAN_CMD (0x80) */
  164. static void iwl_rx_reply_scan(struct iwl_priv *priv,
  165. struct iwl_rx_mem_buffer *rxb)
  166. {
  167. #ifdef CONFIG_IWLWIFI_DEBUG
  168. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  169. struct iwl_scanreq_notification *notif =
  170. (struct iwl_scanreq_notification *)pkt->u.raw;
  171. IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
  172. #endif
  173. }
  174. /* Service SCAN_START_NOTIFICATION (0x82) */
  175. static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
  176. struct iwl_rx_mem_buffer *rxb)
  177. {
  178. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  179. struct iwl_scanstart_notification *notif =
  180. (struct iwl_scanstart_notification *)pkt->u.raw;
  181. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  182. IWL_DEBUG_SCAN(priv, "Scan start: "
  183. "%d [802.11%s] "
  184. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  185. notif->channel,
  186. notif->band ? "bg" : "a",
  187. le32_to_cpu(notif->tsf_high),
  188. le32_to_cpu(notif->tsf_low),
  189. notif->status, notif->beacon_timer);
  190. if (priv->scan_type == IWL_SCAN_ROC)
  191. ieee80211_ready_on_channel(priv->hw);
  192. }
  193. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  194. static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
  195. struct iwl_rx_mem_buffer *rxb)
  196. {
  197. #ifdef CONFIG_IWLWIFI_DEBUG
  198. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  199. struct iwl_scanresults_notification *notif =
  200. (struct iwl_scanresults_notification *)pkt->u.raw;
  201. IWL_DEBUG_SCAN(priv, "Scan ch.res: "
  202. "%d [802.11%s] "
  203. "(TSF: 0x%08X:%08X) - %d "
  204. "elapsed=%lu usec\n",
  205. notif->channel,
  206. notif->band ? "bg" : "a",
  207. le32_to_cpu(notif->tsf_high),
  208. le32_to_cpu(notif->tsf_low),
  209. le32_to_cpu(notif->statistics[0]),
  210. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
  211. #endif
  212. }
  213. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  214. static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  215. struct iwl_rx_mem_buffer *rxb)
  216. {
  217. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  218. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  219. IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  220. scan_notif->scanned_channels,
  221. scan_notif->tsf_low,
  222. scan_notif->tsf_high, scan_notif->status);
  223. /* The HW is no longer scanning */
  224. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  225. IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
  226. (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  227. jiffies_to_msecs(jiffies - priv->scan_start));
  228. queue_work(priv->shrd->workqueue, &priv->scan_completed);
  229. if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
  230. iwl_advanced_bt_coexist(priv) &&
  231. priv->bt_status != scan_notif->bt_status) {
  232. if (scan_notif->bt_status) {
  233. /* BT on */
  234. if (!priv->bt_ch_announce)
  235. priv->bt_traffic_load =
  236. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  237. /*
  238. * otherwise, no traffic load information provided
  239. * no changes made
  240. */
  241. } else {
  242. /* BT off */
  243. priv->bt_traffic_load =
  244. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  245. }
  246. priv->bt_status = scan_notif->bt_status;
  247. queue_work(priv->shrd->workqueue,
  248. &priv->bt_traffic_change_work);
  249. }
  250. }
  251. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  252. {
  253. /* scan handlers */
  254. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  255. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  256. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  257. iwl_rx_scan_results_notif;
  258. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  259. iwl_rx_scan_complete_notif;
  260. }
  261. inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  262. enum ieee80211_band band,
  263. u8 n_probes)
  264. {
  265. if (band == IEEE80211_BAND_5GHZ)
  266. return IWL_ACTIVE_DWELL_TIME_52 +
  267. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  268. else
  269. return IWL_ACTIVE_DWELL_TIME_24 +
  270. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  271. }
  272. u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  273. enum ieee80211_band band,
  274. struct ieee80211_vif *vif)
  275. {
  276. struct iwl_rxon_context *ctx;
  277. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  278. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  279. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  280. if (iwl_is_any_associated(priv)) {
  281. /*
  282. * If we're associated, we clamp the maximum passive
  283. * dwell time to be 98% of the smallest beacon interval
  284. * (minus 2 * channel tune time)
  285. */
  286. for_each_context(priv, ctx) {
  287. u16 value;
  288. if (!iwl_is_associated_ctx(ctx))
  289. continue;
  290. value = ctx->vif ? ctx->vif->bss_conf.beacon_int : 0;
  291. if ((value > IWL_PASSIVE_DWELL_BASE) || !value)
  292. value = IWL_PASSIVE_DWELL_BASE;
  293. value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  294. passive = min(value, passive);
  295. }
  296. }
  297. return passive;
  298. }
  299. void iwl_init_scan_params(struct iwl_priv *priv)
  300. {
  301. u8 ant_idx = fls(hw_params(priv).valid_tx_ant) - 1;
  302. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  303. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  304. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  305. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  306. }
  307. int __must_check iwl_scan_initiate(struct iwl_priv *priv,
  308. struct ieee80211_vif *vif,
  309. enum iwl_scan_type scan_type,
  310. enum ieee80211_band band)
  311. {
  312. int ret;
  313. lockdep_assert_held(&priv->shrd->mutex);
  314. cancel_delayed_work(&priv->scan_check);
  315. if (!iwl_is_ready_rf(priv->shrd)) {
  316. IWL_WARN(priv, "Request scan called when driver not ready.\n");
  317. return -EIO;
  318. }
  319. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  320. IWL_DEBUG_SCAN(priv,
  321. "Multiple concurrent scan requests in parallel.\n");
  322. return -EBUSY;
  323. }
  324. if (test_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
  325. IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
  326. return -EBUSY;
  327. }
  328. IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
  329. scan_type == IWL_SCAN_NORMAL ? "" :
  330. scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
  331. "internal short ");
  332. set_bit(STATUS_SCANNING, &priv->shrd->status);
  333. priv->scan_type = scan_type;
  334. priv->scan_start = jiffies;
  335. priv->scan_band = band;
  336. ret = iwlagn_request_scan(priv, vif);
  337. if (ret) {
  338. clear_bit(STATUS_SCANNING, &priv->shrd->status);
  339. priv->scan_type = IWL_SCAN_NORMAL;
  340. return ret;
  341. }
  342. queue_delayed_work(priv->shrd->workqueue, &priv->scan_check,
  343. IWL_SCAN_CHECK_WATCHDOG);
  344. return 0;
  345. }
  346. int iwl_mac_hw_scan(struct ieee80211_hw *hw,
  347. struct ieee80211_vif *vif,
  348. struct cfg80211_scan_request *req)
  349. {
  350. struct iwl_priv *priv = hw->priv;
  351. int ret;
  352. IWL_DEBUG_MAC80211(priv, "enter\n");
  353. if (req->n_channels == 0)
  354. return -EINVAL;
  355. mutex_lock(&priv->shrd->mutex);
  356. if (test_bit(STATUS_SCANNING, &priv->shrd->status) &&
  357. priv->scan_type != IWL_SCAN_NORMAL) {
  358. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  359. ret = -EAGAIN;
  360. goto out_unlock;
  361. }
  362. /* mac80211 will only ask for one band at a time */
  363. priv->scan_request = req;
  364. priv->scan_vif = vif;
  365. /*
  366. * If an internal scan is in progress, just set
  367. * up the scan_request as per above.
  368. */
  369. if (priv->scan_type != IWL_SCAN_NORMAL) {
  370. IWL_DEBUG_SCAN(priv, "SCAN request during internal scan\n");
  371. ret = 0;
  372. } else
  373. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  374. req->channels[0]->band);
  375. IWL_DEBUG_MAC80211(priv, "leave\n");
  376. out_unlock:
  377. mutex_unlock(&priv->shrd->mutex);
  378. return ret;
  379. }
  380. /*
  381. * internal short scan, this function should only been called while associated.
  382. * It will reset and tune the radio to prevent possible RF related problem
  383. */
  384. void iwl_internal_short_hw_scan(struct iwl_priv *priv)
  385. {
  386. queue_work(priv->shrd->workqueue, &priv->start_internal_scan);
  387. }
  388. static void iwl_bg_start_internal_scan(struct work_struct *work)
  389. {
  390. struct iwl_priv *priv =
  391. container_of(work, struct iwl_priv, start_internal_scan);
  392. IWL_DEBUG_SCAN(priv, "Start internal scan\n");
  393. mutex_lock(&priv->shrd->mutex);
  394. if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
  395. IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
  396. goto unlock;
  397. }
  398. if (test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  399. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  400. goto unlock;
  401. }
  402. if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
  403. IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
  404. unlock:
  405. mutex_unlock(&priv->shrd->mutex);
  406. }
  407. static void iwl_bg_scan_check(struct work_struct *data)
  408. {
  409. struct iwl_priv *priv =
  410. container_of(data, struct iwl_priv, scan_check.work);
  411. IWL_DEBUG_SCAN(priv, "Scan check work\n");
  412. /* Since we are here firmware does not finish scan and
  413. * most likely is in bad shape, so we don't bother to
  414. * send abort command, just force scan complete to mac80211 */
  415. mutex_lock(&priv->shrd->mutex);
  416. iwl_force_scan_end(priv);
  417. mutex_unlock(&priv->shrd->mutex);
  418. }
  419. /**
  420. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  421. */
  422. u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
  423. const u8 *ta, const u8 *ies, int ie_len, int left)
  424. {
  425. int len = 0;
  426. u8 *pos = NULL;
  427. /* Make sure there is enough space for the probe request,
  428. * two mandatory IEs and the data */
  429. left -= 24;
  430. if (left < 0)
  431. return 0;
  432. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  433. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  434. memcpy(frame->sa, ta, ETH_ALEN);
  435. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  436. frame->seq_ctrl = 0;
  437. len += 24;
  438. /* ...next IE... */
  439. pos = &frame->u.probe_req.variable[0];
  440. /* fill in our indirect SSID IE */
  441. left -= 2;
  442. if (left < 0)
  443. return 0;
  444. *pos++ = WLAN_EID_SSID;
  445. *pos++ = 0;
  446. len += 2;
  447. if (WARN_ON(left < ie_len))
  448. return len;
  449. if (ies && ie_len) {
  450. memcpy(pos, ies, ie_len);
  451. len += ie_len;
  452. }
  453. return (u16)len;
  454. }
  455. static void iwl_bg_abort_scan(struct work_struct *work)
  456. {
  457. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  458. IWL_DEBUG_SCAN(priv, "Abort scan work\n");
  459. /* We keep scan_check work queued in case when firmware will not
  460. * report back scan completed notification */
  461. mutex_lock(&priv->shrd->mutex);
  462. iwl_scan_cancel_timeout(priv, 200);
  463. mutex_unlock(&priv->shrd->mutex);
  464. }
  465. static void iwl_bg_scan_completed(struct work_struct *work)
  466. {
  467. struct iwl_priv *priv =
  468. container_of(work, struct iwl_priv, scan_completed);
  469. bool aborted;
  470. IWL_DEBUG_SCAN(priv, "Completed scan.\n");
  471. cancel_delayed_work(&priv->scan_check);
  472. mutex_lock(&priv->shrd->mutex);
  473. aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
  474. if (aborted)
  475. IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
  476. if (!test_and_clear_bit(STATUS_SCANNING, &priv->shrd->status)) {
  477. IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
  478. goto out_settings;
  479. }
  480. if (priv->scan_type == IWL_SCAN_ROC) {
  481. ieee80211_remain_on_channel_expired(priv->hw);
  482. priv->hw_roc_channel = NULL;
  483. schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
  484. }
  485. if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
  486. int err;
  487. /* Check if mac80211 requested scan during our internal scan */
  488. if (priv->scan_request == NULL)
  489. goto out_complete;
  490. /* If so request a new scan */
  491. err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
  492. priv->scan_request->channels[0]->band);
  493. if (err) {
  494. IWL_DEBUG_SCAN(priv,
  495. "failed to initiate pending scan: %d\n", err);
  496. aborted = true;
  497. goto out_complete;
  498. }
  499. goto out;
  500. }
  501. out_complete:
  502. iwl_complete_scan(priv, aborted);
  503. out_settings:
  504. /* Can we still talk to firmware ? */
  505. if (!iwl_is_ready_rf(priv->shrd))
  506. goto out;
  507. iwlagn_post_scan(priv);
  508. out:
  509. mutex_unlock(&priv->shrd->mutex);
  510. }
  511. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  512. {
  513. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  514. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  515. INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
  516. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  517. }
  518. void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
  519. {
  520. cancel_work_sync(&priv->start_internal_scan);
  521. cancel_work_sync(&priv->abort_scan);
  522. cancel_work_sync(&priv->scan_completed);
  523. if (cancel_delayed_work_sync(&priv->scan_check)) {
  524. mutex_lock(&priv->shrd->mutex);
  525. iwl_force_scan_end(priv);
  526. mutex_unlock(&priv->shrd->mutex);
  527. }
  528. }