cmd.c 41 KB

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