PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/st/cw1200/scan.c

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