cmd.c 25 KB

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