cmd.c 38 KB

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