cmd.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  659. u8 ps_mode, u16 auto_ps_timeout)
  660. {
  661. struct wl1271_cmd_ps_params *ps_params = NULL;
  662. int ret = 0;
  663. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  664. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  665. if (!ps_params) {
  666. ret = -ENOMEM;
  667. goto out;
  668. }
  669. ps_params->role_id = wlvif->role_id;
  670. ps_params->ps_mode = ps_mode;
  671. ps_params->auto_ps_timeout = auto_ps_timeout;
  672. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  673. sizeof(*ps_params), 0);
  674. if (ret < 0) {
  675. wl1271_error("cmd set_ps_mode failed");
  676. goto out;
  677. }
  678. out:
  679. kfree(ps_params);
  680. return ret;
  681. }
  682. int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
  683. u16 template_id, void *buf, size_t buf_len,
  684. int index, u32 rates)
  685. {
  686. struct wl1271_cmd_template_set *cmd;
  687. int ret = 0;
  688. wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
  689. template_id, role_id);
  690. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  691. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  692. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  693. if (!cmd) {
  694. ret = -ENOMEM;
  695. goto out;
  696. }
  697. /* during initialization wlvif is NULL */
  698. cmd->role_id = role_id;
  699. cmd->len = cpu_to_le16(buf_len);
  700. cmd->template_type = template_id;
  701. cmd->enabled_rates = cpu_to_le32(rates);
  702. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  703. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  704. cmd->index = index;
  705. if (buf)
  706. memcpy(cmd->template_data, buf, buf_len);
  707. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  708. if (ret < 0) {
  709. wl1271_warning("cmd set_template failed: %d", ret);
  710. goto out_free;
  711. }
  712. out_free:
  713. kfree(cmd);
  714. out:
  715. return ret;
  716. }
  717. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  718. {
  719. struct sk_buff *skb = NULL;
  720. int size;
  721. void *ptr;
  722. int ret = -ENOMEM;
  723. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  724. size = sizeof(struct wl12xx_null_data_template);
  725. ptr = NULL;
  726. } else {
  727. skb = ieee80211_nullfunc_get(wl->hw,
  728. wl12xx_wlvif_to_vif(wlvif));
  729. if (!skb)
  730. goto out;
  731. size = skb->len;
  732. ptr = skb->data;
  733. }
  734. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  735. CMD_TEMPL_NULL_DATA, ptr, size, 0,
  736. wlvif->basic_rate);
  737. out:
  738. dev_kfree_skb(skb);
  739. if (ret)
  740. wl1271_warning("cmd buld null data failed %d", ret);
  741. return ret;
  742. }
  743. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  744. struct wl12xx_vif *wlvif)
  745. {
  746. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  747. struct sk_buff *skb = NULL;
  748. int ret = -ENOMEM;
  749. skb = ieee80211_nullfunc_get(wl->hw, vif);
  750. if (!skb)
  751. goto out;
  752. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
  753. skb->data, skb->len,
  754. CMD_TEMPL_KLV_IDX_NULL_DATA,
  755. wlvif->basic_rate);
  756. out:
  757. dev_kfree_skb(skb);
  758. if (ret)
  759. wl1271_warning("cmd build klv null data failed %d", ret);
  760. return ret;
  761. }
  762. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  763. u16 aid)
  764. {
  765. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  766. struct sk_buff *skb;
  767. int ret = 0;
  768. skb = ieee80211_pspoll_get(wl->hw, vif);
  769. if (!skb)
  770. goto out;
  771. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  772. CMD_TEMPL_PS_POLL, skb->data,
  773. skb->len, 0, wlvif->basic_rate_set);
  774. out:
  775. dev_kfree_skb(skb);
  776. return ret;
  777. }
  778. int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  779. u8 role_id, u8 band,
  780. const u8 *ssid, size_t ssid_len,
  781. const u8 *ie, size_t ie_len)
  782. {
  783. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  784. struct sk_buff *skb;
  785. int ret;
  786. u32 rate;
  787. skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
  788. ie, ie_len);
  789. if (!skb) {
  790. ret = -ENOMEM;
  791. goto out;
  792. }
  793. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  794. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  795. if (band == IEEE80211_BAND_2GHZ)
  796. ret = wl1271_cmd_template_set(wl, role_id,
  797. CMD_TEMPL_CFG_PROBE_REQ_2_4,
  798. skb->data, skb->len, 0, rate);
  799. else
  800. ret = wl1271_cmd_template_set(wl, role_id,
  801. CMD_TEMPL_CFG_PROBE_REQ_5,
  802. skb->data, skb->len, 0, rate);
  803. out:
  804. dev_kfree_skb(skb);
  805. return ret;
  806. }
  807. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  808. struct wl12xx_vif *wlvif,
  809. struct sk_buff *skb)
  810. {
  811. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  812. int ret;
  813. u32 rate;
  814. if (!skb)
  815. skb = ieee80211_ap_probereq_get(wl->hw, vif);
  816. if (!skb)
  817. goto out;
  818. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  819. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
  820. if (wlvif->band == IEEE80211_BAND_2GHZ)
  821. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  822. CMD_TEMPL_CFG_PROBE_REQ_2_4,
  823. skb->data, skb->len, 0, rate);
  824. else
  825. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  826. CMD_TEMPL_CFG_PROBE_REQ_5,
  827. skb->data, skb->len, 0, rate);
  828. if (ret < 0)
  829. wl1271_error("Unable to set ap probe request template.");
  830. out:
  831. return skb;
  832. }
  833. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  834. {
  835. int ret, extra;
  836. u16 fc;
  837. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  838. struct sk_buff *skb;
  839. struct wl12xx_arp_rsp_template *tmpl;
  840. struct ieee80211_hdr_3addr *hdr;
  841. struct arphdr *arp_hdr;
  842. skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
  843. WL1271_EXTRA_SPACE_MAX);
  844. if (!skb) {
  845. wl1271_error("failed to allocate buffer for arp rsp template");
  846. return -ENOMEM;
  847. }
  848. skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
  849. tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
  850. memset(tmpl, 0, sizeof(*tmpl));
  851. /* llc layer */
  852. memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  853. tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
  854. /* arp header */
  855. arp_hdr = &tmpl->arp_hdr;
  856. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  857. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  858. arp_hdr->ar_hln = ETH_ALEN;
  859. arp_hdr->ar_pln = 4;
  860. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  861. /* arp payload */
  862. memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
  863. tmpl->sender_ip = wlvif->ip_addr;
  864. /* encryption space */
  865. switch (wlvif->encryption_type) {
  866. case KEY_TKIP:
  867. extra = WL1271_EXTRA_SPACE_TKIP;
  868. break;
  869. case KEY_AES:
  870. extra = WL1271_EXTRA_SPACE_AES;
  871. break;
  872. case KEY_NONE:
  873. case KEY_WEP:
  874. case KEY_GEM:
  875. extra = 0;
  876. break;
  877. default:
  878. wl1271_warning("Unknown encryption type: %d",
  879. wlvif->encryption_type);
  880. ret = -EINVAL;
  881. goto out;
  882. }
  883. if (extra) {
  884. u8 *space = skb_push(skb, extra);
  885. memset(space, 0, extra);
  886. }
  887. /* QoS header - BE */
  888. if (wlvif->sta.qos)
  889. memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
  890. /* mac80211 header */
  891. hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
  892. memset(hdr, 0, sizeof(*hdr));
  893. fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
  894. if (wlvif->sta.qos)
  895. fc |= IEEE80211_STYPE_QOS_DATA;
  896. else
  897. fc |= IEEE80211_STYPE_DATA;
  898. if (wlvif->encryption_type != KEY_NONE)
  899. fc |= IEEE80211_FCTL_PROTECTED;
  900. hdr->frame_control = cpu_to_le16(fc);
  901. memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
  902. memcpy(hdr->addr2, vif->addr, ETH_ALEN);
  903. memset(hdr->addr3, 0xff, ETH_ALEN);
  904. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
  905. skb->data, skb->len, 0,
  906. wlvif->basic_rate);
  907. out:
  908. dev_kfree_skb(skb);
  909. return ret;
  910. }
  911. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  912. {
  913. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  914. struct ieee80211_qos_hdr template;
  915. memset(&template, 0, sizeof(template));
  916. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  917. memcpy(template.addr2, vif->addr, ETH_ALEN);
  918. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  919. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  920. IEEE80211_STYPE_QOS_NULLFUNC |
  921. IEEE80211_FCTL_TODS);
  922. /* FIXME: not sure what priority to use here */
  923. template.qos_ctrl = cpu_to_le16(0);
  924. return wl1271_cmd_template_set(wl, wlvif->role_id,
  925. CMD_TEMPL_QOS_NULL_DATA, &template,
  926. sizeof(template), 0,
  927. wlvif->basic_rate);
  928. }
  929. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  930. {
  931. struct wl1271_cmd_set_keys *cmd;
  932. int ret = 0;
  933. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  934. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  935. if (!cmd) {
  936. ret = -ENOMEM;
  937. goto out;
  938. }
  939. cmd->hlid = hlid;
  940. cmd->key_id = id;
  941. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  942. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  943. cmd->key_type = KEY_WEP;
  944. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  945. if (ret < 0) {
  946. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  947. goto out;
  948. }
  949. out:
  950. kfree(cmd);
  951. return ret;
  952. }
  953. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  954. u16 action, u8 id, u8 key_type,
  955. u8 key_size, const u8 *key, const u8 *addr,
  956. u32 tx_seq_32, u16 tx_seq_16)
  957. {
  958. struct wl1271_cmd_set_keys *cmd;
  959. int ret = 0;
  960. /* hlid might have already been deleted */
  961. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  962. return 0;
  963. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  964. if (!cmd) {
  965. ret = -ENOMEM;
  966. goto out;
  967. }
  968. cmd->hlid = wlvif->sta.hlid;
  969. if (key_type == KEY_WEP)
  970. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  971. else if (is_broadcast_ether_addr(addr))
  972. cmd->lid_key_type = BROADCAST_LID_TYPE;
  973. else
  974. cmd->lid_key_type = UNICAST_LID_TYPE;
  975. cmd->key_action = cpu_to_le16(action);
  976. cmd->key_size = key_size;
  977. cmd->key_type = key_type;
  978. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  979. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  980. cmd->key_id = id;
  981. if (key_type == KEY_TKIP) {
  982. /*
  983. * We get the key in the following form:
  984. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  985. * but the target is expecting:
  986. * TKIP - RX MIC - TX MIC
  987. */
  988. memcpy(cmd->key, key, 16);
  989. memcpy(cmd->key + 16, key + 24, 8);
  990. memcpy(cmd->key + 24, key + 16, 8);
  991. } else {
  992. memcpy(cmd->key, key, key_size);
  993. }
  994. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  995. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  996. if (ret < 0) {
  997. wl1271_warning("could not set keys");
  998. goto out;
  999. }
  1000. out:
  1001. kfree(cmd);
  1002. return ret;
  1003. }
  1004. /*
  1005. * TODO: merge with sta/ibss into 1 set_key function.
  1006. * note there are slight diffs
  1007. */
  1008. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1009. u16 action, u8 id, u8 key_type,
  1010. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1011. u16 tx_seq_16)
  1012. {
  1013. struct wl1271_cmd_set_keys *cmd;
  1014. int ret = 0;
  1015. u8 lid_type;
  1016. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1017. if (!cmd)
  1018. return -ENOMEM;
  1019. if (hlid == wlvif->ap.bcast_hlid) {
  1020. if (key_type == KEY_WEP)
  1021. lid_type = WEP_DEFAULT_LID_TYPE;
  1022. else
  1023. lid_type = BROADCAST_LID_TYPE;
  1024. } else {
  1025. lid_type = UNICAST_LID_TYPE;
  1026. }
  1027. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1028. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1029. (int)key_type, (int)hlid);
  1030. cmd->lid_key_type = lid_type;
  1031. cmd->hlid = hlid;
  1032. cmd->key_action = cpu_to_le16(action);
  1033. cmd->key_size = key_size;
  1034. cmd->key_type = key_type;
  1035. cmd->key_id = id;
  1036. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1037. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1038. if (key_type == KEY_TKIP) {
  1039. /*
  1040. * We get the key in the following form:
  1041. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1042. * but the target is expecting:
  1043. * TKIP - RX MIC - TX MIC
  1044. */
  1045. memcpy(cmd->key, key, 16);
  1046. memcpy(cmd->key + 16, key + 24, 8);
  1047. memcpy(cmd->key + 24, key + 16, 8);
  1048. } else {
  1049. memcpy(cmd->key, key, key_size);
  1050. }
  1051. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1052. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1053. if (ret < 0) {
  1054. wl1271_warning("could not set ap keys");
  1055. goto out;
  1056. }
  1057. out:
  1058. kfree(cmd);
  1059. return ret;
  1060. }
  1061. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
  1062. {
  1063. struct wl12xx_cmd_set_peer_state *cmd;
  1064. int ret = 0;
  1065. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1066. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1067. if (!cmd) {
  1068. ret = -ENOMEM;
  1069. goto out;
  1070. }
  1071. cmd->hlid = hlid;
  1072. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1073. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1074. if (ret < 0) {
  1075. wl1271_error("failed to send set peer state command");
  1076. goto out_free;
  1077. }
  1078. out_free:
  1079. kfree(cmd);
  1080. out:
  1081. return ret;
  1082. }
  1083. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1084. struct ieee80211_sta *sta, u8 hlid)
  1085. {
  1086. struct wl12xx_cmd_add_peer *cmd;
  1087. int i, ret;
  1088. u32 sta_rates;
  1089. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1090. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1091. if (!cmd) {
  1092. ret = -ENOMEM;
  1093. goto out;
  1094. }
  1095. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1096. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1097. cmd->aid = sta->aid;
  1098. cmd->hlid = hlid;
  1099. cmd->sp_len = sta->max_sp;
  1100. cmd->wmm = sta->wme ? 1 : 0;
  1101. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1102. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1103. cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
  1104. else
  1105. cmd->psd_type[i] = WL1271_PSD_LEGACY;
  1106. sta_rates = sta->supp_rates[wlvif->band];
  1107. if (sta->ht_cap.ht_supported)
  1108. sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
  1109. cmd->supported_rates =
  1110. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1111. wlvif->band));
  1112. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1113. cmd->supported_rates, sta->uapsd_queues);
  1114. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1115. if (ret < 0) {
  1116. wl1271_error("failed to initiate cmd add peer");
  1117. goto out_free;
  1118. }
  1119. out_free:
  1120. kfree(cmd);
  1121. out:
  1122. return ret;
  1123. }
  1124. int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
  1125. {
  1126. struct wl12xx_cmd_remove_peer *cmd;
  1127. int ret;
  1128. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1129. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1130. if (!cmd) {
  1131. ret = -ENOMEM;
  1132. goto out;
  1133. }
  1134. cmd->hlid = hlid;
  1135. /* We never send a deauth, mac80211 is in charge of this */
  1136. cmd->reason_opcode = 0;
  1137. cmd->send_deauth_flag = 0;
  1138. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1139. if (ret < 0) {
  1140. wl1271_error("failed to initiate cmd remove peer");
  1141. goto out_free;
  1142. }
  1143. /*
  1144. * We are ok with a timeout here. The event is sometimes not sent
  1145. * due to a firmware bug.
  1146. */
  1147. wl1271_cmd_wait_for_event_or_timeout(wl,
  1148. PEER_REMOVE_COMPLETE_EVENT_ID);
  1149. out_free:
  1150. kfree(cmd);
  1151. out:
  1152. return ret;
  1153. }
  1154. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1155. {
  1156. struct wl12xx_cmd_config_fwlog *cmd;
  1157. int ret = 0;
  1158. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1159. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1160. if (!cmd) {
  1161. ret = -ENOMEM;
  1162. goto out;
  1163. }
  1164. cmd->logger_mode = wl->conf.fwlog.mode;
  1165. cmd->log_severity = wl->conf.fwlog.severity;
  1166. cmd->timestamp = wl->conf.fwlog.timestamp;
  1167. cmd->output = wl->conf.fwlog.output;
  1168. cmd->threshold = wl->conf.fwlog.threshold;
  1169. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1170. if (ret < 0) {
  1171. wl1271_error("failed to send config firmware logger command");
  1172. goto out_free;
  1173. }
  1174. out_free:
  1175. kfree(cmd);
  1176. out:
  1177. return ret;
  1178. }
  1179. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1180. {
  1181. struct wl12xx_cmd_start_fwlog *cmd;
  1182. int ret = 0;
  1183. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1184. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1185. if (!cmd) {
  1186. ret = -ENOMEM;
  1187. goto out;
  1188. }
  1189. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1190. if (ret < 0) {
  1191. wl1271_error("failed to send start firmware logger command");
  1192. goto out_free;
  1193. }
  1194. out_free:
  1195. kfree(cmd);
  1196. out:
  1197. return ret;
  1198. }
  1199. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1200. {
  1201. struct wl12xx_cmd_stop_fwlog *cmd;
  1202. int ret = 0;
  1203. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1204. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1205. if (!cmd) {
  1206. ret = -ENOMEM;
  1207. goto out;
  1208. }
  1209. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1210. if (ret < 0) {
  1211. wl1271_error("failed to send stop firmware logger command");
  1212. goto out_free;
  1213. }
  1214. out_free:
  1215. kfree(cmd);
  1216. out:
  1217. return ret;
  1218. }
  1219. static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1220. u8 role_id)
  1221. {
  1222. struct wl12xx_cmd_roc *cmd;
  1223. int ret = 0;
  1224. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wlvif->channel, role_id);
  1225. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1226. return -EINVAL;
  1227. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1228. if (!cmd) {
  1229. ret = -ENOMEM;
  1230. goto out;
  1231. }
  1232. cmd->role_id = role_id;
  1233. cmd->channel = wlvif->channel;
  1234. switch (wlvif->band) {
  1235. case IEEE80211_BAND_2GHZ:
  1236. cmd->band = WLCORE_BAND_2_4GHZ;
  1237. break;
  1238. case IEEE80211_BAND_5GHZ:
  1239. cmd->band = WLCORE_BAND_5GHZ;
  1240. break;
  1241. default:
  1242. wl1271_error("roc - unknown band: %d", (int)wlvif->band);
  1243. ret = -EINVAL;
  1244. goto out_free;
  1245. }
  1246. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1247. if (ret < 0) {
  1248. wl1271_error("failed to send ROC command");
  1249. goto out_free;
  1250. }
  1251. out_free:
  1252. kfree(cmd);
  1253. out:
  1254. return ret;
  1255. }
  1256. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1257. {
  1258. struct wl12xx_cmd_croc *cmd;
  1259. int ret = 0;
  1260. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1261. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1262. if (!cmd) {
  1263. ret = -ENOMEM;
  1264. goto out;
  1265. }
  1266. cmd->role_id = role_id;
  1267. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1268. sizeof(*cmd), 0);
  1269. if (ret < 0) {
  1270. wl1271_error("failed to send ROC command");
  1271. goto out_free;
  1272. }
  1273. out_free:
  1274. kfree(cmd);
  1275. out:
  1276. return ret;
  1277. }
  1278. int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id)
  1279. {
  1280. int ret = 0;
  1281. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1282. return 0;
  1283. ret = wl12xx_cmd_roc(wl, wlvif, role_id);
  1284. if (ret < 0)
  1285. goto out;
  1286. ret = wl1271_cmd_wait_for_event(wl,
  1287. REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
  1288. if (ret < 0) {
  1289. wl1271_error("cmd roc event completion error");
  1290. goto out;
  1291. }
  1292. __set_bit(role_id, wl->roc_map);
  1293. out:
  1294. return ret;
  1295. }
  1296. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1297. {
  1298. int ret = 0;
  1299. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1300. return 0;
  1301. ret = wl12xx_cmd_croc(wl, role_id);
  1302. if (ret < 0)
  1303. goto out;
  1304. __clear_bit(role_id, wl->roc_map);
  1305. /*
  1306. * Rearm the tx watchdog when removing the last ROC. This prevents
  1307. * recoveries due to just finished ROCs - when Tx hasn't yet had
  1308. * a chance to get out.
  1309. */
  1310. if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
  1311. wl12xx_rearm_tx_watchdog_locked(wl);
  1312. out:
  1313. return ret;
  1314. }
  1315. int wl12xx_cmd_channel_switch(struct wl1271 *wl,
  1316. struct wl12xx_vif *wlvif,
  1317. struct ieee80211_channel_switch *ch_switch)
  1318. {
  1319. struct wl12xx_cmd_channel_switch *cmd;
  1320. int ret;
  1321. wl1271_debug(DEBUG_ACX, "cmd channel switch");
  1322. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1323. if (!cmd) {
  1324. ret = -ENOMEM;
  1325. goto out;
  1326. }
  1327. cmd->role_id = wlvif->role_id;
  1328. cmd->channel = ch_switch->channel->hw_value;
  1329. cmd->switch_time = ch_switch->count;
  1330. cmd->stop_tx = ch_switch->block_tx;
  1331. /* FIXME: control from mac80211 in the future */
  1332. cmd->post_switch_tx_disable = 0; /* Enable TX on the target channel */
  1333. ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
  1334. if (ret < 0) {
  1335. wl1271_error("failed to send channel switch command");
  1336. goto out_free;
  1337. }
  1338. out_free:
  1339. kfree(cmd);
  1340. out:
  1341. return ret;
  1342. }
  1343. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
  1344. {
  1345. struct wl12xx_cmd_stop_channel_switch *cmd;
  1346. int ret;
  1347. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1348. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1349. if (!cmd) {
  1350. ret = -ENOMEM;
  1351. goto out;
  1352. }
  1353. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1354. if (ret < 0) {
  1355. wl1271_error("failed to stop channel switch command");
  1356. goto out_free;
  1357. }
  1358. out_free:
  1359. kfree(cmd);
  1360. out:
  1361. return ret;
  1362. }
  1363. /* start dev role and roc on its channel */
  1364. int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1365. {
  1366. int ret;
  1367. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1368. wlvif->bss_type == BSS_TYPE_IBSS)))
  1369. return -EINVAL;
  1370. ret = wl12xx_cmd_role_start_dev(wl, wlvif);
  1371. if (ret < 0)
  1372. goto out;
  1373. ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id);
  1374. if (ret < 0)
  1375. goto out_stop;
  1376. return 0;
  1377. out_stop:
  1378. wl12xx_cmd_role_stop_dev(wl, wlvif);
  1379. out:
  1380. return ret;
  1381. }
  1382. /* croc dev hlid, and stop the role */
  1383. int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1384. {
  1385. int ret;
  1386. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1387. wlvif->bss_type == BSS_TYPE_IBSS)))
  1388. return -EINVAL;
  1389. /* flush all pending packets */
  1390. wl1271_tx_work_locked(wl);
  1391. if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
  1392. ret = wl12xx_croc(wl, wlvif->dev_role_id);
  1393. if (ret < 0)
  1394. goto out;
  1395. }
  1396. ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
  1397. if (ret < 0)
  1398. goto out;
  1399. out:
  1400. return ret;
  1401. }