cmd.c 48 KB

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