cmd.c 26 KB

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