wl1271_cmd.c 22 KB

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