wl1271_cmd.c 23 KB

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