scan.c 13 KB

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