wl1271_scan.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "wl1271.h"
  25. #include "wl1271_cmd.h"
  26. #include "wl1271_scan.h"
  27. #include "wl1271_acx.h"
  28. void wl1271_scan_complete_work(struct work_struct *work)
  29. {
  30. struct delayed_work *dwork;
  31. struct wl1271 *wl;
  32. dwork = container_of(work, struct delayed_work, work);
  33. wl = container_of(dwork, struct wl1271, scan_complete_work);
  34. wl1271_debug(DEBUG_SCAN, "Scanning complete");
  35. mutex_lock(&wl->mutex);
  36. if (wl->scan.state == WL1271_SCAN_STATE_IDLE) {
  37. mutex_unlock(&wl->mutex);
  38. return;
  39. }
  40. wl->scan.state = WL1271_SCAN_STATE_IDLE;
  41. kfree(wl->scan.scanned_ch);
  42. wl->scan.scanned_ch = NULL;
  43. mutex_unlock(&wl->mutex);
  44. ieee80211_scan_completed(wl->hw, false);
  45. if (wl->scan.failed) {
  46. wl1271_info("Scan completed due to error.");
  47. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  48. }
  49. }
  50. static int wl1271_get_scan_channels(struct wl1271 *wl,
  51. struct cfg80211_scan_request *req,
  52. struct basic_scan_channel_params *channels,
  53. enum ieee80211_band band, bool passive)
  54. {
  55. int i, j;
  56. u32 flags;
  57. for (i = 0, j = 0;
  58. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  59. i++) {
  60. flags = req->channels[i]->flags;
  61. if (!wl->scan.scanned_ch[i] &&
  62. !(flags & IEEE80211_CHAN_DISABLED) &&
  63. ((!!(flags & IEEE80211_CHAN_PASSIVE_SCAN)) == passive) &&
  64. (req->channels[i]->band == band)) {
  65. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  66. req->channels[i]->band,
  67. req->channels[i]->center_freq);
  68. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  69. req->channels[i]->hw_value,
  70. req->channels[i]->flags);
  71. wl1271_debug(DEBUG_SCAN,
  72. "max_antenna_gain %d, max_power %d",
  73. req->channels[i]->max_antenna_gain,
  74. req->channels[i]->max_power);
  75. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  76. req->channels[i]->beacon_found);
  77. channels[j].min_duration =
  78. cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
  79. channels[j].max_duration =
  80. cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
  81. channels[j].early_termination = 0;
  82. channels[j].tx_power_att = req->channels[i]->max_power;
  83. channels[j].channel = req->channels[i]->hw_value;
  84. memset(&channels[j].bssid_lsb, 0xff, 4);
  85. memset(&channels[j].bssid_msb, 0xff, 2);
  86. /* Mark the channels we already used */
  87. wl->scan.scanned_ch[i] = true;
  88. j++;
  89. }
  90. }
  91. return j;
  92. }
  93. #define WL1271_NOTHING_TO_SCAN 1
  94. static int wl1271_scan_send(struct wl1271 *wl, enum ieee80211_band band,
  95. bool passive, u32 basic_rate)
  96. {
  97. struct wl1271_cmd_scan *cmd;
  98. struct wl1271_cmd_trigger_scan_to *trigger;
  99. int ret;
  100. u16 scan_options = 0;
  101. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  102. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  103. if (!cmd || !trigger) {
  104. ret = -ENOMEM;
  105. goto out;
  106. }
  107. /* We always use high priority scans */
  108. scan_options = WL1271_SCAN_OPT_PRIORITY_HIGH;
  109. /* No SSIDs means that we have a forced passive scan */
  110. if (passive || wl->scan.req->n_ssids == 0)
  111. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  112. cmd->params.scan_options = cpu_to_le16(scan_options);
  113. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  114. cmd->channels,
  115. band, passive);
  116. if (cmd->params.n_ch == 0) {
  117. ret = WL1271_NOTHING_TO_SCAN;
  118. goto out;
  119. }
  120. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  121. cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  122. cmd->params.rx_filter_options =
  123. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  124. cmd->params.n_probe_reqs = WL1271_SCAN_PROBE_REQS;
  125. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  126. cmd->params.tid_trigger = 0;
  127. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  128. if (band == IEEE80211_BAND_2GHZ)
  129. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  130. else
  131. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  132. if (wl->scan.ssid_len && wl->scan.ssid) {
  133. cmd->params.ssid_len = wl->scan.ssid_len;
  134. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  135. }
  136. ret = wl1271_cmd_build_probe_req(wl, wl->scan.ssid, wl->scan.ssid_len,
  137. wl->scan.req->ie, wl->scan.req->ie_len,
  138. band);
  139. if (ret < 0) {
  140. wl1271_error("PROBE request template failed");
  141. goto out;
  142. }
  143. /* disable the timeout */
  144. trigger->timeout = 0;
  145. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  146. sizeof(*trigger), 0);
  147. if (ret < 0) {
  148. wl1271_error("trigger scan to failed for hw scan");
  149. goto out;
  150. }
  151. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  152. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  153. if (ret < 0) {
  154. wl1271_error("SCAN failed");
  155. goto out;
  156. }
  157. out:
  158. kfree(cmd);
  159. kfree(trigger);
  160. return ret;
  161. }
  162. void wl1271_scan_stm(struct wl1271 *wl)
  163. {
  164. int ret = 0;
  165. switch (wl->scan.state) {
  166. case WL1271_SCAN_STATE_IDLE:
  167. break;
  168. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  169. ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, false,
  170. wl->conf.tx.basic_rate);
  171. if (ret == WL1271_NOTHING_TO_SCAN) {
  172. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  173. wl1271_scan_stm(wl);
  174. }
  175. break;
  176. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  177. ret = wl1271_scan_send(wl, IEEE80211_BAND_2GHZ, true,
  178. wl->conf.tx.basic_rate);
  179. if (ret == WL1271_NOTHING_TO_SCAN) {
  180. if (wl->enable_11a)
  181. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  182. else
  183. wl->scan.state = WL1271_SCAN_STATE_DONE;
  184. wl1271_scan_stm(wl);
  185. }
  186. break;
  187. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  188. ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, false,
  189. wl->conf.tx.basic_rate_5);
  190. if (ret == WL1271_NOTHING_TO_SCAN) {
  191. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  192. wl1271_scan_stm(wl);
  193. }
  194. break;
  195. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  196. ret = wl1271_scan_send(wl, IEEE80211_BAND_5GHZ, true,
  197. wl->conf.tx.basic_rate_5);
  198. if (ret == WL1271_NOTHING_TO_SCAN) {
  199. wl->scan.state = WL1271_SCAN_STATE_DONE;
  200. wl1271_scan_stm(wl);
  201. }
  202. break;
  203. case WL1271_SCAN_STATE_DONE:
  204. wl->scan.failed = false;
  205. cancel_delayed_work(&wl->scan_complete_work);
  206. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  207. msecs_to_jiffies(0));
  208. break;
  209. default:
  210. wl1271_error("invalid scan state");
  211. break;
  212. }
  213. if (ret < 0) {
  214. cancel_delayed_work(&wl->scan_complete_work);
  215. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  216. msecs_to_jiffies(0));
  217. }
  218. }
  219. int wl1271_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
  220. struct cfg80211_scan_request *req)
  221. {
  222. if (wl->scan.state != WL1271_SCAN_STATE_IDLE)
  223. return -EBUSY;
  224. wl->scan.state = WL1271_SCAN_STATE_2GHZ_ACTIVE;
  225. if (ssid_len && ssid) {
  226. wl->scan.ssid_len = ssid_len;
  227. memcpy(wl->scan.ssid, ssid, ssid_len);
  228. } else {
  229. wl->scan.ssid_len = 0;
  230. }
  231. wl->scan.req = req;
  232. wl->scan.scanned_ch = kzalloc(req->n_channels *
  233. sizeof(*wl->scan.scanned_ch),
  234. GFP_KERNEL);
  235. /* we assume failure so that timeout scenarios are handled correctly */
  236. wl->scan.failed = true;
  237. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  238. msecs_to_jiffies(WL1271_SCAN_TIMEOUT));
  239. wl1271_scan_stm(wl);
  240. return 0;
  241. }