cmd.c 40 KB

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