cmd.c 45 KB

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