cmd.c 38 KB

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