cmd.c 40 KB

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