cmd.c 45 KB

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