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. join->basic_rate_set = wl->basic_rate_set;
  252. if (wl->band == IEEE80211_BAND_5GHZ)
  253. join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
  254. join->beacon_interval = cpu_to_le16(wl->beacon_int);
  255. join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
  256. join->channel = wl->channel;
  257. join->ssid_len = wl->ssid_len;
  258. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  259. join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
  260. /* increment the session counter */
  261. wl->session_counter++;
  262. if (wl->session_counter >= SESSION_COUNTER_MAX)
  263. wl->session_counter = 0;
  264. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  265. /* reset TX security counters */
  266. wl->tx_security_last_seq = 0;
  267. wl->tx_security_seq = 0;
  268. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
  269. if (ret < 0) {
  270. wl1271_error("failed to initiate cmd join");
  271. goto out_free;
  272. }
  273. ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
  274. if (ret < 0)
  275. wl1271_error("cmd join event completion error");
  276. out_free:
  277. kfree(join);
  278. out:
  279. return ret;
  280. }
  281. /**
  282. * send test command to firmware
  283. *
  284. * @wl: wl struct
  285. * @buf: buffer containing the command, with all headers, must work with dma
  286. * @len: length of the buffer
  287. * @answer: is answer needed
  288. */
  289. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  290. {
  291. int ret;
  292. size_t res_len = 0;
  293. wl1271_debug(DEBUG_CMD, "cmd test");
  294. if (answer)
  295. res_len = buf_len;
  296. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  297. if (ret < 0) {
  298. wl1271_warning("TEST command failed");
  299. return ret;
  300. }
  301. return ret;
  302. }
  303. /**
  304. * read acx from firmware
  305. *
  306. * @wl: wl struct
  307. * @id: acx id
  308. * @buf: buffer for the response, including all headers, must work with dma
  309. * @len: lenght of buf
  310. */
  311. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  312. {
  313. struct acx_header *acx = buf;
  314. int ret;
  315. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  316. acx->id = cpu_to_le16(id);
  317. /* payload length, does not include any headers */
  318. acx->len = cpu_to_le16(len - sizeof(*acx));
  319. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  320. if (ret < 0)
  321. wl1271_error("INTERROGATE command failed");
  322. return ret;
  323. }
  324. /**
  325. * write acx value to firmware
  326. *
  327. * @wl: wl struct
  328. * @id: acx id
  329. * @buf: buffer containing acx, including all headers, must work with dma
  330. * @len: length of buf
  331. */
  332. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  333. {
  334. struct acx_header *acx = buf;
  335. int ret;
  336. wl1271_debug(DEBUG_CMD, "cmd configure");
  337. acx->id = cpu_to_le16(id);
  338. /* payload length, does not include any headers */
  339. acx->len = cpu_to_le16(len - sizeof(*acx));
  340. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  341. if (ret < 0) {
  342. wl1271_warning("CONFIGURE command NOK");
  343. return ret;
  344. }
  345. return 0;
  346. }
  347. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  348. {
  349. struct cmd_enabledisable_path *cmd;
  350. int ret;
  351. u16 cmd_rx, cmd_tx;
  352. wl1271_debug(DEBUG_CMD, "cmd data path");
  353. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  354. if (!cmd) {
  355. ret = -ENOMEM;
  356. goto out;
  357. }
  358. /* the channel here is only used for calibration, so hardcoded to 1 */
  359. cmd->channel = 1;
  360. if (enable) {
  361. cmd_rx = CMD_ENABLE_RX;
  362. cmd_tx = CMD_ENABLE_TX;
  363. } else {
  364. cmd_rx = CMD_DISABLE_RX;
  365. cmd_tx = CMD_DISABLE_TX;
  366. }
  367. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  368. if (ret < 0) {
  369. wl1271_error("rx %s cmd for channel %d failed",
  370. enable ? "start" : "stop", cmd->channel);
  371. goto out;
  372. }
  373. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  374. enable ? "start" : "stop", cmd->channel);
  375. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  376. if (ret < 0) {
  377. wl1271_error("tx %s cmd for channel %d failed",
  378. enable ? "start" : "stop", cmd->channel);
  379. goto out;
  380. }
  381. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  382. enable ? "start" : "stop", cmd->channel);
  383. out:
  384. kfree(cmd);
  385. return ret;
  386. }
  387. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
  388. {
  389. struct wl1271_cmd_ps_params *ps_params = NULL;
  390. int ret = 0;
  391. /* FIXME: this should be in ps.c */
  392. ret = wl1271_acx_wake_up_conditions(wl);
  393. if (ret < 0) {
  394. wl1271_error("couldn't set wake up conditions");
  395. goto out;
  396. }
  397. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  398. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  399. if (!ps_params) {
  400. ret = -ENOMEM;
  401. goto out;
  402. }
  403. ps_params->ps_mode = ps_mode;
  404. ps_params->send_null_data = send;
  405. ps_params->retries = 5;
  406. ps_params->hang_over_period = 128;
  407. ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
  408. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  409. sizeof(*ps_params), 0);
  410. if (ret < 0) {
  411. wl1271_error("cmd set_ps_mode failed");
  412. goto out;
  413. }
  414. out:
  415. kfree(ps_params);
  416. return ret;
  417. }
  418. int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
  419. size_t len)
  420. {
  421. struct cmd_read_write_memory *cmd;
  422. int ret = 0;
  423. wl1271_debug(DEBUG_CMD, "cmd read memory");
  424. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  425. if (!cmd) {
  426. ret = -ENOMEM;
  427. goto out;
  428. }
  429. WARN_ON(len > MAX_READ_SIZE);
  430. len = min_t(size_t, len, MAX_READ_SIZE);
  431. cmd->addr = cpu_to_le32(addr);
  432. cmd->size = cpu_to_le32(len);
  433. ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
  434. sizeof(*cmd));
  435. if (ret < 0) {
  436. wl1271_error("read memory command failed: %d", ret);
  437. goto out;
  438. }
  439. /* the read command got in */
  440. memcpy(answer, cmd->value, len);
  441. out:
  442. kfree(cmd);
  443. return ret;
  444. }
  445. int wl1271_cmd_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
  446. const u8 *ie, size_t ie_len, u8 active_scan,
  447. u8 high_prio, u8 band, u8 probe_requests)
  448. {
  449. struct wl1271_cmd_trigger_scan_to *trigger = NULL;
  450. struct wl1271_cmd_scan *params = NULL;
  451. struct ieee80211_channel *channels;
  452. u32 rate;
  453. int i, j, n_ch, ret;
  454. u16 scan_options = 0;
  455. u8 ieee_band;
  456. if (band == WL1271_SCAN_BAND_2_4_GHZ) {
  457. ieee_band = IEEE80211_BAND_2GHZ;
  458. rate = wl->conf.tx.basic_rate;
  459. } else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled()) {
  460. ieee_band = IEEE80211_BAND_2GHZ;
  461. rate = wl->conf.tx.basic_rate;
  462. } else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled()) {
  463. ieee_band = IEEE80211_BAND_5GHZ;
  464. rate = wl->conf.tx.basic_rate_5;
  465. } else
  466. return -EINVAL;
  467. if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
  468. return -EINVAL;
  469. channels = wl->hw->wiphy->bands[ieee_band]->channels;
  470. n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
  471. if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
  472. return -EINVAL;
  473. params = kzalloc(sizeof(*params), GFP_KERNEL);
  474. if (!params)
  475. return -ENOMEM;
  476. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  477. params->params.rx_filter_options =
  478. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  479. if (!active_scan)
  480. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  481. if (high_prio)
  482. scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
  483. params->params.scan_options = cpu_to_le16(scan_options);
  484. params->params.num_probe_requests = probe_requests;
  485. params->params.tx_rate = rate;
  486. params->params.tid_trigger = 0;
  487. params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  488. if (band == WL1271_SCAN_BAND_DUAL)
  489. params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  490. else
  491. params->params.band = band;
  492. for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
  493. if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
  494. params->channels[j].min_duration =
  495. cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
  496. params->channels[j].max_duration =
  497. cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
  498. memset(&params->channels[j].bssid_lsb, 0xff, 4);
  499. memset(&params->channels[j].bssid_msb, 0xff, 2);
  500. params->channels[j].early_termination = 0;
  501. params->channels[j].tx_power_att =
  502. WL1271_SCAN_CURRENT_TX_PWR;
  503. params->channels[j].channel = channels[i].hw_value;
  504. j++;
  505. }
  506. }
  507. params->params.num_channels = j;
  508. if (ssid_len && ssid) {
  509. params->params.ssid_len = ssid_len;
  510. memcpy(params->params.ssid, ssid, ssid_len);
  511. }
  512. ret = wl1271_cmd_build_probe_req(wl, ssid, ssid_len,
  513. ie, ie_len, ieee_band);
  514. if (ret < 0) {
  515. wl1271_error("PROBE request template failed");
  516. goto out;
  517. }
  518. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  519. if (!trigger) {
  520. ret = -ENOMEM;
  521. goto out;
  522. }
  523. /* disable the timeout */
  524. trigger->timeout = 0;
  525. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  526. sizeof(*trigger), 0);
  527. if (ret < 0) {
  528. wl1271_error("trigger scan to failed for hw scan");
  529. goto out;
  530. }
  531. wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  532. set_bit(WL1271_FLAG_SCANNING, &wl->flags);
  533. if (wl1271_11a_enabled()) {
  534. wl->scan.state = band;
  535. if (band == WL1271_SCAN_BAND_DUAL) {
  536. wl->scan.active = active_scan;
  537. wl->scan.high_prio = high_prio;
  538. wl->scan.probe_requests = probe_requests;
  539. if (ssid_len && ssid) {
  540. wl->scan.ssid_len = ssid_len;
  541. memcpy(wl->scan.ssid, ssid, ssid_len);
  542. } else
  543. wl->scan.ssid_len = 0;
  544. }
  545. }
  546. ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
  547. if (ret < 0) {
  548. wl1271_error("SCAN failed");
  549. clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
  550. goto out;
  551. }
  552. out:
  553. kfree(params);
  554. kfree(trigger);
  555. return ret;
  556. }
  557. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  558. void *buf, size_t buf_len, int index, u32 rates)
  559. {
  560. struct wl1271_cmd_template_set *cmd;
  561. int ret = 0;
  562. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  563. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  564. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  565. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  566. if (!cmd) {
  567. ret = -ENOMEM;
  568. goto out;
  569. }
  570. cmd->len = cpu_to_le16(buf_len);
  571. cmd->template_type = template_id;
  572. cmd->enabled_rates = cpu_to_le32(rates);
  573. cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
  574. cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
  575. cmd->index = index;
  576. if (buf)
  577. memcpy(cmd->template_data, buf, buf_len);
  578. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  579. if (ret < 0) {
  580. wl1271_warning("cmd set_template failed: %d", ret);
  581. goto out_free;
  582. }
  583. out_free:
  584. kfree(cmd);
  585. out:
  586. return ret;
  587. }
  588. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  589. {
  590. struct sk_buff *skb = NULL;
  591. int size;
  592. void *ptr;
  593. int ret = -ENOMEM;
  594. if (wl->bss_type == BSS_TYPE_IBSS) {
  595. size = sizeof(struct wl12xx_null_data_template);
  596. ptr = NULL;
  597. } else {
  598. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  599. if (!skb)
  600. goto out;
  601. size = skb->len;
  602. ptr = skb->data;
  603. }
  604. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
  605. WL1271_RATE_AUTOMATIC);
  606. out:
  607. dev_kfree_skb(skb);
  608. if (ret)
  609. wl1271_warning("cmd buld null data failed %d", ret);
  610. return ret;
  611. }
  612. int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
  613. {
  614. struct sk_buff *skb = NULL;
  615. int ret = -ENOMEM;
  616. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  617. if (!skb)
  618. goto out;
  619. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
  620. skb->data, skb->len,
  621. CMD_TEMPL_KLV_IDX_NULL_DATA,
  622. WL1271_RATE_AUTOMATIC);
  623. out:
  624. dev_kfree_skb(skb);
  625. if (ret)
  626. wl1271_warning("cmd build klv null data failed %d", ret);
  627. return ret;
  628. }
  629. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  630. {
  631. struct sk_buff *skb;
  632. int ret = 0;
  633. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  634. if (!skb)
  635. goto out;
  636. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
  637. skb->len, 0, wl->basic_rate);
  638. out:
  639. dev_kfree_skb(skb);
  640. return ret;
  641. }
  642. int wl1271_cmd_build_probe_req(struct wl1271 *wl,
  643. const u8 *ssid, size_t ssid_len,
  644. const u8 *ie, size_t ie_len, u8 band)
  645. {
  646. struct sk_buff *skb;
  647. int ret;
  648. skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
  649. ie, ie_len);
  650. if (!skb) {
  651. ret = -ENOMEM;
  652. goto out;
  653. }
  654. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  655. if (band == IEEE80211_BAND_2GHZ)
  656. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  657. skb->data, skb->len, 0,
  658. wl->conf.tx.basic_rate);
  659. else
  660. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  661. skb->data, skb->len, 0,
  662. wl->conf.tx.basic_rate_5);
  663. out:
  664. dev_kfree_skb(skb);
  665. return ret;
  666. }
  667. int wl1271_build_qos_null_data(struct wl1271 *wl)
  668. {
  669. struct ieee80211_qos_hdr template;
  670. memset(&template, 0, sizeof(template));
  671. memcpy(template.addr1, wl->bssid, ETH_ALEN);
  672. memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
  673. memcpy(template.addr3, wl->bssid, ETH_ALEN);
  674. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  675. IEEE80211_STYPE_QOS_NULLFUNC |
  676. IEEE80211_FCTL_TODS);
  677. /* FIXME: not sure what priority to use here */
  678. template.qos_ctrl = cpu_to_le16(0);
  679. return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
  680. sizeof(template), 0,
  681. WL1271_RATE_AUTOMATIC);
  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. }