cmd.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. static int wl12xx_allocate_link(struct wl1271 *wl, 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. *hlid = link;
  351. return 0;
  352. }
  353. static void wl12xx_free_link(struct wl1271 *wl, u8 *hlid)
  354. {
  355. if (*hlid == WL12XX_INVALID_LINK_ID)
  356. return;
  357. __clear_bit(*hlid, wl->links_map);
  358. *hlid = WL12XX_INVALID_LINK_ID;
  359. }
  360. static int wl12xx_get_new_session_id(struct wl1271 *wl,
  361. struct wl12xx_vif *wlvif)
  362. {
  363. if (wlvif->session_counter >= SESSION_COUNTER_MAX)
  364. wlvif->session_counter = 0;
  365. wlvif->session_counter++;
  366. return wlvif->session_counter;
  367. }
  368. int wl12xx_cmd_role_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  369. {
  370. struct wl12xx_cmd_role_start *cmd;
  371. int ret;
  372. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  373. if (!cmd) {
  374. ret = -ENOMEM;
  375. goto out;
  376. }
  377. wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
  378. cmd->role_id = wlvif->dev_role_id;
  379. if (wl->band == IEEE80211_BAND_5GHZ)
  380. cmd->band = WL12XX_BAND_5GHZ;
  381. cmd->channel = wl->channel;
  382. if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
  383. ret = wl12xx_allocate_link(wl, &wlvif->dev_hlid);
  384. if (ret)
  385. goto out_free;
  386. }
  387. cmd->device.hlid = wlvif->dev_hlid;
  388. cmd->device.session = wlvif->session_counter;
  389. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
  390. cmd->role_id, cmd->device.hlid, cmd->device.session);
  391. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  392. if (ret < 0) {
  393. wl1271_error("failed to initiate cmd role enable");
  394. goto err_hlid;
  395. }
  396. goto out_free;
  397. err_hlid:
  398. /* clear links on error */
  399. wl12xx_free_link(wl, &wlvif->dev_hlid);
  400. out_free:
  401. kfree(cmd);
  402. out:
  403. return ret;
  404. }
  405. int wl12xx_cmd_role_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  406. {
  407. struct wl12xx_cmd_role_stop *cmd;
  408. int ret;
  409. if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
  410. return -EINVAL;
  411. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  412. if (!cmd) {
  413. ret = -ENOMEM;
  414. goto out;
  415. }
  416. wl1271_debug(DEBUG_CMD, "cmd role stop dev");
  417. cmd->role_id = wlvif->dev_role_id;
  418. cmd->disc_type = DISCONNECT_IMMEDIATE;
  419. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  420. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  421. if (ret < 0) {
  422. wl1271_error("failed to initiate cmd role stop");
  423. goto out_free;
  424. }
  425. ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
  426. if (ret < 0) {
  427. wl1271_error("cmd role stop dev event completion error");
  428. goto out_free;
  429. }
  430. wl12xx_free_link(wl, &wlvif->dev_hlid);
  431. out_free:
  432. kfree(cmd);
  433. out:
  434. return ret;
  435. }
  436. int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  437. {
  438. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  439. struct wl12xx_cmd_role_start *cmd;
  440. int ret;
  441. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  442. if (!cmd) {
  443. ret = -ENOMEM;
  444. goto out;
  445. }
  446. wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
  447. cmd->role_id = wlvif->role_id;
  448. if (wl->band == IEEE80211_BAND_5GHZ)
  449. cmd->band = WL12XX_BAND_5GHZ;
  450. cmd->channel = wl->channel;
  451. cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  452. cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
  453. cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
  454. cmd->sta.ssid_len = wlvif->ssid_len;
  455. memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
  456. memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
  457. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  458. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  459. ret = wl12xx_allocate_link(wl, &wlvif->sta.hlid);
  460. if (ret)
  461. goto out_free;
  462. }
  463. cmd->sta.hlid = wlvif->sta.hlid;
  464. cmd->sta.session = wl12xx_get_new_session_id(wl, wlvif);
  465. cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
  466. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  467. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  468. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  469. wlvif->basic_rate_set, wlvif->rate_set);
  470. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  471. if (ret < 0) {
  472. wl1271_error("failed to initiate cmd role start sta");
  473. goto err_hlid;
  474. }
  475. goto out_free;
  476. err_hlid:
  477. /* clear links on error. */
  478. wl12xx_free_link(wl, &wlvif->sta.hlid);
  479. out_free:
  480. kfree(cmd);
  481. out:
  482. return ret;
  483. }
  484. /* use this function to stop ibss as well */
  485. int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  486. {
  487. struct wl12xx_cmd_role_stop *cmd;
  488. int ret;
  489. if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
  490. return -EINVAL;
  491. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  492. if (!cmd) {
  493. ret = -ENOMEM;
  494. goto out;
  495. }
  496. wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
  497. cmd->role_id = wlvif->role_id;
  498. cmd->disc_type = DISCONNECT_IMMEDIATE;
  499. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  500. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  501. if (ret < 0) {
  502. wl1271_error("failed to initiate cmd role stop sta");
  503. goto out_free;
  504. }
  505. wl12xx_free_link(wl, &wlvif->sta.hlid);
  506. out_free:
  507. kfree(cmd);
  508. out:
  509. return ret;
  510. }
  511. int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  512. {
  513. struct wl12xx_cmd_role_start *cmd;
  514. struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
  515. int ret;
  516. wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
  517. /* trying to use hidden SSID with an old hostapd version */
  518. if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
  519. wl1271_error("got a null SSID from beacon/bss");
  520. ret = -EINVAL;
  521. goto out;
  522. }
  523. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  524. if (!cmd) {
  525. ret = -ENOMEM;
  526. goto out;
  527. }
  528. ret = wl12xx_allocate_link(wl, &wlvif->ap.global_hlid);
  529. if (ret < 0)
  530. goto out_free;
  531. ret = wl12xx_allocate_link(wl, &wlvif->ap.bcast_hlid);
  532. if (ret < 0)
  533. goto out_free_global;
  534. cmd->role_id = wlvif->role_id;
  535. cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
  536. cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
  537. cmd->ap.global_hlid = wlvif->ap.global_hlid;
  538. cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
  539. cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  540. cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
  541. cmd->ap.dtim_interval = bss_conf->dtim_period;
  542. cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  543. cmd->channel = wl->channel;
  544. if (!bss_conf->hidden_ssid) {
  545. /* take the SSID from the beacon for backward compatibility */
  546. cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
  547. cmd->ap.ssid_len = wlvif->ssid_len;
  548. memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
  549. } else {
  550. cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
  551. cmd->ap.ssid_len = bss_conf->ssid_len;
  552. memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
  553. }
  554. cmd->ap.local_rates = cpu_to_le32(0xffffffff);
  555. switch (wl->band) {
  556. case IEEE80211_BAND_2GHZ:
  557. cmd->band = RADIO_BAND_2_4GHZ;
  558. break;
  559. case IEEE80211_BAND_5GHZ:
  560. cmd->band = RADIO_BAND_5GHZ;
  561. break;
  562. default:
  563. wl1271_warning("ap start - unknown band: %d", (int)wl->band);
  564. cmd->band = RADIO_BAND_2_4GHZ;
  565. break;
  566. }
  567. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  568. if (ret < 0) {
  569. wl1271_error("failed to initiate cmd role start ap");
  570. goto out_free_bcast;
  571. }
  572. goto out_free;
  573. out_free_bcast:
  574. wl12xx_free_link(wl, &wlvif->ap.bcast_hlid);
  575. out_free_global:
  576. wl12xx_free_link(wl, &wlvif->ap.global_hlid);
  577. out_free:
  578. kfree(cmd);
  579. out:
  580. return ret;
  581. }
  582. int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  583. {
  584. struct wl12xx_cmd_role_stop *cmd;
  585. int ret;
  586. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  587. if (!cmd) {
  588. ret = -ENOMEM;
  589. goto out;
  590. }
  591. wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
  592. cmd->role_id = wlvif->role_id;
  593. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  594. if (ret < 0) {
  595. wl1271_error("failed to initiate cmd role stop ap");
  596. goto out_free;
  597. }
  598. wl12xx_free_link(wl, &wlvif->ap.bcast_hlid);
  599. wl12xx_free_link(wl, &wlvif->ap.global_hlid);
  600. out_free:
  601. kfree(cmd);
  602. out:
  603. return ret;
  604. }
  605. int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  606. {
  607. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  608. struct wl12xx_cmd_role_start *cmd;
  609. struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
  610. int ret;
  611. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  612. if (!cmd) {
  613. ret = -ENOMEM;
  614. goto out;
  615. }
  616. wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
  617. cmd->role_id = wlvif->role_id;
  618. if (wl->band == IEEE80211_BAND_5GHZ)
  619. cmd->band = WL12XX_BAND_5GHZ;
  620. cmd->channel = wl->channel;
  621. cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  622. cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
  623. cmd->ibss.dtim_interval = bss_conf->dtim_period;
  624. cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
  625. cmd->ibss.ssid_len = wlvif->ssid_len;
  626. memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
  627. memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
  628. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  629. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  630. ret = wl12xx_allocate_link(wl, &wlvif->sta.hlid);
  631. if (ret)
  632. goto out_free;
  633. }
  634. cmd->ibss.hlid = wlvif->sta.hlid;
  635. cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
  636. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  637. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  638. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  639. wlvif->basic_rate_set, wlvif->rate_set);
  640. wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
  641. vif->bss_conf.bssid);
  642. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  643. if (ret < 0) {
  644. wl1271_error("failed to initiate cmd role enable");
  645. goto err_hlid;
  646. }
  647. goto out_free;
  648. err_hlid:
  649. /* clear links on error. */
  650. wl12xx_free_link(wl, &wlvif->sta.hlid);
  651. out_free:
  652. kfree(cmd);
  653. out:
  654. return ret;
  655. }
  656. /**
  657. * send test command to firmware
  658. *
  659. * @wl: wl struct
  660. * @buf: buffer containing the command, with all headers, must work with dma
  661. * @len: length of the buffer
  662. * @answer: is answer needed
  663. */
  664. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  665. {
  666. int ret;
  667. size_t res_len = 0;
  668. wl1271_debug(DEBUG_CMD, "cmd test");
  669. if (answer)
  670. res_len = buf_len;
  671. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  672. if (ret < 0) {
  673. wl1271_warning("TEST command failed");
  674. return ret;
  675. }
  676. return ret;
  677. }
  678. /**
  679. * read acx from firmware
  680. *
  681. * @wl: wl struct
  682. * @id: acx id
  683. * @buf: buffer for the response, including all headers, must work with dma
  684. * @len: length of buf
  685. */
  686. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  687. {
  688. struct acx_header *acx = buf;
  689. int ret;
  690. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  691. acx->id = cpu_to_le16(id);
  692. /* payload length, does not include any headers */
  693. acx->len = cpu_to_le16(len - sizeof(*acx));
  694. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  695. if (ret < 0)
  696. wl1271_error("INTERROGATE command failed");
  697. return ret;
  698. }
  699. /**
  700. * write acx value to firmware
  701. *
  702. * @wl: wl struct
  703. * @id: acx id
  704. * @buf: buffer containing acx, including all headers, must work with dma
  705. * @len: length of buf
  706. */
  707. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  708. {
  709. struct acx_header *acx = buf;
  710. int ret;
  711. wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
  712. acx->id = cpu_to_le16(id);
  713. /* payload length, does not include any headers */
  714. acx->len = cpu_to_le16(len - sizeof(*acx));
  715. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  716. if (ret < 0) {
  717. wl1271_warning("CONFIGURE command NOK");
  718. return ret;
  719. }
  720. return 0;
  721. }
  722. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  723. {
  724. struct cmd_enabledisable_path *cmd;
  725. int ret;
  726. u16 cmd_rx, cmd_tx;
  727. wl1271_debug(DEBUG_CMD, "cmd data path");
  728. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  729. if (!cmd) {
  730. ret = -ENOMEM;
  731. goto out;
  732. }
  733. /* the channel here is only used for calibration, so hardcoded to 1 */
  734. cmd->channel = 1;
  735. if (enable) {
  736. cmd_rx = CMD_ENABLE_RX;
  737. cmd_tx = CMD_ENABLE_TX;
  738. } else {
  739. cmd_rx = CMD_DISABLE_RX;
  740. cmd_tx = CMD_DISABLE_TX;
  741. }
  742. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  743. if (ret < 0) {
  744. wl1271_error("rx %s cmd for channel %d failed",
  745. enable ? "start" : "stop", cmd->channel);
  746. goto out;
  747. }
  748. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  749. enable ? "start" : "stop", cmd->channel);
  750. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  751. if (ret < 0) {
  752. wl1271_error("tx %s cmd for channel %d failed",
  753. enable ? "start" : "stop", cmd->channel);
  754. goto out;
  755. }
  756. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  757. enable ? "start" : "stop", cmd->channel);
  758. out:
  759. kfree(cmd);
  760. return ret;
  761. }
  762. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  763. u8 ps_mode)
  764. {
  765. struct wl1271_cmd_ps_params *ps_params = NULL;
  766. int ret = 0;
  767. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  768. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  769. if (!ps_params) {
  770. ret = -ENOMEM;
  771. goto out;
  772. }
  773. ps_params->role_id = wlvif->role_id;
  774. ps_params->ps_mode = ps_mode;
  775. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  776. sizeof(*ps_params), 0);
  777. if (ret < 0) {
  778. wl1271_error("cmd set_ps_mode failed");
  779. goto out;
  780. }
  781. out:
  782. kfree(ps_params);
  783. return ret;
  784. }
  785. int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
  786. void *buf, size_t buf_len, int index, u32 rates)
  787. {
  788. struct wl1271_cmd_template_set *cmd;
  789. int ret = 0;
  790. wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
  791. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  792. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  793. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  794. if (!cmd) {
  795. ret = -ENOMEM;
  796. goto out;
  797. }
  798. cmd->len = cpu_to_le16(buf_len);
  799. cmd->template_type = template_id;
  800. cmd->enabled_rates = cpu_to_le32(rates);
  801. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  802. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  803. cmd->index = index;
  804. if (buf)
  805. memcpy(cmd->template_data, buf, buf_len);
  806. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  807. if (ret < 0) {
  808. wl1271_warning("cmd set_template failed: %d", ret);
  809. goto out_free;
  810. }
  811. out_free:
  812. kfree(cmd);
  813. out:
  814. return ret;
  815. }
  816. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  817. {
  818. struct sk_buff *skb = NULL;
  819. int size;
  820. void *ptr;
  821. int ret = -ENOMEM;
  822. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  823. size = sizeof(struct wl12xx_null_data_template);
  824. ptr = NULL;
  825. } else {
  826. skb = ieee80211_nullfunc_get(wl->hw,
  827. wl12xx_wlvif_to_vif(wlvif));
  828. if (!skb)
  829. goto out;
  830. size = skb->len;
  831. ptr = skb->data;
  832. }
  833. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
  834. wlvif->basic_rate);
  835. out:
  836. dev_kfree_skb(skb);
  837. if (ret)
  838. wl1271_warning("cmd buld null data failed %d", ret);
  839. return ret;
  840. }
  841. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  842. struct wl12xx_vif *wlvif)
  843. {
  844. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  845. struct sk_buff *skb = NULL;
  846. int ret = -ENOMEM;
  847. skb = ieee80211_nullfunc_get(wl->hw, vif);
  848. if (!skb)
  849. goto out;
  850. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
  851. skb->data, skb->len,
  852. CMD_TEMPL_KLV_IDX_NULL_DATA,
  853. wlvif->basic_rate);
  854. out:
  855. dev_kfree_skb(skb);
  856. if (ret)
  857. wl1271_warning("cmd build klv null data failed %d", ret);
  858. return ret;
  859. }
  860. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  861. u16 aid)
  862. {
  863. struct sk_buff *skb;
  864. int ret = 0;
  865. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  866. if (!skb)
  867. goto out;
  868. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
  869. skb->len, 0, wlvif->basic_rate_set);
  870. out:
  871. dev_kfree_skb(skb);
  872. return ret;
  873. }
  874. int wl1271_cmd_build_probe_req(struct wl1271 *wl,
  875. const u8 *ssid, size_t ssid_len,
  876. const u8 *ie, size_t ie_len, u8 band)
  877. {
  878. struct sk_buff *skb;
  879. int ret;
  880. u32 rate;
  881. skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
  882. ie, ie_len);
  883. if (!skb) {
  884. ret = -ENOMEM;
  885. goto out;
  886. }
  887. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  888. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
  889. if (band == IEEE80211_BAND_2GHZ)
  890. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  891. skb->data, skb->len, 0, rate);
  892. else
  893. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  894. skb->data, skb->len, 0, rate);
  895. out:
  896. dev_kfree_skb(skb);
  897. return ret;
  898. }
  899. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  900. struct sk_buff *skb)
  901. {
  902. int ret;
  903. u32 rate;
  904. if (!skb)
  905. skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
  906. if (!skb)
  907. goto out;
  908. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  909. rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
  910. if (wl->band == IEEE80211_BAND_2GHZ)
  911. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
  912. skb->data, skb->len, 0, rate);
  913. else
  914. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
  915. skb->data, skb->len, 0, rate);
  916. if (ret < 0)
  917. wl1271_error("Unable to set ap probe request template.");
  918. out:
  919. return skb;
  920. }
  921. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  922. __be32 ip_addr)
  923. {
  924. int ret;
  925. struct wl12xx_arp_rsp_template tmpl;
  926. struct ieee80211_hdr_3addr *hdr;
  927. struct arphdr *arp_hdr;
  928. memset(&tmpl, 0, sizeof(tmpl));
  929. /* mac80211 header */
  930. hdr = &tmpl.hdr;
  931. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  932. IEEE80211_STYPE_DATA |
  933. IEEE80211_FCTL_TODS);
  934. memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
  935. memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
  936. memset(hdr->addr3, 0xff, ETH_ALEN);
  937. /* llc layer */
  938. memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  939. tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
  940. /* arp header */
  941. arp_hdr = &tmpl.arp_hdr;
  942. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  943. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  944. arp_hdr->ar_hln = ETH_ALEN;
  945. arp_hdr->ar_pln = 4;
  946. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  947. /* arp payload */
  948. memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
  949. tmpl.sender_ip = ip_addr;
  950. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
  951. &tmpl, sizeof(tmpl), 0,
  952. wlvif->basic_rate);
  953. return ret;
  954. }
  955. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  956. {
  957. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  958. struct ieee80211_qos_hdr template;
  959. memset(&template, 0, sizeof(template));
  960. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  961. memcpy(template.addr2, vif->addr, ETH_ALEN);
  962. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  963. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  964. IEEE80211_STYPE_QOS_NULLFUNC |
  965. IEEE80211_FCTL_TODS);
  966. /* FIXME: not sure what priority to use here */
  967. template.qos_ctrl = cpu_to_le16(0);
  968. return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
  969. sizeof(template), 0,
  970. wlvif->basic_rate);
  971. }
  972. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  973. {
  974. struct wl1271_cmd_set_keys *cmd;
  975. int ret = 0;
  976. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  977. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  978. if (!cmd) {
  979. ret = -ENOMEM;
  980. goto out;
  981. }
  982. cmd->hlid = hlid;
  983. cmd->key_id = id;
  984. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  985. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  986. cmd->key_type = KEY_WEP;
  987. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  988. if (ret < 0) {
  989. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  990. goto out;
  991. }
  992. out:
  993. kfree(cmd);
  994. return ret;
  995. }
  996. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  997. u16 action, u8 id, u8 key_type,
  998. u8 key_size, const u8 *key, const u8 *addr,
  999. u32 tx_seq_32, u16 tx_seq_16)
  1000. {
  1001. struct wl1271_cmd_set_keys *cmd;
  1002. int ret = 0;
  1003. /* hlid might have already been deleted */
  1004. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  1005. return 0;
  1006. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1007. if (!cmd) {
  1008. ret = -ENOMEM;
  1009. goto out;
  1010. }
  1011. cmd->hlid = wlvif->sta.hlid;
  1012. if (key_type == KEY_WEP)
  1013. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1014. else if (is_broadcast_ether_addr(addr))
  1015. cmd->lid_key_type = BROADCAST_LID_TYPE;
  1016. else
  1017. cmd->lid_key_type = UNICAST_LID_TYPE;
  1018. cmd->key_action = cpu_to_le16(action);
  1019. cmd->key_size = key_size;
  1020. cmd->key_type = key_type;
  1021. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1022. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1023. cmd->key_id = id;
  1024. if (key_type == KEY_TKIP) {
  1025. /*
  1026. * We get the key in the following form:
  1027. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1028. * but the target is expecting:
  1029. * TKIP - RX MIC - TX MIC
  1030. */
  1031. memcpy(cmd->key, key, 16);
  1032. memcpy(cmd->key + 16, key + 24, 8);
  1033. memcpy(cmd->key + 24, key + 16, 8);
  1034. } else {
  1035. memcpy(cmd->key, key, key_size);
  1036. }
  1037. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  1038. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1039. if (ret < 0) {
  1040. wl1271_warning("could not set keys");
  1041. goto out;
  1042. }
  1043. out:
  1044. kfree(cmd);
  1045. return ret;
  1046. }
  1047. /*
  1048. * TODO: merge with sta/ibss into 1 set_key function.
  1049. * note there are slight diffs
  1050. */
  1051. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1052. u16 action, u8 id, u8 key_type,
  1053. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1054. u16 tx_seq_16)
  1055. {
  1056. struct wl1271_cmd_set_keys *cmd;
  1057. int ret = 0;
  1058. u8 lid_type;
  1059. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1060. if (!cmd)
  1061. return -ENOMEM;
  1062. if (hlid == wlvif->ap.bcast_hlid) {
  1063. if (key_type == KEY_WEP)
  1064. lid_type = WEP_DEFAULT_LID_TYPE;
  1065. else
  1066. lid_type = BROADCAST_LID_TYPE;
  1067. } else {
  1068. lid_type = UNICAST_LID_TYPE;
  1069. }
  1070. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1071. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1072. (int)key_type, (int)hlid);
  1073. cmd->lid_key_type = lid_type;
  1074. cmd->hlid = hlid;
  1075. cmd->key_action = cpu_to_le16(action);
  1076. cmd->key_size = key_size;
  1077. cmd->key_type = key_type;
  1078. cmd->key_id = id;
  1079. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1080. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1081. if (key_type == KEY_TKIP) {
  1082. /*
  1083. * We get the key in the following form:
  1084. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1085. * but the target is expecting:
  1086. * TKIP - RX MIC - TX MIC
  1087. */
  1088. memcpy(cmd->key, key, 16);
  1089. memcpy(cmd->key + 16, key + 24, 8);
  1090. memcpy(cmd->key + 24, key + 16, 8);
  1091. } else {
  1092. memcpy(cmd->key, key, key_size);
  1093. }
  1094. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1095. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1096. if (ret < 0) {
  1097. wl1271_warning("could not set ap keys");
  1098. goto out;
  1099. }
  1100. out:
  1101. kfree(cmd);
  1102. return ret;
  1103. }
  1104. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
  1105. {
  1106. struct wl12xx_cmd_set_peer_state *cmd;
  1107. int ret = 0;
  1108. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1109. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1110. if (!cmd) {
  1111. ret = -ENOMEM;
  1112. goto out;
  1113. }
  1114. cmd->hlid = hlid;
  1115. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1116. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1117. if (ret < 0) {
  1118. wl1271_error("failed to send set peer state command");
  1119. goto out_free;
  1120. }
  1121. out_free:
  1122. kfree(cmd);
  1123. out:
  1124. return ret;
  1125. }
  1126. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
  1127. {
  1128. struct wl12xx_cmd_add_peer *cmd;
  1129. int i, ret;
  1130. u32 sta_rates;
  1131. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1132. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1133. if (!cmd) {
  1134. ret = -ENOMEM;
  1135. goto out;
  1136. }
  1137. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1138. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1139. cmd->aid = sta->aid;
  1140. cmd->hlid = hlid;
  1141. cmd->sp_len = sta->max_sp;
  1142. cmd->wmm = sta->wme ? 1 : 0;
  1143. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1144. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1145. cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
  1146. else
  1147. cmd->psd_type[i] = WL1271_PSD_LEGACY;
  1148. sta_rates = sta->supp_rates[wl->band];
  1149. if (sta->ht_cap.ht_supported)
  1150. sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
  1151. cmd->supported_rates =
  1152. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1153. wl->band));
  1154. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1155. cmd->supported_rates, sta->uapsd_queues);
  1156. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1157. if (ret < 0) {
  1158. wl1271_error("failed to initiate cmd add peer");
  1159. goto out_free;
  1160. }
  1161. out_free:
  1162. kfree(cmd);
  1163. out:
  1164. return ret;
  1165. }
  1166. int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
  1167. {
  1168. struct wl12xx_cmd_remove_peer *cmd;
  1169. int ret;
  1170. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1171. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1172. if (!cmd) {
  1173. ret = -ENOMEM;
  1174. goto out;
  1175. }
  1176. cmd->hlid = hlid;
  1177. /* We never send a deauth, mac80211 is in charge of this */
  1178. cmd->reason_opcode = 0;
  1179. cmd->send_deauth_flag = 0;
  1180. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1181. if (ret < 0) {
  1182. wl1271_error("failed to initiate cmd remove peer");
  1183. goto out_free;
  1184. }
  1185. /*
  1186. * We are ok with a timeout here. The event is sometimes not sent
  1187. * due to a firmware bug.
  1188. */
  1189. wl1271_cmd_wait_for_event_or_timeout(wl,
  1190. PEER_REMOVE_COMPLETE_EVENT_ID);
  1191. out_free:
  1192. kfree(cmd);
  1193. out:
  1194. return ret;
  1195. }
  1196. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1197. {
  1198. struct wl12xx_cmd_config_fwlog *cmd;
  1199. int ret = 0;
  1200. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1201. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1202. if (!cmd) {
  1203. ret = -ENOMEM;
  1204. goto out;
  1205. }
  1206. cmd->logger_mode = wl->conf.fwlog.mode;
  1207. cmd->log_severity = wl->conf.fwlog.severity;
  1208. cmd->timestamp = wl->conf.fwlog.timestamp;
  1209. cmd->output = wl->conf.fwlog.output;
  1210. cmd->threshold = wl->conf.fwlog.threshold;
  1211. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1212. if (ret < 0) {
  1213. wl1271_error("failed to send config firmware logger command");
  1214. goto out_free;
  1215. }
  1216. out_free:
  1217. kfree(cmd);
  1218. out:
  1219. return ret;
  1220. }
  1221. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1222. {
  1223. struct wl12xx_cmd_start_fwlog *cmd;
  1224. int ret = 0;
  1225. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1226. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1227. if (!cmd) {
  1228. ret = -ENOMEM;
  1229. goto out;
  1230. }
  1231. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1232. if (ret < 0) {
  1233. wl1271_error("failed to send start firmware logger command");
  1234. goto out_free;
  1235. }
  1236. out_free:
  1237. kfree(cmd);
  1238. out:
  1239. return ret;
  1240. }
  1241. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1242. {
  1243. struct wl12xx_cmd_stop_fwlog *cmd;
  1244. int ret = 0;
  1245. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1246. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1247. if (!cmd) {
  1248. ret = -ENOMEM;
  1249. goto out;
  1250. }
  1251. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1252. if (ret < 0) {
  1253. wl1271_error("failed to send stop firmware logger command");
  1254. goto out_free;
  1255. }
  1256. out_free:
  1257. kfree(cmd);
  1258. out:
  1259. return ret;
  1260. }
  1261. static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
  1262. {
  1263. struct wl12xx_cmd_roc *cmd;
  1264. int ret = 0;
  1265. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
  1266. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1267. return -EINVAL;
  1268. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1269. if (!cmd) {
  1270. ret = -ENOMEM;
  1271. goto out;
  1272. }
  1273. cmd->role_id = role_id;
  1274. cmd->channel = wl->channel;
  1275. switch (wl->band) {
  1276. case IEEE80211_BAND_2GHZ:
  1277. cmd->band = RADIO_BAND_2_4GHZ;
  1278. break;
  1279. case IEEE80211_BAND_5GHZ:
  1280. cmd->band = RADIO_BAND_5GHZ;
  1281. break;
  1282. default:
  1283. wl1271_error("roc - unknown band: %d", (int)wl->band);
  1284. ret = -EINVAL;
  1285. goto out_free;
  1286. }
  1287. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1288. if (ret < 0) {
  1289. wl1271_error("failed to send ROC command");
  1290. goto out_free;
  1291. }
  1292. out_free:
  1293. kfree(cmd);
  1294. out:
  1295. return ret;
  1296. }
  1297. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1298. {
  1299. struct wl12xx_cmd_croc *cmd;
  1300. int ret = 0;
  1301. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1302. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1303. if (!cmd) {
  1304. ret = -ENOMEM;
  1305. goto out;
  1306. }
  1307. cmd->role_id = role_id;
  1308. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1309. sizeof(*cmd), 0);
  1310. if (ret < 0) {
  1311. wl1271_error("failed to send ROC command");
  1312. goto out_free;
  1313. }
  1314. out_free:
  1315. kfree(cmd);
  1316. out:
  1317. return ret;
  1318. }
  1319. int wl12xx_roc(struct wl1271 *wl, u8 role_id)
  1320. {
  1321. int ret = 0;
  1322. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1323. return 0;
  1324. ret = wl12xx_cmd_roc(wl, role_id);
  1325. if (ret < 0)
  1326. goto out;
  1327. ret = wl1271_cmd_wait_for_event(wl,
  1328. REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
  1329. if (ret < 0) {
  1330. wl1271_error("cmd roc event completion error");
  1331. goto out;
  1332. }
  1333. __set_bit(role_id, wl->roc_map);
  1334. out:
  1335. return ret;
  1336. }
  1337. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1338. {
  1339. int ret = 0;
  1340. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1341. return 0;
  1342. ret = wl12xx_cmd_croc(wl, role_id);
  1343. if (ret < 0)
  1344. goto out;
  1345. __clear_bit(role_id, wl->roc_map);
  1346. out:
  1347. return ret;
  1348. }
  1349. int wl12xx_cmd_channel_switch(struct wl1271 *wl,
  1350. struct ieee80211_channel_switch *ch_switch)
  1351. {
  1352. struct wl12xx_cmd_channel_switch *cmd;
  1353. int ret;
  1354. wl1271_debug(DEBUG_ACX, "cmd channel switch");
  1355. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1356. if (!cmd) {
  1357. ret = -ENOMEM;
  1358. goto out;
  1359. }
  1360. cmd->channel = ch_switch->channel->hw_value;
  1361. cmd->switch_time = ch_switch->count;
  1362. cmd->tx_suspend = ch_switch->block_tx;
  1363. cmd->flush = 0; /* this value is ignored by the FW */
  1364. ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
  1365. if (ret < 0) {
  1366. wl1271_error("failed to send channel switch command");
  1367. goto out_free;
  1368. }
  1369. out_free:
  1370. kfree(cmd);
  1371. out:
  1372. return ret;
  1373. }
  1374. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
  1375. {
  1376. struct wl12xx_cmd_stop_channel_switch *cmd;
  1377. int ret;
  1378. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1379. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1380. if (!cmd) {
  1381. ret = -ENOMEM;
  1382. goto out;
  1383. }
  1384. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1385. if (ret < 0) {
  1386. wl1271_error("failed to stop channel switch command");
  1387. goto out_free;
  1388. }
  1389. out_free:
  1390. kfree(cmd);
  1391. out:
  1392. return ret;
  1393. }