cmd.c 39 KB

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