cmd.c 39 KB

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