wl1271_cmd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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/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 <linux/ieee80211.h>
  29. #include <linux/slab.h>
  30. #include "wl1271.h"
  31. #include "wl1271_reg.h"
  32. #include "wl1271_io.h"
  33. #include "wl1271_acx.h"
  34. #include "wl12xx_80211.h"
  35. #include "wl1271_cmd.h"
  36. #include "wl1271_event.h"
  37. #define WL1271_CMD_FAST_POLL_COUNT 50
  38. /*
  39. * send command to firmware
  40. *
  41. * @wl: wl struct
  42. * @id: command id
  43. * @buf: buffer containing the command, must work with dma
  44. * @len: length of the buffer
  45. */
  46. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  47. size_t res_len)
  48. {
  49. struct wl1271_cmd_header *cmd;
  50. unsigned long timeout;
  51. u32 intr;
  52. int ret = 0;
  53. u16 status;
  54. u16 poll_count = 0;
  55. cmd = buf;
  56. cmd->id = cpu_to_le16(id);
  57. cmd->status = 0;
  58. WARN_ON(len % 4 != 0);
  59. wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
  60. wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  61. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  62. intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  63. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  64. if (time_after(jiffies, timeout)) {
  65. wl1271_error("command complete timeout");
  66. ret = -ETIMEDOUT;
  67. goto out;
  68. }
  69. poll_count++;
  70. if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
  71. udelay(10);
  72. else
  73. msleep(1);
  74. intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  75. }
  76. /* read back the status code of the command */
  77. if (res_len == 0)
  78. res_len = sizeof(struct wl1271_cmd_header);
  79. wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
  80. status = le16_to_cpu(cmd->status);
  81. if (status != CMD_STATUS_SUCCESS) {
  82. wl1271_error("command execute failure %d", status);
  83. ret = -EIO;
  84. }
  85. wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
  86. WL1271_ACX_INTR_CMD_COMPLETE);
  87. out:
  88. return ret;
  89. }
  90. int wl1271_cmd_general_parms(struct wl1271 *wl)
  91. {
  92. struct wl1271_general_parms_cmd *gen_parms;
  93. int ret;
  94. if (!wl->nvs)
  95. return -ENODEV;
  96. gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
  97. if (!gen_parms)
  98. return -ENOMEM;
  99. gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
  100. memcpy(&gen_parms->general_params, &wl->nvs->general_params,
  101. sizeof(struct wl1271_ini_general_params));
  102. ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
  103. if (ret < 0)
  104. wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
  105. kfree(gen_parms);
  106. return ret;
  107. }
  108. int wl1271_cmd_radio_parms(struct wl1271 *wl)
  109. {
  110. struct wl1271_radio_parms_cmd *radio_parms;
  111. struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
  112. int ret;
  113. if (!wl->nvs)
  114. return -ENODEV;
  115. radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
  116. if (!radio_parms)
  117. return -ENOMEM;
  118. radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
  119. /* 2.4GHz parameters */
  120. memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
  121. sizeof(struct wl1271_ini_band_params_2));
  122. memcpy(&radio_parms->dyn_params_2,
  123. &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
  124. sizeof(struct wl1271_ini_fem_params_2));
  125. /* 5GHz parameters */
  126. memcpy(&radio_parms->static_params_5,
  127. &wl->nvs->stat_radio_params_5,
  128. sizeof(struct wl1271_ini_band_params_5));
  129. memcpy(&radio_parms->dyn_params_5,
  130. &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
  131. sizeof(struct wl1271_ini_fem_params_5));
  132. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
  133. radio_parms, sizeof(*radio_parms));
  134. ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
  135. if (ret < 0)
  136. wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
  137. kfree(radio_parms);
  138. return ret;
  139. }
  140. /*
  141. * Poll the mailbox event field until any of the bits in the mask is set or a
  142. * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
  143. */
  144. static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
  145. {
  146. u32 events_vector, event;
  147. unsigned long timeout;
  148. timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
  149. do {
  150. if (time_after(jiffies, timeout))
  151. return -ETIMEDOUT;
  152. msleep(1);
  153. /* read from both event fields */
  154. wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
  155. sizeof(events_vector), false);
  156. event = events_vector & mask;
  157. wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
  158. sizeof(events_vector), false);
  159. event |= events_vector & mask;
  160. } while (!event);
  161. return 0;
  162. }
  163. int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
  164. {
  165. struct wl1271_cmd_join *join;
  166. int ret, i;
  167. u8 *bssid;
  168. join = kzalloc(sizeof(*join), GFP_KERNEL);
  169. if (!join) {
  170. ret = -ENOMEM;
  171. goto out;
  172. }
  173. wl1271_debug(DEBUG_CMD, "cmd join");
  174. /* Reverse order BSSID */
  175. bssid = (u8 *) &join->bssid_lsb;
  176. for (i = 0; i < ETH_ALEN; i++)
  177. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  178. join->rx_config_options = cpu_to_le32(wl->rx_config);
  179. join->rx_filter_options = cpu_to_le32(wl->rx_filter);
  180. join->bss_type = bss_type;
  181. join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
  182. if (wl->band == IEEE80211_BAND_5GHZ)
  183. join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
  184. join->beacon_interval = cpu_to_le16(wl->beacon_int);
  185. join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
  186. join->channel = wl->channel;
  187. join->ssid_len = wl->ssid_len;
  188. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  189. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  190. /* reset TX security counters */
  191. wl->tx_security_last_seq = 0;
  192. wl->tx_security_seq = 0;
  193. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
  194. if (ret < 0) {
  195. wl1271_error("failed to initiate cmd join");
  196. goto out_free;
  197. }
  198. ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
  199. if (ret < 0)
  200. wl1271_error("cmd join event completion error");
  201. out_free:
  202. kfree(join);
  203. out:
  204. return ret;
  205. }
  206. /**
  207. * send test command to firmware
  208. *
  209. * @wl: wl struct
  210. * @buf: buffer containing the command, with all headers, must work with dma
  211. * @len: length of the buffer
  212. * @answer: is answer needed
  213. */
  214. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  215. {
  216. int ret;
  217. size_t res_len = 0;
  218. wl1271_debug(DEBUG_CMD, "cmd test");
  219. if (answer)
  220. res_len = buf_len;
  221. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  222. if (ret < 0) {
  223. wl1271_warning("TEST command failed");
  224. return ret;
  225. }
  226. return ret;
  227. }
  228. /**
  229. * read acx from firmware
  230. *
  231. * @wl: wl struct
  232. * @id: acx id
  233. * @buf: buffer for the response, including all headers, must work with dma
  234. * @len: lenght of buf
  235. */
  236. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  237. {
  238. struct acx_header *acx = buf;
  239. int ret;
  240. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  241. acx->id = cpu_to_le16(id);
  242. /* payload length, does not include any headers */
  243. acx->len = cpu_to_le16(len - sizeof(*acx));
  244. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  245. if (ret < 0)
  246. wl1271_error("INTERROGATE command failed");
  247. return ret;
  248. }
  249. /**
  250. * write acx value to firmware
  251. *
  252. * @wl: wl struct
  253. * @id: acx id
  254. * @buf: buffer containing acx, including all headers, must work with dma
  255. * @len: length of buf
  256. */
  257. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  258. {
  259. struct acx_header *acx = buf;
  260. int ret;
  261. wl1271_debug(DEBUG_CMD, "cmd configure");
  262. acx->id = cpu_to_le16(id);
  263. /* payload length, does not include any headers */
  264. acx->len = cpu_to_le16(len - sizeof(*acx));
  265. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  266. if (ret < 0) {
  267. wl1271_warning("CONFIGURE command NOK");
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  273. {
  274. struct cmd_enabledisable_path *cmd;
  275. int ret;
  276. u16 cmd_rx, cmd_tx;
  277. wl1271_debug(DEBUG_CMD, "cmd data path");
  278. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  279. if (!cmd) {
  280. ret = -ENOMEM;
  281. goto out;
  282. }
  283. /* the channel here is only used for calibration, so hardcoded to 1 */
  284. cmd->channel = 1;
  285. if (enable) {
  286. cmd_rx = CMD_ENABLE_RX;
  287. cmd_tx = CMD_ENABLE_TX;
  288. } else {
  289. cmd_rx = CMD_DISABLE_RX;
  290. cmd_tx = CMD_DISABLE_TX;
  291. }
  292. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  293. if (ret < 0) {
  294. wl1271_error("rx %s cmd for channel %d failed",
  295. enable ? "start" : "stop", cmd->channel);
  296. goto out;
  297. }
  298. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  299. enable ? "start" : "stop", cmd->channel);
  300. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  301. if (ret < 0) {
  302. wl1271_error("tx %s cmd for channel %d failed",
  303. enable ? "start" : "stop", cmd->channel);
  304. goto out;
  305. }
  306. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  307. enable ? "start" : "stop", cmd->channel);
  308. out:
  309. kfree(cmd);
  310. return ret;
  311. }
  312. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
  313. {
  314. struct wl1271_cmd_ps_params *ps_params = NULL;
  315. int ret = 0;
  316. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  317. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  318. if (!ps_params) {
  319. ret = -ENOMEM;
  320. goto out;
  321. }
  322. ps_params->ps_mode = ps_mode;
  323. ps_params->send_null_data = send;
  324. ps_params->retries = 5;
  325. ps_params->hang_over_period = 1;
  326. ps_params->null_data_rate = cpu_to_le32(wl->basic_rate_set);
  327. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  328. sizeof(*ps_params), 0);
  329. if (ret < 0) {
  330. wl1271_error("cmd set_ps_mode failed");
  331. goto out;
  332. }
  333. out:
  334. kfree(ps_params);
  335. return ret;
  336. }
  337. int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
  338. size_t len)
  339. {
  340. struct cmd_read_write_memory *cmd;
  341. int ret = 0;
  342. wl1271_debug(DEBUG_CMD, "cmd read memory");
  343. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  344. if (!cmd) {
  345. ret = -ENOMEM;
  346. goto out;
  347. }
  348. WARN_ON(len > MAX_READ_SIZE);
  349. len = min_t(size_t, len, MAX_READ_SIZE);
  350. cmd->addr = cpu_to_le32(addr);
  351. cmd->size = cpu_to_le32(len);
  352. ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
  353. sizeof(*cmd));
  354. if (ret < 0) {
  355. wl1271_error("read memory command failed: %d", ret);
  356. goto out;
  357. }
  358. /* the read command got in */
  359. memcpy(answer, cmd->value, len);
  360. out:
  361. kfree(cmd);
  362. return ret;
  363. }
  364. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  365. void *buf, size_t buf_len, int index, u32 rates)
  366. {
  367. struct wl1271_cmd_template_set *cmd;
  368. int ret = 0;
  369. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  370. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  371. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  372. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  373. if (!cmd) {
  374. ret = -ENOMEM;
  375. goto out;
  376. }
  377. cmd->len = cpu_to_le16(buf_len);
  378. cmd->template_type = template_id;
  379. cmd->enabled_rates = cpu_to_le32(rates);
  380. cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
  381. cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
  382. cmd->index = index;
  383. if (buf)
  384. memcpy(cmd->template_data, buf, buf_len);
  385. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  386. if (ret < 0) {
  387. wl1271_warning("cmd set_template failed: %d", ret);
  388. goto out_free;
  389. }
  390. out_free:
  391. kfree(cmd);
  392. out:
  393. return ret;
  394. }
  395. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  396. {
  397. struct sk_buff *skb = NULL;
  398. int size;
  399. void *ptr;
  400. int ret = -ENOMEM;
  401. if (wl->bss_type == BSS_TYPE_IBSS) {
  402. size = sizeof(struct wl12xx_null_data_template);
  403. ptr = NULL;
  404. } else {
  405. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  406. if (!skb)
  407. goto out;
  408. size = skb->len;
  409. ptr = skb->data;
  410. }
  411. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
  412. WL1271_RATE_AUTOMATIC);
  413. out:
  414. dev_kfree_skb(skb);
  415. if (ret)
  416. wl1271_warning("cmd buld null data failed %d", ret);
  417. return ret;
  418. }
  419. int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
  420. {
  421. struct sk_buff *skb = NULL;
  422. int ret = -ENOMEM;
  423. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  424. if (!skb)
  425. goto out;
  426. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
  427. skb->data, skb->len,
  428. CMD_TEMPL_KLV_IDX_NULL_DATA,
  429. WL1271_RATE_AUTOMATIC);
  430. out:
  431. dev_kfree_skb(skb);
  432. if (ret)
  433. wl1271_warning("cmd build klv null data failed %d", ret);
  434. return ret;
  435. }
  436. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  437. {
  438. struct sk_buff *skb;
  439. int ret = 0;
  440. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  441. if (!skb)
  442. goto out;
  443. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
  444. skb->len, 0, wl->basic_rate_set);
  445. out:
  446. dev_kfree_skb(skb);
  447. return ret;
  448. }
  449. int wl1271_cmd_build_probe_req(struct wl1271 *wl,
  450. const u8 *ssid, size_t ssid_len,
  451. const u8 *ie, size_t ie_len, u8 band)
  452. {
  453. struct sk_buff *skb;
  454. int ret;
  455. skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
  456. ie, ie_len);
  457. if (!skb) {
  458. ret = -ENOMEM;
  459. goto out;
  460. }
  461. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  462. if (band == IEEE80211_BAND_2GHZ)
  463. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  464. skb->data, skb->len, 0,
  465. wl->conf.tx.basic_rate);
  466. else
  467. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  468. skb->data, skb->len, 0,
  469. wl->conf.tx.basic_rate_5);
  470. out:
  471. dev_kfree_skb(skb);
  472. return ret;
  473. }
  474. int wl1271_build_qos_null_data(struct wl1271 *wl)
  475. {
  476. struct ieee80211_qos_hdr template;
  477. memset(&template, 0, sizeof(template));
  478. memcpy(template.addr1, wl->bssid, ETH_ALEN);
  479. memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
  480. memcpy(template.addr3, wl->bssid, ETH_ALEN);
  481. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  482. IEEE80211_STYPE_QOS_NULLFUNC |
  483. IEEE80211_FCTL_TODS);
  484. /* FIXME: not sure what priority to use here */
  485. template.qos_ctrl = cpu_to_le16(0);
  486. return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
  487. sizeof(template), 0,
  488. WL1271_RATE_AUTOMATIC);
  489. }
  490. int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
  491. {
  492. struct wl1271_cmd_set_keys *cmd;
  493. int ret = 0;
  494. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  495. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  496. if (!cmd) {
  497. ret = -ENOMEM;
  498. goto out;
  499. }
  500. cmd->id = id;
  501. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  502. cmd->key_type = KEY_WEP;
  503. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  504. if (ret < 0) {
  505. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  506. goto out;
  507. }
  508. out:
  509. kfree(cmd);
  510. return ret;
  511. }
  512. int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  513. u8 key_size, const u8 *key, const u8 *addr,
  514. u32 tx_seq_32, u16 tx_seq_16)
  515. {
  516. struct wl1271_cmd_set_keys *cmd;
  517. int ret = 0;
  518. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  519. if (!cmd) {
  520. ret = -ENOMEM;
  521. goto out;
  522. }
  523. if (key_type != KEY_WEP)
  524. memcpy(cmd->addr, addr, ETH_ALEN);
  525. cmd->key_action = cpu_to_le16(action);
  526. cmd->key_size = key_size;
  527. cmd->key_type = key_type;
  528. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  529. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  530. /* we have only one SSID profile */
  531. cmd->ssid_profile = 0;
  532. cmd->id = id;
  533. if (key_type == KEY_TKIP) {
  534. /*
  535. * We get the key in the following form:
  536. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  537. * but the target is expecting:
  538. * TKIP - RX MIC - TX MIC
  539. */
  540. memcpy(cmd->key, key, 16);
  541. memcpy(cmd->key + 16, key + 24, 8);
  542. memcpy(cmd->key + 24, key + 16, 8);
  543. } else {
  544. memcpy(cmd->key, key, key_size);
  545. }
  546. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  547. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  548. if (ret < 0) {
  549. wl1271_warning("could not set keys");
  550. goto out;
  551. }
  552. out:
  553. kfree(cmd);
  554. return ret;
  555. }
  556. int wl1271_cmd_disconnect(struct wl1271 *wl)
  557. {
  558. struct wl1271_cmd_disconnect *cmd;
  559. int ret = 0;
  560. wl1271_debug(DEBUG_CMD, "cmd disconnect");
  561. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  562. if (!cmd) {
  563. ret = -ENOMEM;
  564. goto out;
  565. }
  566. cmd->rx_config_options = cpu_to_le32(wl->rx_config);
  567. cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
  568. /* disconnect reason is not used in immediate disconnections */
  569. cmd->type = DISCONNECT_IMMEDIATE;
  570. ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
  571. if (ret < 0) {
  572. wl1271_error("failed to send disconnect command");
  573. goto out_free;
  574. }
  575. ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
  576. if (ret < 0)
  577. wl1271_error("cmd disconnect event completion error");
  578. out_free:
  579. kfree(cmd);
  580. out:
  581. return ret;
  582. }
  583. int wl1271_cmd_set_sta_state(struct wl1271 *wl)
  584. {
  585. struct wl1271_cmd_set_sta_state *cmd;
  586. int ret = 0;
  587. wl1271_debug(DEBUG_CMD, "cmd set sta state");
  588. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  589. if (!cmd) {
  590. ret = -ENOMEM;
  591. goto out;
  592. }
  593. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  594. ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
  595. if (ret < 0) {
  596. wl1271_error("failed to send set STA state command");
  597. goto out_free;
  598. }
  599. out_free:
  600. kfree(cmd);
  601. out:
  602. return ret;
  603. }