wl1271_cmd.c 22 KB

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