cmd.c 28 KB

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