wl1271_cmd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009 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 "wl1271.h"
  29. #include "wl1271_reg.h"
  30. #include "wl1271_spi.h"
  31. #include "wl1271_acx.h"
  32. #include "wl12xx_80211.h"
  33. #include "wl1271_cmd.h"
  34. /*
  35. * send command to firmware
  36. *
  37. * @wl: wl struct
  38. * @id: command id
  39. * @buf: buffer containing the command, must work with dma
  40. * @len: length of the buffer
  41. */
  42. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  43. size_t res_len)
  44. {
  45. struct wl1271_cmd_header *cmd;
  46. unsigned long timeout;
  47. u32 intr;
  48. int ret = 0;
  49. u16 status;
  50. cmd = buf;
  51. cmd->id = cpu_to_le16(id);
  52. cmd->status = 0;
  53. WARN_ON(len % 4 != 0);
  54. wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
  55. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  56. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  57. intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  58. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  59. if (time_after(jiffies, timeout)) {
  60. wl1271_error("command complete timeout");
  61. ret = -ETIMEDOUT;
  62. goto out;
  63. }
  64. msleep(1);
  65. intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  66. }
  67. /* read back the status code of the command */
  68. if (res_len == 0)
  69. res_len = sizeof(struct wl1271_cmd_header);
  70. wl1271_spi_read(wl, wl->cmd_box_addr, cmd, res_len, false);
  71. status = le16_to_cpu(cmd->status);
  72. if (status != CMD_STATUS_SUCCESS) {
  73. wl1271_error("command execute failure %d", status);
  74. ret = -EIO;
  75. }
  76. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
  77. WL1271_ACX_INTR_CMD_COMPLETE);
  78. out:
  79. return ret;
  80. }
  81. static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
  82. {
  83. struct wl1271_cmd_cal_channel_tune *cmd;
  84. int ret = 0;
  85. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  86. if (!cmd)
  87. return -ENOMEM;
  88. cmd->test.id = TEST_CMD_CHANNEL_TUNE;
  89. cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
  90. /* set up any channel, 7 is in the middle of the range */
  91. cmd->channel = 7;
  92. ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
  93. if (ret < 0)
  94. wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
  95. kfree(cmd);
  96. return ret;
  97. }
  98. static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
  99. {
  100. struct wl1271_cmd_cal_update_ref_point *cmd;
  101. int ret = 0;
  102. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  103. if (!cmd)
  104. return -ENOMEM;
  105. cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
  106. /* FIXME: still waiting for the correct values */
  107. cmd->ref_power = 0;
  108. cmd->ref_detector = 0;
  109. cmd->sub_band = WL1271_PD_REFERENCE_POINT_BAND_B_G;
  110. ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
  111. if (ret < 0)
  112. wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
  113. kfree(cmd);
  114. return ret;
  115. }
  116. static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
  117. {
  118. struct wl1271_cmd_cal_p2g *cmd;
  119. int ret = 0;
  120. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  121. if (!cmd)
  122. return -ENOMEM;
  123. cmd->test.id = TEST_CMD_P2G_CAL;
  124. cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
  125. ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
  126. if (ret < 0)
  127. wl1271_warning("TEST_CMD_P2G_CAL failed");
  128. kfree(cmd);
  129. return ret;
  130. }
  131. static int wl1271_cmd_cal(struct wl1271 *wl)
  132. {
  133. /*
  134. * FIXME: we must make sure that we're not sleeping when calibration
  135. * is done
  136. */
  137. int ret;
  138. wl1271_notice("performing tx calibration");
  139. ret = wl1271_cmd_cal_channel_tune(wl);
  140. if (ret < 0)
  141. return ret;
  142. ret = wl1271_cmd_cal_update_ref_point(wl);
  143. if (ret < 0)
  144. return ret;
  145. ret = wl1271_cmd_cal_p2g(wl);
  146. if (ret < 0)
  147. return ret;
  148. return ret;
  149. }
  150. int wl1271_cmd_general_parms(struct wl1271 *wl)
  151. {
  152. struct wl1271_general_parms_cmd *gen_parms;
  153. struct conf_general_parms *g = &wl->conf.init.genparam;
  154. int ret;
  155. gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
  156. if (!gen_parms)
  157. return -ENOMEM;
  158. gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
  159. gen_parms->ref_clk = g->ref_clk;
  160. gen_parms->settling_time = g->settling_time;
  161. gen_parms->clk_valid_on_wakeup = g->clk_valid_on_wakeup;
  162. gen_parms->dc2dcmode = g->dc2dcmode;
  163. gen_parms->single_dual_band = g->single_dual_band;
  164. gen_parms->tx_bip_fem_autodetect = g->tx_bip_fem_autodetect;
  165. gen_parms->tx_bip_fem_manufacturer = g->tx_bip_fem_manufacturer;
  166. gen_parms->settings = g->settings;
  167. ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
  168. if (ret < 0)
  169. wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
  170. kfree(gen_parms);
  171. return ret;
  172. }
  173. int wl1271_cmd_radio_parms(struct wl1271 *wl)
  174. {
  175. struct wl1271_radio_parms_cmd *radio_parms;
  176. struct conf_radio_parms *r = &wl->conf.init.radioparam;
  177. int i, ret;
  178. radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
  179. if (!radio_parms)
  180. return -ENOMEM;
  181. radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
  182. /* Static radio parameters */
  183. radio_parms->rx_trace_loss = r->rx_trace_loss;
  184. radio_parms->tx_trace_loss = r->tx_trace_loss;
  185. memcpy(radio_parms->rx_rssi_and_proc_compens,
  186. r->rx_rssi_and_proc_compens,
  187. CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
  188. memcpy(radio_parms->rx_trace_loss_5, r->rx_trace_loss_5,
  189. CONF_NUMBER_OF_SUB_BANDS_5);
  190. memcpy(radio_parms->tx_trace_loss_5, r->tx_trace_loss_5,
  191. CONF_NUMBER_OF_SUB_BANDS_5);
  192. memcpy(radio_parms->rx_rssi_and_proc_compens_5,
  193. r->rx_rssi_and_proc_compens_5,
  194. CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
  195. /* Dynamic radio parameters */
  196. radio_parms->tx_ref_pd_voltage = cpu_to_le16(r->tx_ref_pd_voltage);
  197. radio_parms->tx_ref_power = r->tx_ref_power;
  198. radio_parms->tx_offset_db = r->tx_offset_db;
  199. memcpy(radio_parms->tx_rate_limits_normal, r->tx_rate_limits_normal,
  200. CONF_NUMBER_OF_RATE_GROUPS);
  201. memcpy(radio_parms->tx_rate_limits_degraded, r->tx_rate_limits_degraded,
  202. CONF_NUMBER_OF_RATE_GROUPS);
  203. memcpy(radio_parms->tx_channel_limits_11b, r->tx_channel_limits_11b,
  204. CONF_NUMBER_OF_CHANNELS_2_4);
  205. memcpy(radio_parms->tx_channel_limits_ofdm, r->tx_channel_limits_ofdm,
  206. CONF_NUMBER_OF_CHANNELS_2_4);
  207. memcpy(radio_parms->tx_pdv_rate_offsets, r->tx_pdv_rate_offsets,
  208. CONF_NUMBER_OF_RATE_GROUPS);
  209. memcpy(radio_parms->tx_ibias, r->tx_ibias, CONF_NUMBER_OF_RATE_GROUPS);
  210. radio_parms->rx_fem_insertion_loss = r->rx_fem_insertion_loss;
  211. for (i = 0; i < CONF_NUMBER_OF_SUB_BANDS_5; i++)
  212. radio_parms->tx_ref_pd_voltage_5[i] =
  213. cpu_to_le16(r->tx_ref_pd_voltage_5[i]);
  214. memcpy(radio_parms->tx_ref_power_5, r->tx_ref_power_5,
  215. CONF_NUMBER_OF_SUB_BANDS_5);
  216. memcpy(radio_parms->tx_offset_db_5, r->tx_offset_db_5,
  217. CONF_NUMBER_OF_SUB_BANDS_5);
  218. memcpy(radio_parms->tx_rate_limits_normal_5,
  219. r->tx_rate_limits_normal_5, CONF_NUMBER_OF_RATE_GROUPS);
  220. memcpy(radio_parms->tx_rate_limits_degraded_5,
  221. r->tx_rate_limits_degraded_5, CONF_NUMBER_OF_RATE_GROUPS);
  222. memcpy(radio_parms->tx_channel_limits_ofdm_5,
  223. r->tx_channel_limits_ofdm_5, CONF_NUMBER_OF_CHANNELS_5);
  224. memcpy(radio_parms->tx_pdv_rate_offsets_5, r->tx_pdv_rate_offsets_5,
  225. CONF_NUMBER_OF_RATE_GROUPS);
  226. memcpy(radio_parms->tx_ibias_5, r->tx_ibias_5,
  227. CONF_NUMBER_OF_RATE_GROUPS);
  228. memcpy(radio_parms->rx_fem_insertion_loss_5,
  229. r->rx_fem_insertion_loss_5, CONF_NUMBER_OF_SUB_BANDS_5);
  230. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
  231. radio_parms, sizeof(*radio_parms));
  232. ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
  233. if (ret < 0)
  234. wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
  235. kfree(radio_parms);
  236. return ret;
  237. }
  238. int wl1271_cmd_join(struct wl1271 *wl)
  239. {
  240. static bool do_cal = true;
  241. struct wl1271_cmd_join *join;
  242. int ret, i;
  243. u8 *bssid;
  244. /* FIXME: remove when we get calibration from the factory */
  245. if (do_cal) {
  246. ret = wl1271_cmd_cal(wl);
  247. if (ret < 0)
  248. wl1271_warning("couldn't calibrate");
  249. else
  250. do_cal = false;
  251. }
  252. /* FIXME: This is a workaround, because with the current stack, we
  253. * cannot know when we have disassociated. So, if we have already
  254. * joined, we disconnect before joining again. */
  255. if (wl->joined) {
  256. ret = wl1271_cmd_disconnect(wl);
  257. if (ret < 0) {
  258. wl1271_error("failed to disconnect before rejoining");
  259. goto out;
  260. }
  261. wl->joined = false;
  262. }
  263. join = kzalloc(sizeof(*join), GFP_KERNEL);
  264. if (!join) {
  265. ret = -ENOMEM;
  266. goto out;
  267. }
  268. wl1271_debug(DEBUG_CMD, "cmd join");
  269. /* Reverse order BSSID */
  270. bssid = (u8 *) &join->bssid_lsb;
  271. for (i = 0; i < ETH_ALEN; i++)
  272. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  273. join->rx_config_options = cpu_to_le32(wl->rx_config);
  274. join->rx_filter_options = cpu_to_le32(wl->rx_filter);
  275. join->bss_type = wl->bss_type;
  276. /*
  277. * FIXME: disable temporarily all filters because after commit
  278. * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
  279. * association. The filter logic needs to be implemented properly
  280. * and once that is done, this hack can be removed.
  281. */
  282. join->rx_config_options = cpu_to_le32(0);
  283. join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
  284. if (wl->band == IEEE80211_BAND_2GHZ)
  285. join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS |
  286. CONF_HW_BIT_RATE_2MBPS |
  287. CONF_HW_BIT_RATE_5_5MBPS |
  288. CONF_HW_BIT_RATE_11MBPS);
  289. else {
  290. join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
  291. join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS |
  292. CONF_HW_BIT_RATE_12MBPS |
  293. CONF_HW_BIT_RATE_24MBPS);
  294. }
  295. join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
  296. join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
  297. join->channel = wl->channel;
  298. join->ssid_len = wl->ssid_len;
  299. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  300. join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
  301. /* increment the session counter */
  302. wl->session_counter++;
  303. if (wl->session_counter >= SESSION_COUNTER_MAX)
  304. wl->session_counter = 0;
  305. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  306. /* reset TX security counters */
  307. wl->tx_security_last_seq = 0;
  308. wl->tx_security_seq_16 = 0;
  309. wl->tx_security_seq_32 = 0;
  310. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
  311. if (ret < 0) {
  312. wl1271_error("failed to initiate cmd join");
  313. goto out_free;
  314. }
  315. wl->joined = true;
  316. /*
  317. * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
  318. * simplify locking we just sleep instead, for now
  319. */
  320. msleep(10);
  321. out_free:
  322. kfree(join);
  323. out:
  324. return ret;
  325. }
  326. /**
  327. * send test command to firmware
  328. *
  329. * @wl: wl struct
  330. * @buf: buffer containing the command, with all headers, must work with dma
  331. * @len: length of the buffer
  332. * @answer: is answer needed
  333. */
  334. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  335. {
  336. int ret;
  337. size_t res_len = 0;
  338. wl1271_debug(DEBUG_CMD, "cmd test");
  339. if (answer)
  340. res_len = buf_len;
  341. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  342. if (ret < 0) {
  343. wl1271_warning("TEST command failed");
  344. return ret;
  345. }
  346. return ret;
  347. }
  348. /**
  349. * read acx from firmware
  350. *
  351. * @wl: wl struct
  352. * @id: acx id
  353. * @buf: buffer for the response, including all headers, must work with dma
  354. * @len: lenght of buf
  355. */
  356. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  357. {
  358. struct acx_header *acx = buf;
  359. int ret;
  360. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  361. acx->id = cpu_to_le16(id);
  362. /* payload length, does not include any headers */
  363. acx->len = cpu_to_le16(len - sizeof(*acx));
  364. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  365. if (ret < 0)
  366. wl1271_error("INTERROGATE command failed");
  367. return ret;
  368. }
  369. /**
  370. * write acx value to firmware
  371. *
  372. * @wl: wl struct
  373. * @id: acx id
  374. * @buf: buffer containing acx, including all headers, must work with dma
  375. * @len: length of buf
  376. */
  377. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  378. {
  379. struct acx_header *acx = buf;
  380. int ret;
  381. wl1271_debug(DEBUG_CMD, "cmd configure");
  382. acx->id = cpu_to_le16(id);
  383. /* payload length, does not include any headers */
  384. acx->len = cpu_to_le16(len - sizeof(*acx));
  385. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  386. if (ret < 0) {
  387. wl1271_warning("CONFIGURE command NOK");
  388. return ret;
  389. }
  390. return 0;
  391. }
  392. int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
  393. {
  394. struct cmd_enabledisable_path *cmd;
  395. int ret;
  396. u16 cmd_rx, cmd_tx;
  397. wl1271_debug(DEBUG_CMD, "cmd data path");
  398. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  399. if (!cmd) {
  400. ret = -ENOMEM;
  401. goto out;
  402. }
  403. cmd->channel = channel;
  404. if (enable) {
  405. cmd_rx = CMD_ENABLE_RX;
  406. cmd_tx = CMD_ENABLE_TX;
  407. } else {
  408. cmd_rx = CMD_DISABLE_RX;
  409. cmd_tx = CMD_DISABLE_TX;
  410. }
  411. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  412. if (ret < 0) {
  413. wl1271_error("rx %s cmd for channel %d failed",
  414. enable ? "start" : "stop", channel);
  415. goto out;
  416. }
  417. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  418. enable ? "start" : "stop", channel);
  419. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  420. if (ret < 0) {
  421. wl1271_error("tx %s cmd for channel %d failed",
  422. enable ? "start" : "stop", channel);
  423. return ret;
  424. }
  425. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  426. enable ? "start" : "stop", channel);
  427. out:
  428. kfree(cmd);
  429. return ret;
  430. }
  431. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
  432. {
  433. struct wl1271_cmd_ps_params *ps_params = NULL;
  434. int ret = 0;
  435. /* FIXME: this should be in ps.c */
  436. ret = wl1271_acx_wake_up_conditions(wl);
  437. if (ret < 0) {
  438. wl1271_error("couldn't set wake up conditions");
  439. goto out;
  440. }
  441. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  442. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  443. if (!ps_params) {
  444. ret = -ENOMEM;
  445. goto out;
  446. }
  447. ps_params->ps_mode = ps_mode;
  448. ps_params->send_null_data = 1;
  449. ps_params->retries = 5;
  450. ps_params->hang_over_period = 128;
  451. ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
  452. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  453. sizeof(*ps_params), 0);
  454. if (ret < 0) {
  455. wl1271_error("cmd set_ps_mode failed");
  456. goto out;
  457. }
  458. out:
  459. kfree(ps_params);
  460. return ret;
  461. }
  462. int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
  463. size_t len)
  464. {
  465. struct cmd_read_write_memory *cmd;
  466. int ret = 0;
  467. wl1271_debug(DEBUG_CMD, "cmd read memory");
  468. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  469. if (!cmd) {
  470. ret = -ENOMEM;
  471. goto out;
  472. }
  473. WARN_ON(len > MAX_READ_SIZE);
  474. len = min_t(size_t, len, MAX_READ_SIZE);
  475. cmd->addr = cpu_to_le32(addr);
  476. cmd->size = cpu_to_le32(len);
  477. ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
  478. sizeof(*cmd));
  479. if (ret < 0) {
  480. wl1271_error("read memory command failed: %d", ret);
  481. goto out;
  482. }
  483. /* the read command got in */
  484. memcpy(answer, cmd->value, len);
  485. out:
  486. kfree(cmd);
  487. return ret;
  488. }
  489. int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
  490. u8 active_scan, u8 high_prio, u8 band,
  491. u8 probe_requests)
  492. {
  493. struct wl1271_cmd_trigger_scan_to *trigger = NULL;
  494. struct wl1271_cmd_scan *params = NULL;
  495. struct ieee80211_channel *channels;
  496. int i, j, n_ch, ret;
  497. u16 scan_options = 0;
  498. u8 ieee_band;
  499. if (band == WL1271_SCAN_BAND_2_4_GHZ)
  500. ieee_band = IEEE80211_BAND_2GHZ;
  501. else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
  502. ieee_band = IEEE80211_BAND_2GHZ;
  503. else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
  504. ieee_band = IEEE80211_BAND_5GHZ;
  505. else
  506. return -EINVAL;
  507. if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
  508. return -EINVAL;
  509. channels = wl->hw->wiphy->bands[ieee_band]->channels;
  510. n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
  511. if (wl->scanning)
  512. return -EINVAL;
  513. params = kzalloc(sizeof(*params), GFP_KERNEL);
  514. if (!params)
  515. return -ENOMEM;
  516. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  517. params->params.rx_filter_options =
  518. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  519. if (!active_scan)
  520. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  521. if (high_prio)
  522. scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
  523. params->params.scan_options = cpu_to_le16(scan_options);
  524. params->params.num_probe_requests = probe_requests;
  525. /* Let the fw autodetect suitable tx_rate for probes */
  526. params->params.tx_rate = 0;
  527. params->params.tid_trigger = 0;
  528. params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  529. if (band == WL1271_SCAN_BAND_DUAL)
  530. params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  531. else
  532. params->params.band = band;
  533. for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
  534. if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
  535. params->channels[j].min_duration =
  536. cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
  537. params->channels[j].max_duration =
  538. cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
  539. memset(&params->channels[j].bssid_lsb, 0xff, 4);
  540. memset(&params->channels[j].bssid_msb, 0xff, 2);
  541. params->channels[j].early_termination = 0;
  542. params->channels[j].tx_power_att =
  543. WL1271_SCAN_CURRENT_TX_PWR;
  544. params->channels[j].channel = channels[i].hw_value;
  545. j++;
  546. }
  547. }
  548. params->params.num_channels = j;
  549. if (len && ssid) {
  550. params->params.ssid_len = len;
  551. memcpy(params->params.ssid, ssid, len);
  552. }
  553. ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
  554. if (ret < 0) {
  555. wl1271_error("PROBE request template failed");
  556. goto out;
  557. }
  558. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  559. if (!trigger) {
  560. ret = -ENOMEM;
  561. goto out;
  562. }
  563. /* disable the timeout */
  564. trigger->timeout = 0;
  565. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  566. sizeof(*trigger), 0);
  567. if (ret < 0) {
  568. wl1271_error("trigger scan to failed for hw scan");
  569. goto out;
  570. }
  571. wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  572. wl->scanning = true;
  573. if (wl1271_11a_enabled()) {
  574. wl->scan.state = band;
  575. if (band == WL1271_SCAN_BAND_DUAL) {
  576. wl->scan.active = active_scan;
  577. wl->scan.high_prio = high_prio;
  578. wl->scan.probe_requests = probe_requests;
  579. if (len && ssid) {
  580. wl->scan.ssid_len = len;
  581. memcpy(wl->scan.ssid, ssid, len);
  582. } else
  583. wl->scan.ssid_len = 0;
  584. }
  585. }
  586. ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
  587. if (ret < 0) {
  588. wl1271_error("SCAN failed");
  589. wl->scanning = false;
  590. goto out;
  591. }
  592. out:
  593. kfree(params);
  594. return ret;
  595. }
  596. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  597. void *buf, size_t buf_len)
  598. {
  599. struct wl1271_cmd_template_set *cmd;
  600. int ret = 0;
  601. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  602. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  603. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  604. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  605. if (!cmd) {
  606. ret = -ENOMEM;
  607. goto out;
  608. }
  609. cmd->len = cpu_to_le16(buf_len);
  610. cmd->template_type = template_id;
  611. cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
  612. cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
  613. cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
  614. if (buf)
  615. memcpy(cmd->template_data, buf, buf_len);
  616. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  617. if (ret < 0) {
  618. wl1271_warning("cmd set_template failed: %d", ret);
  619. goto out_free;
  620. }
  621. out_free:
  622. kfree(cmd);
  623. out:
  624. return ret;
  625. }
  626. static int wl1271_build_basic_rates(u8 *rates, u8 band)
  627. {
  628. u8 index = 0;
  629. if (band == IEEE80211_BAND_2GHZ) {
  630. rates[index++] =
  631. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  632. rates[index++] =
  633. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  634. rates[index++] =
  635. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  636. rates[index++] =
  637. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  638. } else if (band == IEEE80211_BAND_5GHZ) {
  639. rates[index++] =
  640. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
  641. rates[index++] =
  642. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
  643. rates[index++] =
  644. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  645. } else {
  646. wl1271_error("build_basic_rates invalid band: %d", band);
  647. }
  648. return index;
  649. }
  650. static int wl1271_build_extended_rates(u8 *rates, u8 band)
  651. {
  652. u8 index = 0;
  653. if (band == IEEE80211_BAND_2GHZ) {
  654. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  655. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  656. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  657. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  658. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  659. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  660. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  661. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  662. } else if (band == IEEE80211_BAND_5GHZ) {
  663. rates[index++] =
  664. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
  665. rates[index++] =
  666. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
  667. rates[index++] =
  668. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  669. rates[index++] =
  670. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
  671. rates[index++] =
  672. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
  673. rates[index++] =
  674. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
  675. } else {
  676. wl1271_error("build_basic_rates invalid band: %d", band);
  677. }
  678. return index;
  679. }
  680. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  681. {
  682. struct wl12xx_null_data_template template;
  683. if (!is_zero_ether_addr(wl->bssid)) {
  684. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  685. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  686. } else {
  687. memset(template.header.da, 0xff, ETH_ALEN);
  688. memset(template.header.bssid, 0xff, ETH_ALEN);
  689. }
  690. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  691. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  692. IEEE80211_STYPE_NULLFUNC |
  693. IEEE80211_FCTL_TODS);
  694. return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
  695. sizeof(template));
  696. }
  697. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  698. {
  699. struct wl12xx_ps_poll_template template;
  700. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  701. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  702. /* aid in PS-Poll has its two MSBs each set to 1 */
  703. template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
  704. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  705. return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
  706. sizeof(template));
  707. }
  708. int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
  709. u8 band)
  710. {
  711. struct wl12xx_probe_req_template template;
  712. struct wl12xx_ie_rates *rates;
  713. char *ptr;
  714. u16 size;
  715. int ret;
  716. ptr = (char *)&template;
  717. size = sizeof(struct ieee80211_header);
  718. memset(template.header.da, 0xff, ETH_ALEN);
  719. memset(template.header.bssid, 0xff, ETH_ALEN);
  720. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  721. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  722. /* IEs */
  723. /* SSID */
  724. template.ssid.header.id = WLAN_EID_SSID;
  725. template.ssid.header.len = ssid_len;
  726. if (ssid_len && ssid)
  727. memcpy(template.ssid.ssid, ssid, ssid_len);
  728. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  729. ptr += size;
  730. /* Basic Rates */
  731. rates = (struct wl12xx_ie_rates *)ptr;
  732. rates->header.id = WLAN_EID_SUPP_RATES;
  733. rates->header.len = wl1271_build_basic_rates(rates->rates, band);
  734. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  735. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  736. /* Extended rates */
  737. rates = (struct wl12xx_ie_rates *)ptr;
  738. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  739. rates->header.len = wl1271_build_extended_rates(rates->rates, band);
  740. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  741. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  742. if (band == IEEE80211_BAND_2GHZ)
  743. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  744. &template, size);
  745. else
  746. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  747. &template, size);
  748. return ret;
  749. }
  750. int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
  751. {
  752. struct wl1271_cmd_set_keys *cmd;
  753. int ret = 0;
  754. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  755. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  756. if (!cmd) {
  757. ret = -ENOMEM;
  758. goto out;
  759. }
  760. cmd->id = id;
  761. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  762. cmd->key_type = KEY_WEP;
  763. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  764. if (ret < 0) {
  765. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  766. goto out;
  767. }
  768. out:
  769. kfree(cmd);
  770. return ret;
  771. }
  772. int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  773. u8 key_size, const u8 *key, const u8 *addr,
  774. u32 tx_seq_32, u16 tx_seq_16)
  775. {
  776. struct wl1271_cmd_set_keys *cmd;
  777. int ret = 0;
  778. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  779. if (!cmd) {
  780. ret = -ENOMEM;
  781. goto out;
  782. }
  783. if (key_type != KEY_WEP)
  784. memcpy(cmd->addr, addr, ETH_ALEN);
  785. cmd->key_action = cpu_to_le16(action);
  786. cmd->key_size = key_size;
  787. cmd->key_type = key_type;
  788. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  789. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  790. /* we have only one SSID profile */
  791. cmd->ssid_profile = 0;
  792. cmd->id = id;
  793. if (key_type == KEY_TKIP) {
  794. /*
  795. * We get the key in the following form:
  796. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  797. * but the target is expecting:
  798. * TKIP - RX MIC - TX MIC
  799. */
  800. memcpy(cmd->key, key, 16);
  801. memcpy(cmd->key + 16, key + 24, 8);
  802. memcpy(cmd->key + 24, key + 16, 8);
  803. } else {
  804. memcpy(cmd->key, key, key_size);
  805. }
  806. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  807. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  808. if (ret < 0) {
  809. wl1271_warning("could not set keys");
  810. goto out;
  811. }
  812. out:
  813. kfree(cmd);
  814. return ret;
  815. }
  816. int wl1271_cmd_disconnect(struct wl1271 *wl)
  817. {
  818. struct wl1271_cmd_disconnect *cmd;
  819. int ret = 0;
  820. wl1271_debug(DEBUG_CMD, "cmd disconnect");
  821. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  822. if (!cmd) {
  823. ret = -ENOMEM;
  824. goto out;
  825. }
  826. cmd->rx_config_options = cpu_to_le32(wl->rx_config);
  827. cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
  828. /* disconnect reason is not used in immediate disconnections */
  829. cmd->type = DISCONNECT_IMMEDIATE;
  830. ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
  831. if (ret < 0) {
  832. wl1271_error("failed to send disconnect command");
  833. goto out_free;
  834. }
  835. out_free:
  836. kfree(cmd);
  837. out:
  838. return ret;
  839. }