wl1271_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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 = id;
  50. cmd->status = 0;
  51. WARN_ON(len % 4 != 0);
  52. wl1271_spi_mem_write(wl, wl->cmd_box_addr, buf, len);
  53. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  54. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  55. intr = wl1271_reg_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_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  64. }
  65. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
  66. WL1271_ACX_INTR_CMD_COMPLETE);
  67. out:
  68. return ret;
  69. }
  70. 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. 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. 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. 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, u8 bss_type, u8 dtim_interval,
  140. u16 beacon_interval, u8 wait)
  141. {
  142. static bool do_cal = true;
  143. unsigned long timeout;
  144. struct wl1271_cmd_join *join;
  145. int ret, i;
  146. u8 *bssid;
  147. /* FIXME: remove when we get calibration from the factory */
  148. if (do_cal) {
  149. ret = wl1271_cmd_cal(wl);
  150. if (ret < 0)
  151. wl1271_warning("couldn't calibrate");
  152. else
  153. do_cal = false;
  154. }
  155. join = kzalloc(sizeof(*join), GFP_KERNEL);
  156. if (!join) {
  157. ret = -ENOMEM;
  158. goto out;
  159. }
  160. wl1271_debug(DEBUG_CMD, "cmd join");
  161. /* Reverse order BSSID */
  162. bssid = (u8 *) &join->bssid_lsb;
  163. for (i = 0; i < ETH_ALEN; i++)
  164. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  165. join->rx_config_options = wl->rx_config;
  166. join->rx_filter_options = wl->rx_filter;
  167. join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
  168. RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
  169. join->beacon_interval = beacon_interval;
  170. join->dtim_interval = dtim_interval;
  171. join->bss_type = bss_type;
  172. join->channel = wl->channel;
  173. join->ssid_len = wl->ssid_len;
  174. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  175. join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
  176. /* increment the session counter */
  177. wl->session_counter++;
  178. if (wl->session_counter >= SESSION_COUNTER_MAX)
  179. wl->session_counter = 0;
  180. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  181. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
  182. if (ret < 0) {
  183. wl1271_error("failed to initiate cmd join");
  184. goto out_free;
  185. }
  186. timeout = msecs_to_jiffies(JOIN_TIMEOUT);
  187. /*
  188. * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
  189. * simplify locking we just sleep instead, for now
  190. */
  191. if (wait)
  192. msleep(10);
  193. out_free:
  194. kfree(join);
  195. out:
  196. return ret;
  197. }
  198. /**
  199. * send test command to firmware
  200. *
  201. * @wl: wl struct
  202. * @buf: buffer containing the command, with all headers, must work with dma
  203. * @len: length of the buffer
  204. * @answer: is answer needed
  205. */
  206. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  207. {
  208. int ret;
  209. wl1271_debug(DEBUG_CMD, "cmd test");
  210. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len);
  211. if (ret < 0) {
  212. wl1271_warning("TEST command failed");
  213. return ret;
  214. }
  215. if (answer) {
  216. struct wl1271_command *cmd_answer;
  217. /*
  218. * The test command got in, we can read the answer.
  219. * The answer would be a wl1271_command, where the
  220. * parameter array contains the actual answer.
  221. */
  222. wl1271_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
  223. cmd_answer = buf;
  224. if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
  225. wl1271_error("TEST command answer error: %d",
  226. cmd_answer->header.status);
  227. }
  228. return 0;
  229. }
  230. /**
  231. * read acx from firmware
  232. *
  233. * @wl: wl struct
  234. * @id: acx id
  235. * @buf: buffer for the response, including all headers, must work with dma
  236. * @len: lenght of buf
  237. */
  238. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  239. {
  240. struct acx_header *acx = buf;
  241. int ret;
  242. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  243. acx->id = id;
  244. /* payload length, does not include any headers */
  245. acx->len = len - sizeof(*acx);
  246. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
  247. if (ret < 0) {
  248. wl1271_error("INTERROGATE command failed");
  249. goto out;
  250. }
  251. /* the interrogate command got in, we can read the answer */
  252. wl1271_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
  253. acx = buf;
  254. if (acx->cmd.status != CMD_STATUS_SUCCESS)
  255. wl1271_error("INTERROGATE command error: %d",
  256. acx->cmd.status);
  257. out:
  258. return ret;
  259. }
  260. /**
  261. * write acx value to firmware
  262. *
  263. * @wl: wl struct
  264. * @id: acx id
  265. * @buf: buffer containing acx, including all headers, must work with dma
  266. * @len: length of buf
  267. */
  268. int wl1271_cmd_configure(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 configure");
  273. acx->id = id;
  274. /* payload length, does not include any headers */
  275. acx->len = len - sizeof(*acx);
  276. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len);
  277. if (ret < 0) {
  278. wl1271_warning("CONFIGURE command NOK");
  279. return ret;
  280. }
  281. return 0;
  282. }
  283. int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
  284. {
  285. struct cmd_enabledisable_path *cmd;
  286. int ret;
  287. u16 cmd_rx, cmd_tx;
  288. wl1271_debug(DEBUG_CMD, "cmd data path");
  289. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  290. if (!cmd) {
  291. ret = -ENOMEM;
  292. goto out;
  293. }
  294. cmd->channel = channel;
  295. if (enable) {
  296. cmd_rx = CMD_ENABLE_RX;
  297. cmd_tx = CMD_ENABLE_TX;
  298. } else {
  299. cmd_rx = CMD_DISABLE_RX;
  300. cmd_tx = CMD_DISABLE_TX;
  301. }
  302. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
  303. if (ret < 0) {
  304. wl1271_error("rx %s cmd for channel %d failed",
  305. enable ? "start" : "stop", channel);
  306. goto out;
  307. }
  308. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  309. enable ? "start" : "stop", channel);
  310. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
  311. if (ret < 0) {
  312. wl1271_error("tx %s cmd for channel %d failed",
  313. enable ? "start" : "stop", channel);
  314. return ret;
  315. }
  316. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  317. enable ? "start" : "stop", channel);
  318. out:
  319. kfree(cmd);
  320. return ret;
  321. }
  322. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
  323. {
  324. struct wl1271_cmd_ps_params *ps_params = NULL;
  325. int ret = 0;
  326. /* FIXME: this should be in ps.c */
  327. ret = wl1271_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
  328. wl->listen_int);
  329. if (ret < 0) {
  330. wl1271_error("couldn't set wake up conditions");
  331. goto out;
  332. }
  333. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  334. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  335. if (!ps_params) {
  336. ret = -ENOMEM;
  337. goto out;
  338. }
  339. ps_params->ps_mode = ps_mode;
  340. ps_params->send_null_data = 1;
  341. ps_params->retries = 5;
  342. ps_params->hang_over_period = 128;
  343. ps_params->null_data_rate = 1; /* 1 Mbps */
  344. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  345. sizeof(*ps_params));
  346. if (ret < 0) {
  347. wl1271_error("cmd set_ps_mode failed");
  348. goto out;
  349. }
  350. out:
  351. kfree(ps_params);
  352. return ret;
  353. }
  354. int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
  355. size_t len)
  356. {
  357. struct cmd_read_write_memory *cmd;
  358. int ret = 0;
  359. wl1271_debug(DEBUG_CMD, "cmd read memory");
  360. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  361. if (!cmd) {
  362. ret = -ENOMEM;
  363. goto out;
  364. }
  365. WARN_ON(len > MAX_READ_SIZE);
  366. len = min_t(size_t, len, MAX_READ_SIZE);
  367. cmd->addr = addr;
  368. cmd->size = len;
  369. ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
  370. if (ret < 0) {
  371. wl1271_error("read memory command failed: %d", ret);
  372. goto out;
  373. }
  374. /* the read command got in, we can now read the answer */
  375. wl1271_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
  376. if (cmd->header.status != CMD_STATUS_SUCCESS)
  377. wl1271_error("error in read command result: %d",
  378. cmd->header.status);
  379. memcpy(answer, cmd->value, len);
  380. out:
  381. kfree(cmd);
  382. return ret;
  383. }
  384. int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
  385. u8 active_scan, u8 high_prio, u8 num_channels,
  386. u8 probe_requests)
  387. {
  388. struct wl1271_cmd_trigger_scan_to *trigger = NULL;
  389. struct wl1271_cmd_scan *params = NULL;
  390. int i, ret;
  391. u16 scan_options = 0;
  392. if (wl->scanning)
  393. return -EINVAL;
  394. params = kzalloc(sizeof(*params), GFP_KERNEL);
  395. if (!params)
  396. return -ENOMEM;
  397. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  398. params->params.rx_filter_options =
  399. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  400. if (!active_scan)
  401. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  402. if (high_prio)
  403. scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
  404. params->params.scan_options = scan_options;
  405. params->params.num_channels = num_channels;
  406. params->params.num_probe_requests = probe_requests;
  407. params->params.tx_rate = cpu_to_le32(RATE_MASK_2MBPS);
  408. params->params.tid_trigger = 0;
  409. params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  410. for (i = 0; i < num_channels; i++) {
  411. params->channels[i].min_duration =
  412. cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
  413. params->channels[i].max_duration =
  414. cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
  415. memset(&params->channels[i].bssid_lsb, 0xff, 4);
  416. memset(&params->channels[i].bssid_msb, 0xff, 2);
  417. params->channels[i].early_termination = 0;
  418. params->channels[i].tx_power_att = WL1271_SCAN_CURRENT_TX_PWR;
  419. params->channels[i].channel = i + 1;
  420. }
  421. if (len && ssid) {
  422. params->params.ssid_len = len;
  423. memcpy(params->params.ssid, ssid, len);
  424. }
  425. ret = wl1271_cmd_build_probe_req(wl, ssid, len);
  426. if (ret < 0) {
  427. wl1271_error("PROBE request template failed");
  428. goto out;
  429. }
  430. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  431. if (!trigger) {
  432. ret = -ENOMEM;
  433. goto out;
  434. }
  435. /* disable the timeout */
  436. trigger->timeout = 0;
  437. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  438. sizeof(*trigger));
  439. if (ret < 0) {
  440. wl1271_error("trigger scan to failed for hw scan");
  441. goto out;
  442. }
  443. wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  444. wl->scanning = true;
  445. ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
  446. if (ret < 0) {
  447. wl1271_error("SCAN failed");
  448. goto out;
  449. }
  450. wl1271_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
  451. if (params->header.status != CMD_STATUS_SUCCESS) {
  452. wl1271_error("Scan command error: %d",
  453. params->header.status);
  454. wl->scanning = false;
  455. ret = -EIO;
  456. goto out;
  457. }
  458. out:
  459. kfree(params);
  460. return ret;
  461. }
  462. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  463. void *buf, size_t buf_len)
  464. {
  465. struct wl1271_cmd_template_set *cmd;
  466. int ret = 0;
  467. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  468. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  469. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  470. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  471. if (!cmd) {
  472. ret = -ENOMEM;
  473. goto out;
  474. }
  475. cmd->len = cpu_to_le16(buf_len);
  476. cmd->template_type = template_id;
  477. cmd->enabled_rates = ACX_RATE_MASK_UNSPECIFIED;
  478. cmd->short_retry_limit = ACX_RATE_RETRY_LIMIT;
  479. cmd->long_retry_limit = ACX_RATE_RETRY_LIMIT;
  480. if (buf)
  481. memcpy(cmd->template_data, buf, buf_len);
  482. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd));
  483. if (ret < 0) {
  484. wl1271_warning("cmd set_template failed: %d", ret);
  485. goto out_free;
  486. }
  487. out_free:
  488. kfree(cmd);
  489. out:
  490. return ret;
  491. }
  492. static int wl1271_build_basic_rates(char *rates)
  493. {
  494. u8 index = 0;
  495. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  496. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  497. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  498. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  499. return index;
  500. }
  501. static int wl1271_build_extended_rates(char *rates)
  502. {
  503. u8 index = 0;
  504. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  505. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  506. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  507. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  508. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  509. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  510. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  511. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  512. return index;
  513. }
  514. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  515. {
  516. struct wl12xx_null_data_template template;
  517. if (!is_zero_ether_addr(wl->bssid)) {
  518. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  519. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  520. } else {
  521. memset(template.header.da, 0xff, ETH_ALEN);
  522. memset(template.header.bssid, 0xff, ETH_ALEN);
  523. }
  524. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  525. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  526. IEEE80211_STYPE_NULLFUNC);
  527. return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
  528. sizeof(template));
  529. }
  530. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  531. {
  532. struct wl12xx_ps_poll_template template;
  533. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  534. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  535. template.aid = aid;
  536. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  537. return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
  538. sizeof(template));
  539. }
  540. int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len)
  541. {
  542. struct wl12xx_probe_req_template template;
  543. struct wl12xx_ie_rates *rates;
  544. char *ptr;
  545. u16 size;
  546. ptr = (char *)&template;
  547. size = sizeof(struct ieee80211_header);
  548. memset(template.header.da, 0xff, ETH_ALEN);
  549. memset(template.header.bssid, 0xff, ETH_ALEN);
  550. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  551. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  552. /* IEs */
  553. /* SSID */
  554. template.ssid.header.id = WLAN_EID_SSID;
  555. template.ssid.header.len = ssid_len;
  556. if (ssid_len && ssid)
  557. memcpy(template.ssid.ssid, ssid, ssid_len);
  558. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  559. ptr += size;
  560. /* Basic Rates */
  561. rates = (struct wl12xx_ie_rates *)ptr;
  562. rates->header.id = WLAN_EID_SUPP_RATES;
  563. rates->header.len = wl1271_build_basic_rates(rates->rates);
  564. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  565. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  566. /* Extended rates */
  567. rates = (struct wl12xx_ie_rates *)ptr;
  568. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  569. rates->header.len = wl1271_build_extended_rates(rates->rates);
  570. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  571. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  572. return wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  573. &template, size);
  574. }
  575. int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
  576. {
  577. struct wl1271_cmd_set_keys *cmd;
  578. int ret = 0;
  579. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  580. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  581. if (!cmd) {
  582. ret = -ENOMEM;
  583. goto out;
  584. }
  585. cmd->id = id;
  586. cmd->key_action = KEY_SET_ID;
  587. cmd->key_type = KEY_WEP;
  588. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
  589. if (ret < 0) {
  590. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  591. goto out;
  592. }
  593. out:
  594. kfree(cmd);
  595. return ret;
  596. }
  597. int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  598. u8 key_size, const u8 *key, const u8 *addr)
  599. {
  600. struct wl1271_cmd_set_keys *cmd;
  601. int ret = 0;
  602. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  603. if (!cmd) {
  604. ret = -ENOMEM;
  605. goto out;
  606. }
  607. if (key_type != KEY_WEP)
  608. memcpy(cmd->addr, addr, ETH_ALEN);
  609. cmd->key_action = action;
  610. cmd->key_size = key_size;
  611. cmd->key_type = key_type;
  612. /* we have only one SSID profile */
  613. cmd->ssid_profile = 0;
  614. cmd->id = id;
  615. /* FIXME: this is from wl1251, needs to be checked */
  616. if (key_type == KEY_TKIP) {
  617. /*
  618. * We get the key in the following form:
  619. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  620. * but the target is expecting:
  621. * TKIP - RX MIC - TX MIC
  622. */
  623. memcpy(cmd->key, key, 16);
  624. memcpy(cmd->key + 16, key + 24, 8);
  625. memcpy(cmd->key + 24, key + 16, 8);
  626. } else {
  627. memcpy(cmd->key, key, key_size);
  628. }
  629. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  630. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
  631. if (ret < 0) {
  632. wl1271_warning("could not set keys");
  633. goto out;
  634. }
  635. out:
  636. kfree(cmd);
  637. return ret;
  638. }