cmd.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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/spi/spi.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ieee80211.h>
  28. #include <linux/slab.h>
  29. #include "wl12xx.h"
  30. #include "reg.h"
  31. #include "io.h"
  32. #include "acx.h"
  33. #include "wl12xx_80211.h"
  34. #include "cmd.h"
  35. #include "event.h"
  36. #include "tx.h"
  37. #define WL1271_CMD_FAST_POLL_COUNT 50
  38. /*
  39. * send command to firmware
  40. *
  41. * @wl: wl struct
  42. * @id: command id
  43. * @buf: buffer containing the command, must work with dma
  44. * @len: length of the buffer
  45. */
  46. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  47. size_t res_len)
  48. {
  49. struct wl1271_cmd_header *cmd;
  50. unsigned long timeout;
  51. u32 intr;
  52. int ret = 0;
  53. u16 status;
  54. u16 poll_count = 0;
  55. cmd = buf;
  56. cmd->id = cpu_to_le16(id);
  57. cmd->status = 0;
  58. WARN_ON(len % 4 != 0);
  59. WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  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 fail;
  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. ret = -EIO;
  85. goto fail;
  86. }
  87. wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
  88. WL1271_ACX_INTR_CMD_COMPLETE);
  89. return 0;
  90. fail:
  91. WARN_ON(1);
  92. wl12xx_queue_recovery_work(wl);
  93. return ret;
  94. }
  95. int wl1271_cmd_general_parms(struct wl1271 *wl)
  96. {
  97. struct wl1271_general_parms_cmd *gen_parms;
  98. struct wl1271_ini_general_params *gp =
  99. &((struct wl1271_nvs_file *)wl->nvs)->general_params;
  100. bool answer = false;
  101. int ret;
  102. if (!wl->nvs)
  103. return -ENODEV;
  104. gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
  105. if (!gen_parms)
  106. return -ENOMEM;
  107. gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
  108. memcpy(&gen_parms->general_params, gp, sizeof(*gp));
  109. if (gp->tx_bip_fem_auto_detect)
  110. answer = true;
  111. /* Override the REF CLK from the NVS with the one from platform data */
  112. gen_parms->general_params.ref_clock = wl->ref_clock;
  113. ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
  114. if (ret < 0) {
  115. wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
  116. goto out;
  117. }
  118. gp->tx_bip_fem_manufacturer =
  119. gen_parms->general_params.tx_bip_fem_manufacturer;
  120. wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
  121. answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
  122. out:
  123. kfree(gen_parms);
  124. return ret;
  125. }
  126. int wl128x_cmd_general_parms(struct wl1271 *wl)
  127. {
  128. struct wl128x_general_parms_cmd *gen_parms;
  129. struct wl128x_ini_general_params *gp =
  130. &((struct wl128x_nvs_file *)wl->nvs)->general_params;
  131. bool answer = false;
  132. int ret;
  133. if (!wl->nvs)
  134. return -ENODEV;
  135. gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
  136. if (!gen_parms)
  137. return -ENOMEM;
  138. gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
  139. memcpy(&gen_parms->general_params, gp, sizeof(*gp));
  140. if (gp->tx_bip_fem_auto_detect)
  141. answer = true;
  142. /* Replace REF and TCXO CLKs with the ones from platform data */
  143. gen_parms->general_params.ref_clock = wl->ref_clock;
  144. gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
  145. ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
  146. if (ret < 0) {
  147. wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
  148. goto out;
  149. }
  150. gp->tx_bip_fem_manufacturer =
  151. gen_parms->general_params.tx_bip_fem_manufacturer;
  152. wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
  153. answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
  154. out:
  155. kfree(gen_parms);
  156. return ret;
  157. }
  158. int wl1271_cmd_radio_parms(struct wl1271 *wl)
  159. {
  160. struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
  161. struct wl1271_radio_parms_cmd *radio_parms;
  162. struct wl1271_ini_general_params *gp = &nvs->general_params;
  163. int ret;
  164. if (!wl->nvs)
  165. return -ENODEV;
  166. radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
  167. if (!radio_parms)
  168. return -ENOMEM;
  169. radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
  170. /* 2.4GHz parameters */
  171. memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
  172. sizeof(struct wl1271_ini_band_params_2));
  173. memcpy(&radio_parms->dyn_params_2,
  174. &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
  175. sizeof(struct wl1271_ini_fem_params_2));
  176. /* 5GHz parameters */
  177. memcpy(&radio_parms->static_params_5,
  178. &nvs->stat_radio_params_5,
  179. sizeof(struct wl1271_ini_band_params_5));
  180. memcpy(&radio_parms->dyn_params_5,
  181. &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
  182. sizeof(struct wl1271_ini_fem_params_5));
  183. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
  184. radio_parms, sizeof(*radio_parms));
  185. ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
  186. if (ret < 0)
  187. wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
  188. kfree(radio_parms);
  189. return ret;
  190. }
  191. int wl128x_cmd_radio_parms(struct wl1271 *wl)
  192. {
  193. struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
  194. struct wl128x_radio_parms_cmd *radio_parms;
  195. struct wl128x_ini_general_params *gp = &nvs->general_params;
  196. int ret;
  197. if (!wl->nvs)
  198. return -ENODEV;
  199. radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
  200. if (!radio_parms)
  201. return -ENOMEM;
  202. radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
  203. /* 2.4GHz parameters */
  204. memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
  205. sizeof(struct wl128x_ini_band_params_2));
  206. memcpy(&radio_parms->dyn_params_2,
  207. &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
  208. sizeof(struct wl128x_ini_fem_params_2));
  209. /* 5GHz parameters */
  210. memcpy(&radio_parms->static_params_5,
  211. &nvs->stat_radio_params_5,
  212. sizeof(struct wl128x_ini_band_params_5));
  213. memcpy(&radio_parms->dyn_params_5,
  214. &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
  215. sizeof(struct wl128x_ini_fem_params_5));
  216. radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
  217. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
  218. radio_parms, sizeof(*radio_parms));
  219. ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
  220. if (ret < 0)
  221. wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
  222. kfree(radio_parms);
  223. return ret;
  224. }
  225. int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
  226. {
  227. struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
  228. struct conf_rf_settings *rf = &wl->conf.rf;
  229. int ret;
  230. if (!wl->nvs)
  231. return -ENODEV;
  232. ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
  233. if (!ext_radio_parms)
  234. return -ENOMEM;
  235. ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
  236. memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
  237. rf->tx_per_channel_power_compensation_2,
  238. CONF_TX_PWR_COMPENSATION_LEN_2);
  239. memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
  240. rf->tx_per_channel_power_compensation_5,
  241. CONF_TX_PWR_COMPENSATION_LEN_5);
  242. wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
  243. ext_radio_parms, sizeof(*ext_radio_parms));
  244. ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
  245. if (ret < 0)
  246. wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
  247. kfree(ext_radio_parms);
  248. return ret;
  249. }
  250. /*
  251. * Poll the mailbox event field until any of the bits in the mask is set or a
  252. * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
  253. */
  254. static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
  255. {
  256. u32 events_vector, event;
  257. unsigned long timeout;
  258. timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
  259. do {
  260. if (time_after(jiffies, timeout)) {
  261. wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
  262. (int)mask);
  263. return -ETIMEDOUT;
  264. }
  265. msleep(1);
  266. /* read from both event fields */
  267. wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
  268. sizeof(events_vector), false);
  269. event = events_vector & mask;
  270. wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
  271. sizeof(events_vector), false);
  272. event |= events_vector & mask;
  273. } while (!event);
  274. return 0;
  275. }
  276. static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
  277. {
  278. int ret;
  279. ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
  280. if (ret != 0) {
  281. wl12xx_queue_recovery_work(wl);
  282. return ret;
  283. }
  284. return 0;
  285. }
  286. int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
  287. u8 *role_id)
  288. {
  289. struct wl12xx_cmd_role_enable *cmd;
  290. int ret;
  291. wl1271_debug(DEBUG_CMD, "cmd role enable");
  292. if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
  293. return -EBUSY;
  294. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  295. if (!cmd) {
  296. ret = -ENOMEM;
  297. goto out;
  298. }
  299. /* get role id */
  300. cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
  301. if (cmd->role_id >= WL12XX_MAX_ROLES) {
  302. ret = -EBUSY;
  303. goto out_free;
  304. }
  305. memcpy(cmd->mac_address, addr, ETH_ALEN);
  306. cmd->role_type = role_type;
  307. ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
  308. if (ret < 0) {
  309. wl1271_error("failed to initiate cmd role enable");
  310. goto out_free;
  311. }
  312. __set_bit(cmd->role_id, wl->roles_map);
  313. *role_id = cmd->role_id;
  314. out_free:
  315. kfree(cmd);
  316. out:
  317. return ret;
  318. }
  319. int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
  320. {
  321. struct wl12xx_cmd_role_disable *cmd;
  322. int ret;
  323. wl1271_debug(DEBUG_CMD, "cmd role disable");
  324. if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
  325. return -ENOENT;
  326. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  327. if (!cmd) {
  328. ret = -ENOMEM;
  329. goto out;
  330. }
  331. cmd->role_id = *role_id;
  332. ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
  333. if (ret < 0) {
  334. wl1271_error("failed to initiate cmd role disable");
  335. goto out_free;
  336. }
  337. __clear_bit(*role_id, wl->roles_map);
  338. *role_id = WL12XX_INVALID_ROLE_ID;
  339. out_free:
  340. kfree(cmd);
  341. out:
  342. return ret;
  343. }
  344. int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  345. {
  346. u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
  347. if (link >= WL12XX_MAX_LINKS)
  348. return -EBUSY;
  349. __set_bit(link, wl->links_map);
  350. __set_bit(link, wlvif->links_map);
  351. *hlid = link;
  352. return 0;
  353. }
  354. void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  355. {
  356. if (*hlid == WL12XX_INVALID_LINK_ID)
  357. return;
  358. __clear_bit(*hlid, wl->links_map);
  359. __clear_bit(*hlid, wlvif->links_map);
  360. *hlid = WL12XX_INVALID_LINK_ID;
  361. }
  362. static int wl12xx_get_new_session_id(struct wl1271 *wl,
  363. struct wl12xx_vif *wlvif)
  364. {
  365. if (wlvif->session_counter >= SESSION_COUNTER_MAX)
  366. wlvif->session_counter = 0;
  367. wlvif->session_counter++;
  368. return wlvif->session_counter;
  369. }
  370. int wl12xx_cmd_role_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  371. {
  372. struct wl12xx_cmd_role_start *cmd;
  373. int ret;
  374. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  375. if (!cmd) {
  376. ret = -ENOMEM;
  377. goto out;
  378. }
  379. wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
  380. cmd->role_id = wlvif->dev_role_id;
  381. if (wl->band == IEEE80211_BAND_5GHZ)
  382. cmd->band = WL12XX_BAND_5GHZ;
  383. cmd->channel = wl->channel;
  384. if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
  385. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
  386. if (ret)
  387. goto out_free;
  388. }
  389. cmd->device.hlid = wlvif->dev_hlid;
  390. cmd->device.session = wlvif->session_counter;
  391. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
  392. cmd->role_id, cmd->device.hlid, cmd->device.session);
  393. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  394. if (ret < 0) {
  395. wl1271_error("failed to initiate cmd role enable");
  396. goto err_hlid;
  397. }
  398. goto out_free;
  399. err_hlid:
  400. /* clear links on error */
  401. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  402. out_free:
  403. kfree(cmd);
  404. out:
  405. return ret;
  406. }
  407. int wl12xx_cmd_role_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  408. {
  409. struct wl12xx_cmd_role_stop *cmd;
  410. int ret;
  411. if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
  412. return -EINVAL;
  413. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  414. if (!cmd) {
  415. ret = -ENOMEM;
  416. goto out;
  417. }
  418. wl1271_debug(DEBUG_CMD, "cmd role stop dev");
  419. cmd->role_id = wlvif->dev_role_id;
  420. cmd->disc_type = DISCONNECT_IMMEDIATE;
  421. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  422. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  423. if (ret < 0) {
  424. wl1271_error("failed to initiate cmd role stop");
  425. goto out_free;
  426. }
  427. ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
  428. if (ret < 0) {
  429. wl1271_error("cmd role stop dev event completion error");
  430. goto out_free;
  431. }
  432. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  433. out_free:
  434. kfree(cmd);
  435. out:
  436. return ret;
  437. }
  438. int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  439. {
  440. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  441. struct wl12xx_cmd_role_start *cmd;
  442. int ret;
  443. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  444. if (!cmd) {
  445. ret = -ENOMEM;
  446. goto out;
  447. }
  448. wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
  449. cmd->role_id = wlvif->role_id;
  450. if (wl->band == IEEE80211_BAND_5GHZ)
  451. cmd->band = WL12XX_BAND_5GHZ;
  452. cmd->channel = wl->channel;
  453. cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  454. cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  455. cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
  456. cmd->sta.ssid_len = wlvif->ssid_len;
  457. memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
  458. memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
  459. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  460. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  461. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  462. if (ret)
  463. goto out_free;
  464. }
  465. cmd->sta.hlid = wlvif->sta.hlid;
  466. cmd->sta.session = wl12xx_get_new_session_id(wl, wlvif);
  467. cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
  468. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  469. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  470. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  471. wlvif->basic_rate_set, wlvif->rate_set);
  472. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  473. if (ret < 0) {
  474. wl1271_error("failed to initiate cmd role start sta");
  475. goto err_hlid;
  476. }
  477. goto out_free;
  478. err_hlid:
  479. /* clear links on error. */
  480. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  481. out_free:
  482. kfree(cmd);
  483. out:
  484. return ret;
  485. }
  486. /* use this function to stop ibss as well */
  487. int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  488. {
  489. struct wl12xx_cmd_role_stop *cmd;
  490. int ret;
  491. if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
  492. return -EINVAL;
  493. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  494. if (!cmd) {
  495. ret = -ENOMEM;
  496. goto out;
  497. }
  498. wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
  499. cmd->role_id = wlvif->role_id;
  500. cmd->disc_type = DISCONNECT_IMMEDIATE;
  501. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  502. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  503. if (ret < 0) {
  504. wl1271_error("failed to initiate cmd role stop sta");
  505. goto out_free;
  506. }
  507. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  508. out_free:
  509. kfree(cmd);
  510. out:
  511. return ret;
  512. }
  513. int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  514. {
  515. struct wl12xx_cmd_role_start *cmd;
  516. struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
  517. int ret;
  518. wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
  519. /* trying to use hidden SSID with an old hostapd version */
  520. if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
  521. wl1271_error("got a null SSID from beacon/bss");
  522. ret = -EINVAL;
  523. goto out;
  524. }
  525. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  526. if (!cmd) {
  527. ret = -ENOMEM;
  528. goto out;
  529. }
  530. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
  531. if (ret < 0)
  532. goto out_free;
  533. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  534. if (ret < 0)
  535. goto out_free_global;
  536. cmd->role_id = wlvif->role_id;
  537. cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
  538. cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
  539. cmd->ap.global_hlid = wlvif->ap.global_hlid;
  540. cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
  541. cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  542. cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  543. cmd->ap.dtim_interval = bss_conf->dtim_period;
  544. cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  545. cmd->channel = wl->channel;
  546. if (!bss_conf->hidden_ssid) {
  547. /* take the SSID from the beacon for backward compatibility */
  548. cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
  549. cmd->ap.ssid_len = wlvif->ssid_len;
  550. memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
  551. } else {
  552. cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
  553. cmd->ap.ssid_len = bss_conf->ssid_len;
  554. memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
  555. }
  556. cmd->ap.local_rates = cpu_to_le32(0xffffffff);
  557. switch (wl->band) {
  558. case IEEE80211_BAND_2GHZ:
  559. cmd->band = RADIO_BAND_2_4GHZ;
  560. break;
  561. case IEEE80211_BAND_5GHZ:
  562. cmd->band = RADIO_BAND_5GHZ;
  563. break;
  564. default:
  565. wl1271_warning("ap start - unknown band: %d", (int)wl->band);
  566. cmd->band = RADIO_BAND_2_4GHZ;
  567. break;
  568. }
  569. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  570. if (ret < 0) {
  571. wl1271_error("failed to initiate cmd role start ap");
  572. goto out_free_bcast;
  573. }
  574. goto out_free;
  575. out_free_bcast:
  576. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  577. out_free_global:
  578. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  579. out_free:
  580. kfree(cmd);
  581. out:
  582. return ret;
  583. }
  584. int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  585. {
  586. struct wl12xx_cmd_role_stop *cmd;
  587. int ret;
  588. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  589. if (!cmd) {
  590. ret = -ENOMEM;
  591. goto out;
  592. }
  593. wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
  594. cmd->role_id = wlvif->role_id;
  595. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  596. if (ret < 0) {
  597. wl1271_error("failed to initiate cmd role stop ap");
  598. goto out_free;
  599. }
  600. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  601. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  602. out_free:
  603. kfree(cmd);
  604. out:
  605. return ret;
  606. }
  607. int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  608. {
  609. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  610. struct wl12xx_cmd_role_start *cmd;
  611. struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
  612. int ret;
  613. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  614. if (!cmd) {
  615. ret = -ENOMEM;
  616. goto out;
  617. }
  618. wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
  619. cmd->role_id = wlvif->role_id;
  620. if (wl->band == IEEE80211_BAND_5GHZ)
  621. cmd->band = WL12XX_BAND_5GHZ;
  622. cmd->channel = wl->channel;
  623. cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  624. cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  625. cmd->ibss.dtim_interval = bss_conf->dtim_period;
  626. cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
  627. cmd->ibss.ssid_len = wlvif->ssid_len;
  628. memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
  629. memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
  630. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  631. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  632. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  633. if (ret)
  634. goto out_free;
  635. }
  636. cmd->ibss.hlid = wlvif->sta.hlid;
  637. cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
  638. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  639. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  640. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  641. wlvif->basic_rate_set, wlvif->rate_set);
  642. wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
  643. vif->bss_conf.bssid);
  644. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  645. if (ret < 0) {
  646. wl1271_error("failed to initiate cmd role enable");
  647. goto err_hlid;
  648. }
  649. goto out_free;
  650. err_hlid:
  651. /* clear links on error. */
  652. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  653. out_free:
  654. kfree(cmd);
  655. out:
  656. return ret;
  657. }
  658. /**
  659. * send test command to firmware
  660. *
  661. * @wl: wl struct
  662. * @buf: buffer containing the command, with all headers, must work with dma
  663. * @len: length of the buffer
  664. * @answer: is answer needed
  665. */
  666. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  667. {
  668. int ret;
  669. size_t res_len = 0;
  670. wl1271_debug(DEBUG_CMD, "cmd test");
  671. if (answer)
  672. res_len = buf_len;
  673. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  674. if (ret < 0) {
  675. wl1271_warning("TEST command failed");
  676. return ret;
  677. }
  678. return ret;
  679. }
  680. /**
  681. * read acx from firmware
  682. *
  683. * @wl: wl struct
  684. * @id: acx id
  685. * @buf: buffer for the response, including all headers, must work with dma
  686. * @len: length of buf
  687. */
  688. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  689. {
  690. struct acx_header *acx = buf;
  691. int ret;
  692. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  693. acx->id = cpu_to_le16(id);
  694. /* payload length, does not include any headers */
  695. acx->len = cpu_to_le16(len - sizeof(*acx));
  696. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  697. if (ret < 0)
  698. wl1271_error("INTERROGATE command failed");
  699. return ret;
  700. }
  701. /**
  702. * write acx value to firmware
  703. *
  704. * @wl: wl struct
  705. * @id: acx id
  706. * @buf: buffer containing acx, including all headers, must work with dma
  707. * @len: length of buf
  708. */
  709. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  710. {
  711. struct acx_header *acx = buf;
  712. int ret;
  713. wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
  714. acx->id = cpu_to_le16(id);
  715. /* payload length, does not include any headers */
  716. acx->len = cpu_to_le16(len - sizeof(*acx));
  717. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  718. if (ret < 0) {
  719. wl1271_warning("CONFIGURE command NOK");
  720. return ret;
  721. }
  722. return 0;
  723. }
  724. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  725. {
  726. struct cmd_enabledisable_path *cmd;
  727. int ret;
  728. u16 cmd_rx, cmd_tx;
  729. wl1271_debug(DEBUG_CMD, "cmd data path");
  730. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  731. if (!cmd) {
  732. ret = -ENOMEM;
  733. goto out;
  734. }
  735. /* the channel here is only used for calibration, so hardcoded to 1 */
  736. cmd->channel = 1;
  737. if (enable) {
  738. cmd_rx = CMD_ENABLE_RX;
  739. cmd_tx = CMD_ENABLE_TX;
  740. } else {
  741. cmd_rx = CMD_DISABLE_RX;
  742. cmd_tx = CMD_DISABLE_TX;
  743. }
  744. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  745. if (ret < 0) {
  746. wl1271_error("rx %s cmd for channel %d failed",
  747. enable ? "start" : "stop", cmd->channel);
  748. goto out;
  749. }
  750. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  751. enable ? "start" : "stop", cmd->channel);
  752. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  753. if (ret < 0) {
  754. wl1271_error("tx %s cmd for channel %d failed",
  755. enable ? "start" : "stop", cmd->channel);
  756. goto out;
  757. }
  758. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  759. enable ? "start" : "stop", cmd->channel);
  760. out:
  761. kfree(cmd);
  762. return ret;
  763. }
  764. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  765. u8 ps_mode)
  766. {
  767. struct wl1271_cmd_ps_params *ps_params = NULL;
  768. int ret = 0;
  769. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  770. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  771. if (!ps_params) {
  772. ret = -ENOMEM;
  773. goto out;
  774. }
  775. ps_params->role_id = wlvif->role_id;
  776. ps_params->ps_mode = ps_mode;
  777. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  778. sizeof(*ps_params), 0);
  779. if (ret < 0) {
  780. wl1271_error("cmd set_ps_mode failed");
  781. goto out;
  782. }
  783. out:
  784. kfree(ps_params);
  785. return ret;
  786. }
  787. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  788. void *buf, size_t buf_len, int index, u32 rates)
  789. {
  790. struct wl1271_cmd_template_set *cmd;
  791. int ret = 0;
  792. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  793. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  794. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  795. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  796. if (!cmd) {
  797. ret = -ENOMEM;
  798. goto out;
  799. }
  800. cmd->len = cpu_to_le16(buf_len);
  801. cmd->template_type = template_id;
  802. cmd->enabled_rates = cpu_to_le32(rates);
  803. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  804. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  805. cmd->index = index;
  806. if (buf)
  807. memcpy(cmd->template_data, buf, buf_len);
  808. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  809. if (ret < 0) {
  810. wl1271_warning("cmd set_template failed: %d", ret);
  811. goto out_free;
  812. }
  813. out_free:
  814. kfree(cmd);
  815. out:
  816. return ret;
  817. }
  818. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  819. {
  820. struct sk_buff *skb = NULL;
  821. int size;
  822. void *ptr;
  823. int ret = -ENOMEM;
  824. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  825. size = sizeof(struct wl12xx_null_data_template);
  826. ptr = NULL;
  827. } else {
  828. skb = ieee80211_nullfunc_get(wl->hw,
  829. wl12xx_wlvif_to_vif(wlvif));
  830. if (!skb)
  831. goto out;
  832. size = skb->len;
  833. ptr = skb->data;
  834. }
  835. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
  836. wlvif->basic_rate);
  837. out:
  838. dev_kfree_skb(skb);
  839. if (ret)
  840. wl1271_warning("cmd buld null data failed %d", ret);
  841. return ret;
  842. }
  843. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  844. struct wl12xx_vif *wlvif)
  845. {
  846. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  847. struct sk_buff *skb = NULL;
  848. int ret = -ENOMEM;
  849. skb = ieee80211_nullfunc_get(wl->hw, vif);
  850. if (!skb)
  851. goto out;
  852. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
  853. skb->data, skb->len,
  854. CMD_TEMPL_KLV_IDX_NULL_DATA,
  855. wlvif->basic_rate);
  856. out:
  857. dev_kfree_skb(skb);
  858. if (ret)
  859. wl1271_warning("cmd build klv null data failed %d", ret);
  860. return ret;
  861. }
  862. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  863. u16 aid)
  864. {
  865. struct sk_buff *skb;
  866. int ret = 0;
  867. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  868. if (!skb)
  869. goto out;
  870. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
  871. skb->len, 0, wlvif->basic_rate_set);
  872. out:
  873. dev_kfree_skb(skb);
  874. return ret;
  875. }
  876. int wl1271_cmd_build_probe_req(struct wl1271 *wl,
  877. const u8 *ssid, size_t ssid_len,
  878. const u8 *ie, size_t ie_len, u8 band)
  879. {
  880. struct sk_buff *skb;
  881. int ret;
  882. u32 rate;
  883. skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
  884. ie, ie_len);
  885. if (!skb) {
  886. ret = -ENOMEM;
  887. goto out;
  888. }
  889. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  890. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
  891. if (band == IEEE80211_BAND_2GHZ)
  892. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  893. skb->data, skb->len, 0, rate);
  894. else
  895. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  896. skb->data, skb->len, 0, rate);
  897. out:
  898. dev_kfree_skb(skb);
  899. return ret;
  900. }
  901. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  902. struct sk_buff *skb)
  903. {
  904. int ret;
  905. u32 rate;
  906. if (!skb)
  907. skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
  908. if (!skb)
  909. goto out;
  910. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  911. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
  912. if (wl->band == IEEE80211_BAND_2GHZ)
  913. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  914. skb->data, skb->len, 0, rate);
  915. else
  916. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  917. skb->data, skb->len, 0, rate);
  918. if (ret < 0)
  919. wl1271_error("Unable to set ap probe request template.");
  920. out:
  921. return skb;
  922. }
  923. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  924. __be32 ip_addr)
  925. {
  926. int ret;
  927. struct wl12xx_arp_rsp_template tmpl;
  928. struct ieee80211_hdr_3addr *hdr;
  929. struct arphdr *arp_hdr;
  930. memset(&tmpl, 0, sizeof(tmpl));
  931. /* mac80211 header */
  932. hdr = &tmpl.hdr;
  933. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  934. IEEE80211_STYPE_DATA |
  935. IEEE80211_FCTL_TODS);
  936. memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
  937. memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
  938. memset(hdr->addr3, 0xff, ETH_ALEN);
  939. /* llc layer */
  940. memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  941. tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
  942. /* arp header */
  943. arp_hdr = &tmpl.arp_hdr;
  944. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  945. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  946. arp_hdr->ar_hln = ETH_ALEN;
  947. arp_hdr->ar_pln = 4;
  948. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  949. /* arp payload */
  950. memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
  951. tmpl.sender_ip = ip_addr;
  952. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
  953. &tmpl, sizeof(tmpl), 0,
  954. wlvif->basic_rate);
  955. return ret;
  956. }
  957. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  958. {
  959. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  960. struct ieee80211_qos_hdr template;
  961. memset(&template, 0, sizeof(template));
  962. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  963. memcpy(template.addr2, vif->addr, ETH_ALEN);
  964. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  965. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  966. IEEE80211_STYPE_QOS_NULLFUNC |
  967. IEEE80211_FCTL_TODS);
  968. /* FIXME: not sure what priority to use here */
  969. template.qos_ctrl = cpu_to_le16(0);
  970. return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
  971. sizeof(template), 0,
  972. wlvif->basic_rate);
  973. }
  974. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  975. {
  976. struct wl1271_cmd_set_keys *cmd;
  977. int ret = 0;
  978. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  979. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  980. if (!cmd) {
  981. ret = -ENOMEM;
  982. goto out;
  983. }
  984. cmd->hlid = hlid;
  985. cmd->key_id = id;
  986. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  987. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  988. cmd->key_type = KEY_WEP;
  989. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  990. if (ret < 0) {
  991. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  992. goto out;
  993. }
  994. out:
  995. kfree(cmd);
  996. return ret;
  997. }
  998. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  999. u16 action, u8 id, u8 key_type,
  1000. u8 key_size, const u8 *key, const u8 *addr,
  1001. u32 tx_seq_32, u16 tx_seq_16)
  1002. {
  1003. struct wl1271_cmd_set_keys *cmd;
  1004. int ret = 0;
  1005. /* hlid might have already been deleted */
  1006. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  1007. return 0;
  1008. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1009. if (!cmd) {
  1010. ret = -ENOMEM;
  1011. goto out;
  1012. }
  1013. cmd->hlid = wlvif->sta.hlid;
  1014. if (key_type == KEY_WEP)
  1015. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1016. else if (is_broadcast_ether_addr(addr))
  1017. cmd->lid_key_type = BROADCAST_LID_TYPE;
  1018. else
  1019. cmd->lid_key_type = UNICAST_LID_TYPE;
  1020. cmd->key_action = cpu_to_le16(action);
  1021. cmd->key_size = key_size;
  1022. cmd->key_type = key_type;
  1023. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1024. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1025. cmd->key_id = id;
  1026. if (key_type == KEY_TKIP) {
  1027. /*
  1028. * We get the key in the following form:
  1029. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1030. * but the target is expecting:
  1031. * TKIP - RX MIC - TX MIC
  1032. */
  1033. memcpy(cmd->key, key, 16);
  1034. memcpy(cmd->key + 16, key + 24, 8);
  1035. memcpy(cmd->key + 24, key + 16, 8);
  1036. } else {
  1037. memcpy(cmd->key, key, key_size);
  1038. }
  1039. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  1040. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1041. if (ret < 0) {
  1042. wl1271_warning("could not set keys");
  1043. goto out;
  1044. }
  1045. out:
  1046. kfree(cmd);
  1047. return ret;
  1048. }
  1049. /*
  1050. * TODO: merge with sta/ibss into 1 set_key function.
  1051. * note there are slight diffs
  1052. */
  1053. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1054. u16 action, u8 id, u8 key_type,
  1055. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1056. u16 tx_seq_16)
  1057. {
  1058. struct wl1271_cmd_set_keys *cmd;
  1059. int ret = 0;
  1060. u8 lid_type;
  1061. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1062. if (!cmd)
  1063. return -ENOMEM;
  1064. if (hlid == wlvif->ap.bcast_hlid) {
  1065. if (key_type == KEY_WEP)
  1066. lid_type = WEP_DEFAULT_LID_TYPE;
  1067. else
  1068. lid_type = BROADCAST_LID_TYPE;
  1069. } else {
  1070. lid_type = UNICAST_LID_TYPE;
  1071. }
  1072. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1073. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1074. (int)key_type, (int)hlid);
  1075. cmd->lid_key_type = lid_type;
  1076. cmd->hlid = hlid;
  1077. cmd->key_action = cpu_to_le16(action);
  1078. cmd->key_size = key_size;
  1079. cmd->key_type = key_type;
  1080. cmd->key_id = id;
  1081. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1082. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1083. if (key_type == KEY_TKIP) {
  1084. /*
  1085. * We get the key in the following form:
  1086. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1087. * but the target is expecting:
  1088. * TKIP - RX MIC - TX MIC
  1089. */
  1090. memcpy(cmd->key, key, 16);
  1091. memcpy(cmd->key + 16, key + 24, 8);
  1092. memcpy(cmd->key + 24, key + 16, 8);
  1093. } else {
  1094. memcpy(cmd->key, key, key_size);
  1095. }
  1096. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1097. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1098. if (ret < 0) {
  1099. wl1271_warning("could not set ap keys");
  1100. goto out;
  1101. }
  1102. out:
  1103. kfree(cmd);
  1104. return ret;
  1105. }
  1106. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
  1107. {
  1108. struct wl12xx_cmd_set_peer_state *cmd;
  1109. int ret = 0;
  1110. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1111. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1112. if (!cmd) {
  1113. ret = -ENOMEM;
  1114. goto out;
  1115. }
  1116. cmd->hlid = hlid;
  1117. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1118. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1119. if (ret < 0) {
  1120. wl1271_error("failed to send set peer state command");
  1121. goto out_free;
  1122. }
  1123. out_free:
  1124. kfree(cmd);
  1125. out:
  1126. return ret;
  1127. }
  1128. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
  1129. {
  1130. struct wl12xx_cmd_add_peer *cmd;
  1131. int i, ret;
  1132. u32 sta_rates;
  1133. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1134. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1135. if (!cmd) {
  1136. ret = -ENOMEM;
  1137. goto out;
  1138. }
  1139. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1140. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1141. cmd->aid = sta->aid;
  1142. cmd->hlid = hlid;
  1143. cmd->sp_len = sta->max_sp;
  1144. cmd->wmm = sta->wme ? 1 : 0;
  1145. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1146. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1147. cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
  1148. else
  1149. cmd->psd_type[i] = WL1271_PSD_LEGACY;
  1150. sta_rates = sta->supp_rates[wl->band];
  1151. if (sta->ht_cap.ht_supported)
  1152. sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
  1153. cmd->supported_rates =
  1154. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1155. wl->band));
  1156. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1157. cmd->supported_rates, sta->uapsd_queues);
  1158. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1159. if (ret < 0) {
  1160. wl1271_error("failed to initiate cmd add peer");
  1161. goto out_free;
  1162. }
  1163. out_free:
  1164. kfree(cmd);
  1165. out:
  1166. return ret;
  1167. }
  1168. int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
  1169. {
  1170. struct wl12xx_cmd_remove_peer *cmd;
  1171. int ret;
  1172. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1173. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1174. if (!cmd) {
  1175. ret = -ENOMEM;
  1176. goto out;
  1177. }
  1178. cmd->hlid = hlid;
  1179. /* We never send a deauth, mac80211 is in charge of this */
  1180. cmd->reason_opcode = 0;
  1181. cmd->send_deauth_flag = 0;
  1182. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1183. if (ret < 0) {
  1184. wl1271_error("failed to initiate cmd remove peer");
  1185. goto out_free;
  1186. }
  1187. /*
  1188. * We are ok with a timeout here. The event is sometimes not sent
  1189. * due to a firmware bug.
  1190. */
  1191. wl1271_cmd_wait_for_event_or_timeout(wl,
  1192. PEER_REMOVE_COMPLETE_EVENT_ID);
  1193. out_free:
  1194. kfree(cmd);
  1195. out:
  1196. return ret;
  1197. }
  1198. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1199. {
  1200. struct wl12xx_cmd_config_fwlog *cmd;
  1201. int ret = 0;
  1202. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1203. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1204. if (!cmd) {
  1205. ret = -ENOMEM;
  1206. goto out;
  1207. }
  1208. cmd->logger_mode = wl->conf.fwlog.mode;
  1209. cmd->log_severity = wl->conf.fwlog.severity;
  1210. cmd->timestamp = wl->conf.fwlog.timestamp;
  1211. cmd->output = wl->conf.fwlog.output;
  1212. cmd->threshold = wl->conf.fwlog.threshold;
  1213. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1214. if (ret < 0) {
  1215. wl1271_error("failed to send config firmware logger command");
  1216. goto out_free;
  1217. }
  1218. out_free:
  1219. kfree(cmd);
  1220. out:
  1221. return ret;
  1222. }
  1223. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1224. {
  1225. struct wl12xx_cmd_start_fwlog *cmd;
  1226. int ret = 0;
  1227. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1228. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1229. if (!cmd) {
  1230. ret = -ENOMEM;
  1231. goto out;
  1232. }
  1233. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1234. if (ret < 0) {
  1235. wl1271_error("failed to send start firmware logger command");
  1236. goto out_free;
  1237. }
  1238. out_free:
  1239. kfree(cmd);
  1240. out:
  1241. return ret;
  1242. }
  1243. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1244. {
  1245. struct wl12xx_cmd_stop_fwlog *cmd;
  1246. int ret = 0;
  1247. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1248. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1249. if (!cmd) {
  1250. ret = -ENOMEM;
  1251. goto out;
  1252. }
  1253. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1254. if (ret < 0) {
  1255. wl1271_error("failed to send stop firmware logger command");
  1256. goto out_free;
  1257. }
  1258. out_free:
  1259. kfree(cmd);
  1260. out:
  1261. return ret;
  1262. }
  1263. static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
  1264. {
  1265. struct wl12xx_cmd_roc *cmd;
  1266. int ret = 0;
  1267. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
  1268. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1269. return -EINVAL;
  1270. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1271. if (!cmd) {
  1272. ret = -ENOMEM;
  1273. goto out;
  1274. }
  1275. cmd->role_id = role_id;
  1276. cmd->channel = wl->channel;
  1277. switch (wl->band) {
  1278. case IEEE80211_BAND_2GHZ:
  1279. cmd->band = RADIO_BAND_2_4GHZ;
  1280. break;
  1281. case IEEE80211_BAND_5GHZ:
  1282. cmd->band = RADIO_BAND_5GHZ;
  1283. break;
  1284. default:
  1285. wl1271_error("roc - unknown band: %d", (int)wl->band);
  1286. ret = -EINVAL;
  1287. goto out_free;
  1288. }
  1289. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1290. if (ret < 0) {
  1291. wl1271_error("failed to send ROC command");
  1292. goto out_free;
  1293. }
  1294. out_free:
  1295. kfree(cmd);
  1296. out:
  1297. return ret;
  1298. }
  1299. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1300. {
  1301. struct wl12xx_cmd_croc *cmd;
  1302. int ret = 0;
  1303. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1304. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1305. if (!cmd) {
  1306. ret = -ENOMEM;
  1307. goto out;
  1308. }
  1309. cmd->role_id = role_id;
  1310. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1311. sizeof(*cmd), 0);
  1312. if (ret < 0) {
  1313. wl1271_error("failed to send ROC command");
  1314. goto out_free;
  1315. }
  1316. out_free:
  1317. kfree(cmd);
  1318. out:
  1319. return ret;
  1320. }
  1321. int wl12xx_roc(struct wl1271 *wl, u8 role_id)
  1322. {
  1323. int ret = 0;
  1324. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1325. return 0;
  1326. ret = wl12xx_cmd_roc(wl, role_id);
  1327. if (ret < 0)
  1328. goto out;
  1329. ret = wl1271_cmd_wait_for_event(wl,
  1330. REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
  1331. if (ret < 0) {
  1332. wl1271_error("cmd roc event completion error");
  1333. goto out;
  1334. }
  1335. __set_bit(role_id, wl->roc_map);
  1336. out:
  1337. return ret;
  1338. }
  1339. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1340. {
  1341. int ret = 0;
  1342. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1343. return 0;
  1344. ret = wl12xx_cmd_croc(wl, role_id);
  1345. if (ret < 0)
  1346. goto out;
  1347. __clear_bit(role_id, wl->roc_map);
  1348. out:
  1349. return ret;
  1350. }
  1351. int wl12xx_cmd_channel_switch(struct wl1271 *wl,
  1352. struct ieee80211_channel_switch *ch_switch)
  1353. {
  1354. struct wl12xx_cmd_channel_switch *cmd;
  1355. int ret;
  1356. wl1271_debug(DEBUG_ACX, "cmd channel switch");
  1357. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1358. if (!cmd) {
  1359. ret = -ENOMEM;
  1360. goto out;
  1361. }
  1362. cmd->channel = ch_switch->channel->hw_value;
  1363. cmd->switch_time = ch_switch->count;
  1364. cmd->tx_suspend = ch_switch->block_tx;
  1365. cmd->flush = 0; /* this value is ignored by the FW */
  1366. ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
  1367. if (ret < 0) {
  1368. wl1271_error("failed to send channel switch command");
  1369. goto out_free;
  1370. }
  1371. out_free:
  1372. kfree(cmd);
  1373. out:
  1374. return ret;
  1375. }
  1376. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
  1377. {
  1378. struct wl12xx_cmd_stop_channel_switch *cmd;
  1379. int ret;
  1380. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1381. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1382. if (!cmd) {
  1383. ret = -ENOMEM;
  1384. goto out;
  1385. }
  1386. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1387. if (ret < 0) {
  1388. wl1271_error("failed to stop channel switch command");
  1389. goto out_free;
  1390. }
  1391. out_free:
  1392. kfree(cmd);
  1393. out:
  1394. return ret;
  1395. }