cmd.c 26 KB

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