cmd.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/crc7.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/ieee80211.h>
  29. #include <linux/slab.h>
  30. #include "wl12xx.h"
  31. #include "reg.h"
  32. #include "io.h"
  33. #include "acx.h"
  34. #include "wl12xx_80211.h"
  35. #include "cmd.h"
  36. #include "event.h"
  37. #include "tx.h"
  38. #define WL1271_CMD_FAST_POLL_COUNT 50
  39. /*
  40. * send command to firmware
  41. *
  42. * @wl: wl struct
  43. * @id: command id
  44. * @buf: buffer containing the command, must work with dma
  45. * @len: length of the buffer
  46. */
  47. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  48. size_t res_len)
  49. {
  50. struct wl1271_cmd_header *cmd;
  51. unsigned long timeout;
  52. u32 intr;
  53. int ret = 0;
  54. u16 status;
  55. u16 poll_count = 0;
  56. cmd = buf;
  57. cmd->id = cpu_to_le16(id);
  58. cmd->status = 0;
  59. WARN_ON(len % 4 != 0);
  60. WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  61. wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
  62. wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  63. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  64. intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  65. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  66. if (time_after(jiffies, timeout)) {
  67. wl1271_error("command complete timeout");
  68. ret = -ETIMEDOUT;
  69. goto out;
  70. }
  71. poll_count++;
  72. if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
  73. udelay(10);
  74. else
  75. msleep(1);
  76. intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  77. }
  78. /* read back the status code of the command */
  79. if (res_len == 0)
  80. res_len = sizeof(struct wl1271_cmd_header);
  81. wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
  82. status = le16_to_cpu(cmd->status);
  83. if (status != CMD_STATUS_SUCCESS) {
  84. wl1271_error("command execute failure %d", status);
  85. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  86. ret = -EIO;
  87. }
  88. wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
  89. WL1271_ACX_INTR_CMD_COMPLETE);
  90. out:
  91. return ret;
  92. }
  93. int wl1271_cmd_general_parms(struct wl1271 *wl)
  94. {
  95. struct wl1271_general_parms_cmd *gen_parms;
  96. struct wl1271_ini_general_params *gp =
  97. &((struct wl1271_nvs_file *)wl->nvs)->general_params;
  98. bool answer = false;
  99. int ret;
  100. if (!wl->nvs)
  101. return -ENODEV;
  102. gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
  103. if (!gen_parms)
  104. return -ENOMEM;
  105. gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
  106. memcpy(&gen_parms->general_params, gp, sizeof(*gp));
  107. if (gp->tx_bip_fem_auto_detect)
  108. answer = true;
  109. ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
  110. if (ret < 0) {
  111. wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
  112. goto out;
  113. }
  114. gp->tx_bip_fem_manufacturer =
  115. gen_parms->general_params.tx_bip_fem_manufacturer;
  116. wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
  117. answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
  118. out:
  119. kfree(gen_parms);
  120. return ret;
  121. }
  122. int wl128x_cmd_general_parms(struct wl1271 *wl)
  123. {
  124. struct wl128x_general_parms_cmd *gen_parms;
  125. struct wl128x_ini_general_params *gp =
  126. &((struct wl128x_nvs_file *)wl->nvs)->general_params;
  127. bool answer = false;
  128. int ret;
  129. if (!wl->nvs)
  130. return -ENODEV;
  131. gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
  132. if (!gen_parms)
  133. return -ENOMEM;
  134. gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
  135. memcpy(&gen_parms->general_params, gp, sizeof(*gp));
  136. if (gp->tx_bip_fem_auto_detect)
  137. answer = true;
  138. ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
  139. if (ret < 0) {
  140. wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
  141. goto out;
  142. }
  143. gp->tx_bip_fem_manufacturer =
  144. gen_parms->general_params.tx_bip_fem_manufacturer;
  145. wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
  146. answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
  147. out:
  148. kfree(gen_parms);
  149. return ret;
  150. }
  151. int wl1271_cmd_radio_parms(struct wl1271 *wl)
  152. {
  153. struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
  154. struct wl1271_radio_parms_cmd *radio_parms;
  155. struct wl1271_ini_general_params *gp = &nvs->general_params;
  156. int ret;
  157. if (!wl->nvs)
  158. return -ENODEV;
  159. radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
  160. if (!radio_parms)
  161. return -ENOMEM;
  162. radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
  163. /* 2.4GHz parameters */
  164. memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
  165. sizeof(struct wl1271_ini_band_params_2));
  166. memcpy(&radio_parms->dyn_params_2,
  167. &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
  168. sizeof(struct wl1271_ini_fem_params_2));
  169. /* 5GHz parameters */
  170. memcpy(&radio_parms->static_params_5,
  171. &nvs->stat_radio_params_5,
  172. sizeof(struct wl1271_ini_band_params_5));
  173. memcpy(&radio_parms->dyn_params_5,
  174. &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
  175. sizeof(struct wl1271_ini_fem_params_5));
  176. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
  177. radio_parms, sizeof(*radio_parms));
  178. ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
  179. if (ret < 0)
  180. wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
  181. kfree(radio_parms);
  182. return ret;
  183. }
  184. int wl128x_cmd_radio_parms(struct wl1271 *wl)
  185. {
  186. struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
  187. struct wl128x_radio_parms_cmd *radio_parms;
  188. struct wl128x_ini_general_params *gp = &nvs->general_params;
  189. int ret;
  190. if (!wl->nvs)
  191. return -ENODEV;
  192. radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
  193. if (!radio_parms)
  194. return -ENOMEM;
  195. radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
  196. /* 2.4GHz parameters */
  197. memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
  198. sizeof(struct wl128x_ini_band_params_2));
  199. memcpy(&radio_parms->dyn_params_2,
  200. &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
  201. sizeof(struct wl128x_ini_fem_params_2));
  202. /* 5GHz parameters */
  203. memcpy(&radio_parms->static_params_5,
  204. &nvs->stat_radio_params_5,
  205. sizeof(struct wl128x_ini_band_params_5));
  206. memcpy(&radio_parms->dyn_params_5,
  207. &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
  208. sizeof(struct wl128x_ini_fem_params_5));
  209. radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
  210. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
  211. radio_parms, sizeof(*radio_parms));
  212. ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
  213. if (ret < 0)
  214. wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
  215. kfree(radio_parms);
  216. return ret;
  217. }
  218. int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
  219. {
  220. struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
  221. struct conf_rf_settings *rf = &wl->conf.rf;
  222. int ret;
  223. if (!wl->nvs)
  224. return -ENODEV;
  225. ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
  226. if (!ext_radio_parms)
  227. return -ENOMEM;
  228. ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
  229. memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
  230. rf->tx_per_channel_power_compensation_2,
  231. CONF_TX_PWR_COMPENSATION_LEN_2);
  232. memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
  233. rf->tx_per_channel_power_compensation_5,
  234. CONF_TX_PWR_COMPENSATION_LEN_5);
  235. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
  236. ext_radio_parms, sizeof(*ext_radio_parms));
  237. ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
  238. if (ret < 0)
  239. wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
  240. kfree(ext_radio_parms);
  241. return ret;
  242. }
  243. /*
  244. * Poll the mailbox event field until any of the bits in the mask is set or a
  245. * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
  246. */
  247. static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
  248. {
  249. u32 events_vector, event;
  250. unsigned long timeout;
  251. timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
  252. do {
  253. if (time_after(jiffies, timeout)) {
  254. wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
  255. (int)mask);
  256. return -ETIMEDOUT;
  257. }
  258. msleep(1);
  259. /* read from both event fields */
  260. wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
  261. sizeof(events_vector), false);
  262. event = events_vector & mask;
  263. wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
  264. sizeof(events_vector), false);
  265. event |= events_vector & mask;
  266. } while (!event);
  267. return 0;
  268. }
  269. static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
  270. {
  271. int ret;
  272. ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
  273. if (ret != 0) {
  274. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  275. return ret;
  276. }
  277. return 0;
  278. }
  279. int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
  280. {
  281. struct wl1271_cmd_join *join;
  282. int ret, i;
  283. u8 *bssid;
  284. join = kzalloc(sizeof(*join), GFP_KERNEL);
  285. if (!join) {
  286. ret = -ENOMEM;
  287. goto out;
  288. }
  289. wl1271_debug(DEBUG_CMD, "cmd join");
  290. /* Reverse order BSSID */
  291. bssid = (u8 *) &join->bssid_lsb;
  292. for (i = 0; i < ETH_ALEN; i++)
  293. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  294. join->rx_config_options = cpu_to_le32(wl->rx_config);
  295. join->rx_filter_options = cpu_to_le32(wl->rx_filter);
  296. join->bss_type = bss_type;
  297. join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
  298. join->supported_rate_set = cpu_to_le32(wl->rate_set);
  299. if (wl->band == IEEE80211_BAND_5GHZ)
  300. join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
  301. join->beacon_interval = cpu_to_le16(wl->beacon_int);
  302. join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
  303. join->channel = wl->channel;
  304. join->ssid_len = wl->ssid_len;
  305. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  306. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  307. /* reset TX security counters */
  308. wl->tx_security_last_seq = 0;
  309. wl->tx_security_seq = 0;
  310. wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
  311. join->basic_rate_set, join->supported_rate_set);
  312. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
  313. if (ret < 0) {
  314. wl1271_error("failed to initiate cmd join");
  315. goto out_free;
  316. }
  317. ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
  318. if (ret < 0)
  319. wl1271_error("cmd join event completion error");
  320. out_free:
  321. kfree(join);
  322. out:
  323. return ret;
  324. }
  325. /**
  326. * send test command to firmware
  327. *
  328. * @wl: wl struct
  329. * @buf: buffer containing the command, with all headers, must work with dma
  330. * @len: length of the buffer
  331. * @answer: is answer needed
  332. */
  333. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  334. {
  335. int ret;
  336. size_t res_len = 0;
  337. wl1271_debug(DEBUG_CMD, "cmd test");
  338. if (answer)
  339. res_len = buf_len;
  340. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  341. if (ret < 0) {
  342. wl1271_warning("TEST command failed");
  343. return ret;
  344. }
  345. return ret;
  346. }
  347. /**
  348. * read acx from firmware
  349. *
  350. * @wl: wl struct
  351. * @id: acx id
  352. * @buf: buffer for the response, including all headers, must work with dma
  353. * @len: lenght of buf
  354. */
  355. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  356. {
  357. struct acx_header *acx = buf;
  358. int ret;
  359. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  360. acx->id = cpu_to_le16(id);
  361. /* payload length, does not include any headers */
  362. acx->len = cpu_to_le16(len - sizeof(*acx));
  363. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  364. if (ret < 0)
  365. wl1271_error("INTERROGATE command failed");
  366. return ret;
  367. }
  368. /**
  369. * write acx value to firmware
  370. *
  371. * @wl: wl struct
  372. * @id: acx id
  373. * @buf: buffer containing acx, including all headers, must work with dma
  374. * @len: length of buf
  375. */
  376. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  377. {
  378. struct acx_header *acx = buf;
  379. int ret;
  380. wl1271_debug(DEBUG_CMD, "cmd configure");
  381. acx->id = cpu_to_le16(id);
  382. /* payload length, does not include any headers */
  383. acx->len = cpu_to_le16(len - sizeof(*acx));
  384. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  385. if (ret < 0) {
  386. wl1271_warning("CONFIGURE command NOK");
  387. return ret;
  388. }
  389. return 0;
  390. }
  391. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  392. {
  393. struct cmd_enabledisable_path *cmd;
  394. int ret;
  395. u16 cmd_rx, cmd_tx;
  396. wl1271_debug(DEBUG_CMD, "cmd data path");
  397. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  398. if (!cmd) {
  399. ret = -ENOMEM;
  400. goto out;
  401. }
  402. /* the channel here is only used for calibration, so hardcoded to 1 */
  403. cmd->channel = 1;
  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", cmd->channel);
  415. goto out;
  416. }
  417. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  418. enable ? "start" : "stop", cmd->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", cmd->channel);
  423. goto out;
  424. }
  425. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  426. enable ? "start" : "stop", cmd->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. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  436. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  437. if (!ps_params) {
  438. ret = -ENOMEM;
  439. goto out;
  440. }
  441. ps_params->ps_mode = ps_mode;
  442. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  443. sizeof(*ps_params), 0);
  444. if (ret < 0) {
  445. wl1271_error("cmd set_ps_mode failed");
  446. goto out;
  447. }
  448. out:
  449. kfree(ps_params);
  450. return ret;
  451. }
  452. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  453. void *buf, size_t buf_len, int index, u32 rates)
  454. {
  455. struct wl1271_cmd_template_set *cmd;
  456. int ret = 0;
  457. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  458. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  459. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  460. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  461. if (!cmd) {
  462. ret = -ENOMEM;
  463. goto out;
  464. }
  465. cmd->len = cpu_to_le16(buf_len);
  466. cmd->template_type = template_id;
  467. cmd->enabled_rates = cpu_to_le32(rates);
  468. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  469. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  470. cmd->index = index;
  471. if (buf)
  472. memcpy(cmd->template_data, buf, buf_len);
  473. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  474. if (ret < 0) {
  475. wl1271_warning("cmd set_template failed: %d", ret);
  476. goto out_free;
  477. }
  478. out_free:
  479. kfree(cmd);
  480. out:
  481. return ret;
  482. }
  483. int wl1271_cmd_build_null_data(struct wl1271 *wl)
  484. {
  485. struct sk_buff *skb = NULL;
  486. int size;
  487. void *ptr;
  488. int ret = -ENOMEM;
  489. if (wl->bss_type == BSS_TYPE_IBSS) {
  490. size = sizeof(struct wl12xx_null_data_template);
  491. ptr = NULL;
  492. } else {
  493. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  494. if (!skb)
  495. goto out;
  496. size = skb->len;
  497. ptr = skb->data;
  498. }
  499. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
  500. wl->basic_rate);
  501. out:
  502. dev_kfree_skb(skb);
  503. if (ret)
  504. wl1271_warning("cmd buld null data failed %d", ret);
  505. return ret;
  506. }
  507. int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
  508. {
  509. struct sk_buff *skb = NULL;
  510. int ret = -ENOMEM;
  511. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  512. if (!skb)
  513. goto out;
  514. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
  515. skb->data, skb->len,
  516. CMD_TEMPL_KLV_IDX_NULL_DATA,
  517. wl->basic_rate);
  518. out:
  519. dev_kfree_skb(skb);
  520. if (ret)
  521. wl1271_warning("cmd build klv null data failed %d", ret);
  522. return ret;
  523. }
  524. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
  525. {
  526. struct sk_buff *skb;
  527. int ret = 0;
  528. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  529. if (!skb)
  530. goto out;
  531. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
  532. skb->len, 0, wl->basic_rate_set);
  533. out:
  534. dev_kfree_skb(skb);
  535. return ret;
  536. }
  537. int wl1271_cmd_build_probe_req(struct wl1271 *wl,
  538. const u8 *ssid, size_t ssid_len,
  539. const u8 *ie, size_t ie_len, u8 band)
  540. {
  541. struct sk_buff *skb;
  542. int ret;
  543. skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
  544. ie, ie_len);
  545. if (!skb) {
  546. ret = -ENOMEM;
  547. goto out;
  548. }
  549. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  550. if (band == IEEE80211_BAND_2GHZ)
  551. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  552. skb->data, skb->len, 0,
  553. wl->conf.tx.basic_rate);
  554. else
  555. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  556. skb->data, skb->len, 0,
  557. wl->conf.tx.basic_rate_5);
  558. out:
  559. dev_kfree_skb(skb);
  560. return ret;
  561. }
  562. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  563. struct sk_buff *skb)
  564. {
  565. int ret;
  566. if (!skb)
  567. skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
  568. if (!skb)
  569. goto out;
  570. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  571. if (wl->band == IEEE80211_BAND_2GHZ)
  572. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  573. skb->data, skb->len, 0,
  574. wl->conf.tx.basic_rate);
  575. else
  576. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  577. skb->data, skb->len, 0,
  578. wl->conf.tx.basic_rate_5);
  579. if (ret < 0)
  580. wl1271_error("Unable to set ap probe request template.");
  581. out:
  582. return skb;
  583. }
  584. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
  585. {
  586. int ret;
  587. struct wl12xx_arp_rsp_template tmpl;
  588. struct ieee80211_hdr_3addr *hdr;
  589. struct arphdr *arp_hdr;
  590. memset(&tmpl, 0, sizeof(tmpl));
  591. /* mac80211 header */
  592. hdr = &tmpl.hdr;
  593. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  594. IEEE80211_STYPE_DATA |
  595. IEEE80211_FCTL_TODS);
  596. memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
  597. memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
  598. memset(hdr->addr3, 0xff, ETH_ALEN);
  599. /* llc layer */
  600. memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  601. tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
  602. /* arp header */
  603. arp_hdr = &tmpl.arp_hdr;
  604. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  605. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  606. arp_hdr->ar_hln = ETH_ALEN;
  607. arp_hdr->ar_pln = 4;
  608. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  609. /* arp payload */
  610. memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
  611. tmpl.sender_ip = ip_addr;
  612. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
  613. &tmpl, sizeof(tmpl), 0,
  614. wl->basic_rate);
  615. return ret;
  616. }
  617. int wl1271_build_qos_null_data(struct wl1271 *wl)
  618. {
  619. struct ieee80211_qos_hdr template;
  620. memset(&template, 0, sizeof(template));
  621. memcpy(template.addr1, wl->bssid, ETH_ALEN);
  622. memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
  623. memcpy(template.addr3, wl->bssid, ETH_ALEN);
  624. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  625. IEEE80211_STYPE_QOS_NULLFUNC |
  626. IEEE80211_FCTL_TODS);
  627. /* FIXME: not sure what priority to use here */
  628. template.qos_ctrl = cpu_to_le16(0);
  629. return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
  630. sizeof(template), 0,
  631. wl->basic_rate);
  632. }
  633. int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
  634. {
  635. struct wl1271_cmd_set_sta_keys *cmd;
  636. int ret = 0;
  637. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  638. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  639. if (!cmd) {
  640. ret = -ENOMEM;
  641. goto out;
  642. }
  643. cmd->id = id;
  644. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  645. cmd->key_type = KEY_WEP;
  646. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  647. if (ret < 0) {
  648. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  649. goto out;
  650. }
  651. out:
  652. kfree(cmd);
  653. return ret;
  654. }
  655. int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
  656. {
  657. struct wl1271_cmd_set_ap_keys *cmd;
  658. int ret = 0;
  659. wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
  660. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  661. if (!cmd) {
  662. ret = -ENOMEM;
  663. goto out;
  664. }
  665. cmd->hlid = WL1271_AP_BROADCAST_HLID;
  666. cmd->key_id = id;
  667. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  668. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  669. cmd->key_type = KEY_WEP;
  670. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  671. if (ret < 0) {
  672. wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
  673. goto out;
  674. }
  675. out:
  676. kfree(cmd);
  677. return ret;
  678. }
  679. int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  680. u8 key_size, const u8 *key, const u8 *addr,
  681. u32 tx_seq_32, u16 tx_seq_16)
  682. {
  683. struct wl1271_cmd_set_sta_keys *cmd;
  684. int ret = 0;
  685. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  686. if (!cmd) {
  687. ret = -ENOMEM;
  688. goto out;
  689. }
  690. if (key_type != KEY_WEP)
  691. memcpy(cmd->addr, addr, ETH_ALEN);
  692. cmd->key_action = cpu_to_le16(action);
  693. cmd->key_size = key_size;
  694. cmd->key_type = key_type;
  695. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  696. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  697. /* we have only one SSID profile */
  698. cmd->ssid_profile = 0;
  699. cmd->id = id;
  700. if (key_type == KEY_TKIP) {
  701. /*
  702. * We get the key in the following form:
  703. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  704. * but the target is expecting:
  705. * TKIP - RX MIC - TX MIC
  706. */
  707. memcpy(cmd->key, key, 16);
  708. memcpy(cmd->key + 16, key + 24, 8);
  709. memcpy(cmd->key + 24, key + 16, 8);
  710. } else {
  711. memcpy(cmd->key, key, key_size);
  712. }
  713. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  714. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  715. if (ret < 0) {
  716. wl1271_warning("could not set keys");
  717. goto out;
  718. }
  719. out:
  720. kfree(cmd);
  721. return ret;
  722. }
  723. int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
  724. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  725. u16 tx_seq_16)
  726. {
  727. struct wl1271_cmd_set_ap_keys *cmd;
  728. int ret = 0;
  729. u8 lid_type;
  730. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  731. if (!cmd)
  732. return -ENOMEM;
  733. if (hlid == WL1271_AP_BROADCAST_HLID) {
  734. if (key_type == KEY_WEP)
  735. lid_type = WEP_DEFAULT_LID_TYPE;
  736. else
  737. lid_type = BROADCAST_LID_TYPE;
  738. } else {
  739. lid_type = UNICAST_LID_TYPE;
  740. }
  741. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  742. " hlid: %d", (int)action, (int)id, (int)lid_type,
  743. (int)key_type, (int)hlid);
  744. cmd->lid_key_type = lid_type;
  745. cmd->hlid = hlid;
  746. cmd->key_action = cpu_to_le16(action);
  747. cmd->key_size = key_size;
  748. cmd->key_type = key_type;
  749. cmd->key_id = id;
  750. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  751. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  752. if (key_type == KEY_TKIP) {
  753. /*
  754. * We get the key in the following form:
  755. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  756. * but the target is expecting:
  757. * TKIP - RX MIC - TX MIC
  758. */
  759. memcpy(cmd->key, key, 16);
  760. memcpy(cmd->key + 16, key + 24, 8);
  761. memcpy(cmd->key + 24, key + 16, 8);
  762. } else {
  763. memcpy(cmd->key, key, key_size);
  764. }
  765. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  766. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  767. if (ret < 0) {
  768. wl1271_warning("could not set ap keys");
  769. goto out;
  770. }
  771. out:
  772. kfree(cmd);
  773. return ret;
  774. }
  775. int wl1271_cmd_disconnect(struct wl1271 *wl)
  776. {
  777. struct wl1271_cmd_disconnect *cmd;
  778. int ret = 0;
  779. wl1271_debug(DEBUG_CMD, "cmd disconnect");
  780. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  781. if (!cmd) {
  782. ret = -ENOMEM;
  783. goto out;
  784. }
  785. cmd->rx_config_options = cpu_to_le32(wl->rx_config);
  786. cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
  787. /* disconnect reason is not used in immediate disconnections */
  788. cmd->type = DISCONNECT_IMMEDIATE;
  789. ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
  790. if (ret < 0) {
  791. wl1271_error("failed to send disconnect command");
  792. goto out_free;
  793. }
  794. ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
  795. if (ret < 0)
  796. wl1271_error("cmd disconnect event completion error");
  797. out_free:
  798. kfree(cmd);
  799. out:
  800. return ret;
  801. }
  802. int wl1271_cmd_set_sta_state(struct wl1271 *wl)
  803. {
  804. struct wl1271_cmd_set_sta_state *cmd;
  805. int ret = 0;
  806. wl1271_debug(DEBUG_CMD, "cmd set sta state");
  807. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  808. if (!cmd) {
  809. ret = -ENOMEM;
  810. goto out;
  811. }
  812. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  813. ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
  814. if (ret < 0) {
  815. wl1271_error("failed to send set STA state command");
  816. goto out_free;
  817. }
  818. out_free:
  819. kfree(cmd);
  820. out:
  821. return ret;
  822. }
  823. int wl1271_cmd_start_bss(struct wl1271 *wl)
  824. {
  825. struct wl1271_cmd_bss_start *cmd;
  826. struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
  827. int ret;
  828. wl1271_debug(DEBUG_CMD, "cmd start bss");
  829. /*
  830. * FIXME: We currently do not support hidden SSID. The real SSID
  831. * should be fetched from mac80211 first.
  832. */
  833. if (wl->ssid_len == 0) {
  834. wl1271_warning("Hidden SSID currently not supported for AP");
  835. ret = -EINVAL;
  836. goto out;
  837. }
  838. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  839. if (!cmd) {
  840. ret = -ENOMEM;
  841. goto out;
  842. }
  843. memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
  844. cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
  845. cmd->bss_index = WL1271_AP_BSS_INDEX;
  846. cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
  847. cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
  848. cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
  849. cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
  850. cmd->dtim_interval = bss_conf->dtim_period;
  851. cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  852. cmd->channel = wl->channel;
  853. cmd->ssid_len = wl->ssid_len;
  854. cmd->ssid_type = SSID_TYPE_PUBLIC;
  855. memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
  856. switch (wl->band) {
  857. case IEEE80211_BAND_2GHZ:
  858. cmd->band = RADIO_BAND_2_4GHZ;
  859. break;
  860. case IEEE80211_BAND_5GHZ:
  861. cmd->band = RADIO_BAND_5GHZ;
  862. break;
  863. default:
  864. wl1271_warning("bss start - unknown band: %d", (int)wl->band);
  865. cmd->band = RADIO_BAND_2_4GHZ;
  866. break;
  867. }
  868. ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
  869. if (ret < 0) {
  870. wl1271_error("failed to initiate cmd start bss");
  871. goto out_free;
  872. }
  873. out_free:
  874. kfree(cmd);
  875. out:
  876. return ret;
  877. }
  878. int wl1271_cmd_stop_bss(struct wl1271 *wl)
  879. {
  880. struct wl1271_cmd_bss_start *cmd;
  881. int ret;
  882. wl1271_debug(DEBUG_CMD, "cmd stop bss");
  883. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  884. if (!cmd) {
  885. ret = -ENOMEM;
  886. goto out;
  887. }
  888. cmd->bss_index = WL1271_AP_BSS_INDEX;
  889. ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
  890. if (ret < 0) {
  891. wl1271_error("failed to initiate cmd stop bss");
  892. goto out_free;
  893. }
  894. out_free:
  895. kfree(cmd);
  896. out:
  897. return ret;
  898. }
  899. int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
  900. {
  901. struct wl1271_cmd_add_sta *cmd;
  902. int ret;
  903. wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
  904. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  905. if (!cmd) {
  906. ret = -ENOMEM;
  907. goto out;
  908. }
  909. /* currently we don't support UAPSD */
  910. cmd->sp_len = 0;
  911. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  912. cmd->bss_index = WL1271_AP_BSS_INDEX;
  913. cmd->aid = sta->aid;
  914. cmd->hlid = hlid;
  915. /*
  916. * FIXME: Does STA support QOS? We need to propagate this info from
  917. * hostapd. Currently not that important since this is only used for
  918. * sending the correct flavor of null-data packet in response to a
  919. * trigger.
  920. */
  921. cmd->wmm = 0;
  922. cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
  923. sta->supp_rates[wl->band]));
  924. wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
  925. ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
  926. if (ret < 0) {
  927. wl1271_error("failed to initiate cmd add sta");
  928. goto out_free;
  929. }
  930. out_free:
  931. kfree(cmd);
  932. out:
  933. return ret;
  934. }
  935. int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
  936. {
  937. struct wl1271_cmd_remove_sta *cmd;
  938. int ret;
  939. wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
  940. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  941. if (!cmd) {
  942. ret = -ENOMEM;
  943. goto out;
  944. }
  945. cmd->hlid = hlid;
  946. /* We never send a deauth, mac80211 is in charge of this */
  947. cmd->reason_opcode = 0;
  948. cmd->send_deauth_flag = 0;
  949. ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
  950. if (ret < 0) {
  951. wl1271_error("failed to initiate cmd remove sta");
  952. goto out_free;
  953. }
  954. /*
  955. * We are ok with a timeout here. The event is sometimes not sent
  956. * due to a firmware bug.
  957. */
  958. wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
  959. out_free:
  960. kfree(cmd);
  961. out:
  962. return ret;
  963. }