wl1271_cmd.c 22 KB

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