wl1271_cmd.c 23 KB

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