wl1271_cmd.c 22 KB

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