scan.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/ieee80211.h>
  24. #include "wl12xx.h"
  25. #include "cmd.h"
  26. #include "scan.h"
  27. #include "acx.h"
  28. #include "ps.h"
  29. void wl1271_scan_complete_work(struct work_struct *work)
  30. {
  31. struct delayed_work *dwork;
  32. struct wl1271 *wl;
  33. dwork = container_of(work, struct delayed_work, work);
  34. wl = container_of(dwork, struct wl1271, scan_complete_work);
  35. wl1271_debug(DEBUG_SCAN, "Scanning complete");
  36. mutex_lock(&wl->mutex);
  37. if (wl->state == WL1271_STATE_OFF)
  38. goto out;
  39. if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
  40. goto out;
  41. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  42. kfree(wl->scan.scanned_ch);
  43. wl->scan.scanned_ch = NULL;
  44. wl->scan.req = NULL;
  45. ieee80211_scan_completed(wl->hw, false);
  46. /* restore hardware connection monitoring template */
  47. if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
  48. if (wl1271_ps_elp_wakeup(wl) == 0) {
  49. wl1271_cmd_build_ap_probe_req(wl, wl->probereq);
  50. wl1271_ps_elp_sleep(wl);
  51. }
  52. }
  53. if (wl->scan.failed) {
  54. wl1271_info("Scan completed due to error.");
  55. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  56. }
  57. out:
  58. mutex_unlock(&wl->mutex);
  59. }
  60. static int wl1271_get_scan_channels(struct wl1271 *wl,
  61. struct cfg80211_scan_request *req,
  62. struct basic_scan_channel_params *channels,
  63. enum ieee80211_band band, bool passive)
  64. {
  65. struct conf_scan_settings *c = &wl->conf.scan;
  66. int i, j;
  67. u32 flags;
  68. for (i = 0, j = 0;
  69. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  70. i++) {
  71. flags = req->channels[i]->flags;
  72. if (!wl->scan.scanned_ch[i] &&
  73. !(flags & IEEE80211_CHAN_DISABLED) &&
  74. ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
  75. (req->channels[i]->band == band)) {
  76. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  77. req->channels[i]->band,
  78. req->channels[i]->center_freq);
  79. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  80. req->channels[i]->hw_value,
  81. req->channels[i]->flags);
  82. wl1271_debug(DEBUG_SCAN,
  83. "max_antenna_gain %d, max_power %d",
  84. req->channels[i]->max_antenna_gain,
  85. req->channels[i]->max_power);
  86. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  87. req->channels[i]->beacon_found);
  88. if (!passive) {
  89. channels[j].min_duration =
  90. cpu_to_le32(c->min_dwell_time_active);
  91. channels[j].max_duration =
  92. cpu_to_le32(c->max_dwell_time_active);
  93. } else {
  94. channels[j].min_duration =
  95. cpu_to_le32(c->min_dwell_time_passive);
  96. channels[j].max_duration =
  97. cpu_to_le32(c->max_dwell_time_passive);
  98. }
  99. channels[j].early_termination = 0;
  100. channels[j].tx_power_att = req->channels[i]->max_power;
  101. channels[j].channel = req->channels[i]->hw_value;
  102. memset(&channels[j].bssid_lsb, 0xff, 4);
  103. memset(&channels[j].bssid_msb, 0xff, 2);
  104. /* Mark the channels we already used */
  105. wl->scan.scanned_ch[i] = true;
  106. j++;
  107. }
  108. }
  109. return j;
  110. }
  111. #define WL1271_NOTHING_TO_SCAN 1
  112. static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
  113. bool passive, u32 basic_rate)
  114. {
  115. struct wl1271_cmd_scan *cmd;
  116. struct wl1271_cmd_trigger_scan_to *trigger;
  117. int ret;
  118. u16 scan_options = 0;
  119. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  120. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  121. if (!cmd || !trigger) {
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. /* We always use high priority scans */
  126. scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
  127. /* No SSIDs means that we have a forced passive scan */
  128. if (passive || wl->scan.req->n_ssids == 0)
  129. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  130. cmd->params.scan_options = cpu_to_le16(scan_options);
  131. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  132. cmd->channels,
  133. band, passive);
  134. if (cmd->params.n_ch == 0) {
  135. ret = WL1271_NOTHING_TO_SCAN;
  136. goto out;
  137. }
  138. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  139. cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  140. cmd->params.rx_filter_options =
  141. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  142. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  143. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  144. cmd->params.tid_trigger = 0;
  145. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  146. if (band == IEEE80211_BAND_2GHZ)
  147. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  148. else
  149. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  150. if (wl->scan.ssid_len && wl->scan.ssid) {
  151. cmd->params.ssid_len = wl->scan.ssid_len;
  152. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  153. }
  154. ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
  155. wl->scan.req->ie, wl->scan.req->ie_len,
  156. band);
  157. if (ret < 0) {
  158. wl1271_error("PROBE request template failed");
  159. goto out;
  160. }
  161. /* disable the timeout */
  162. trigger->timeout = 0;
  163. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  164. sizeof(*trigger), 0);
  165. if (ret < 0) {
  166. wl1271_error("trigger scan to failed for hw scan");
  167. goto out;
  168. }
  169. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  170. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  171. if (ret < 0) {
  172. wl1271_error("SCAN failed");
  173. goto out;
  174. }
  175. out:
  176. kfree(cmd);
  177. kfree(trigger);
  178. return ret;
  179. }
  180. void wl1271_scan_stm(struct wl1271 *wl)
  181. {
  182. int ret = 0;
  183. switch (wl->scan.state) {
  184. case WL1271_SCAN_STATE_IDLE:
  185. break;
  186. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  187. ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
  188. wl->conf.tx.basic_rate);
  189. if (ret == WL1271_NOTHING_TO_SCAN) {
  190. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  191. wl1271_scan_stm(wl);
  192. }
  193. break;
  194. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  195. ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
  196. wl->conf.tx.basic_rate);
  197. if (ret == WL1271_NOTHING_TO_SCAN) {
  198. if (wl->enable_11a)
  199. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  200. else
  201. wl->scan.state = WL1271_SCAN_STATE_DONE;
  202. wl1271_scan_stm(wl);
  203. }
  204. break;
  205. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  206. ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
  207. wl->conf.tx.basic_rate_5);
  208. if (ret == WL1271_NOTHING_TO_SCAN) {
  209. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  210. wl1271_scan_stm(wl);
  211. }
  212. break;
  213. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  214. ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
  215. wl->conf.tx.basic_rate_5);
  216. if (ret == WL1271_NOTHING_TO_SCAN) {
  217. wl->scan.state = WL1271_SCAN_STATE_DONE;
  218. wl1271_scan_stm(wl);
  219. }
  220. break;
  221. case WL1271_SCAN_STATE_DONE:
  222. wl->scan.failed = false;
  223. cancel_delayed_work(&wl->scan_complete_work);
  224. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  225. msecs_to_jiffies(0));
  226. break;
  227. default:
  228. wl1271_error("invalid scan state");
  229. break;
  230. }
  231. if (ret < 0) {
  232. cancel_delayed_work(&wl->scan_complete_work);
  233. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  234. msecs_to_jiffies(0));
  235. }
  236. }
  237. int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
  238. struct cfg80211_scan_request *req)
  239. {
  240. if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
  241. return -EBUSY;
  242. wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
  243. if (ssid_len && ssid) {
  244. wl->scan.ssid_len = ssid_len;
  245. memcpy(wl->scan.ssid, ssid, ssid_len);
  246. } else {
  247. wl->scan.ssid_len = 0;
  248. }
  249. wl->scan.req = req;
  250. wl->scan.scanned_ch = kcalloc(req->n_channels,
  251. sizeof(*wl->scan.scanned_ch),
  252. GFP_KERNEL);
  253. /* we assume failure so that timeout scenarios are handled correctly */
  254. wl->scan.failed = true;
  255. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  256. msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
  257. wl1271_scan_stm(wl);
  258. return 0;
  259. }