cmd.c 39 KB

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