wl1271_cmd.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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, 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. /* the channel here is only used for calibration, so hardcoded to 1 */
  421. cmd->channel = 1;
  422. if (enable) {
  423. cmd_rx = CMD_ENABLE_RX;
  424. cmd_tx = CMD_ENABLE_TX;
  425. } else {
  426. cmd_rx = CMD_DISABLE_RX;
  427. cmd_tx = CMD_DISABLE_TX;
  428. }
  429. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  430. if (ret < 0) {
  431. wl1271_error("rx %s cmd for channel %d failed",
  432. enable ? "start" : "stop", cmd->channel);
  433. goto out;
  434. }
  435. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  436. enable ? "start" : "stop", cmd->channel);
  437. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  438. if (ret < 0) {
  439. wl1271_error("tx %s cmd for channel %d failed",
  440. enable ? "start" : "stop", cmd->channel);
  441. return ret;
  442. }
  443. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  444. enable ? "start" : "stop", cmd->channel);
  445. out:
  446. kfree(cmd);
  447. return ret;
  448. }
  449. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
  450. {
  451. struct wl1271_cmd_ps_params *ps_params = NULL;
  452. int ret = 0;
  453. /* FIXME: this should be in ps.c */
  454. ret = wl1271_acx_wake_up_conditions(wl);
  455. if (ret < 0) {
  456. wl1271_error("couldn't set wake up conditions");
  457. goto out;
  458. }
  459. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  460. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  461. if (!ps_params) {
  462. ret = -ENOMEM;
  463. goto out;
  464. }
  465. ps_params->ps_mode = ps_mode;
  466. ps_params->send_null_data = send;
  467. ps_params->retries = 5;
  468. ps_params->hang_over_period = 128;
  469. ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
  470. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  471. sizeof(*ps_params), 0);
  472. if (ret < 0) {
  473. wl1271_error("cmd set_ps_mode failed");
  474. goto out;
  475. }
  476. out:
  477. kfree(ps_params);
  478. return ret;
  479. }
  480. int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
  481. size_t len)
  482. {
  483. struct cmd_read_write_memory *cmd;
  484. int ret = 0;
  485. wl1271_debug(DEBUG_CMD, "cmd read memory");
  486. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  487. if (!cmd) {
  488. ret = -ENOMEM;
  489. goto out;
  490. }
  491. WARN_ON(len > MAX_READ_SIZE);
  492. len = min_t(size_t, len, MAX_READ_SIZE);
  493. cmd->addr = cpu_to_le32(addr);
  494. cmd->size = cpu_to_le32(len);
  495. ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
  496. sizeof(*cmd));
  497. if (ret < 0) {
  498. wl1271_error("read memory command failed: %d", ret);
  499. goto out;
  500. }
  501. /* the read command got in */
  502. memcpy(answer, cmd->value, len);
  503. out:
  504. kfree(cmd);
  505. return ret;
  506. }
  507. int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
  508. u8 active_scan, u8 high_prio, u8 band,
  509. u8 probe_requests)
  510. {
  511. struct wl1271_cmd_trigger_scan_to *trigger = NULL;
  512. struct wl1271_cmd_scan *params = NULL;
  513. struct ieee80211_channel *channels;
  514. int i, j, n_ch, ret;
  515. u16 scan_options = 0;
  516. u8 ieee_band;
  517. if (band == WL1271_SCAN_BAND_2_4_GHZ)
  518. ieee_band = IEEE80211_BAND_2GHZ;
  519. else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
  520. ieee_band = IEEE80211_BAND_2GHZ;
  521. else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
  522. ieee_band = IEEE80211_BAND_5GHZ;
  523. else
  524. return -EINVAL;
  525. if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
  526. return -EINVAL;
  527. channels = wl->hw->wiphy->bands[ieee_band]->channels;
  528. n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
  529. if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
  530. return -EINVAL;
  531. params = kzalloc(sizeof(*params), GFP_KERNEL);
  532. if (!params)
  533. return -ENOMEM;
  534. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  535. params->params.rx_filter_options =
  536. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  537. if (!active_scan)
  538. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  539. if (high_prio)
  540. scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
  541. params->params.scan_options = cpu_to_le16(scan_options);
  542. params->params.num_probe_requests = probe_requests;
  543. /* Let the fw autodetect suitable tx_rate for probes */
  544. params->params.tx_rate = 0;
  545. params->params.tid_trigger = 0;
  546. params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  547. if (band == WL1271_SCAN_BAND_DUAL)
  548. params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  549. else
  550. params->params.band = band;
  551. for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
  552. if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
  553. params->channels[j].min_duration =
  554. cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
  555. params->channels[j].max_duration =
  556. cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
  557. memset(&params->channels[j].bssid_lsb, 0xff, 4);
  558. memset(&params->channels[j].bssid_msb, 0xff, 2);
  559. params->channels[j].early_termination = 0;
  560. params->channels[j].tx_power_att =
  561. WL1271_SCAN_CURRENT_TX_PWR;
  562. params->channels[j].channel = channels[i].hw_value;
  563. j++;
  564. }
  565. }
  566. params->params.num_channels = j;
  567. if (len && ssid) {
  568. params->params.ssid_len = len;
  569. memcpy(params->params.ssid, ssid, len);
  570. }
  571. ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
  572. if (ret < 0) {
  573. wl1271_error("PROBE request template failed");
  574. goto out;
  575. }
  576. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  577. if (!trigger) {
  578. ret = -ENOMEM;
  579. goto out;
  580. }
  581. /* disable the timeout */
  582. trigger->timeout = 0;
  583. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  584. sizeof(*trigger), 0);
  585. if (ret < 0) {
  586. wl1271_error("trigger scan to failed for hw scan");
  587. goto out;
  588. }
  589. wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  590. set_bit(WL1271_FLAG_SCANNING, &wl->flags);
  591. if (wl1271_11a_enabled()) {
  592. wl->scan.state = band;
  593. if (band == WL1271_SCAN_BAND_DUAL) {
  594. wl->scan.active = active_scan;
  595. wl->scan.high_prio = high_prio;
  596. wl->scan.probe_requests = probe_requests;
  597. if (len && ssid) {
  598. wl->scan.ssid_len = len;
  599. memcpy(wl->scan.ssid, ssid, len);
  600. } else
  601. wl->scan.ssid_len = 0;
  602. }
  603. }
  604. ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
  605. if (ret < 0) {
  606. wl1271_error("SCAN failed");
  607. clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
  608. goto out;
  609. }
  610. out:
  611. kfree(params);
  612. return ret;
  613. }
  614. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  615. void *buf, size_t buf_len)
  616. {
  617. struct wl1271_cmd_template_set *cmd;
  618. int ret = 0;
  619. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  620. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  621. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  622. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  623. if (!cmd) {
  624. ret = -ENOMEM;
  625. goto out;
  626. }
  627. cmd->len = cpu_to_le16(buf_len);
  628. cmd->template_type = template_id;
  629. cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
  630. cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
  631. cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
  632. if (buf)
  633. memcpy(cmd->template_data, buf, buf_len);
  634. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  635. if (ret < 0) {
  636. wl1271_warning("cmd set_template failed: %d", ret);
  637. goto out_free;
  638. }
  639. out_free:
  640. kfree(cmd);
  641. out:
  642. return ret;
  643. }
  644. static int wl1271_build_basic_rates(u8 *rates, u8 band)
  645. {
  646. u8 index = 0;
  647. if (band == IEEE80211_BAND_2GHZ) {
  648. rates[index++] =
  649. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  650. rates[index++] =
  651. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  652. rates[index++] =
  653. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  654. rates[index++] =
  655. IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  656. } else if (band == IEEE80211_BAND_5GHZ) {
  657. rates[index++] =
  658. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
  659. rates[index++] =
  660. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
  661. rates[index++] =
  662. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  663. } else {
  664. wl1271_error("build_basic_rates invalid band: %d", band);
  665. }
  666. return index;
  667. }
  668. static int wl1271_build_extended_rates(u8 *rates, u8 band)
  669. {
  670. u8 index = 0;
  671. if (band == IEEE80211_BAND_2GHZ) {
  672. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  673. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  674. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  675. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  676. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  677. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  678. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  679. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  680. } else if (band == IEEE80211_BAND_5GHZ) {
  681. rates[index++] =
  682. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
  683. rates[index++] =
  684. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
  685. rates[index++] =
  686. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  687. rates[index++] =
  688. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
  689. rates[index++] =
  690. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
  691. rates[index++] =
  692. IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
  693. } else {
  694. wl1271_error("build_basic_rates invalid band: %d", band);
  695. }
  696. return index;
  697. }
  698. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  699. {
  700. struct wl12xx_null_data_template template;
  701. if (!is_zero_ether_addr(wl->bssid)) {
  702. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  703. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  704. } else {
  705. memset(template.header.da, 0xff, ETH_ALEN);
  706. memset(template.header.bssid, 0xff, ETH_ALEN);
  707. }
  708. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  709. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  710. IEEE80211_STYPE_NULLFUNC |
  711. IEEE80211_FCTL_TODS);
  712. return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
  713. sizeof(template));
  714. }
  715. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  716. {
  717. struct wl12xx_ps_poll_template template;
  718. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  719. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  720. /* aid in PS-Poll has its two MSBs each set to 1 */
  721. template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
  722. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  723. return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
  724. sizeof(template));
  725. }
  726. int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
  727. u8 band)
  728. {
  729. struct wl12xx_probe_req_template template;
  730. struct wl12xx_ie_rates *rates;
  731. char *ptr;
  732. u16 size;
  733. int ret;
  734. ptr = (char *)&template;
  735. size = sizeof(struct ieee80211_header);
  736. memset(template.header.da, 0xff, ETH_ALEN);
  737. memset(template.header.bssid, 0xff, ETH_ALEN);
  738. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  739. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  740. /* IEs */
  741. /* SSID */
  742. template.ssid.header.id = WLAN_EID_SSID;
  743. template.ssid.header.len = ssid_len;
  744. if (ssid_len && ssid)
  745. memcpy(template.ssid.ssid, ssid, ssid_len);
  746. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  747. ptr += size;
  748. /* Basic Rates */
  749. rates = (struct wl12xx_ie_rates *)ptr;
  750. rates->header.id = WLAN_EID_SUPP_RATES;
  751. rates->header.len = wl1271_build_basic_rates(rates->rates, band);
  752. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  753. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  754. /* Extended rates */
  755. rates = (struct wl12xx_ie_rates *)ptr;
  756. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  757. rates->header.len = wl1271_build_extended_rates(rates->rates, band);
  758. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  759. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  760. if (band == IEEE80211_BAND_2GHZ)
  761. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  762. &template, size);
  763. else
  764. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  765. &template, size);
  766. return ret;
  767. }
  768. int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
  769. {
  770. struct wl1271_cmd_set_keys *cmd;
  771. int ret = 0;
  772. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  773. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  774. if (!cmd) {
  775. ret = -ENOMEM;
  776. goto out;
  777. }
  778. cmd->id = id;
  779. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  780. cmd->key_type = KEY_WEP;
  781. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  782. if (ret < 0) {
  783. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  784. goto out;
  785. }
  786. out:
  787. kfree(cmd);
  788. return ret;
  789. }
  790. int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  791. u8 key_size, const u8 *key, const u8 *addr,
  792. u32 tx_seq_32, u16 tx_seq_16)
  793. {
  794. struct wl1271_cmd_set_keys *cmd;
  795. int ret = 0;
  796. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  797. if (!cmd) {
  798. ret = -ENOMEM;
  799. goto out;
  800. }
  801. if (key_type != KEY_WEP)
  802. memcpy(cmd->addr, addr, ETH_ALEN);
  803. cmd->key_action = cpu_to_le16(action);
  804. cmd->key_size = key_size;
  805. cmd->key_type = key_type;
  806. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  807. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  808. /* we have only one SSID profile */
  809. cmd->ssid_profile = 0;
  810. cmd->id = id;
  811. if (key_type == KEY_TKIP) {
  812. /*
  813. * We get the key in the following form:
  814. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  815. * but the target is expecting:
  816. * TKIP - RX MIC - TX MIC
  817. */
  818. memcpy(cmd->key, key, 16);
  819. memcpy(cmd->key + 16, key + 24, 8);
  820. memcpy(cmd->key + 24, key + 16, 8);
  821. } else {
  822. memcpy(cmd->key, key, key_size);
  823. }
  824. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  825. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  826. if (ret < 0) {
  827. wl1271_warning("could not set keys");
  828. goto out;
  829. }
  830. out:
  831. kfree(cmd);
  832. return ret;
  833. }
  834. int wl1271_cmd_disconnect(struct wl1271 *wl)
  835. {
  836. struct wl1271_cmd_disconnect *cmd;
  837. int ret = 0;
  838. wl1271_debug(DEBUG_CMD, "cmd disconnect");
  839. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  840. if (!cmd) {
  841. ret = -ENOMEM;
  842. goto out;
  843. }
  844. cmd->rx_config_options = cpu_to_le32(wl->rx_config);
  845. cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
  846. /* disconnect reason is not used in immediate disconnections */
  847. cmd->type = DISCONNECT_IMMEDIATE;
  848. ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
  849. if (ret < 0) {
  850. wl1271_error("failed to send disconnect command");
  851. goto out_free;
  852. }
  853. out_free:
  854. kfree(cmd);
  855. out:
  856. return ret;
  857. }