cmd.c 28 KB

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