wl1271_cmd.c 27 KB

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