wl1271_cmd.c 23 KB

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