scan.c 13 KB

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