cmd.c 46 KB

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