cmd.c 40 KB

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