cmd.c 40 KB

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