wl1271_scan.c 6.5 KB

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