wl1271_cmd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009 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/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/crc7.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/etherdevice.h>
  28. #include "wl1271.h"
  29. #include "wl1271_reg.h"
  30. #include "wl1271_spi.h"
  31. #include "wl1271_acx.h"
  32. #include "wl12xx_80211.h"
  33. #include "wl1271_cmd.h"
  34. /*
  35. * send command to firmware
  36. *
  37. * @wl: wl struct
  38. * @id: command id
  39. * @buf: buffer containing the command, must work with dma
  40. * @len: length of the buffer
  41. */
  42. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len)
  43. {
  44. struct wl1271_cmd_header *cmd;
  45. unsigned long timeout;
  46. u32 intr;
  47. int ret = 0;
  48. cmd = buf;
  49. cmd->id = cpu_to_le16(id);
  50. cmd->status = 0;
  51. WARN_ON(len % 4 != 0);
  52. wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
  53. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  54. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  55. intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  56. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  57. if (time_after(jiffies, timeout)) {
  58. wl1271_error("command complete timeout");
  59. ret = -ETIMEDOUT;
  60. goto out;
  61. }
  62. msleep(1);
  63. intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  64. }
  65. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
  66. WL1271_ACX_INTR_CMD_COMPLETE);
  67. out:
  68. return ret;
  69. }
  70. static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
  71. {
  72. struct wl1271_cmd_cal_channel_tune *cmd;
  73. int ret = 0;
  74. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  75. if (!cmd)
  76. return -ENOMEM;
  77. cmd->test.id = TEST_CMD_CHANNEL_TUNE;
  78. cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
  79. /* set up any channel, 7 is in the middle of the range */
  80. cmd->channel = 7;
  81. ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
  82. if (ret < 0)
  83. wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
  84. kfree(cmd);
  85. return ret;
  86. }
  87. static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
  88. {
  89. struct wl1271_cmd_cal_update_ref_point *cmd;
  90. int ret = 0;
  91. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  92. if (!cmd)
  93. return -ENOMEM;
  94. cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
  95. /* FIXME: still waiting for the correct values */
  96. cmd->ref_power = 0;
  97. cmd->ref_detector = 0;
  98. cmd->sub_band = WL1271_PD_REFERENCE_POINT_BAND_B_G;
  99. ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
  100. if (ret < 0)
  101. wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
  102. kfree(cmd);
  103. return ret;
  104. }
  105. static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
  106. {
  107. struct wl1271_cmd_cal_p2g *cmd;
  108. int ret = 0;
  109. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  110. if (!cmd)
  111. return -ENOMEM;
  112. cmd->test.id = TEST_CMD_P2G_CAL;
  113. cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
  114. ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
  115. if (ret < 0)
  116. wl1271_warning("TEST_CMD_P2G_CAL failed");
  117. kfree(cmd);
  118. return ret;
  119. }
  120. static int wl1271_cmd_cal(struct wl1271 *wl)
  121. {
  122. /*
  123. * FIXME: we must make sure that we're not sleeping when calibration
  124. * is done
  125. */
  126. int ret;
  127. wl1271_notice("performing tx calibration");
  128. ret = wl1271_cmd_cal_channel_tune(wl);
  129. if (ret < 0)
  130. return ret;
  131. ret = wl1271_cmd_cal_update_ref_point(wl);
  132. if (ret < 0)
  133. return ret;
  134. ret = wl1271_cmd_cal_p2g(wl);
  135. if (ret < 0)
  136. return ret;
  137. return ret;
  138. }
  139. int wl1271_cmd_join(struct wl1271 *wl)
  140. {
  141. static bool do_cal = true;
  142. struct wl1271_cmd_join *join;
  143. int ret, i;
  144. u8 *bssid;
  145. /* FIXME: remove when we get calibration from the factory */
  146. if (do_cal) {
  147. ret = wl1271_cmd_cal(wl);
  148. if (ret < 0)
  149. wl1271_warning("couldn't calibrate");
  150. else
  151. do_cal = false;
  152. }
  153. /* FIXME: This is a workaround, because with the current stack, we
  154. * cannot know when we have disassociated. So, if we have already
  155. * joined, we disconnect before joining again. */
  156. if (wl->joined) {
  157. ret = wl1271_cmd_disconnect(wl);
  158. if (ret < 0) {
  159. wl1271_error("failed to disconnect before rejoining");
  160. goto out;
  161. }
  162. wl->joined = false;
  163. }
  164. join = kzalloc(sizeof(*join), GFP_KERNEL);
  165. if (!join) {
  166. ret = -ENOMEM;
  167. goto out;
  168. }
  169. wl1271_debug(DEBUG_CMD, "cmd join");
  170. /* Reverse order BSSID */
  171. bssid = (u8 *) &join->bssid_lsb;
  172. for (i = 0; i < ETH_ALEN; i++)
  173. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  174. join->rx_config_options = cpu_to_le32(wl->rx_config);
  175. join->rx_filter_options = cpu_to_le32(wl->rx_filter);
  176. join->bss_type = wl->bss_type;
  177. /*
  178. * FIXME: disable temporarily all filters because after commit
  179. * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
  180. * association. The filter logic needs to be implemented properly
  181. * and once that is done, this hack can be removed.
  182. */
  183. join->rx_config_options = cpu_to_le32(0);
  184. join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
  185. if (wl->band == IEEE80211_BAND_2GHZ)
  186. join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS |
  187. CONF_HW_BIT_RATE_2MBPS |
  188. CONF_HW_BIT_RATE_5_5MBPS |
  189. CONF_HW_BIT_RATE_11MBPS);
  190. else {
  191. join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
  192. join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS |
  193. CONF_HW_BIT_RATE_12MBPS |
  194. CONF_HW_BIT_RATE_24MBPS);
  195. }
  196. join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
  197. join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
  198. join->channel = wl->channel;
  199. join->ssid_len = wl->ssid_len;
  200. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  201. join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
  202. /* increment the session counter */
  203. wl->session_counter++;
  204. if (wl->session_counter >= SESSION_COUNTER_MAX)
  205. wl->session_counter = 0;
  206. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  207. /* reset TX security counters */
  208. wl->tx_security_last_seq = 0;
  209. wl->tx_security_seq_16 = 0;
  210. wl->tx_security_seq_32 = 0;
  211. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
  212. if (ret < 0) {
  213. wl1271_error("failed to initiate cmd join");
  214. goto out_free;
  215. }
  216. wl->joined = true;
  217. /*
  218. * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
  219. * simplify locking we just sleep instead, for now
  220. */
  221. msleep(10);
  222. out_free:
  223. kfree(join);
  224. out:
  225. return ret;
  226. }
  227. /**
  228. * send test command to firmware
  229. *
  230. * @wl: wl struct
  231. * @buf: buffer containing the command, with all headers, must work with dma
  232. * @len: length of the buffer
  233. * @answer: is answer needed
  234. */
  235. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  236. {
  237. int ret;
  238. wl1271_debug(DEBUG_CMD, "cmd test");
  239. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len);
  240. if (ret < 0) {
  241. wl1271_warning("TEST command failed");
  242. return ret;
  243. }
  244. if (answer) {
  245. struct wl1271_command *cmd_answer;
  246. u16 status;
  247. /*
  248. * The test command got in, we can read the answer.
  249. * The answer would be a wl1271_command, where the
  250. * parameter array contains the actual answer.
  251. */
  252. wl1271_spi_read(wl, wl->cmd_box_addr, buf, buf_len, false);
  253. cmd_answer = buf;
  254. status = le16_to_cpu(cmd_answer->header.status);
  255. if (status != CMD_STATUS_SUCCESS)
  256. wl1271_error("TEST command answer error: %d", status);
  257. }
  258. return 0;
  259. }
  260. /**
  261. * read acx from firmware
  262. *
  263. * @wl: wl struct
  264. * @id: acx id
  265. * @buf: buffer for the response, including all headers, must work with dma
  266. * @len: lenght of buf
  267. */
  268. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  269. {
  270. struct acx_header *acx = buf;
  271. int ret;
  272. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  273. acx->id = cpu_to_le16(id);
  274. /* payload length, does not include any headers */
  275. acx->len = cpu_to_le16(len - sizeof(*acx));
  276. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
  277. if (ret < 0) {
  278. wl1271_error("INTERROGATE command failed");
  279. goto out;
  280. }
  281. /* the interrogate command got in, we can read the answer */
  282. wl1271_spi_read(wl, wl->cmd_box_addr, buf, len, false);
  283. acx = buf;
  284. if (le16_to_cpu(acx->cmd.status) != CMD_STATUS_SUCCESS)
  285. wl1271_error("INTERROGATE command error: %d",
  286. le16_to_cpu(acx->cmd.status));
  287. out:
  288. return ret;
  289. }
  290. /**
  291. * write acx value to firmware
  292. *
  293. * @wl: wl struct
  294. * @id: acx id
  295. * @buf: buffer containing acx, including all headers, must work with dma
  296. * @len: length of buf
  297. */
  298. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  299. {
  300. struct acx_header *acx = buf;
  301. int ret;
  302. wl1271_debug(DEBUG_CMD, "cmd configure");
  303. acx->id = cpu_to_le16(id);
  304. /* payload length, does not include any headers */
  305. acx->len = cpu_to_le16(len - sizeof(*acx));
  306. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len);
  307. if (ret < 0) {
  308. wl1271_warning("CONFIGURE command NOK");
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
  314. {
  315. struct cmd_enabledisable_path *cmd;
  316. int ret;
  317. u16 cmd_rx, cmd_tx;
  318. wl1271_debug(DEBUG_CMD, "cmd data path");
  319. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  320. if (!cmd) {
  321. ret = -ENOMEM;
  322. goto out;
  323. }
  324. cmd->channel = channel;
  325. if (enable) {
  326. cmd_rx = CMD_ENABLE_RX;
  327. cmd_tx = CMD_ENABLE_TX;
  328. } else {
  329. cmd_rx = CMD_DISABLE_RX;
  330. cmd_tx = CMD_DISABLE_TX;
  331. }
  332. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
  333. if (ret < 0) {
  334. wl1271_error("rx %s cmd for channel %d failed",
  335. enable ? "start" : "stop", channel);
  336. goto out;
  337. }
  338. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  339. enable ? "start" : "stop", channel);
  340. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
  341. if (ret < 0) {
  342. wl1271_error("tx %s cmd for channel %d failed",
  343. enable ? "start" : "stop", channel);
  344. return ret;
  345. }
  346. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  347. enable ? "start" : "stop", channel);
  348. out:
  349. kfree(cmd);
  350. return ret;
  351. }
  352. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
  353. {
  354. struct wl1271_cmd_ps_params *ps_params = NULL;
  355. int ret = 0;
  356. /* FIXME: this should be in ps.c */
  357. ret = wl1271_acx_wake_up_conditions(wl);
  358. if (ret < 0) {
  359. wl1271_error("couldn't set wake up conditions");
  360. goto out;
  361. }
  362. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  363. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  364. if (!ps_params) {
  365. ret = -ENOMEM;
  366. goto out;
  367. }
  368. ps_params->ps_mode = ps_mode;
  369. ps_params->send_null_data = 1;
  370. ps_params->retries = 5;
  371. ps_params->hang_over_period = 128;
  372. ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
  373. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  374. sizeof(*ps_params));
  375. if (ret < 0) {
  376. wl1271_error("cmd set_ps_mode failed");
  377. goto out;
  378. }
  379. out:
  380. kfree(ps_params);
  381. return ret;
  382. }
  383. int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
  384. size_t len)
  385. {
  386. struct cmd_read_write_memory *cmd;
  387. int ret = 0;
  388. wl1271_debug(DEBUG_CMD, "cmd read memory");
  389. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  390. if (!cmd) {
  391. ret = -ENOMEM;
  392. goto out;
  393. }
  394. WARN_ON(len > MAX_READ_SIZE);
  395. len = min_t(size_t, len, MAX_READ_SIZE);
  396. cmd->addr = cpu_to_le32(addr);
  397. cmd->size = cpu_to_le32(len);
  398. ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
  399. if (ret < 0) {
  400. wl1271_error("read memory command failed: %d", ret);
  401. goto out;
  402. }
  403. /* the read command got in, we can now read the answer */
  404. wl1271_spi_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd), false);
  405. if (le16_to_cpu(cmd->header.status) != CMD_STATUS_SUCCESS)
  406. wl1271_error("error in read command result: %d",
  407. le16_to_cpu(cmd->header.status));
  408. memcpy(answer, cmd->value, len);
  409. out:
  410. kfree(cmd);
  411. return ret;
  412. }
  413. int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
  414. u8 active_scan, u8 high_prio, u8 band,
  415. u8 probe_requests)
  416. {
  417. struct wl1271_cmd_trigger_scan_to *trigger = NULL;
  418. struct wl1271_cmd_scan *params = NULL;
  419. struct ieee80211_channel *channels;
  420. int i, j, n_ch, ret;
  421. u16 scan_options = 0;
  422. u8 ieee_band;
  423. if (band == WL1271_SCAN_BAND_2_4_GHZ)
  424. ieee_band = IEEE80211_BAND_2GHZ;
  425. else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
  426. ieee_band = IEEE80211_BAND_2GHZ;
  427. else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
  428. ieee_band = IEEE80211_BAND_5GHZ;
  429. else
  430. return -EINVAL;
  431. if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
  432. return -EINVAL;
  433. channels = wl->hw->wiphy->bands[ieee_band]->channels;
  434. n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
  435. if (wl->scanning)
  436. return -EINVAL;
  437. params = kzalloc(sizeof(*params), GFP_KERNEL);
  438. if (!params)
  439. return -ENOMEM;
  440. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  441. params->params.rx_filter_options =
  442. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  443. if (!active_scan)
  444. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  445. if (high_prio)
  446. scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
  447. params->params.scan_options = cpu_to_le16(scan_options);
  448. params->params.num_probe_requests = probe_requests;
  449. /* Let the fw autodetect suitable tx_rate for probes */
  450. params->params.tx_rate = 0;
  451. params->params.tid_trigger = 0;
  452. params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  453. if (band == WL1271_SCAN_BAND_DUAL)
  454. params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  455. else
  456. params->params.band = band;
  457. for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
  458. if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
  459. params->channels[j].min_duration =
  460. cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
  461. params->channels[j].max_duration =
  462. cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
  463. memset(&params->channels[j].bssid_lsb, 0xff, 4);
  464. memset(&params->channels[j].bssid_msb, 0xff, 2);
  465. params->channels[j].early_termination = 0;
  466. params->channels[j].tx_power_att =
  467. WL1271_SCAN_CURRENT_TX_PWR;
  468. params->channels[j].channel = channels[i].hw_value;
  469. j++;
  470. }
  471. }
  472. params->params.num_channels = j;
  473. if (len && ssid) {
  474. params->params.ssid_len = len;
  475. memcpy(params->params.ssid, ssid, len);
  476. }
  477. ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
  478. if (ret < 0) {
  479. wl1271_error("PROBE request template failed");
  480. goto out;
  481. }
  482. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  483. if (!trigger) {
  484. ret = -ENOMEM;
  485. goto out;
  486. }
  487. /* disable the timeout */
  488. trigger->timeout = 0;
  489. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  490. sizeof(*trigger));
  491. if (ret < 0) {
  492. wl1271_error("trigger scan to failed for hw scan");
  493. goto out;
  494. }
  495. wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  496. wl->scanning = true;
  497. if (wl1271_11a_enabled()) {
  498. wl->scan.state = band;
  499. if (band == WL1271_SCAN_BAND_DUAL) {
  500. wl->scan.active = active_scan;
  501. wl->scan.high_prio = high_prio;
  502. wl->scan.probe_requests = probe_requests;
  503. if (len && ssid) {
  504. wl->scan.ssid_len = len;
  505. memcpy(wl->scan.ssid, ssid, len);
  506. } else
  507. wl->scan.ssid_len = 0;
  508. }
  509. }
  510. ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
  511. if (ret < 0) {
  512. wl1271_error("SCAN failed");
  513. goto out;
  514. }
  515. wl1271_spi_read(wl, wl->cmd_box_addr, params, sizeof(*params),
  516. false);
  517. if (le16_to_cpu(params->header.status) != CMD_STATUS_SUCCESS) {
  518. wl1271_error("Scan command error: %d",
  519. le16_to_cpu(params->header.status));
  520. wl->scanning = false;
  521. ret = -EIO;
  522. goto out;
  523. }
  524. out:
  525. kfree(params);
  526. return ret;
  527. }
  528. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  529. void *buf, size_t buf_len)
  530. {
  531. struct wl1271_cmd_template_set *cmd;
  532. int ret = 0;
  533. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  534. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  535. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  536. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  537. if (!cmd) {
  538. ret = -ENOMEM;
  539. goto out;
  540. }
  541. cmd->len = cpu_to_le16(buf_len);
  542. cmd->template_type = template_id;
  543. cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
  544. cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
  545. cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
  546. if (buf)
  547. memcpy(cmd->template_data, buf, buf_len);
  548. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd));
  549. if (ret < 0) {
  550. wl1271_warning("cmd set_template failed: %d", ret);
  551. goto out_free;
  552. }
  553. out_free:
  554. kfree(cmd);
  555. out:
  556. return ret;
  557. }
  558. static int wl1271_build_basic_rates(char *rates, u8 band)
  559. {
  560. u8 index = 0;
  561. if (band == IEEE80211_BAND_2GHZ) {
  562. rates[index++] =
  563. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  564. rates[index++] =
  565. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  566. rates[index++] =
  567. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  568. rates[index++] =
  569. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  570. } else if (band == IEEE80211_BAND_5GHZ) {
  571. rates[index++] =
  572. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
  573. rates[index++] =
  574. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
  575. rates[index++] =
  576. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  577. } else {
  578. wl1271_error("build_basic_rates invalid band: %d", band);
  579. }
  580. return index;
  581. }
  582. static int wl1271_build_extended_rates(char *rates, u8 band)
  583. {
  584. u8 index = 0;
  585. if (band == IEEE80211_BAND_2GHZ) {
  586. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  587. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  588. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  589. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  590. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  591. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  592. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  593. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  594. } else if (band == IEEE80211_BAND_5GHZ) {
  595. rates[index++] =
  596. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
  597. rates[index++] =
  598. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
  599. rates[index++] =
  600. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  601. rates[index++] =
  602. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
  603. rates[index++] =
  604. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
  605. rates[index++] =
  606. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
  607. } else {
  608. wl1271_error("build_basic_rates invalid band: %d", band);
  609. }
  610. return index;
  611. }
  612. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  613. {
  614. struct wl12xx_null_data_template template;
  615. if (!is_zero_ether_addr(wl->bssid)) {
  616. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  617. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  618. } else {
  619. memset(template.header.da, 0xff, ETH_ALEN);
  620. memset(template.header.bssid, 0xff, ETH_ALEN);
  621. }
  622. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  623. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  624. IEEE80211_STYPE_NULLFUNC |
  625. IEEE80211_FCTL_TODS);
  626. return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
  627. sizeof(template));
  628. }
  629. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  630. {
  631. struct wl12xx_ps_poll_template template;
  632. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  633. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  634. /* aid in PS-Poll has its two MSBs each set to 1 */
  635. template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
  636. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  637. return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
  638. sizeof(template));
  639. }
  640. int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
  641. u8 band)
  642. {
  643. struct wl12xx_probe_req_template template;
  644. struct wl12xx_ie_rates *rates;
  645. char *ptr;
  646. u16 size;
  647. int ret;
  648. ptr = (char *)&template;
  649. size = sizeof(struct ieee80211_header);
  650. memset(template.header.da, 0xff, ETH_ALEN);
  651. memset(template.header.bssid, 0xff, ETH_ALEN);
  652. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  653. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  654. /* IEs */
  655. /* SSID */
  656. template.ssid.header.id = WLAN_EID_SSID;
  657. template.ssid.header.len = ssid_len;
  658. if (ssid_len && ssid)
  659. memcpy(template.ssid.ssid, ssid, ssid_len);
  660. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  661. ptr += size;
  662. /* Basic Rates */
  663. rates = (struct wl12xx_ie_rates *)ptr;
  664. rates->header.id = WLAN_EID_SUPP_RATES;
  665. rates->header.len = wl1271_build_basic_rates(rates->rates, band);
  666. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  667. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  668. /* Extended rates */
  669. rates = (struct wl12xx_ie_rates *)ptr;
  670. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  671. rates->header.len = wl1271_build_extended_rates(rates->rates, band);
  672. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  673. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  674. if (band == IEEE80211_BAND_2GHZ)
  675. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  676. &template, size);
  677. else
  678. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  679. &template, size);
  680. return ret;
  681. }
  682. int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
  683. {
  684. struct wl1271_cmd_set_keys *cmd;
  685. int ret = 0;
  686. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  687. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  688. if (!cmd) {
  689. ret = -ENOMEM;
  690. goto out;
  691. }
  692. cmd->id = id;
  693. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  694. cmd->key_type = KEY_WEP;
  695. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
  696. if (ret < 0) {
  697. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  698. goto out;
  699. }
  700. out:
  701. kfree(cmd);
  702. return ret;
  703. }
  704. int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  705. u8 key_size, const u8 *key, const u8 *addr,
  706. u32 tx_seq_32, u16 tx_seq_16)
  707. {
  708. struct wl1271_cmd_set_keys *cmd;
  709. int ret = 0;
  710. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  711. if (!cmd) {
  712. ret = -ENOMEM;
  713. goto out;
  714. }
  715. if (key_type != KEY_WEP)
  716. memcpy(cmd->addr, addr, ETH_ALEN);
  717. cmd->key_action = cpu_to_le16(action);
  718. cmd->key_size = key_size;
  719. cmd->key_type = key_type;
  720. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  721. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  722. /* we have only one SSID profile */
  723. cmd->ssid_profile = 0;
  724. cmd->id = id;
  725. if (key_type == KEY_TKIP) {
  726. /*
  727. * We get the key in the following form:
  728. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  729. * but the target is expecting:
  730. * TKIP - RX MIC - TX MIC
  731. */
  732. memcpy(cmd->key, key, 16);
  733. memcpy(cmd->key + 16, key + 24, 8);
  734. memcpy(cmd->key + 24, key + 16, 8);
  735. } else {
  736. memcpy(cmd->key, key, key_size);
  737. }
  738. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  739. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
  740. if (ret < 0) {
  741. wl1271_warning("could not set keys");
  742. goto out;
  743. }
  744. out:
  745. kfree(cmd);
  746. return ret;
  747. }
  748. int wl1271_cmd_disconnect(struct wl1271 *wl)
  749. {
  750. struct wl1271_cmd_disconnect *cmd;
  751. int ret = 0;
  752. wl1271_debug(DEBUG_CMD, "cmd disconnect");
  753. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  754. if (!cmd) {
  755. ret = -ENOMEM;
  756. goto out;
  757. }
  758. cmd->rx_config_options = cpu_to_le32(wl->rx_config);
  759. cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
  760. /* disconnect reason is not used in immediate disconnections */
  761. cmd->type = DISCONNECT_IMMEDIATE;
  762. ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd));
  763. if (ret < 0) {
  764. wl1271_error("failed to send disconnect command");
  765. goto out_free;
  766. }
  767. out_free:
  768. kfree(cmd);
  769. out:
  770. return ret;
  771. }