wl1271_scan.c 6.8 KB

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