wl1271_cmd.c 21 KB

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