cmd.c 45 KB

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