cmd.c 44 KB

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