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. if (wl->band == IEEE80211_BAND_5GHZ)
  233. join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
  234. join->beacon_interval = cpu_to_le16(wl->beacon_int);
  235. join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
  236. join->channel = wl->channel;
  237. join->ssid_len = wl->ssid_len;
  238. memcpy(join->ssid, wl->ssid, wl->ssid_len);
  239. join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
  240. /* reset TX security counters */
  241. wl->tx_security_last_seq = 0;
  242. wl->tx_security_seq = 0;
  243. ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
  244. if (ret < 0) {
  245. wl1271_error("failed to initiate cmd join");
  246. goto out_free;
  247. }
  248. ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
  249. if (ret < 0)
  250. wl1271_error("cmd join event completion error");
  251. out_free:
  252. kfree(join);
  253. out:
  254. return ret;
  255. }
  256. /**
  257. * send test command to firmware
  258. *
  259. * @wl: wl struct
  260. * @buf: buffer containing the command, with all headers, must work with dma
  261. * @len: length of the buffer
  262. * @answer: is answer needed
  263. */
  264. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  265. {
  266. int ret;
  267. size_t res_len = 0;
  268. wl1271_debug(DEBUG_CMD, "cmd test");
  269. if (answer)
  270. res_len = buf_len;
  271. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  272. if (ret < 0) {
  273. wl1271_warning("TEST command failed");
  274. return ret;
  275. }
  276. return ret;
  277. }
  278. /**
  279. * read acx from firmware
  280. *
  281. * @wl: wl struct
  282. * @id: acx id
  283. * @buf: buffer for the response, including all headers, must work with dma
  284. * @len: lenght of buf
  285. */
  286. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  287. {
  288. struct acx_header *acx = buf;
  289. int ret;
  290. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  291. acx->id = cpu_to_le16(id);
  292. /* payload length, does not include any headers */
  293. acx->len = cpu_to_le16(len - sizeof(*acx));
  294. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  295. if (ret < 0)
  296. wl1271_error("INTERROGATE command failed");
  297. return ret;
  298. }
  299. /**
  300. * write acx value to firmware
  301. *
  302. * @wl: wl struct
  303. * @id: acx id
  304. * @buf: buffer containing acx, including all headers, must work with dma
  305. * @len: length of buf
  306. */
  307. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  308. {
  309. struct acx_header *acx = buf;
  310. int ret;
  311. wl1271_debug(DEBUG_CMD, "cmd configure");
  312. acx->id = cpu_to_le16(id);
  313. /* payload length, does not include any headers */
  314. acx->len = cpu_to_le16(len - sizeof(*acx));
  315. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  316. if (ret < 0) {
  317. wl1271_warning("CONFIGURE command NOK");
  318. return ret;
  319. }
  320. return 0;
  321. }
  322. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  323. {
  324. struct cmd_enabledisable_path *cmd;
  325. int ret;
  326. u16 cmd_rx, cmd_tx;
  327. wl1271_debug(DEBUG_CMD, "cmd data path");
  328. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  329. if (!cmd) {
  330. ret = -ENOMEM;
  331. goto out;
  332. }
  333. /* the channel here is only used for calibration, so hardcoded to 1 */
  334. cmd->channel = 1;
  335. if (enable) {
  336. cmd_rx = CMD_ENABLE_RX;
  337. cmd_tx = CMD_ENABLE_TX;
  338. } else {
  339. cmd_rx = CMD_DISABLE_RX;
  340. cmd_tx = CMD_DISABLE_TX;
  341. }
  342. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  343. if (ret < 0) {
  344. wl1271_error("rx %s cmd for channel %d failed",
  345. enable ? "start" : "stop", cmd->channel);
  346. goto out;
  347. }
  348. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  349. enable ? "start" : "stop", cmd->channel);
  350. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  351. if (ret < 0) {
  352. wl1271_error("tx %s cmd for channel %d failed",
  353. enable ? "start" : "stop", cmd->channel);
  354. goto out;
  355. }
  356. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  357. enable ? "start" : "stop", cmd->channel);
  358. out:
  359. kfree(cmd);
  360. return ret;
  361. }
  362. int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, u32 rates, bool send)
  363. {
  364. struct wl1271_cmd_ps_params *ps_params = NULL;
  365. int ret = 0;
  366. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  367. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  368. if (!ps_params) {
  369. ret = -ENOMEM;
  370. goto out;
  371. }
  372. ps_params->ps_mode = ps_mode;
  373. ps_params->send_null_data = send;
  374. ps_params->retries = wl->conf.conn.psm_entry_nullfunc_retries;
  375. ps_params->hang_over_period = wl->conf.conn.psm_entry_hangover_period;
  376. ps_params->null_data_rate = cpu_to_le32(rates);
  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. }