cmd.c 40 KB

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