wl1271_cmd.c 22 KB

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