scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Scan implementation for ST-Ericsson CW1200 mac80211 drivers
  3. *
  4. * Copyright (c) 2010, ST-Ericsson
  5. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/sched.h>
  12. #include "cw1200.h"
  13. #include "scan.h"
  14. #include "sta.h"
  15. #include "pm.h"
  16. static void cw1200_scan_restart_delayed(struct cw1200_common *priv);
  17. static int cw1200_scan_start(struct cw1200_common *priv, struct wsm_scan *scan)
  18. {
  19. int ret, i;
  20. int tmo = 2000;
  21. switch (priv->join_status) {
  22. case CW1200_JOIN_STATUS_PRE_STA:
  23. case CW1200_JOIN_STATUS_JOINING:
  24. return -EBUSY;
  25. default:
  26. break;
  27. }
  28. wiphy_dbg(priv->hw->wiphy, "[SCAN] hw req, type %d, %d channels, flags: 0x%x.\n",
  29. scan->type, scan->num_channels, scan->flags);
  30. for (i = 0; i < scan->num_channels; ++i)
  31. tmo += scan->ch[i].max_chan_time + 10;
  32. cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  33. atomic_set(&priv->scan.in_progress, 1);
  34. atomic_set(&priv->recent_scan, 1);
  35. cw1200_pm_stay_awake(&priv->pm_state, tmo * HZ / 1000);
  36. queue_delayed_work(priv->workqueue, &priv->scan.timeout,
  37. tmo * HZ / 1000);
  38. ret = wsm_scan(priv, scan);
  39. if (ret) {
  40. atomic_set(&priv->scan.in_progress, 0);
  41. cancel_delayed_work_sync(&priv->scan.timeout);
  42. cw1200_scan_restart_delayed(priv);
  43. }
  44. return ret;
  45. }
  46. int cw1200_hw_scan(struct ieee80211_hw *hw,
  47. struct ieee80211_vif *vif,
  48. struct cfg80211_scan_request *req)
  49. {
  50. struct cw1200_common *priv = hw->priv;
  51. struct wsm_template_frame frame = {
  52. .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
  53. };
  54. int i, ret;
  55. if (!priv->vif)
  56. return -EINVAL;
  57. /* Scan when P2P_GO corrupt firmware MiniAP mode */
  58. if (priv->join_status == CW1200_JOIN_STATUS_AP)
  59. return -EOPNOTSUPP;
  60. if (req->n_ssids == 1 && !req->ssids[0].ssid_len)
  61. req->n_ssids = 0;
  62. wiphy_dbg(hw->wiphy, "[SCAN] Scan request for %d SSIDs.\n",
  63. req->n_ssids);
  64. if (req->n_ssids > WSM_SCAN_MAX_NUM_OF_SSIDS)
  65. return -EINVAL;
  66. frame.skb = ieee80211_probereq_get(hw, priv->vif, NULL, 0,
  67. req->ie_len);
  68. if (!frame.skb)
  69. return -ENOMEM;
  70. if (req->ie_len)
  71. memcpy(skb_put(frame.skb, req->ie_len), req->ie, req->ie_len);
  72. /* will be unlocked in cw1200_scan_work() */
  73. down(&priv->scan.lock);
  74. mutex_lock(&priv->conf_mutex);
  75. ret = wsm_set_template_frame(priv, &frame);
  76. if (!ret) {
  77. /* Host want to be the probe responder. */
  78. ret = wsm_set_probe_responder(priv, true);
  79. }
  80. if (ret) {
  81. mutex_unlock(&priv->conf_mutex);
  82. up(&priv->scan.lock);
  83. dev_kfree_skb(frame.skb);
  84. return ret;
  85. }
  86. wsm_lock_tx(priv);
  87. BUG_ON(priv->scan.req);
  88. priv->scan.req = req;
  89. priv->scan.n_ssids = 0;
  90. priv->scan.status = 0;
  91. priv->scan.begin = &req->channels[0];
  92. priv->scan.curr = priv->scan.begin;
  93. priv->scan.end = &req->channels[req->n_channels];
  94. priv->scan.output_power = priv->output_power;
  95. for (i = 0; i < req->n_ssids; ++i) {
  96. struct wsm_ssid *dst = &priv->scan.ssids[priv->scan.n_ssids];
  97. memcpy(&dst->ssid[0], req->ssids[i].ssid, sizeof(dst->ssid));
  98. dst->length = req->ssids[i].ssid_len;
  99. ++priv->scan.n_ssids;
  100. }
  101. mutex_unlock(&priv->conf_mutex);
  102. if (frame.skb)
  103. dev_kfree_skb(frame.skb);
  104. queue_work(priv->workqueue, &priv->scan.work);
  105. return 0;
  106. }
  107. void cw1200_scan_work(struct work_struct *work)
  108. {
  109. struct cw1200_common *priv = container_of(work, struct cw1200_common,
  110. scan.work);
  111. struct ieee80211_channel **it;
  112. struct wsm_scan scan = {
  113. .type = WSM_SCAN_TYPE_FOREGROUND,
  114. .flags = WSM_SCAN_FLAG_SPLIT_METHOD,
  115. };
  116. bool first_run = (priv->scan.begin == priv->scan.curr &&
  117. priv->scan.begin != priv->scan.end);
  118. int i;
  119. if (first_run) {
  120. /* Firmware gets crazy if scan request is sent
  121. * when STA is joined but not yet associated.
  122. * Force unjoin in this case.
  123. */
  124. if (cancel_delayed_work_sync(&priv->join_timeout) > 0)
  125. cw1200_join_timeout(&priv->join_timeout.work);
  126. }
  127. mutex_lock(&priv->conf_mutex);
  128. if (first_run) {
  129. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  130. !(priv->powersave_mode.mode & WSM_PSM_PS)) {
  131. struct wsm_set_pm pm = priv->powersave_mode;
  132. pm.mode = WSM_PSM_PS;
  133. cw1200_set_pm(priv, &pm);
  134. } else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
  135. /* FW bug: driver has to restart p2p-dev mode
  136. * after scan
  137. */
  138. cw1200_disable_listening(priv);
  139. }
  140. }
  141. if (!priv->scan.req || (priv->scan.curr == priv->scan.end)) {
  142. if (priv->scan.output_power != priv->output_power)
  143. wsm_set_output_power(priv, priv->output_power * 10);
  144. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  145. !(priv->powersave_mode.mode & WSM_PSM_PS))
  146. cw1200_set_pm(priv, &priv->powersave_mode);
  147. if (priv->scan.status < 0)
  148. wiphy_dbg(priv->hw->wiphy, "[SCAN] Scan failed (%d).\n",
  149. priv->scan.status);
  150. else if (priv->scan.req)
  151. wiphy_dbg(priv->hw->wiphy,
  152. "[SCAN] Scan completed.\n");
  153. else
  154. wiphy_dbg(priv->hw->wiphy,
  155. "[SCAN] Scan canceled.\n");
  156. priv->scan.req = NULL;
  157. cw1200_scan_restart_delayed(priv);
  158. wsm_unlock_tx(priv);
  159. mutex_unlock(&priv->conf_mutex);
  160. ieee80211_scan_completed(priv->hw, priv->scan.status ? 1 : 0);
  161. up(&priv->scan.lock);
  162. return;
  163. } else {
  164. struct ieee80211_channel *first = *priv->scan.curr;
  165. for (it = priv->scan.curr + 1, i = 1;
  166. it != priv->scan.end && i < WSM_SCAN_MAX_NUM_OF_CHANNELS;
  167. ++it, ++i) {
  168. if ((*it)->band != first->band)
  169. break;
  170. if (((*it)->flags ^ first->flags) &
  171. IEEE80211_CHAN_PASSIVE_SCAN)
  172. break;
  173. if (!(first->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
  174. (*it)->max_power != first->max_power)
  175. break;
  176. }
  177. scan.band = first->band;
  178. if (priv->scan.req->no_cck)
  179. scan.max_tx_rate = WSM_TRANSMIT_RATE_6;
  180. else
  181. scan.max_tx_rate = WSM_TRANSMIT_RATE_1;
  182. scan.num_probes =
  183. (first->flags & IEEE80211_CHAN_PASSIVE_SCAN) ? 0 : 2;
  184. scan.num_ssids = priv->scan.n_ssids;
  185. scan.ssids = &priv->scan.ssids[0];
  186. scan.num_channels = it - priv->scan.curr;
  187. /* TODO: Is it optimal? */
  188. scan.probe_delay = 100;
  189. /* It is not stated in WSM specification, however
  190. * FW team says that driver may not use FG scan
  191. * when joined.
  192. */
  193. if (priv->join_status == CW1200_JOIN_STATUS_STA) {
  194. scan.type = WSM_SCAN_TYPE_BACKGROUND;
  195. scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
  196. }
  197. scan.ch = kzalloc(
  198. sizeof(struct wsm_scan_ch) * (it - priv->scan.curr),
  199. GFP_KERNEL);
  200. if (!scan.ch) {
  201. priv->scan.status = -ENOMEM;
  202. goto fail;
  203. }
  204. for (i = 0; i < scan.num_channels; ++i) {
  205. scan.ch[i].number = priv->scan.curr[i]->hw_value;
  206. if (priv->scan.curr[i]->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  207. scan.ch[i].min_chan_time = 50;
  208. scan.ch[i].max_chan_time = 100;
  209. } else {
  210. scan.ch[i].min_chan_time = 10;
  211. scan.ch[i].max_chan_time = 25;
  212. }
  213. }
  214. if (!(first->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
  215. priv->scan.output_power != first->max_power) {
  216. priv->scan.output_power = first->max_power;
  217. wsm_set_output_power(priv,
  218. priv->scan.output_power * 10);
  219. }
  220. priv->scan.status = cw1200_scan_start(priv, &scan);
  221. kfree(scan.ch);
  222. if (priv->scan.status)
  223. goto fail;
  224. priv->scan.curr = it;
  225. }
  226. mutex_unlock(&priv->conf_mutex);
  227. return;
  228. fail:
  229. priv->scan.curr = priv->scan.end;
  230. mutex_unlock(&priv->conf_mutex);
  231. queue_work(priv->workqueue, &priv->scan.work);
  232. return;
  233. }
  234. static void cw1200_scan_restart_delayed(struct cw1200_common *priv)
  235. {
  236. /* FW bug: driver has to restart p2p-dev mode after scan. */
  237. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
  238. cw1200_enable_listening(priv);
  239. cw1200_update_filtering(priv);
  240. }
  241. if (priv->delayed_unjoin) {
  242. priv->delayed_unjoin = false;
  243. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  244. wsm_unlock_tx(priv);
  245. } else if (priv->delayed_link_loss) {
  246. wiphy_dbg(priv->hw->wiphy, "[CQM] Requeue BSS loss.\n");
  247. priv->delayed_link_loss = 0;
  248. cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
  249. }
  250. }
  251. static void cw1200_scan_complete(struct cw1200_common *priv)
  252. {
  253. queue_delayed_work(priv->workqueue, &priv->clear_recent_scan_work, HZ);
  254. if (priv->scan.direct_probe) {
  255. wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe complete.\n");
  256. cw1200_scan_restart_delayed(priv);
  257. priv->scan.direct_probe = 0;
  258. up(&priv->scan.lock);
  259. wsm_unlock_tx(priv);
  260. } else {
  261. cw1200_scan_work(&priv->scan.work);
  262. }
  263. }
  264. void cw1200_scan_failed_cb(struct cw1200_common *priv)
  265. {
  266. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  267. /* STA is stopped. */
  268. return;
  269. if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
  270. priv->scan.status = -EIO;
  271. queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
  272. }
  273. }
  274. void cw1200_scan_complete_cb(struct cw1200_common *priv,
  275. struct wsm_scan_complete *arg)
  276. {
  277. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  278. /* STA is stopped. */
  279. return;
  280. if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
  281. priv->scan.status = 1;
  282. queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
  283. }
  284. }
  285. void cw1200_clear_recent_scan_work(struct work_struct *work)
  286. {
  287. struct cw1200_common *priv =
  288. container_of(work, struct cw1200_common,
  289. clear_recent_scan_work.work);
  290. atomic_xchg(&priv->recent_scan, 0);
  291. }
  292. void cw1200_scan_timeout(struct work_struct *work)
  293. {
  294. struct cw1200_common *priv =
  295. container_of(work, struct cw1200_common, scan.timeout.work);
  296. if (atomic_xchg(&priv->scan.in_progress, 0)) {
  297. if (priv->scan.status > 0) {
  298. priv->scan.status = 0;
  299. } else if (!priv->scan.status) {
  300. wiphy_warn(priv->hw->wiphy,
  301. "Timeout waiting for scan complete notification.\n");
  302. priv->scan.status = -ETIMEDOUT;
  303. priv->scan.curr = priv->scan.end;
  304. wsm_stop_scan(priv);
  305. }
  306. cw1200_scan_complete(priv);
  307. }
  308. }
  309. void cw1200_probe_work(struct work_struct *work)
  310. {
  311. struct cw1200_common *priv =
  312. container_of(work, struct cw1200_common, scan.probe_work.work);
  313. u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
  314. struct cw1200_queue *queue = &priv->tx_queue[queue_id];
  315. const struct cw1200_txpriv *txpriv;
  316. struct wsm_tx *wsm;
  317. struct wsm_template_frame frame = {
  318. .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
  319. };
  320. struct wsm_ssid ssids[1] = {{
  321. .length = 0,
  322. } };
  323. struct wsm_scan_ch ch[1] = {{
  324. .min_chan_time = 0,
  325. .max_chan_time = 10,
  326. } };
  327. struct wsm_scan scan = {
  328. .type = WSM_SCAN_TYPE_FOREGROUND,
  329. .num_probes = 1,
  330. .probe_delay = 0,
  331. .num_channels = 1,
  332. .ssids = ssids,
  333. .ch = ch,
  334. };
  335. u8 *ies;
  336. size_t ies_len;
  337. int ret;
  338. wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe work.\n");
  339. mutex_lock(&priv->conf_mutex);
  340. if (down_trylock(&priv->scan.lock)) {
  341. /* Scan is already in progress. Requeue self. */
  342. schedule();
  343. queue_delayed_work(priv->workqueue,
  344. &priv->scan.probe_work, HZ / 10);
  345. mutex_unlock(&priv->conf_mutex);
  346. return;
  347. }
  348. /* Make sure we still have a pending probe req */
  349. if (cw1200_queue_get_skb(queue, priv->pending_frame_id,
  350. &frame.skb, &txpriv)) {
  351. up(&priv->scan.lock);
  352. mutex_unlock(&priv->conf_mutex);
  353. wsm_unlock_tx(priv);
  354. return;
  355. }
  356. wsm = (struct wsm_tx *)frame.skb->data;
  357. scan.max_tx_rate = wsm->max_tx_rate;
  358. scan.band = (priv->channel->band == IEEE80211_BAND_5GHZ) ?
  359. WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
  360. if (priv->join_status == CW1200_JOIN_STATUS_STA ||
  361. priv->join_status == CW1200_JOIN_STATUS_IBSS) {
  362. scan.type = WSM_SCAN_TYPE_BACKGROUND;
  363. scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
  364. }
  365. ch[0].number = priv->channel->hw_value;
  366. skb_pull(frame.skb, txpriv->offset);
  367. ies = &frame.skb->data[sizeof(struct ieee80211_hdr_3addr)];
  368. ies_len = frame.skb->len - sizeof(struct ieee80211_hdr_3addr);
  369. if (ies_len) {
  370. u8 *ssidie =
  371. (u8 *)cfg80211_find_ie(WLAN_EID_SSID, ies, ies_len);
  372. if (ssidie && ssidie[1] && ssidie[1] <= sizeof(ssids[0].ssid)) {
  373. u8 *nextie = &ssidie[2 + ssidie[1]];
  374. /* Remove SSID from the IE list. It has to be provided
  375. * as a separate argument in cw1200_scan_start call
  376. */
  377. /* Store SSID localy */
  378. ssids[0].length = ssidie[1];
  379. memcpy(ssids[0].ssid, &ssidie[2], ssids[0].length);
  380. scan.num_ssids = 1;
  381. /* Remove SSID from IE list */
  382. ssidie[1] = 0;
  383. memmove(&ssidie[2], nextie, &ies[ies_len] - nextie);
  384. skb_trim(frame.skb, frame.skb->len - ssids[0].length);
  385. }
  386. }
  387. /* FW bug: driver has to restart p2p-dev mode after scan */
  388. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
  389. cw1200_disable_listening(priv);
  390. ret = wsm_set_template_frame(priv, &frame);
  391. priv->scan.direct_probe = 1;
  392. if (!ret) {
  393. wsm_flush_tx(priv);
  394. ret = cw1200_scan_start(priv, &scan);
  395. }
  396. mutex_unlock(&priv->conf_mutex);
  397. skb_push(frame.skb, txpriv->offset);
  398. if (!ret)
  399. IEEE80211_SKB_CB(frame.skb)->flags |= IEEE80211_TX_STAT_ACK;
  400. BUG_ON(cw1200_queue_remove(queue, priv->pending_frame_id));
  401. if (ret) {
  402. priv->scan.direct_probe = 0;
  403. up(&priv->scan.lock);
  404. wsm_unlock_tx(priv);
  405. }
  406. return;
  407. }