wl1271_cmd.c 22 KB

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