cmd.c 39 KB

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