wl1271_cmd.c 23 KB

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